Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "components/signin/core/browser/signin_manager_base.h" | |
| 16 #include "google_apis/gaia/oauth2_token_service.h" | |
| 17 #include "net/http/http_request_headers.h" | |
|
Marc Treib
2016/05/04 11:50:10
I think you can forward-declare this, and keep the
jkrcal
2016/05/04 14:04:09
Done. SigninManagerBase as well.
| |
| 15 #include "net/url_request/url_fetcher_delegate.h" | 18 #include "net/url_request/url_fetcher_delegate.h" |
| 16 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 17 | 20 |
| 18 namespace ntp_snippets { | 21 namespace ntp_snippets { |
| 19 | 22 |
| 20 // Fetches snippet data for the NTP from the server | 23 // Fetches snippet data for the NTP from the server |
| 21 class NTPSnippetsFetcher : public net::URLFetcherDelegate { | 24 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, |
| 25 public OAuth2TokenService::Observer, | |
| 26 public net::URLFetcherDelegate { | |
| 22 public: | 27 public: |
| 23 // If problems occur (explained in |status_message|), |snippets_json| is | 28 // If problems occur (explained in |status_message|), |snippets_json| is |
| 24 // empty; otherwise, |status_message| is empty. | 29 // empty; otherwise, |status_message| is empty. |
| 25 using SnippetsAvailableCallback = | 30 using SnippetsAvailableCallback = |
| 26 base::Callback<void(const std::string& snippets_json, | 31 base::Callback<void(const std::string& snippets_json, |
| 27 const std::string& status_message)>; | 32 const std::string& status_message)>; |
| 28 using SnippetsAvailableCallbackList = | 33 using SnippetsAvailableCallbackList = |
| 29 base::CallbackList<void(const std::string&, const std::string&)>; | 34 base::CallbackList<void(const std::string&, const std::string&)>; |
| 30 | 35 |
| 31 NTPSnippetsFetcher( | 36 NTPSnippetsFetcher( |
| 37 SigninManagerBase* signin_manager, | |
|
Bernhard Bauer
2016/05/04 11:46:58
If you take ownership of these pointers, you shoul
jkrcal
2016/05/04 14:04:09
Done.
| |
| 38 OAuth2TokenService* oauth2_token_service, | |
| 32 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 39 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 33 bool is_stable_channel); | 40 bool is_stable_channel); |
| 34 ~NTPSnippetsFetcher() override; | 41 ~NTPSnippetsFetcher() override; |
| 35 | 42 |
| 36 // Adds a callback that is called when a new set of snippets are downloaded. | 43 // Adds a callback that is called when a new set of snippets are downloaded. |
| 37 std::unique_ptr<SnippetsAvailableCallbackList::Subscription> AddCallback( | 44 std::unique_ptr<SnippetsAvailableCallbackList::Subscription> AddCallback( |
| 38 const SnippetsAvailableCallback& callback) WARN_UNUSED_RESULT; | 45 const SnippetsAvailableCallback& callback) WARN_UNUSED_RESULT; |
| 39 | 46 |
| 40 // Fetches snippets from the server. |hosts| can be used to restrict the | 47 // 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 | 48 // results to a set of hosts, e.g. "www.google.com". If it is empty, no |
| 42 // restrictions are applied. | 49 // restrictions are applied. |
| 43 // | 50 // |
| 44 // If an ongoing fetch exists, it will be cancelled and a new one started, | 51 // If an ongoing fetch exists, it will be cancelled and a new one started, |
| 45 // without triggering additional callbacks (i.e. not noticeable by | 52 // without triggering additional callbacks (i.e. not noticeable by |
| 46 // subscribers). | 53 // subscribers). |
| 47 void FetchSnippets(const std::set<std::string>& hosts, int count); | 54 void FetchSnippets(const std::set<std::string>& hosts, |
| 55 const std::string& language_code, | |
| 56 int count); | |
| 48 | 57 |
| 49 private: | 58 private: |
| 59 void FetchSnippetsImpl(const std::string& url, | |
| 60 net::HttpRequestHeaders& headers, | |
|
Marc Treib
2016/05/04 11:50:10
const please. If you need it to be non-const, use
jkrcal
2016/05/04 14:04:09
Done.
| |
| 61 const std::string& request); | |
| 62 std::string GetHostsRestrict() const; | |
| 63 bool UseAuthenticated(); | |
|
Marc Treib
2016/05/04 11:50:10
Authenticated what? Either "UseAuthentication" or
jkrcal
2016/05/04 14:04:09
Done.
| |
| 64 void FetchSnippetsNonAuthenticated(); | |
| 65 void FetchSnippetsAuthenticated(const std::string& account_id, | |
| 66 const std::string& oauth_access_token); | |
| 67 void StartTokenRequest(); | |
| 68 | |
| 69 // OAuth2TokenService::Consumer overrides: | |
| 70 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
| 71 const std::string& access_token, | |
| 72 const base::Time& expiration_time) override; | |
| 73 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
| 74 const GoogleServiceAuthError& error) override; | |
| 75 | |
| 76 // OAuth2TokenService::Observer overrides: | |
| 77 void OnRefreshTokenAvailable(const std::string& account_id) override; | |
| 78 | |
| 50 // URLFetcherDelegate implementation. | 79 // URLFetcherDelegate implementation. |
| 51 void OnURLFetchComplete(const net::URLFetcher* source) override; | 80 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 52 | 81 |
| 82 // The SequencedTaskRunner on which file system operations will be run. | |
| 83 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | |
|
Bernhard Bauer
2016/05/04 11:46:58
Why do we add this again? Bad merge conflict?
Marc Treib
2016/05/04 11:50:10
Doesn't seem to be needed?
jkrcal
2016/05/04 14:04:09
Done.
jkrcal
2016/05/04 14:04:09
Sorry about that, a merging error of mine.
| |
| 84 | |
| 53 // Holds the URL request context. | 85 // Holds the URL request context. |
| 54 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 86 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 55 | 87 |
| 88 // Hosts to restrict the snippets to | |
|
Marc Treib
2016/05/04 11:50:10
nit: Periods at the end of comments please.
jkrcal
2016/05/04 14:04:09
Done.
| |
| 89 std::set<std::string> hosts_; | |
| 90 | |
| 91 // Count of snippets to fetch | |
| 92 int count_; | |
| 93 | |
| 94 // Language code to restrict to for personalized results | |
| 95 std::string locale_; | |
| 96 | |
| 56 // The fetcher for downloading the snippets. | 97 // The fetcher for downloading the snippets. |
| 57 std::unique_ptr<net::URLFetcher> url_fetcher_; | 98 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 58 | 99 |
| 59 // The callbacks to notify when new snippets get fetched. | 100 // The callbacks to notify when new snippets get fetched. |
| 60 SnippetsAvailableCallbackList callback_list_; | 101 SnippetsAvailableCallbackList callback_list_; |
| 61 | 102 |
| 103 // Authorization for signed-in users | |
| 104 std::unique_ptr<SigninManagerBase> signin_manager_; | |
| 105 std::unique_ptr<OAuth2TokenService> token_service_; | |
| 106 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | |
| 107 bool waiting_for_refresh_token_; | |
| 108 | |
| 62 // Flag for picking the right (stable/non-stable) API key for Chrome Reader | 109 // Flag for picking the right (stable/non-stable) API key for Chrome Reader |
| 63 bool is_stable_channel_; | 110 bool is_stable_channel_; |
| 64 | 111 |
| 112 // The variant of the fetching to use. | |
| 113 std::string variant_; | |
|
Bernhard Bauer
2016/05/04 11:46:58
I would store this as an enum and only use the str
Marc Treib
2016/05/04 11:50:10
Make this an enum?
jkrcal
2016/05/04 14:04:09
Done.
jkrcal
2016/05/04 14:04:09
Done.
| |
| 114 | |
| 65 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 115 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
| 66 }; | 116 }; |
| 67 } // namespace ntp_snippets | 117 } // namespace ntp_snippets |
| 68 | 118 |
| 69 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | 119 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
| OLD | NEW |