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

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

Powered by Google App Engine
This is Rietveld 408576698