| 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/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 // Don't parse data if attempt to download was unsuccessful. | 90 // Don't parse data if attempt to download was unsuccessful. |
| 91 // Stop loading new web resource data, and silently exit. | 91 // Stop loading new web resource data, and silently exit. |
| 92 // We do not call parse_json_callback_, so we need to call EndFetch() | 92 // We do not call parse_json_callback_, so we need to call EndFetch() |
| 93 // ourselves. | 93 // ourselves. |
| 94 EndFetch(); | 94 EndFetch(); |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 // Delay initial load of resource data into cache so as not to interfere | 98 // Delay initial load of resource data into cache so as not to interfere |
| 99 // with startup time. | 99 // with startup time. |
| 100 void WebResourceService::ScheduleFetch(int64 delay_ms) { | 100 void WebResourceService::ScheduleFetch(int64_t delay_ms) { |
| 101 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 101 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 102 FROM_HERE, base::Bind(&WebResourceService::StartFetch, | 102 FROM_HERE, base::Bind(&WebResourceService::StartFetch, |
| 103 weak_ptr_factory_.GetWeakPtr()), | 103 weak_ptr_factory_.GetWeakPtr()), |
| 104 base::TimeDelta::FromMilliseconds(delay_ms)); | 104 base::TimeDelta::FromMilliseconds(delay_ms)); |
| 105 } | 105 } |
| 106 | 106 |
| 107 // Initializes the fetching of data from the resource server. Data | 107 // Initializes the fetching of data from the resource server. Data |
| 108 // load calls OnURLFetchComplete. | 108 // load calls OnURLFetchComplete. |
| 109 void WebResourceService::StartFetch() { | 109 void WebResourceService::StartFetch() { |
| 110 // First, put our next cache load on the MessageLoop. | 110 // First, put our next cache load on the MessageLoop. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 156 |
| 157 EndFetch(); | 157 EndFetch(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void WebResourceService::OnUnpackError(const std::string& error_message) { | 160 void WebResourceService::OnUnpackError(const std::string& error_message) { |
| 161 LOG(ERROR) << error_message; | 161 LOG(ERROR) << error_message; |
| 162 EndFetch(); | 162 EndFetch(); |
| 163 } | 163 } |
| 164 | 164 |
| 165 void WebResourceService::OnResourceRequestsAllowed() { | 165 void WebResourceService::OnResourceRequestsAllowed() { |
| 166 int64 delay = start_fetch_delay_ms_; | 166 int64_t delay = start_fetch_delay_ms_; |
| 167 // Check whether we have ever put a value in the web resource cache; | 167 // Check whether we have ever put a value in the web resource cache; |
| 168 // if so, pull it out and see if it's time to update again. | 168 // if so, pull it out and see if it's time to update again. |
| 169 if (prefs_->HasPrefPath(last_update_time_pref_name_)) { | 169 if (prefs_->HasPrefPath(last_update_time_pref_name_)) { |
| 170 std::string last_update_pref = | 170 std::string last_update_pref = |
| 171 prefs_->GetString(last_update_time_pref_name_); | 171 prefs_->GetString(last_update_time_pref_name_); |
| 172 if (!last_update_pref.empty()) { | 172 if (!last_update_pref.empty()) { |
| 173 double last_update_value; | 173 double last_update_value; |
| 174 base::StringToDouble(last_update_pref, &last_update_value); | 174 base::StringToDouble(last_update_pref, &last_update_value); |
| 175 int64 ms_until_update = | 175 int64_t ms_until_update = |
| 176 cache_update_delay_ms_ - | 176 cache_update_delay_ms_ - |
| 177 static_cast<int64>( | 177 static_cast<int64_t>( |
| 178 (base::Time::Now() - base::Time::FromDoubleT(last_update_value)) | 178 (base::Time::Now() - base::Time::FromDoubleT(last_update_value)) |
| 179 .InMilliseconds()); | 179 .InMilliseconds()); |
| 180 // Wait at least |start_fetch_delay_ms_|. | 180 // Wait at least |start_fetch_delay_ms_|. |
| 181 if (ms_until_update > start_fetch_delay_ms_) | 181 if (ms_until_update > start_fetch_delay_ms_) |
| 182 delay = ms_until_update; | 182 delay = ms_until_update; |
| 183 } | 183 } |
| 184 } | 184 } |
| 185 // Start fetch and wait for UpdateResourceCache. | 185 // Start fetch and wait for UpdateResourceCache. |
| 186 ScheduleFetch(delay); | 186 ScheduleFetch(delay); |
| 187 } | 187 } |
| 188 | 188 |
| 189 } // namespace web_resource | 189 } // namespace web_resource |
| OLD | NEW |