| 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 #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 "components/ntp_snippets/ntp_snippet.h" | 15 #include "components/ntp_snippets/ntp_snippet.h" |
| 16 #include "google_apis/gaia/oauth2_token_service.h" |
| 16 #include "net/url_request/url_fetcher_delegate.h" | 17 #include "net/url_request/url_fetcher_delegate.h" |
| 17 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
| 18 | 19 |
| 20 class SigninManagerBase; |
| 21 |
| 19 namespace base { | 22 namespace base { |
| 20 class Value; | 23 class Value; |
| 21 } | 24 } // namespace base |
| 25 |
| 26 namespace net { |
| 27 class HttpRequestHeaders; |
| 28 } // namespace net |
| 22 | 29 |
| 23 namespace ntp_snippets { | 30 namespace ntp_snippets { |
| 24 | 31 |
| 25 // Fetches snippet data for the NTP from the server | 32 // Fetches snippet data for the NTP from the server. |
| 26 class NTPSnippetsFetcher : public net::URLFetcherDelegate { | 33 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, |
| 34 public OAuth2TokenService::Observer, |
| 35 public net::URLFetcherDelegate { |
| 27 public: | 36 public: |
| 28 // Callbacks for JSON parsing, needed because the parsing is platform- | 37 // Callbacks for JSON parsing, needed because the parsing is platform- |
| 29 // dependent. | 38 // dependent. |
| 30 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; | 39 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; |
| 31 using ErrorCallback = base::Callback<void(const std::string&)>; | 40 using ErrorCallback = base::Callback<void(const std::string&)>; |
| 32 using ParseJSONCallback = base::Callback< | 41 using ParseJSONCallback = base::Callback< |
| 33 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>; | 42 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>; |
| 34 | 43 |
| 35 // |snippets| contains parsed snippets. If problems occur (explained in | 44 // |snippets| contains parsed snippets. If problems occur (explained in |
| 36 // |status_message|), |status_message| is non-empty; otherwise, | 45 // |status_message|), |status_message| is non-empty; otherwise, |
| 37 // |status_message| is empty. | 46 // |status_message| is empty. |
| 38 using SnippetsAvailableCallback = | 47 using SnippetsAvailableCallback = |
| 39 base::Callback<void(NTPSnippet::PtrVector snippets, | 48 base::Callback<void(NTPSnippet::PtrVector snippets, |
| 40 const std::string& status_message)>; | 49 const std::string& status_message)>; |
| 41 | 50 |
| 42 NTPSnippetsFetcher( | 51 NTPSnippetsFetcher( |
| 52 SigninManagerBase* signin_manager, |
| 53 OAuth2TokenService* oauth2_token_service, |
| 43 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 54 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 44 const ParseJSONCallback& parse_json_callback, | 55 const ParseJSONCallback& parse_json_callback, |
| 45 bool is_stable_channel); | 56 bool is_stable_channel); |
| 46 ~NTPSnippetsFetcher() override; | 57 ~NTPSnippetsFetcher() override; |
| 47 | 58 |
| 48 // Set a callback that is called when a new set of snippets are downloaded, | 59 // Set a callback that is called when a new set of snippets are downloaded, |
| 49 // overriding any previously set callback. | 60 // overriding any previously set callback. |
| 50 void SetCallback(const SnippetsAvailableCallback& callback); | 61 void SetCallback(const SnippetsAvailableCallback& callback); |
| 51 | 62 |
| 52 // Fetches snippets from the server. |hosts| restricts the results to a set of | 63 // 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. | 64 // hosts, e.g. "www.google.com". An empty host set produces an error. |
| 54 // | 65 // |
| 55 // If an ongoing fetch exists, it will be cancelled and a new one started, | 66 // 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 | 67 // without triggering an additional callback (i.e. not noticeable by |
| 57 // subscriber of SetCallback()). | 68 // subscriber of SetCallback()). |
| 58 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, int count); | 69 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, |
| 70 const std::string& language_code, |
| 71 int count); |
| 59 | 72 |
| 60 // Returns the last json fetched from the server. | 73 // Returns the last json fetched from the server. |
| 61 const std::string& last_json() const { | 74 const std::string& last_json() const { |
| 62 return last_fetch_json_; | 75 return last_fetch_json_; |
| 63 } | 76 } |
| 64 | 77 |
| 65 private: | 78 private: |
| 79 enum class Variant { |
| 80 kRestrictedPersonalized, |
| 81 kRestricted, |
| 82 kPersonalized |
| 83 }; |
| 84 |
| 85 void FetchSnippetsImpl(const GURL& url, |
| 86 const std::string& auth_header, |
| 87 const std::string& request); |
| 88 std::string GetHostRestricts() const; |
| 89 bool UseHostRestriction() const; |
| 90 bool UseAuthentication() const; |
| 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. |
| 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_to_fetch_; |
| 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_ |
| OLD | NEW |