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

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.h

Issue 2227973002: Add request throttler to thumbnail fetching for articles on mobile NTP (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: One forgotten comment Created 4 years, 4 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_SERVICE_H_ 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <set> 11 #include <set>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/callback_forward.h" 15 #include "base/callback_forward.h"
16 #include "base/gtest_prod_util.h" 16 #include "base/gtest_prod_util.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/timer/timer.h" 18 #include "base/timer/timer.h"
19 #include "components/image_fetcher/image_fetcher_delegate.h" 19 #include "components/image_fetcher/image_fetcher_delegate.h"
20 #include "components/ntp_snippets/category.h" 20 #include "components/ntp_snippets/category.h"
21 #include "components/ntp_snippets/category_factory.h" 21 #include "components/ntp_snippets/category_factory.h"
22 #include "components/ntp_snippets/category_status.h" 22 #include "components/ntp_snippets/category_status.h"
23 #include "components/ntp_snippets/content_suggestion.h" 23 #include "components/ntp_snippets/content_suggestion.h"
24 #include "components/ntp_snippets/content_suggestions_provider.h" 24 #include "components/ntp_snippets/content_suggestions_provider.h"
25 #include "components/ntp_snippets/ntp_snippet.h" 25 #include "components/ntp_snippets/ntp_snippet.h"
26 #include "components/ntp_snippets/ntp_snippets_fetcher.h" 26 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
27 #include "components/ntp_snippets/ntp_snippets_scheduler.h" 27 #include "components/ntp_snippets/ntp_snippets_scheduler.h"
28 #include "components/ntp_snippets/ntp_snippets_status_service.h" 28 #include "components/ntp_snippets/ntp_snippets_status_service.h"
29 #include "components/ntp_snippets/request_throttler.h"
29 #include "components/suggestions/suggestions_service.h" 30 #include "components/suggestions/suggestions_service.h"
30 #include "components/sync/driver/sync_service_observer.h" 31 #include "components/sync/driver/sync_service_observer.h"
31 32
32 class PrefRegistrySimple; 33 class PrefRegistrySimple;
33 class PrefService; 34 class PrefService;
34 class SigninManagerBase; 35 class SigninManagerBase;
35 36
36 namespace base { 37 namespace base {
37 class RefCountedMemory; 38 class RefCountedMemory;
38 class Value; 39 class Value;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 // Returns whether the service is ready. While this is false, the list of 91 // Returns whether the service is ready. While this is false, the list of
91 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) 92 // snippets will be empty, and all modifications to it (fetch, dismiss, etc)
92 // will be ignored. 93 // will be ignored.
93 bool ready() const { return state_ == State::READY; } 94 bool ready() const { return state_ == State::READY; }
94 95
95 // Returns whether the service is initialized. While this is false, some 96 // Returns whether the service is initialized. While this is false, some
96 // calls may trigger DCHECKs. 97 // calls may trigger DCHECKs.
97 bool initialized() const { return ready() || state_ == State::DISABLED; } 98 bool initialized() const { return ready() || state_ == State::DISABLED; }
98 99
99 // Fetches snippets from the server and adds them to the current ones. 100 // Fetches snippets from the server and adds them to the current ones.
100 // Requests can be marked more important by setting |force_request| to true 101 // Requests can be marked more important by setting |interactive_request| to
101 // (such request might circumvent the daily quota for requests, etc.) Useful 102 // true (such request might circumvent the daily quota for requests, etc.)
102 // for requests triggered by the user. 103 // Useful for requests triggered by the user.
103 void FetchSnippets(bool force_request); 104 void FetchSnippets(bool interactive_request);
104 105
105 // Fetches snippets from the server for specified hosts (overriding 106 // Fetches snippets from the server for specified hosts (overriding
106 // suggestions from the suggestion service) and adds them to the current ones. 107 // suggestions from the suggestion service) and adds them to the current ones.
107 // Only called from chrome://snippets-internals, DO NOT USE otherwise! 108 // Only called from chrome://snippets-internals, DO NOT USE otherwise!
108 // Ignored while |loaded()| is false. 109 // Ignored while |loaded()| is false.
109 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, 110 void FetchSnippetsFromHosts(const std::set<std::string>& hosts,
110 bool force_request); 111 bool interactive_request);
111 112
112 const NTPSnippetsFetcher* snippets_fetcher() const { 113 const NTPSnippetsFetcher* snippets_fetcher() const {
113 return snippets_fetcher_.get(); 114 return snippets_fetcher_.get();
114 } 115 }
115 116
116 // Returns a reason why the service is disabled, or DisabledReason::NONE 117 // Returns a reason why the service is disabled, or DisabledReason::NONE
117 // if it's not. 118 // if it's not.
118 DisabledReason disabled_reason() const { 119 DisabledReason disabled_reason() const {
119 return snippets_status_service_->disabled_reason(); 120 return snippets_status_service_->disabled_reason();
120 } 121 }
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); 209 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts);
209 210
210 // Removes the expired snippets (including dismissed) from the service and the 211 // Removes the expired snippets (including dismissed) from the service and the
211 // database, and schedules another pass for the next expiration. 212 // database, and schedules another pass for the next expiration.
212 void ClearExpiredSnippets(); 213 void ClearExpiredSnippets();
213 214
214 // Completes the initialization phase of the service, registering the last 215 // Completes the initialization phase of the service, registering the last
215 // observers. This is done after construction, once the database is loaded. 216 // observers. This is done after construction, once the database is loaded.
216 void FinishInitialization(); 217 void FinishInitialization();
217 218
218 void OnSnippetImageFetchedFromDatabase(const std::string& snippet_id, 219 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback,
219 const ImageFetchedCallback& callback, 220 const std::string& snippet_id,
220 std::string data); 221 std::string data);
221 222
222 void OnSnippetImageDecoded(const std::string& snippet_id, 223 void OnSnippetImageDecodedFromDatabase(const ImageFetchedCallback& callback,
223 const ImageFetchedCallback& callback, 224 const std::string& snippet_id,
224 const gfx::Image& image); 225 const gfx::Image& image);
225 226
226 void FetchSnippetImageFromNetwork(const std::string& snippet_id, 227 void FetchSnippetImageFromNetwork(const std::string& snippet_id,
227 const ImageFetchedCallback& callback); 228 const ImageFetchedCallback& callback);
228 229
230 void OnSnippetImageDecodedFromNetwork(const ImageFetchedCallback& callback,
231 const std::string& snippet_id,
232 const gfx::Image& image);
233
229 // Triggers a state transition depending on the provided reason to be 234 // Triggers a state transition depending on the provided reason to be
230 // disabled (or lack thereof). This method is called when a change is detected 235 // disabled (or lack thereof). This method is called when a change is detected
231 // by |snippets_status_service_|. 236 // by |snippets_status_service_|.
232 void OnDisabledReasonChanged(DisabledReason disabled_reason); 237 void OnDisabledReasonChanged(DisabledReason disabled_reason);
233 238
234 // Verifies state transitions (see |State|'s documentation) and applies them. 239 // Verifies state transitions (see |State|'s documentation) and applies them.
235 // Also updates the provider status. Does nothing except updating the provider 240 // Also updates the provider status. Does nothing except updating the provider
236 // status if called with the current state. 241 // status if called with the current state.
237 void EnterState(State state, CategoryStatus status); 242 void EnterState(State state, CategoryStatus status);
238 243
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 302
298 // The service that provides events and data about the signin and sync state. 303 // The service that provides events and data about the signin and sync state.
299 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; 304 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_;
300 305
301 // Set to true if FetchSnippets is called before the database has been loaded. 306 // Set to true if FetchSnippets is called before the database has been loaded.
302 // The fetch will be executed after the database load finishes. 307 // The fetch will be executed after the database load finishes.
303 bool fetch_after_load_; 308 bool fetch_after_load_;
304 309
305 const Category provided_category_; 310 const Category provided_category_;
306 311
312 // Request throttler for limiting requests to thumbnail images.
313 RequestThrottler thumbnail_requests_throttler_;
314
307 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); 315 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService);
308 }; 316 };
309 317
310 } // namespace ntp_snippets 318 } // namespace ntp_snippets
311 319
312 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ 320 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698