Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: components/ntp_snippets/remote/json_request.h

Issue 2686063003: [remote suggestions] Attach the fetch time to RemoteSnippets, ContentSnippets and SnippetArticle (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/ntp_snippets/remote/request_params.h" 16 #include "components/ntp_snippets/remote/request_params.h"
16 #include "components/ntp_snippets/status.h" 17 #include "components/ntp_snippets/status.h"
17 #include "components/translate/core/browser/language_model.h" 18 #include "components/translate/core/browser/language_model.h"
18 #include "google_apis/gaia/oauth2_token_service.h" 19 #include "google_apis/gaia/oauth2_token_service.h"
19 #include "net/http/http_request_headers.h" 20 #include "net/http/http_request_headers.h"
20 21
21 namespace base { 22 namespace base {
22 class Value; 23 class Value;
23 class TickClock; 24 class Clock;
24 } // namespace base 25 } // namespace base
25 26
26 class FetchAPI; 27 class FetchAPI;
27 28
28 namespace ntp_snippets { 29 namespace ntp_snippets {
29 class UserClassifier; 30 class UserClassifier;
30 31
31 namespace internal { 32 namespace internal {
32 33
33 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA 34 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 75
75 Builder& SetAuthentication(const std::string& account_id, 76 Builder& SetAuthentication(const std::string& account_id,
76 const std::string& auth_header); 77 const std::string& auth_header);
77 Builder& SetCreationTime(base::TimeTicks creation_time); 78 Builder& SetCreationTime(base::TimeTicks creation_time);
78 Builder& SetFetchAPI(FetchAPI fetch_api); 79 Builder& SetFetchAPI(FetchAPI fetch_api);
79 // The language_model borrowed from the fetcher needs to stay alive until 80 // The language_model borrowed from the fetcher needs to stay alive until
80 // the request body is built. 81 // the request body is built.
81 Builder& SetLanguageModel(const translate::LanguageModel* language_model); 82 Builder& SetLanguageModel(const translate::LanguageModel* language_model);
82 Builder& SetParams(const RequestParams& params); 83 Builder& SetParams(const RequestParams& params);
83 Builder& SetParseJsonCallback(ParseJSONCallback callback); 84 Builder& SetParseJsonCallback(ParseJSONCallback callback);
84 // The tick_clock borrowed from the fetcher will be injected into the 85 // The clock borrowed from the fetcher will be injected into the
85 // request. It will be used at build time and after the fetch returned. 86 // request. It will be used at build time and after the fetch returned.
86 // It has to be alive until the request is destroyed. 87 // It has to be alive until the request is destroyed.
87 Builder& SetTickClock(base::TickClock* tick_clock); 88 Builder& SetClock(base::Clock* clock);
88 Builder& SetUrl(const GURL& url); 89 Builder& SetUrl(const GURL& url);
89 Builder& SetUrlRequestContextGetter( 90 Builder& SetUrlRequestContextGetter(
90 const scoped_refptr<net::URLRequestContextGetter>& context_getter); 91 const scoped_refptr<net::URLRequestContextGetter>& context_getter);
91 Builder& SetUserClassifier(const UserClassifier& user_classifier); 92 Builder& SetUserClassifier(const UserClassifier& user_classifier);
92 93
93 // These preview methods allow to inspect the Request without exposing it 94 // These preview methods allow to inspect the Request without exposing it
94 // publicly. 95 // publicly.
95 // TODO(fhorschig): Remove these when moving the Builder to 96 // TODO(fhorschig): Remove these when moving the Builder to
96 // snippets::internal and trigger the request to intercept the request. 97 // snippets::internal and trigger the request to intercept the request.
97 std::string PreviewRequestBodyForTesting() { return BuildBody(); } 98 std::string PreviewRequestBodyForTesting() { return BuildBody(); }
(...skipping 10 matching lines...) Expand all
108 net::URLFetcherDelegate* request, 109 net::URLFetcherDelegate* request,
109 const std::string& headers, 110 const std::string& headers,
110 const std::string& body) const; 111 const std::string& body) const;
111 112
112 void PrepareLanguages( 113 void PrepareLanguages(
113 translate::LanguageModel::LanguageInfo* ui_language, 114 translate::LanguageModel::LanguageInfo* ui_language,
114 translate::LanguageModel::LanguageInfo* other_top_language) const; 115 translate::LanguageModel::LanguageInfo* other_top_language) const;
115 116
116 // Only required, if the request needs to be sent. 117 // Only required, if the request needs to be sent.
117 std::string auth_header_; 118 std::string auth_header_;
118 base::TickClock* tick_clock_; 119 base::Clock* clock_;
119 FetchAPI fetch_api_; 120 FetchAPI fetch_api_;
120 RequestParams params_; 121 RequestParams params_;
121 ParseJSONCallback parse_json_callback_; 122 ParseJSONCallback parse_json_callback_;
122 GURL url_; 123 GURL url_;
123 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 124 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
124 125
125 // Optional properties. 126 // Optional properties.
126 std::string obfuscated_gaia_id_; 127 std::string obfuscated_gaia_id_;
127 std::string user_class_; 128 std::string user_class_;
128 const translate::LanguageModel* language_model_; 129 const translate::LanguageModel* language_model_;
129 130
130 DISALLOW_COPY_AND_ASSIGN(Builder); 131 DISALLOW_COPY_AND_ASSIGN(Builder);
131 }; 132 };
132 133
133 JsonRequest(base::Optional<Category> exclusive_category, 134 JsonRequest(base::Optional<Category> exclusive_category,
134 base::TickClock* tick_clock, 135 base::Clock* clock,
135 const ParseJSONCallback& callback); 136 const ParseJSONCallback& callback);
136 JsonRequest(JsonRequest&&); 137 JsonRequest(JsonRequest&&);
137 ~JsonRequest() override; 138 ~JsonRequest() override;
138 139
139 void Start(CompletedCallback callback); 140 void Start(CompletedCallback callback);
140 141
141 const base::Optional<Category>& exclusive_category() const { 142 const base::Optional<Category>& exclusive_category() const {
142 return exclusive_category_; 143 return exclusive_category_;
143 } 144 }
144 145
145 base::TimeDelta GetFetchDuration() const; 146 base::TimeDelta GetFetchDuration() const;
146 std::string GetResponseString() const; 147 std::string GetResponseString() const;
147 148
148 private: 149 private:
149 // URLFetcherDelegate implementation. 150 // URLFetcherDelegate implementation.
150 void OnURLFetchComplete(const net::URLFetcher* source) override; 151 void OnURLFetchComplete(const net::URLFetcher* source) override;
151 152
152 void ParseJsonResponse(); 153 void ParseJsonResponse();
153 void OnJsonParsed(std::unique_ptr<base::Value> result); 154 void OnJsonParsed(std::unique_ptr<base::Value> result);
154 void OnJsonError(const std::string& error); 155 void OnJsonError(const std::string& error);
155 156
156 // The fetcher for downloading the snippets. Only non-null if a fetch is 157 // The fetcher for downloading the snippets. Only non-null if a fetch is
157 // currently ongoing. 158 // currently ongoing.
158 std::unique_ptr<net::URLFetcher> url_fetcher_; 159 std::unique_ptr<net::URLFetcher> url_fetcher_;
159 160
160 // If set, only return results for this category. 161 // If set, only return results for this category.
161 base::Optional<Category> exclusive_category_; 162 base::Optional<Category> exclusive_category_;
162 163
163 // Use the TickClock from the Fetcher to measure the fetch time. It will be 164 // Use the Clock from the Fetcher to measure the fetch time. It will be
164 // used on creation and after the fetch returned. It has to be alive until the 165 // used on creation and after the fetch returned. It has to be alive until the
165 // request is destroyed. 166 // request is destroyed.
166 base::TickClock* tick_clock_; 167 base::Clock* clock_;
167 base::TimeTicks creation_time_; 168 base::Time creation_time_;
168 169
169 // This callback is called to parse a json string. It contains callbacks for 170 // This callback is called to parse a json string. It contains callbacks for
170 // error and success cases. 171 // error and success cases.
171 ParseJSONCallback parse_json_callback_; 172 ParseJSONCallback parse_json_callback_;
172 173
173 // The callback to notify when URLFetcher finished and results are available. 174 // The callback to notify when URLFetcher finished and results are available.
174 CompletedCallback request_completed_callback_; 175 CompletedCallback request_completed_callback_;
175 176
176 base::WeakPtrFactory<JsonRequest> weak_ptr_factory_; 177 base::WeakPtrFactory<JsonRequest> weak_ptr_factory_;
177 178
178 DISALLOW_COPY_AND_ASSIGN(JsonRequest); 179 DISALLOW_COPY_AND_ASSIGN(JsonRequest);
179 }; 180 };
180 181
181 } // namespace internal 182 } // namespace internal
182 183
183 } // namespace ntp_snippets 184 } // namespace ntp_snippets
184 185
185 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_JSON_REQUEST_H_ 186 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_JSON_REQUEST_H_
OLDNEW
« no previous file with comments | « components/ntp_snippets/content_suggestions_metrics_unittest.cc ('k') | components/ntp_snippets/remote/json_request.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698