Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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_(new ResourceRequestAllowedNotifier( |
| 47 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 Loading... | |
| 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 | |
| 108 // Initializes the fetching of data from the resource server. Data | 119 // Initializes the fetching of data from the resource server. Data |
| 109 // load calls OnURLFetchComplete. | 120 // load calls OnURLFetchComplete. |
| 110 void WebResourceService::StartFetch() { | 121 void WebResourceService::StartFetch() { |
| 122 // Set to false so that next fetch can be scheduled after this fetch or | |
| 123 // if we recieve notification that resource is allowed. | |
| 124 fetch_scheduled_ = false; | |
| 125 // Check whether fetching is allowed. | |
| 126 if (!resource_request_allowed_notifier_->ResourceRequestsAllowed()) { | |
| 127 return; | |
|
Dan Beam
2016/08/08 18:15:33
nit: this is currently 3 space indent, but should
| |
| 128 } | |
|
Dan Beam
2016/08/08 18:15:33
nit: no curlies
| |
| 129 | |
| 111 // First, put our next cache load on the MessageLoop. | 130 // First, put our next cache load on the MessageLoop. |
| 112 ScheduleFetch(cache_update_delay_ms_); | 131 ScheduleFetch(cache_update_delay_ms_); |
| 113 | 132 |
| 114 // Set cache update time in preferences. | 133 // Set cache update time in preferences. |
| 115 prefs_->SetString(last_update_time_pref_name_, | 134 prefs_->SetString(last_update_time_pref_name_, |
| 116 base::DoubleToString(base::Time::Now().ToDoubleT())); | 135 base::DoubleToString(base::Time::Now().ToDoubleT())); |
| 117 | 136 |
| 118 // If we are still fetching data, exit. | 137 // If we are still fetching data, exit. |
| 119 if (in_fetch_) | 138 if (in_fetch_) |
| 120 return; | 139 return; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 // Wait at least |start_fetch_delay_ms_|. | 200 // Wait at least |start_fetch_delay_ms_|. |
| 182 if (ms_until_update > start_fetch_delay_ms_) | 201 if (ms_until_update > start_fetch_delay_ms_) |
| 183 delay = ms_until_update; | 202 delay = ms_until_update; |
| 184 } | 203 } |
| 185 } | 204 } |
| 186 // Start fetch and wait for UpdateResourceCache. | 205 // Start fetch and wait for UpdateResourceCache. |
| 187 ScheduleFetch(delay); | 206 ScheduleFetch(delay); |
| 188 } | 207 } |
| 189 | 208 |
| 190 } // namespace web_resource | 209 } // namespace web_resource |
| OLD | NEW |