| 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_REMOTE_JSON_REQUEST_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_JSON_REQUEST_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_JSON_REQUEST_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_JSON_REQUEST_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/optional.h" | 14 #include "base/optional.h" |
| 15 #include "components/ntp_snippets/remote/request_params.h" | 15 #include "components/ntp_snippets/remote/request_params.h" |
| 16 #include "components/ntp_snippets/status.h" | 16 #include "components/ntp_snippets/status.h" |
| 17 #include "components/translate/core/browser/language_model.h" | 17 #include "components/translate/core/browser/language_model.h" |
| 18 #include "google_apis/gaia/oauth2_token_service.h" | 18 #include "google_apis/gaia/oauth2_token_service.h" |
| 19 #include "net/http/http_request_headers.h" | 19 #include "net/http/http_request_headers.h" |
| 20 | 20 |
| 21 namespace base { | 21 namespace base { |
| 22 class Value; | 22 class Value; |
| 23 class TickClock; | 23 class TickClock; |
| 24 } // namespace base | 24 } // namespace base |
| 25 | 25 |
| 26 class FetchAPI; | 26 class FetchAPI; |
| 27 class Personalization; | |
| 28 | 27 |
| 29 namespace ntp_snippets { | 28 namespace ntp_snippets { |
| 30 class UserClassifier; | 29 class UserClassifier; |
| 31 | 30 |
| 32 namespace internal { | 31 namespace internal { |
| 33 | 32 |
| 34 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | 33 // 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, | 34 // histograms, so do not change existing values. Insert new values at the end, |
| 36 // and update the histogram definition. | 35 // and update the histogram definition. |
| 37 enum class FetchResult { | 36 enum class FetchResult { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 | 74 |
| 76 Builder& SetAuthentication(const std::string& account_id, | 75 Builder& SetAuthentication(const std::string& account_id, |
| 77 const std::string& auth_header); | 76 const std::string& auth_header); |
| 78 Builder& SetCreationTime(base::TimeTicks creation_time); | 77 Builder& SetCreationTime(base::TimeTicks creation_time); |
| 79 Builder& SetFetchAPI(FetchAPI fetch_api); | 78 Builder& SetFetchAPI(FetchAPI fetch_api); |
| 80 // The language_model borrowed from the fetcher needs to stay alive until | 79 // The language_model borrowed from the fetcher needs to stay alive until |
| 81 // the request body is built. | 80 // the request body is built. |
| 82 Builder& SetLanguageModel(const translate::LanguageModel* language_model); | 81 Builder& SetLanguageModel(const translate::LanguageModel* language_model); |
| 83 Builder& SetParams(const RequestParams& params); | 82 Builder& SetParams(const RequestParams& params); |
| 84 Builder& SetParseJsonCallback(ParseJSONCallback callback); | 83 Builder& SetParseJsonCallback(ParseJSONCallback callback); |
| 85 Builder& SetPersonalization(Personalization personalization); | |
| 86 // The tick_clock borrowed from the fetcher will be injected into the | 84 // The tick_clock borrowed from the fetcher will be injected into the |
| 87 // request. It will be used at build time and after the fetch returned. | 85 // request. It will be used at build time and after the fetch returned. |
| 88 // It has to be alive until the request is destroyed. | 86 // It has to be alive until the request is destroyed. |
| 89 Builder& SetTickClock(base::TickClock* tick_clock); | 87 Builder& SetTickClock(base::TickClock* tick_clock); |
| 90 Builder& SetUrl(const GURL& url); | 88 Builder& SetUrl(const GURL& url); |
| 91 Builder& SetUrlRequestContextGetter( | 89 Builder& SetUrlRequestContextGetter( |
| 92 const scoped_refptr<net::URLRequestContextGetter>& context_getter); | 90 const scoped_refptr<net::URLRequestContextGetter>& context_getter); |
| 93 Builder& SetUserClassifier(const UserClassifier& user_classifier); | 91 Builder& SetUserClassifier(const UserClassifier& user_classifier); |
| 94 | 92 |
| 95 // These preview methods allow to inspect the Request without exposing it | 93 // These preview methods allow to inspect the Request without exposing it |
| 96 // publicly. | 94 // publicly. |
| 97 // TODO(fhorschig): Remove these when moving the Builder to | 95 // TODO(fhorschig): Remove these when moving the Builder to |
| 98 // snippets::internal and trigger the request to intercept the request. | 96 // snippets::internal and trigger the request to intercept the request. |
| 99 std::string PreviewRequestBodyForTesting() { return BuildBody(); } | 97 std::string PreviewRequestBodyForTesting() { return BuildBody(); } |
| 100 std::string PreviewRequestHeadersForTesting() { return BuildHeaders(); } | 98 std::string PreviewRequestHeadersForTesting() { return BuildHeaders(); } |
| 101 Builder& SetUserClassForTesting(const std::string& user_class) { | 99 Builder& SetUserClassForTesting(const std::string& user_class) { |
| 102 user_class_ = user_class; | 100 user_class_ = user_class; |
| 103 return *this; | 101 return *this; |
| 104 } | 102 } |
| 105 | 103 |
| 106 private: | 104 private: |
| 107 std::string BuildHeaders() const; | 105 std::string BuildHeaders() const; |
| 108 std::string BuildBody() const; | 106 std::string BuildBody() const; |
| 109 std::unique_ptr<net::URLFetcher> BuildURLFetcher( | 107 std::unique_ptr<net::URLFetcher> BuildURLFetcher( |
| 110 net::URLFetcherDelegate* request, | 108 net::URLFetcherDelegate* request, |
| 111 const std::string& headers, | 109 const std::string& headers, |
| 112 const std::string& body) const; | 110 const std::string& body) const; |
| 113 | 111 |
| 114 bool ReturnOnlyPersonalizedResults() const { | |
| 115 return !obfuscated_gaia_id_.empty() && | |
| 116 personalization_ == Personalization::kPersonal; | |
| 117 } | |
| 118 | |
| 119 void PrepareLanguages( | 112 void PrepareLanguages( |
| 120 translate::LanguageModel::LanguageInfo* ui_language, | 113 translate::LanguageModel::LanguageInfo* ui_language, |
| 121 translate::LanguageModel::LanguageInfo* other_top_language) const; | 114 translate::LanguageModel::LanguageInfo* other_top_language) const; |
| 122 | 115 |
| 123 // Only required, if the request needs to be sent. | 116 // Only required, if the request needs to be sent. |
| 124 std::string auth_header_; | 117 std::string auth_header_; |
| 125 base::TickClock* tick_clock_; | 118 base::TickClock* tick_clock_; |
| 126 FetchAPI fetch_api_; | 119 FetchAPI fetch_api_; |
| 127 RequestParams params_; | 120 RequestParams params_; |
| 128 ParseJSONCallback parse_json_callback_; | 121 ParseJSONCallback parse_json_callback_; |
| 129 Personalization personalization_; | |
| 130 GURL url_; | 122 GURL url_; |
| 131 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 123 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 132 | 124 |
| 133 // Optional properties. | 125 // Optional properties. |
| 134 std::string obfuscated_gaia_id_; | 126 std::string obfuscated_gaia_id_; |
| 135 std::string user_class_; | 127 std::string user_class_; |
| 136 const translate::LanguageModel* language_model_; | 128 const translate::LanguageModel* language_model_; |
| 137 | 129 |
| 138 DISALLOW_COPY_AND_ASSIGN(Builder); | 130 DISALLOW_COPY_AND_ASSIGN(Builder); |
| 139 }; | 131 }; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 base::WeakPtrFactory<JsonRequest> weak_ptr_factory_; | 176 base::WeakPtrFactory<JsonRequest> weak_ptr_factory_; |
| 185 | 177 |
| 186 DISALLOW_COPY_AND_ASSIGN(JsonRequest); | 178 DISALLOW_COPY_AND_ASSIGN(JsonRequest); |
| 187 }; | 179 }; |
| 188 | 180 |
| 189 } // namespace internal | 181 } // namespace internal |
| 190 | 182 |
| 191 } // namespace ntp_snippets | 183 } // namespace ntp_snippets |
| 192 | 184 |
| 193 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_JSON_REQUEST_H_ | 185 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_JSON_REQUEST_H_ |
| OLD | NEW |