| 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 "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "components/ntp_snippets/remote/request_params.h" | 16 #include "components/ntp_snippets/remote/request_params.h" |
| 17 #include "components/ntp_snippets/status.h" | 17 #include "components/ntp_snippets/status.h" |
| 18 #include "components/translate/core/browser/language_model.h" | 18 #include "components/translate/core/browser/language_model.h" |
| 19 #include "google_apis/gaia/oauth2_token_service.h" | 19 #include "google_apis/gaia/oauth2_token_service.h" |
| 20 #include "net/http/http_request_headers.h" | 20 #include "net/http/http_request_headers.h" |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 class Value; | 23 class Value; |
| 24 class Clock; | 24 class Clock; |
| 25 } // namespace base | 25 } // namespace base |
| 26 | 26 |
| 27 class FetchAPI; | |
| 28 | |
| 29 namespace ntp_snippets { | 27 namespace ntp_snippets { |
| 30 class UserClassifier; | 28 class UserClassifier; |
| 31 | 29 |
| 32 namespace internal { | 30 namespace internal { |
| 33 | 31 |
| 34 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | 32 // 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, | 33 // histograms, so do not change existing values. Insert new values at the end, |
| 36 // and update the histogram definition. | 34 // and update the histogram definition. |
| 37 enum class FetchResult { | 35 enum class FetchResult { |
| 38 SUCCESS = 0, | 36 SUCCESS = 0, |
| 39 // DEPRECATED_EMPTY_HOSTS = 1, | 37 // DEPRECATED_EMPTY_HOSTS = 1, |
| 40 URL_REQUEST_STATUS_ERROR = 2, | 38 URL_REQUEST_STATUS_ERROR = 2, |
| 41 HTTP_ERROR = 3, | 39 HTTP_ERROR = 3, |
| 42 JSON_PARSE_ERROR = 4, | 40 JSON_PARSE_ERROR = 4, |
| 43 INVALID_SNIPPET_CONTENT_ERROR = 5, | 41 INVALID_SNIPPET_CONTENT_ERROR = 5, |
| 44 OAUTH_TOKEN_ERROR = 6, | 42 OAUTH_TOKEN_ERROR = 6, |
| 45 // DEPRECATED_INTERACTIVE_QUOTA_ERROR = 7, | 43 // DEPRECATED_INTERACTIVE_QUOTA_ERROR = 7, |
| 46 // DEPRECATED_NON_INTERACTIVE_QUOTA_ERROR = 8, | 44 // DEPRECATED_NON_INTERACTIVE_QUOTA_ERROR = 8, |
| 47 MISSING_API_KEY = 9, | 45 MISSING_API_KEY = 9, |
| 48 RESULT_MAX = 10 | 46 RESULT_MAX = 10 |
| 49 }; | 47 }; |
| 50 | 48 |
| 51 enum FetchAPI { | |
| 52 CHROME_READER_API, | |
| 53 CHROME_CONTENT_SUGGESTIONS_API, | |
| 54 }; | |
| 55 | |
| 56 // A single request to query remote suggestions. On success, the suggestions are | 49 // A single request to query remote suggestions. On success, the suggestions are |
| 57 // returned in parsed JSON form (base::Value). | 50 // returned in parsed JSON form (base::Value). |
| 58 class JsonRequest : public net::URLFetcherDelegate { | 51 class JsonRequest : public net::URLFetcherDelegate { |
| 59 public: | 52 public: |
| 60 // A client can expect error_details only, if there was any error during the | 53 // A client can expect error_details only, if there was any error during the |
| 61 // fetching or parsing. In successful cases, it will be an empty string. | 54 // fetching or parsing. In successful cases, it will be an empty string. |
| 62 using CompletedCallback = | 55 using CompletedCallback = |
| 63 base::OnceCallback<void(std::unique_ptr<base::Value> result, | 56 base::OnceCallback<void(std::unique_ptr<base::Value> result, |
| 64 FetchResult result_code, | 57 FetchResult result_code, |
| 65 const std::string& error_details)>; | 58 const std::string& error_details)>; |
| 66 | 59 |
| 67 // Builds authenticated and non-authenticated JsonRequests. | 60 // Builds authenticated and non-authenticated JsonRequests. |
| 68 class Builder { | 61 class Builder { |
| 69 public: | 62 public: |
| 70 Builder(); | 63 Builder(); |
| 71 Builder(Builder&&); | 64 Builder(Builder&&); |
| 72 ~Builder(); | 65 ~Builder(); |
| 73 | 66 |
| 74 // Builds a Request object that contains all data to fetch new snippets. | 67 // Builds a Request object that contains all data to fetch new snippets. |
| 75 std::unique_ptr<JsonRequest> Build() const; | 68 std::unique_ptr<JsonRequest> Build() const; |
| 76 | 69 |
| 77 Builder& SetAuthentication(const std::string& account_id, | 70 Builder& SetAuthentication(const std::string& account_id, |
| 78 const std::string& auth_header); | 71 const std::string& auth_header); |
| 79 Builder& SetCreationTime(base::TimeTicks creation_time); | 72 Builder& SetCreationTime(base::TimeTicks creation_time); |
| 80 Builder& SetFetchAPI(FetchAPI fetch_api); | |
| 81 // The language_model borrowed from the fetcher needs to stay alive until | 73 // The language_model borrowed from the fetcher needs to stay alive until |
| 82 // the request body is built. | 74 // the request body is built. |
| 83 Builder& SetLanguageModel(const translate::LanguageModel* language_model); | 75 Builder& SetLanguageModel(const translate::LanguageModel* language_model); |
| 84 Builder& SetParams(const RequestParams& params); | 76 Builder& SetParams(const RequestParams& params); |
| 85 Builder& SetParseJsonCallback(ParseJSONCallback callback); | 77 Builder& SetParseJsonCallback(ParseJSONCallback callback); |
| 86 // The clock borrowed from the fetcher will be injected into the | 78 // The clock borrowed from the fetcher will be injected into the |
| 87 // request. It will be used at build time and after the fetch returned. | 79 // request. It will be used at build time and after the fetch returned. |
| 88 // It has to be alive until the request is destroyed. | 80 // It has to be alive until the request is destroyed. |
| 89 Builder& SetClock(base::Clock* clock); | 81 Builder& SetClock(base::Clock* clock); |
| 90 Builder& SetUrl(const GURL& url); | 82 Builder& SetUrl(const GURL& url); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 111 const std::string& headers, | 103 const std::string& headers, |
| 112 const std::string& body) const; | 104 const std::string& body) const; |
| 113 | 105 |
| 114 void PrepareLanguages( | 106 void PrepareLanguages( |
| 115 translate::LanguageModel::LanguageInfo* ui_language, | 107 translate::LanguageModel::LanguageInfo* ui_language, |
| 116 translate::LanguageModel::LanguageInfo* other_top_language) const; | 108 translate::LanguageModel::LanguageInfo* other_top_language) const; |
| 117 | 109 |
| 118 // Only required, if the request needs to be sent. | 110 // Only required, if the request needs to be sent. |
| 119 std::string auth_header_; | 111 std::string auth_header_; |
| 120 base::Clock* clock_; | 112 base::Clock* clock_; |
| 121 FetchAPI fetch_api_; | |
| 122 RequestParams params_; | 113 RequestParams params_; |
| 123 ParseJSONCallback parse_json_callback_; | 114 ParseJSONCallback parse_json_callback_; |
| 124 GURL url_; | 115 GURL url_; |
| 125 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 116 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 126 | 117 |
| 127 // Optional properties. | 118 // Optional properties. |
| 128 std::string obfuscated_gaia_id_; | 119 std::string obfuscated_gaia_id_; |
| 129 std::string user_class_; | 120 std::string user_class_; |
| 130 const translate::LanguageModel* language_model_; | 121 const translate::LanguageModel* language_model_; |
| 131 | 122 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 base::WeakPtrFactory<JsonRequest> weak_ptr_factory_; | 169 base::WeakPtrFactory<JsonRequest> weak_ptr_factory_; |
| 179 | 170 |
| 180 DISALLOW_COPY_AND_ASSIGN(JsonRequest); | 171 DISALLOW_COPY_AND_ASSIGN(JsonRequest); |
| 181 }; | 172 }; |
| 182 | 173 |
| 183 } // namespace internal | 174 } // namespace internal |
| 184 | 175 |
| 185 } // namespace ntp_snippets | 176 } // namespace ntp_snippets |
| 186 | 177 |
| 187 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_JSON_REQUEST_H_ | 178 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_JSON_REQUEST_H_ |
| OLD | NEW |