summaryrefslogtreecommitdiff
path: root/chromium/components/ntp_snippets/contextual/contextual_content_suggestions_service_proxy.h
blob: d5dfc58e8c5b43ae16aecabe3c04047a6c59d62e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_NTP_SNIPPETS_CONTEXTUAL_CONTEXTUAL_CONTENT_SUGGESTIONS_SERVICE_PROXY_H_
#define COMPONENTS_NTP_SNIPPETS_CONTEXTUAL_CONTEXTUAL_CONTENT_SUGGESTIONS_SERVICE_PROXY_H_

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/ntp_snippets/contextual/cluster.h"
#include "components/ntp_snippets/contextual/contextual_content_suggestions_service.h"

class GURL;

namespace contextual_suggestions {

// Class providing access to the Contextual Content Suggestions Service and
// caching the list of current suggestions shown in a tab. It is owned by a UI
// object. There could be multiple instances of serivce proxy. Either can be
// torn down with a part of UI that owns it, which doesn't affect other proxies.
class ContextualContentSuggestionsServiceProxy {
 public:
  using ClustersCallback = ntp_snippets::FetchClustersCallback;
  using Cluster = ntp_snippets::Cluster;

  ContextualContentSuggestionsServiceProxy(
      ntp_snippets::ContextualContentSuggestionsService* service,
      std::unique_ptr<ContextualSuggestionsMetricsReporter> metrics_reporter);
  ~ContextualContentSuggestionsServiceProxy();

  // Fetches contextual suggestions for a given |url|.
  void FetchContextualSuggestions(const GURL& url, ClustersCallback callback);

  // Fetches an image for a contextual suggestion with specified
  // |suggestion_id|.
  void FetchContextualSuggestionImage(
      const std::string& suggestion_id,
      ntp_snippets::ImageFetchedCallback callback);

  // Fetches a favicon for a contextual suggestion with specified
  // |suggestion_id|.
  void FetchContextualSuggestionFavicon(
      const std::string& suggestion_id,
      ntp_snippets::ImageFetchedCallback callback);

  // Clears the state of the proxy.
  void ClearState();

  // Reports user interface event to the service.
  void ReportEvent(ukm::SourceId, ContextualSuggestionsEvent event);

  // Ensures that all metrics are properly flushed.
  void FlushMetrics();

 private:
  void FetchImageImpl(const GURL& image_url,
                      const std::string& image_id,
                      ntp_snippets::ImageFetchedCallback callback);

  void CacheSuggestions(ClustersCallback callback,
                        std::string peek_text,
                        std::vector<Cluster> clusters);
  // Pointer to the service.
  ntp_snippets::ContextualContentSuggestionsService* service_;
  // Cache of contextual suggestions.
  std::map<std::string, ntp_snippets::ContextualSuggestion> suggestions_;

  // Sink for reporting metrics for this proxy.
  std::unique_ptr<contextual_suggestions::ContextualSuggestionsMetricsReporter>
      metrics_reporter_;

  // The most recent SourceId in use by metrics_reporter_, or
  // ukm::kInvalidSourceId.
  ukm::SourceId last_ukm_source_id_;

  // Weak pointer factory to cancel pending callbacks.
  base::WeakPtrFactory<ContextualContentSuggestionsServiceProxy>
      weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(ContextualContentSuggestionsServiceProxy);
};

}  // namespace contextual_suggestions

#endif  // COMPONENTS_NTP_SNIPPETS_CONTEXTUAL_CONTEXTUAL_CONTENT_SUGGESTIONS_SERVICE_PROXY_H_