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

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: Minor fixes 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/callback_list.h"
Marc Treib 2016/05/09 12:33:34 Not needed I think?
jkrcal 2016/05/09 15:30:13 Done.
14 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
15 #include "components/ntp_snippets/ntp_snippet.h" 16 #include "components/ntp_snippets/ntp_snippet.h"
17 #include "google_apis/gaia/oauth2_token_service.h"
16 #include "net/url_request/url_fetcher_delegate.h" 18 #include "net/url_request/url_fetcher_delegate.h"
17 #include "net/url_request/url_request_context_getter.h" 19 #include "net/url_request/url_request_context_getter.h"
18 20
21 class SigninManagerBase;
22
19 namespace base { 23 namespace base {
20 class Value; 24 class Value;
21 } 25 } // namespace base
26
27 namespace net {
28 class HttpRequestHeaders;
29 } // namespace net
22 30
23 namespace ntp_snippets { 31 namespace ntp_snippets {
24 32
25 // Fetches snippet data for the NTP from the server 33 // Fetches snippet data for the NTP from the server
26 class NTPSnippetsFetcher : public net::URLFetcherDelegate { 34 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer,
35 public OAuth2TokenService::Observer,
36 public net::URLFetcherDelegate {
27 public: 37 public:
28 // Callbacks for JSON parsing, needed because the parsing is platform- 38 // Callbacks for JSON parsing, needed because the parsing is platform-
29 // dependent. 39 // dependent.
30 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; 40 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>;
31 using ErrorCallback = base::Callback<void(const std::string&)>; 41 using ErrorCallback = base::Callback<void(const std::string&)>;
32 using ParseJSONCallback = base::Callback< 42 using ParseJSONCallback = base::Callback<
33 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>; 43 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>;
34 44
35 // |snippets| contains parsed snippets. If problems occur (explained in 45 // |snippets| contains parsed snippets. If problems occur (explained in
36 // |status_message|), |status_message| is non-empty; otherwise, 46 // |status_message|), |status_message| is non-empty; otherwise,
37 // |status_message| is empty. 47 // |status_message| is empty.
38 using SnippetsAvailableCallback = 48 using SnippetsAvailableCallback =
39 base::Callback<void(NTPSnippet::PtrVector snippets, 49 base::Callback<void(NTPSnippet::PtrVector snippets,
40 const std::string& status_message)>; 50 const std::string& status_message)>;
41 51
42 NTPSnippetsFetcher( 52 NTPSnippetsFetcher(
53 SigninManagerBase* signin_manager,
54 OAuth2TokenService* oauth2_token_service,
43 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, 55 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
44 const ParseJSONCallback& parse_json_callback, 56 const ParseJSONCallback& parse_json_callback,
45 bool is_stable_channel); 57 bool is_stable_channel);
46 ~NTPSnippetsFetcher() override; 58 ~NTPSnippetsFetcher() override;
47 59
48 // Set a callback that is called when a new set of snippets are downloaded, 60 // Set a callback that is called when a new set of snippets are downloaded,
49 // overriding any previously set callback. 61 // overriding any previously set callback.
50 void SetCallback(const SnippetsAvailableCallback& callback); 62 void SetCallback(const SnippetsAvailableCallback& callback);
51 63
52 // Fetches snippets from the server. |hosts| restricts the results to a set of 64 // Fetches snippets from the server. |hosts| restricts the results to a set of
53 // hosts, e.g. "www.google.com". An empty host set produces an error. 65 // hosts, e.g. "www.google.com". An empty host set produces an error.
54 // 66 //
55 // If an ongoing fetch exists, it will be cancelled and a new one started, 67 // If an ongoing fetch exists, it will be cancelled and a new one started,
56 // without triggering an additional callback (i.e. not noticeable by 68 // without triggering an additional callback (i.e. not noticeable by
57 // subscriber of SetCallback()). 69 // subscriber of SetCallback()).
58 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, int count); 70 void FetchSnippetsFromHosts(const std::set<std::string>& hosts,
71 const std::string& language_code,
72 int count);
59 73
60 // Returns the last json fetched from the server. 74 // Returns the last json fetched from the server.
61 const std::string& last_json() const { 75 const std::string& last_json() const {
62 return last_fetch_json_; 76 return last_fetch_json_;
63 } 77 }
64 78
65 private: 79 private:
80 enum Variant {
Marc Treib 2016/05/09 12:33:34 optional: Make this an enum class? Then it's membe
jkrcal 2016/05/09 15:30:13 Done.
81 kRestrictedPersonalized,
82 kRestricted,
83 kPersonalized
84 };
85
86 void FetchSnippetsImpl(const GURL& url,
87 const std::string& auth_header,
88 const std::string& request);
89 std::string GetHostRestricts() const;
90 bool UseAuthentication();
91 void FetchSnippetsNonAuthenticated();
92 void FetchSnippetsAuthenticated(const std::string& account_id,
93 const std::string& oauth_access_token);
94 void StartTokenRequest();
95
96 // OAuth2TokenService::Consumer overrides:
97 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
98 const std::string& access_token,
99 const base::Time& expiration_time) override;
100 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
101 const GoogleServiceAuthError& error) override;
102
103 // OAuth2TokenService::Observer overrides:
104 void OnRefreshTokenAvailable(const std::string& account_id) override;
105
66 // URLFetcherDelegate implementation. 106 // URLFetcherDelegate implementation.
67 void OnURLFetchComplete(const net::URLFetcher* source) override; 107 void OnURLFetchComplete(const net::URLFetcher* source) override;
68 108
69 void OnJsonParsed(std::unique_ptr<base::Value> parsed); 109 void OnJsonParsed(std::unique_ptr<base::Value> parsed);
70 void OnJsonError(const std::string& error); 110 void OnJsonError(const std::string& error);
71 111
112 // Authorization for signed-in users
Marc Treib 2016/05/09 12:33:34 nit: Period after comment
jkrcal 2016/05/09 15:30:13 Done.
113 SigninManagerBase* signin_manager_;
114 OAuth2TokenService* token_service_;
115 std::unique_ptr<OAuth2TokenService::Request> oauth_request_;
116 bool waiting_for_refresh_token_;
117
72 // Holds the URL request context. 118 // Holds the URL request context.
73 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 119 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
74 120
75 const ParseJSONCallback parse_json_callback_; 121 const ParseJSONCallback parse_json_callback_;
76 std::string last_fetch_json_; 122 std::string last_fetch_json_;
77 123
124 // Hosts to restrict the snippets to.
125 std::set<std::string> hosts_;
126
127 // Count of snippets to fetch.
128 int count_;
Marc Treib 2016/05/09 12:33:33 count_to_fetch_? Just count_ seems ambiguous.
jkrcal 2016/05/09 15:30:13 Done.
129
130 // Language code to restrict to for personalized results.
131 std::string locale_;
132
78 // The fetcher for downloading the snippets. 133 // The fetcher for downloading the snippets.
79 std::unique_ptr<net::URLFetcher> url_fetcher_; 134 std::unique_ptr<net::URLFetcher> url_fetcher_;
80 135
81 // The callback to notify when new snippets get fetched. 136 // The callback to notify when new snippets get fetched.
82 SnippetsAvailableCallback snippets_available_callback_; 137 SnippetsAvailableCallback snippets_available_callback_;
83 138
84 // Flag for picking the right (stable/non-stable) API key for Chrome Reader 139 // Flag for picking the right (stable/non-stable) API key for Chrome Reader
85 bool is_stable_channel_; 140 bool is_stable_channel_;
86 141
142 // The variant of the fetching to use.
143 Variant variant_;
144
87 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; 145 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_;
88 146
89 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); 147 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher);
90 }; 148 };
91 } // namespace ntp_snippets 149 } // namespace ntp_snippets
92 150
93 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ 151 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698