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

Side by Side Diff: components/web_resource/web_resource_service.cc

Issue 2217683002: Allow embedder to use custom ResourceRequestAllowedNotifier (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Allow embedder to use custom ResourceRequestAllowedNotifier 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 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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 #include "components/web_resource/web_resource_service.h" 5 #include "components/web_resource/web_resource_service.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
(...skipping 25 matching lines...) Expand all
36 PrefService* prefs, 36 PrefService* prefs,
37 const GURL& web_resource_server, 37 const GURL& web_resource_server,
38 const std::string& application_locale, 38 const std::string& application_locale,
39 const char* last_update_time_pref_name, 39 const char* last_update_time_pref_name,
40 int start_fetch_delay_ms, 40 int start_fetch_delay_ms,
41 int cache_update_delay_ms, 41 int cache_update_delay_ms,
42 net::URLRequestContextGetter* request_context, 42 net::URLRequestContextGetter* request_context,
43 const char* disable_network_switch, 43 const char* disable_network_switch,
44 const ParseJSONCallback& parse_json_callback) 44 const ParseJSONCallback& parse_json_callback)
45 : prefs_(prefs), 45 : prefs_(prefs),
46 resource_request_allowed_notifier_(prefs, disable_network_switch), 46 resource_request_allowed_notifier_(
47 new ResourceRequestAllowedNotifier(prefs, disable_network_switch)),
48 fetch_scheduled_(false),
47 in_fetch_(false), 49 in_fetch_(false),
48 web_resource_server_(web_resource_server), 50 web_resource_server_(web_resource_server),
49 application_locale_(application_locale), 51 application_locale_(application_locale),
50 last_update_time_pref_name_(last_update_time_pref_name), 52 last_update_time_pref_name_(last_update_time_pref_name),
51 start_fetch_delay_ms_(start_fetch_delay_ms), 53 start_fetch_delay_ms_(start_fetch_delay_ms),
52 cache_update_delay_ms_(cache_update_delay_ms), 54 cache_update_delay_ms_(cache_update_delay_ms),
53 request_context_(request_context), 55 request_context_(request_context),
54 parse_json_callback_(parse_json_callback), 56 parse_json_callback_(parse_json_callback),
55 weak_ptr_factory_(this) { 57 weak_ptr_factory_(this) {
56 resource_request_allowed_notifier_.Init(this); 58 resource_request_allowed_notifier_->Init(this);
57 DCHECK(prefs); 59 DCHECK(prefs);
58 } 60 }
59 61
60 void WebResourceService::StartAfterDelay() { 62 void WebResourceService::StartAfterDelay() {
61 // If resource requests are not allowed, we'll get a callback when they are. 63 // If resource requests are not allowed, we'll get a callback when they are.
62 if (resource_request_allowed_notifier_.ResourceRequestsAllowed()) 64 if (resource_request_allowed_notifier_->ResourceRequestsAllowed())
63 OnResourceRequestsAllowed(); 65 OnResourceRequestsAllowed();
64 } 66 }
65 67
66 WebResourceService::~WebResourceService() { 68 WebResourceService::~WebResourceService() {
67 } 69 }
68 70
69 void WebResourceService::OnURLFetchComplete(const net::URLFetcher* source) { 71 void WebResourceService::OnURLFetchComplete(const net::URLFetcher* source) {
70 // Delete the URLFetcher when this function exits. 72 // Delete the URLFetcher when this function exits.
71 std::unique_ptr<net::URLFetcher> clean_up_fetcher(url_fetcher_.release()); 73 std::unique_ptr<net::URLFetcher> clean_up_fetcher(url_fetcher_.release());
72 74
(...skipping 19 matching lines...) Expand all
92 // Stop loading new web resource data, and silently exit. 94 // Stop loading new web resource data, and silently exit.
93 // We do not call parse_json_callback_, so we need to call EndFetch() 95 // We do not call parse_json_callback_, so we need to call EndFetch()
94 // ourselves. 96 // ourselves.
95 EndFetch(); 97 EndFetch();
96 } 98 }
97 } 99 }
98 100
99 // Delay initial load of resource data into cache so as not to interfere 101 // Delay initial load of resource data into cache so as not to interfere
100 // with startup time. 102 // with startup time.
101 void WebResourceService::ScheduleFetch(int64_t delay_ms) { 103 void WebResourceService::ScheduleFetch(int64_t delay_ms) {
104 if (fetch_scheduled_)
105 return;
106 fetch_scheduled_ = true;
102 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 107 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
103 FROM_HERE, base::Bind(&WebResourceService::StartFetch, 108 FROM_HERE, base::Bind(&WebResourceService::StartFetch,
104 weak_ptr_factory_.GetWeakPtr()), 109 weak_ptr_factory_.GetWeakPtr()),
105 base::TimeDelta::FromMilliseconds(delay_ms)); 110 base::TimeDelta::FromMilliseconds(delay_ms));
106 } 111 }
107 112
113 void WebResourceService::SetResourceRequestAllowedNotifier(
114 std::unique_ptr<ResourceRequestAllowedNotifier> notifier) {
115 resource_request_allowed_notifier_ = std::move(notifier);
116 resource_request_allowed_notifier_->Init(this);
117 }
118
119 bool WebResourceService::GetFetchScheduled() const {
120 return fetch_scheduled_;
121 }
122
108 // Initializes the fetching of data from the resource server. Data 123 // Initializes the fetching of data from the resource server. Data
109 // load calls OnURLFetchComplete. 124 // load calls OnURLFetchComplete.
110 void WebResourceService::StartFetch() { 125 void WebResourceService::StartFetch() {
126 // Set to false so that next fetch can be scheduled after this fetch or
127 // if we recieve notification that resource is allowed.
128 fetch_scheduled_ = false;
129 // Check whether fetching is allowed.
130 if (!resource_request_allowed_notifier_->ResourceRequestsAllowed())
131 return;
132
111 // First, put our next cache load on the MessageLoop. 133 // First, put our next cache load on the MessageLoop.
112 ScheduleFetch(cache_update_delay_ms_); 134 ScheduleFetch(cache_update_delay_ms_);
113 135
114 // Set cache update time in preferences. 136 // Set cache update time in preferences.
115 prefs_->SetString(last_update_time_pref_name_, 137 prefs_->SetString(last_update_time_pref_name_,
116 base::DoubleToString(base::Time::Now().ToDoubleT())); 138 base::DoubleToString(base::Time::Now().ToDoubleT()));
117 139
118 // If we are still fetching data, exit. 140 // If we are still fetching data, exit.
119 if (in_fetch_) 141 if (in_fetch_)
120 return; 142 return;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 // Wait at least |start_fetch_delay_ms_|. 203 // Wait at least |start_fetch_delay_ms_|.
182 if (ms_until_update > start_fetch_delay_ms_) 204 if (ms_until_update > start_fetch_delay_ms_)
183 delay = ms_until_update; 205 delay = ms_until_update;
184 } 206 }
185 } 207 }
186 // Start fetch and wait for UpdateResourceCache. 208 // Start fetch and wait for UpdateResourceCache.
187 ScheduleFetch(delay); 209 ScheduleFetch(delay);
188 } 210 }
189 211
190 } // namespace web_resource 212 } // namespace web_resource
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698