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