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

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: Build issue 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 18 matching lines...) Expand all
57 58
58 namespace ntp_snippets { 59 namespace ntp_snippets {
59 60
60 class NTPSnippetsDatabase; 61 class NTPSnippetsDatabase;
61 class NTPSnippetsServiceObserver; 62 class NTPSnippetsServiceObserver;
62 63
63 // Retrieves fresh content data (articles) from the server, stores them and 64 // Retrieves fresh content data (articles) from the server, stores them and
64 // provides them as content suggestions. 65 // provides them as content suggestions.
65 // TODO(pke): Rename this service to ArticleSuggestionsProvider and move to 66 // TODO(pke): Rename this service to ArticleSuggestionsProvider and move to
66 // a subdirectory. 67 // a subdirectory.
68 // TODO(jkrcal): this class grows really, really large. The fact that
69 // NTPSnippetService also implements ImageFetcherDelegate adds unnecssary
70 // complexity (and after all the Service is conceptually not an
71 // ImagerFetcherDeletage ;-)). Instead, the cleaner solution would be to define
72 // a CachedImageFetcher class that handles the caching aspects and looks like an
73 // image fetcher to the NTPSnippetService.
67 class NTPSnippetsService : public image_fetcher::ImageFetcherDelegate, 74 class NTPSnippetsService : public image_fetcher::ImageFetcherDelegate,
68 public ContentSuggestionsProvider { 75 public ContentSuggestionsProvider {
69 public: 76 public:
70 // |application_language_code| should be a ISO 639-1 compliant string, e.g. 77 // |application_language_code| should be a ISO 639-1 compliant string, e.g.
71 // 'en' or 'en-US'. Note that this code should only specify the language, not 78 // 'en' or 'en-US'. Note that this code should only specify the language, not
72 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' 79 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US'
73 // (British English person in the US) are not language codes. 80 // (British English person in the US) are not language codes.
74 NTPSnippetsService(Observer* observer, 81 NTPSnippetsService(Observer* observer,
75 CategoryFactory* category_factory, 82 CategoryFactory* category_factory,
76 PrefService* pref_service, 83 PrefService* pref_service,
(...skipping 13 matching lines...) Expand all
90 // Returns whether the service is ready. While this is false, the list of 97 // 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) 98 // snippets will be empty, and all modifications to it (fetch, dismiss, etc)
92 // will be ignored. 99 // will be ignored.
93 bool ready() const { return state_ == State::READY; } 100 bool ready() const { return state_ == State::READY; }
94 101
95 // Returns whether the service is initialized. While this is false, some 102 // Returns whether the service is initialized. While this is false, some
96 // calls may trigger DCHECKs. 103 // calls may trigger DCHECKs.
97 bool initialized() const { return ready() || state_ == State::DISABLED; } 104 bool initialized() const { return ready() || state_ == State::DISABLED; }
98 105
99 // Fetches snippets from the server and adds them to the current ones. 106 // 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 107 // Requests can be marked more important by setting |interactive_request| to
101 // (such request might circumvent the daily quota for requests, etc.) Useful 108 // true (such request might circumvent the daily quota for requests, etc.)
102 // for requests triggered by the user. 109 // Useful for requests triggered by the user.
103 void FetchSnippets(bool force_request); 110 void FetchSnippets(bool interactive_request);
104 111
105 // Fetches snippets from the server for specified hosts (overriding 112 // Fetches snippets from the server for specified hosts (overriding
106 // suggestions from the suggestion service) and adds them to the current ones. 113 // suggestions from the suggestion service) and adds them to the current ones.
107 // Only called from chrome://snippets-internals, DO NOT USE otherwise! 114 // Only called from chrome://snippets-internals, DO NOT USE otherwise!
108 // Ignored while |loaded()| is false. 115 // Ignored while |loaded()| is false.
109 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, 116 void FetchSnippetsFromHosts(const std::set<std::string>& hosts,
110 bool force_request); 117 bool interactive_request);
111 118
112 const NTPSnippetsFetcher* snippets_fetcher() const { 119 const NTPSnippetsFetcher* snippets_fetcher() const {
113 return snippets_fetcher_.get(); 120 return snippets_fetcher_.get();
114 } 121 }
115 122
116 // Returns a reason why the service is disabled, or DisabledReason::NONE 123 // Returns a reason why the service is disabled, or DisabledReason::NONE
117 // if it's not. 124 // if it's not.
118 DisabledReason disabled_reason() const { 125 DisabledReason disabled_reason() const {
119 return snippets_status_service_->disabled_reason(); 126 return snippets_status_service_->disabled_reason();
120 } 127 }
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); 215 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts);
209 216
210 // Removes the expired snippets (including dismissed) from the service and the 217 // Removes the expired snippets (including dismissed) from the service and the
211 // database, and schedules another pass for the next expiration. 218 // database, and schedules another pass for the next expiration.
212 void ClearExpiredSnippets(); 219 void ClearExpiredSnippets();
213 220
214 // Completes the initialization phase of the service, registering the last 221 // Completes the initialization phase of the service, registering the last
215 // observers. This is done after construction, once the database is loaded. 222 // observers. This is done after construction, once the database is loaded.
216 void FinishInitialization(); 223 void FinishInitialization();
217 224
218 void OnSnippetImageFetchedFromDatabase(const std::string& snippet_id, 225 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback,
219 const ImageFetchedCallback& callback, 226 const std::string& snippet_id,
220 std::string data); 227 std::string data);
221 228
222 void OnSnippetImageDecoded(const std::string& snippet_id, 229 void OnSnippetImageDecodedFromDatabase(const ImageFetchedCallback& callback,
223 const ImageFetchedCallback& callback, 230 const std::string& snippet_id,
224 const gfx::Image& image); 231 const gfx::Image& image);
225 232
226 void FetchSnippetImageFromNetwork(const std::string& snippet_id, 233 void FetchSnippetImageFromNetwork(const std::string& snippet_id,
227 const ImageFetchedCallback& callback); 234 const ImageFetchedCallback& callback);
228 235
236 void OnSnippetImageDecodedFromNetwork(const ImageFetchedCallback& callback,
237 const std::string& snippet_id,
238 const gfx::Image& image);
239
229 // Triggers a state transition depending on the provided reason to be 240 // 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 241 // disabled (or lack thereof). This method is called when a change is detected
231 // by |snippets_status_service_|. 242 // by |snippets_status_service_|.
232 void OnDisabledReasonChanged(DisabledReason disabled_reason); 243 void OnDisabledReasonChanged(DisabledReason disabled_reason);
233 244
234 // Verifies state transitions (see |State|'s documentation) and applies them. 245 // Verifies state transitions (see |State|'s documentation) and applies them.
235 // Also updates the provider status. Does nothing except updating the provider 246 // Also updates the provider status. Does nothing except updating the provider
236 // status if called with the current state. 247 // status if called with the current state.
237 void EnterState(State state, CategoryStatus status); 248 void EnterState(State state, CategoryStatus status);
238 249
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 308
298 // The service that provides events and data about the signin and sync state. 309 // The service that provides events and data about the signin and sync state.
299 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; 310 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_;
300 311
301 // Set to true if FetchSnippets is called before the database has been loaded. 312 // Set to true if FetchSnippets is called before the database has been loaded.
302 // The fetch will be executed after the database load finishes. 313 // The fetch will be executed after the database load finishes.
303 bool fetch_after_load_; 314 bool fetch_after_load_;
304 315
305 const Category provided_category_; 316 const Category provided_category_;
306 317
318 // Request throttler for limiting requests to thumbnail images.
319 RequestThrottler thumbnail_requests_throttler_;
320
307 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); 321 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService);
308 }; 322 };
309 323
310 } // namespace ntp_snippets 324 } // namespace ntp_snippets
311 325
312 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ 326 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_fetcher_unittest.cc ('k') | components/ntp_snippets/ntp_snippets_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698