OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_JSON_REQUEST_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_JSON_REQUEST_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/callback.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/optional.h" |
| 15 #include "components/ntp_snippets/remote/ntp_snippets_request_params.h" |
| 16 #include "components/ntp_snippets/status.h" |
| 17 #include "components/translate/core/browser/language_model.h" |
| 18 #include "google_apis/gaia/oauth2_token_service.h" |
| 19 #include "net/http/http_request_headers.h" |
| 20 |
| 21 namespace base { |
| 22 class Value; |
| 23 class TickClock; |
| 24 } // namespace base |
| 25 |
| 26 class FetchAPI; |
| 27 class Personalization; |
| 28 |
| 29 namespace ntp_snippets { |
| 30 class UserClassifier; |
| 31 |
| 32 namespace internal { |
| 33 |
| 34 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA |
| 35 // histograms, so do not change existing values. Insert new values at the end, |
| 36 // and update the histogram definition. |
| 37 enum class FetchResult { |
| 38 SUCCESS, |
| 39 DEPRECATED_EMPTY_HOSTS, |
| 40 URL_REQUEST_STATUS_ERROR, |
| 41 HTTP_ERROR, |
| 42 JSON_PARSE_ERROR, |
| 43 INVALID_SNIPPET_CONTENT_ERROR, |
| 44 OAUTH_TOKEN_ERROR, |
| 45 INTERACTIVE_QUOTA_ERROR, |
| 46 NON_INTERACTIVE_QUOTA_ERROR, |
| 47 RESULT_MAX |
| 48 }; |
| 49 |
| 50 enum FetchAPI { |
| 51 CHROME_READER_API, |
| 52 CHROME_CONTENT_SUGGESTIONS_API, |
| 53 }; |
| 54 |
| 55 // A single request to query snippets. |
| 56 class NTPSnippetsJsonRequest : public net::URLFetcherDelegate { |
| 57 public: |
| 58 // A client can expect error_details only, if there was any error during the |
| 59 // fetching or parsing. In successful cases, it will be an empty string. |
| 60 using CompletedCallback = |
| 61 base::OnceCallback<void(std::unique_ptr<base::Value> result, |
| 62 FetchResult result_code, |
| 63 const std::string& error_details)>; |
| 64 |
| 65 // Builds authenticated and non-authenticated NTPSnippetsJsonRequests. |
| 66 class Builder { |
| 67 public: |
| 68 Builder(); |
| 69 Builder(Builder&&); |
| 70 ~Builder(); |
| 71 |
| 72 // Builds a Request object that contains all data to fetch new snippets. |
| 73 std::unique_ptr<NTPSnippetsJsonRequest> Build() const; |
| 74 |
| 75 Builder& SetAuthentication(const std::string& account_id, |
| 76 const std::string& auth_header); |
| 77 Builder& SetCreationTime(base::TimeTicks creation_time); |
| 78 Builder& SetFetchAPI(FetchAPI fetch_api); |
| 79 // The language_model borrowed from the fetcher needs to stay alive until |
| 80 // the request body is built. |
| 81 Builder& SetLanguageModel(const translate::LanguageModel* language_model); |
| 82 Builder& SetParams(const NTPSnippetsRequestParams& params); |
| 83 Builder& SetParseJsonCallback(ParseJSONCallback callback); |
| 84 Builder& SetPersonalization(Personalization personalization); |
| 85 // The tick_clock borrowed from the fetcher will be injected into the |
| 86 // request. It will be used at build time and after the fetch returned. |
| 87 // It has to be alive until the request is destroyed. |
| 88 Builder& SetTickClock(base::TickClock* tick_clock); |
| 89 Builder& SetUrl(const GURL& url); |
| 90 Builder& SetUrlRequestContextGetter( |
| 91 const scoped_refptr<net::URLRequestContextGetter>& context_getter); |
| 92 Builder& SetUserClassifier(const UserClassifier& user_classifier); |
| 93 |
| 94 // These preview methods allow to inspect the Request without exposing it |
| 95 // publicly. |
| 96 // TODO(fhorschig): Remove these when moving the Builder to |
| 97 // snippets::internal and trigger the request to intercept the request. |
| 98 std::string PreviewRequestBodyForTesting() { return BuildBody(); } |
| 99 std::string PreviewRequestHeadersForTesting() { return BuildHeaders(); } |
| 100 Builder& SetUserClassForTesting(const std::string& user_class) { |
| 101 user_class_ = user_class; |
| 102 return *this; |
| 103 } |
| 104 |
| 105 private: |
| 106 std::string BuildHeaders() const; |
| 107 std::string BuildBody() const; |
| 108 std::unique_ptr<net::URLFetcher> BuildURLFetcher( |
| 109 net::URLFetcherDelegate* request, |
| 110 const std::string& headers, |
| 111 const std::string& body) const; |
| 112 |
| 113 bool ReturnOnlyPersonalizedResults() const { |
| 114 return !obfuscated_gaia_id_.empty() && |
| 115 personalization_ == Personalization::kPersonal; |
| 116 } |
| 117 |
| 118 void PrepareLanguages( |
| 119 translate::LanguageModel::LanguageInfo* ui_language, |
| 120 translate::LanguageModel::LanguageInfo* other_top_language) const; |
| 121 |
| 122 // Only required, if the request needs to be sent. |
| 123 std::string auth_header_; |
| 124 base::TickClock* tick_clock_; |
| 125 FetchAPI fetch_api_; |
| 126 NTPSnippetsRequestParams params_; |
| 127 ParseJSONCallback parse_json_callback_; |
| 128 Personalization personalization_; |
| 129 GURL url_; |
| 130 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 131 |
| 132 // Optional properties. |
| 133 std::string obfuscated_gaia_id_; |
| 134 std::string user_class_; |
| 135 const translate::LanguageModel* language_model_; |
| 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(Builder); |
| 138 }; |
| 139 |
| 140 NTPSnippetsJsonRequest(base::Optional<Category> exclusive_category, |
| 141 base::TickClock* tick_clock, |
| 142 const ParseJSONCallback& callback); |
| 143 NTPSnippetsJsonRequest(NTPSnippetsJsonRequest&&); |
| 144 ~NTPSnippetsJsonRequest() override; |
| 145 |
| 146 void Start(CompletedCallback callback); |
| 147 |
| 148 const base::Optional<Category>& exclusive_category() const { |
| 149 return exclusive_category_; |
| 150 } |
| 151 |
| 152 base::TimeDelta GetFetchDuration() const; |
| 153 std::string GetResponseString() const; |
| 154 |
| 155 private: |
| 156 friend class Builder; |
| 157 |
| 158 // URLFetcherDelegate implementation. |
| 159 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 160 |
| 161 void ParseJsonResponse(); |
| 162 void OnJsonParsed(std::unique_ptr<base::Value> result); |
| 163 void OnJsonError(const std::string& error); |
| 164 |
| 165 // The fetcher for downloading the snippets. Only non-null if a fetch is |
| 166 // currently ongoing. |
| 167 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 168 |
| 169 // If set, only return results for this category. |
| 170 base::Optional<Category> exclusive_category_; |
| 171 |
| 172 // Use the TickClock from the Fetcher to measure the fetch time. It will be |
| 173 // used on creation and after the fetch returned. It has to be alive until the |
| 174 // request is destroyed. |
| 175 base::TickClock* tick_clock_; |
| 176 base::TimeTicks creation_time_; |
| 177 |
| 178 // This callback is called to parse a json string. It contains callbacks for |
| 179 // error and success cases. |
| 180 ParseJSONCallback parse_json_callback_; |
| 181 |
| 182 // The callback to notify when URLFetcher finished and results are available. |
| 183 CompletedCallback request_completed_callback_; |
| 184 |
| 185 base::WeakPtrFactory<NTPSnippetsJsonRequest> weak_ptr_factory_; |
| 186 |
| 187 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsJsonRequest); |
| 188 }; |
| 189 |
| 190 } // namespace internal |
| 191 |
| 192 } // namespace ntp_snippets |
| 193 |
| 194 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_JSON_REQUEST_H_ |
OLD | NEW |