Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: components/ntp_snippets/ntp_snippets_fetcher.h

Issue 1922083004: Allow fetching personalized snippets from ChromeReader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Yet another unittest fix Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_
6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/optional.h" 15 #include "base/optional.h"
16 #include "base/time/tick_clock.h" 16 #include "base/time/tick_clock.h"
17 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "components/ntp_snippets/ntp_snippet.h" 18 #include "components/ntp_snippets/ntp_snippet.h"
19 #include "google_apis/gaia/oauth2_token_service.h"
19 #include "net/url_request/url_fetcher_delegate.h" 20 #include "net/url_request/url_fetcher_delegate.h"
20 #include "net/url_request/url_request_context_getter.h" 21 #include "net/url_request/url_request_context_getter.h"
21 22
23 class SigninManagerBase;
24
22 namespace base { 25 namespace base {
23 class Value; 26 class Value;
24 } 27 } // namespace base
28
29 namespace net {
30 class HttpRequestHeaders;
31 } // namespace net
25 32
26 namespace ntp_snippets { 33 namespace ntp_snippets {
27 34
28 // Fetches snippet data for the NTP from the server 35 // Fetches snippet data for the NTP from the server.
29 class NTPSnippetsFetcher : public net::URLFetcherDelegate { 36 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer,
37 public OAuth2TokenService::Observer,
38 public net::URLFetcherDelegate {
30 public: 39 public:
31 // Callbacks for JSON parsing, needed because the parsing is platform- 40 // Callbacks for JSON parsing, needed because the parsing is platform-
32 // dependent. 41 // dependent.
33 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; 42 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>;
34 using ErrorCallback = base::Callback<void(const std::string&)>; 43 using ErrorCallback = base::Callback<void(const std::string&)>;
35 using ParseJSONCallback = base::Callback< 44 using ParseJSONCallback = base::Callback<
36 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>; 45 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>;
37 46
38 using OptionalSnippets = base::Optional<NTPSnippet::PtrVector>; 47 using OptionalSnippets = base::Optional<NTPSnippet::PtrVector>;
39 // |snippets| contains parsed snippets if a fetch succeeded. If problems 48 // |snippets| contains parsed snippets if a fetch succeeded. If problems
40 // occur, |snippets| contains no value (no actual vector in base::Optional). 49 // occur, |snippets| contains no value (no actual vector in base::Optional).
41 // Error details can be retrieved using last_status(). 50 // Error details can be retrieved using last_status().
42 using SnippetsAvailableCallback = 51 using SnippetsAvailableCallback =
43 base::Callback<void(OptionalSnippets snippets)>; 52 base::Callback<void(OptionalSnippets snippets)>;
44 53
45 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA 54 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA
46 // histograms, so do not change existing values. 55 // histograms, so do not change existing values.
47 enum class FetchResult { 56 enum class FetchResult {
48 SUCCESS, 57 SUCCESS,
49 EMPTY_HOSTS, 58 EMPTY_HOSTS,
50 URL_REQUEST_STATUS_ERROR, 59 URL_REQUEST_STATUS_ERROR,
51 HTTP_ERROR, 60 HTTP_ERROR,
52 JSON_PARSE_ERROR, 61 JSON_PARSE_ERROR,
53 INVALID_SNIPPET_CONTENT_ERROR, 62 INVALID_SNIPPET_CONTENT_ERROR,
54 RESULT_MAX 63 RESULT_MAX
55 }; 64 };
56 65
57 NTPSnippetsFetcher( 66 NTPSnippetsFetcher(
67 SigninManagerBase* signin_manager,
68 OAuth2TokenService* oauth2_token_service,
58 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, 69 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
59 const ParseJSONCallback& parse_json_callback, 70 const ParseJSONCallback& parse_json_callback,
60 bool is_stable_channel); 71 bool is_stable_channel);
61 ~NTPSnippetsFetcher() override; 72 ~NTPSnippetsFetcher() override;
62 73
63 // Set a callback that is called when a new set of snippets are downloaded, 74 // Set a callback that is called when a new set of snippets are downloaded,
64 // overriding any previously set callback. 75 // overriding any previously set callback.
65 void SetCallback(const SnippetsAvailableCallback& callback); 76 void SetCallback(const SnippetsAvailableCallback& callback);
66 77
67 // Fetches snippets from the server. |hosts| restricts the results to a set of 78 // Fetches snippets from the server. |hosts| restricts the results to a set of
68 // hosts, e.g. "www.google.com". An empty host set produces an error. 79 // hosts, e.g. "www.google.com". An empty host set produces an error.
69 // 80 //
70 // If an ongoing fetch exists, it will be cancelled and a new one started, 81 // If an ongoing fetch exists, it will be cancelled and a new one started,
71 // without triggering an additional callback (i.e. not noticeable by 82 // without triggering an additional callback (i.e. not noticeable by
72 // subscriber of SetCallback()). 83 // subscriber of SetCallback()).
73 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, int count); 84 void FetchSnippetsFromHosts(const std::set<std::string>& hosts,
85 const std::string& language_code,
86 int count);
74 87
75 // Debug string representing the status/result of the last fetch attempt. 88 // Debug string representing the status/result of the last fetch attempt.
76 const std::string& last_status() const { return last_status_; } 89 const std::string& last_status() const { return last_status_; }
77 90
78 // Returns the last json fetched from the server. 91 // Returns the last json fetched from the server.
79 const std::string& last_json() const { 92 const std::string& last_json() const {
80 return last_fetch_json_; 93 return last_fetch_json_;
81 } 94 }
82 95
83 // Overrides internal clock for testing purposes. 96 // Overrides internal clock for testing purposes.
84 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) { 97 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) {
85 tick_clock_ = std::move(tick_clock); 98 tick_clock_ = std::move(tick_clock);
86 } 99 }
87 100
88 private: 101 private:
102 enum class Variant { kRestrictedPersonalized, kRestricted, kPersonalized };
103
104 void FetchSnippetsImpl(const GURL& url,
105 const std::string& auth_header,
106 const std::string& request);
107 std::string GetHostRestricts() const;
108 bool UseHostRestriction() const;
109 bool UseAuthentication() const;
110 void FetchSnippetsNonAuthenticated();
111 void FetchSnippetsAuthenticated(const std::string& account_id,
112 const std::string& oauth_access_token);
113 void StartTokenRequest();
114
115 // OAuth2TokenService::Consumer overrides:
116 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
117 const std::string& access_token,
118 const base::Time& expiration_time) override;
119 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
120 const GoogleServiceAuthError& error) override;
121
122 // OAuth2TokenService::Observer overrides:
123 void OnRefreshTokenAvailable(const std::string& account_id) override;
124
89 // URLFetcherDelegate implementation. 125 // URLFetcherDelegate implementation.
90 void OnURLFetchComplete(const net::URLFetcher* source) override; 126 void OnURLFetchComplete(const net::URLFetcher* source) override;
91 127
92 void OnJsonParsed(std::unique_ptr<base::Value> parsed); 128 void OnJsonParsed(std::unique_ptr<base::Value> parsed);
93 void OnJsonError(const std::string& error); 129 void OnJsonError(const std::string& error);
94 void FetchFinished(OptionalSnippets snippets, 130 void FetchFinished(OptionalSnippets snippets,
95 FetchResult result, 131 FetchResult result,
96 const std::string& extra_message); 132 const std::string& extra_message);
97 133
134 // Authorization for signed-in users.
135 SigninManagerBase* signin_manager_;
136 OAuth2TokenService* token_service_;
137 std::unique_ptr<OAuth2TokenService::Request> oauth_request_;
138 bool waiting_for_refresh_token_;
139
98 // Holds the URL request context. 140 // Holds the URL request context.
99 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 141 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
100 142
101 const ParseJSONCallback parse_json_callback_; 143 const ParseJSONCallback parse_json_callback_;
102 base::TimeTicks fetch_start_time_; 144 base::TimeTicks fetch_start_time_;
103 std::string last_status_; 145 std::string last_status_;
104 std::string last_fetch_json_; 146 std::string last_fetch_json_;
105 147
148 // Hosts to restrict the snippets to.
149 std::set<std::string> hosts_;
150
151 // Count of snippets to fetch.
152 int count_to_fetch_;
153
154 // Language code to restrict to for personalized results.
155 std::string locale_;
156
106 // The fetcher for downloading the snippets. 157 // The fetcher for downloading the snippets.
107 std::unique_ptr<net::URLFetcher> url_fetcher_; 158 std::unique_ptr<net::URLFetcher> url_fetcher_;
108 159
109 // The callback to notify when new snippets get fetched. 160 // The callback to notify when new snippets get fetched.
110 SnippetsAvailableCallback snippets_available_callback_; 161 SnippetsAvailableCallback snippets_available_callback_;
111 162
112 // Flag for picking the right (stable/non-stable) API key for Chrome Reader 163 // Flag for picking the right (stable/non-stable) API key for Chrome Reader.
113 bool is_stable_channel_; 164 bool is_stable_channel_;
114 165
166 // The variant of the fetching to use.
167 Variant variant_;
168
115 // Allow for an injectable tick clock for testing. 169 // Allow for an injectable tick clock for testing.
116 std::unique_ptr<base::TickClock> tick_clock_; 170 std::unique_ptr<base::TickClock> tick_clock_;
117 171
118 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; 172 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_;
119 173
120 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); 174 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher);
121 }; 175 };
122 } // namespace ntp_snippets 176 } // namespace ntp_snippets
123 177
124 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ 178 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_constants.cc ('k') | components/ntp_snippets/ntp_snippets_fetcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698