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

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: Fixing a broken unittest + reflecting code review comments 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 11
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/callback_list.h" 13 #include "base/callback_list.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "google_apis/gaia/oauth2_token_service.h"
15 #include "net/url_request/url_fetcher_delegate.h" 16 #include "net/url_request/url_fetcher_delegate.h"
16 #include "net/url_request/url_request_context_getter.h" 17 #include "net/url_request/url_request_context_getter.h"
17 18
19 class SigninManagerBase;
20
21 namespace net {
22
Bernhard Bauer 2016/05/04 14:11:55 The empty lines aren't necessary here.
jkrcal 2016/05/04 14:58:19 Done.
23 class HttpRequestHeaders;
24
25 } // namespace net
26
18 namespace ntp_snippets { 27 namespace ntp_snippets {
19 28
20 // Fetches snippet data for the NTP from the server 29 // Fetches snippet data for the NTP from the server
21 class NTPSnippetsFetcher : public net::URLFetcherDelegate { 30 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer,
31 public OAuth2TokenService::Observer,
32 public net::URLFetcherDelegate {
22 public: 33 public:
23 // If problems occur (explained in |status_message|), |snippets_json| is 34 // If problems occur (explained in |status_message|), |snippets_json| is
24 // empty; otherwise, |status_message| is empty. 35 // empty; otherwise, |status_message| is empty.
25 using SnippetsAvailableCallback = 36 using SnippetsAvailableCallback =
26 base::Callback<void(const std::string& snippets_json, 37 base::Callback<void(const std::string& snippets_json,
27 const std::string& status_message)>; 38 const std::string& status_message)>;
28 using SnippetsAvailableCallbackList = 39 using SnippetsAvailableCallbackList =
29 base::CallbackList<void(const std::string&, const std::string&)>; 40 base::CallbackList<void(const std::string&, const std::string&)>;
30 41
31 NTPSnippetsFetcher( 42 NTPSnippetsFetcher(
43 SigninManagerBase* signin_manager,
44 OAuth2TokenService* oauth2_token_service,
32 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, 45 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
33 bool is_stable_channel); 46 bool is_stable_channel);
34 ~NTPSnippetsFetcher() override; 47 ~NTPSnippetsFetcher() override;
35 48
36 // Adds a callback that is called when a new set of snippets are downloaded. 49 // Adds a callback that is called when a new set of snippets are downloaded.
37 std::unique_ptr<SnippetsAvailableCallbackList::Subscription> AddCallback( 50 std::unique_ptr<SnippetsAvailableCallbackList::Subscription> AddCallback(
38 const SnippetsAvailableCallback& callback) WARN_UNUSED_RESULT; 51 const SnippetsAvailableCallback& callback) WARN_UNUSED_RESULT;
39 52
40 // Fetches snippets from the server. |hosts| can be used to restrict the 53 // Fetches snippets from the server. |hosts| can be used to restrict the
41 // results to a set of hosts, e.g. "www.google.com". If it is empty, no 54 // results to a set of hosts, e.g. "www.google.com". If it is empty, no
42 // restrictions are applied. 55 // restrictions are applied.
43 // 56 //
44 // If an ongoing fetch exists, it will be cancelled and a new one started, 57 // If an ongoing fetch exists, it will be cancelled and a new one started,
45 // without triggering additional callbacks (i.e. not noticeable by 58 // without triggering additional callbacks (i.e. not noticeable by
46 // subscribers). 59 // subscribers).
47 void FetchSnippets(const std::set<std::string>& hosts, int count); 60 void FetchSnippets(const std::set<std::string>& hosts,
61 const std::string& language_code,
62 int count);
48 63
49 private: 64 private:
65 enum Variant {
66 kRestrictedPersonalized,
67 kRestricted,
68 kPersonalized
69 };
70
71 void FetchSnippetsImpl(const GURL& url,
72 const std::string& auth_header,
73 const std::string& request);
74 std::string GetHostsRestricts() const;
75 bool UseAuthentication();
76 void FetchSnippetsNonAuthenticated();
77 void FetchSnippetsAuthenticated(const std::string& account_id,
78 const std::string& oauth_access_token);
79 void StartTokenRequest();
80
81 // OAuth2TokenService::Consumer overrides:
82 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
83 const std::string& access_token,
84 const base::Time& expiration_time) override;
85 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
86 const GoogleServiceAuthError& error) override;
87
88 // OAuth2TokenService::Observer overrides:
89 void OnRefreshTokenAvailable(const std::string& account_id) override;
90
50 // URLFetcherDelegate implementation. 91 // URLFetcherDelegate implementation.
51 void OnURLFetchComplete(const net::URLFetcher* source) override; 92 void OnURLFetchComplete(const net::URLFetcher* source) override;
52 93
53 // Holds the URL request context. 94 // Holds the URL request context.
54 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 95 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
55 96
97 // Hosts to restrict the snippets to.
98 std::set<std::string> hosts_;
99
100 // Count of snippets to fetch.
101 int count_;
102
103 // Language code to restrict to for personalized results.
104 std::string locale_;
105
56 // The fetcher for downloading the snippets. 106 // The fetcher for downloading the snippets.
57 std::unique_ptr<net::URLFetcher> url_fetcher_; 107 std::unique_ptr<net::URLFetcher> url_fetcher_;
58 108
59 // The callbacks to notify when new snippets get fetched. 109 // The callbacks to notify when new snippets get fetched.
60 SnippetsAvailableCallbackList callback_list_; 110 SnippetsAvailableCallbackList callback_list_;
61 111
112 // Authorization for signed-in users
113 SigninManagerBase* signin_manager_;
114 OAuth2TokenService* token_service_;
115 std::unique_ptr<OAuth2TokenService::Request> oauth_request_;
116 bool waiting_for_refresh_token_;
117
62 // Flag for picking the right (stable/non-stable) API key for Chrome Reader 118 // Flag for picking the right (stable/non-stable) API key for Chrome Reader
63 bool is_stable_channel_; 119 bool is_stable_channel_;
64 120
121 // The variant of the fetching to use.
122 Variant variant_;
123
65 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); 124 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher);
66 }; 125 };
67 } // namespace ntp_snippets 126 } // namespace ntp_snippets
68 127
69 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ 128 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698