| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/web_resource/web_resource_service.h" | 5 #include "chrome/browser/web_resource/web_resource_service.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
| 17 #include "chrome/browser/sync/sync_ui_util.h" | 17 #include "chrome/browser/sync/sync_ui_util.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/chrome_utility_messages.h" | 20 #include "chrome/common/chrome_utility_messages.h" |
| 21 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
| 22 #include "chrome/common/web_resource/web_resource_unpacker.h" | 22 #include "chrome/common/web_resource/web_resource_unpacker.h" |
| 23 #include "content/browser/browser_thread.h" | 23 #include "content/browser/browser_thread.h" |
| 24 #include "content/common/net/url_fetcher.h" | 24 #include "content/common/net/url_fetcher.h" |
| 25 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
| 26 #include "content/public/common/url_fetcher_delegate.h" |
| 26 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
| 27 #include "net/base/load_flags.h" | 28 #include "net/base/load_flags.h" |
| 28 #include "net/url_request/url_request_status.h" | 29 #include "net/url_request/url_request_status.h" |
| 29 | 30 |
| 30 class WebResourceService::WebResourceFetcher | 31 class WebResourceService::WebResourceFetcher |
| 31 : public URLFetcher::Delegate { | 32 : public content::URLFetcherDelegate { |
| 32 public: | 33 public: |
| 33 explicit WebResourceFetcher(WebResourceService* web_resource_service) : | 34 explicit WebResourceFetcher(WebResourceService* web_resource_service) : |
| 34 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), | 35 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), |
| 35 web_resource_service_(web_resource_service) { | 36 web_resource_service_(web_resource_service) { |
| 36 } | 37 } |
| 37 | 38 |
| 38 // Delay initial load of resource data into cache so as not to interfere | 39 // Delay initial load of resource data into cache so as not to interfere |
| 39 // with startup time. | 40 // with startup time. |
| 40 void StartAfterDelay(int64 delay_ms) { | 41 void StartAfterDelay(int64 delay_ms) { |
| 41 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 42 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 71 // Do not let url fetcher affect existing state in system context (by | 72 // Do not let url fetcher affect existing state in system context (by |
| 72 // setting cookies, for example). | 73 // setting cookies, for example). |
| 73 url_fetcher_->set_load_flags(net::LOAD_DISABLE_CACHE | | 74 url_fetcher_->set_load_flags(net::LOAD_DISABLE_CACHE | |
| 74 net::LOAD_DO_NOT_SAVE_COOKIES); | 75 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 75 net::URLRequestContextGetter* url_request_context_getter = | 76 net::URLRequestContextGetter* url_request_context_getter = |
| 76 g_browser_process->system_request_context(); | 77 g_browser_process->system_request_context(); |
| 77 url_fetcher_->set_request_context(url_request_context_getter); | 78 url_fetcher_->set_request_context(url_request_context_getter); |
| 78 url_fetcher_->Start(); | 79 url_fetcher_->Start(); |
| 79 } | 80 } |
| 80 | 81 |
| 81 // From URLFetcher::Delegate. | 82 // From content::URLFetcherDelegate. |
| 82 void OnURLFetchComplete(const URLFetcher* source, | 83 void OnURLFetchComplete(const URLFetcher* source) { |
| 83 const GURL& url, | |
| 84 const net::URLRequestStatus& status, | |
| 85 int response_code, | |
| 86 const net::ResponseCookies& cookies, | |
| 87 const std::string& data) { | |
| 88 // Delete the URLFetcher when this function exits. | 84 // Delete the URLFetcher when this function exits. |
| 89 scoped_ptr<URLFetcher> clean_up_fetcher(url_fetcher_.release()); | 85 scoped_ptr<URLFetcher> clean_up_fetcher(url_fetcher_.release()); |
| 90 | 86 |
| 91 // Don't parse data if attempt to download was unsuccessful. | 87 // Don't parse data if attempt to download was unsuccessful. |
| 92 // Stop loading new web resource data, and silently exit. | 88 // Stop loading new web resource data, and silently exit. |
| 93 if (!status.is_success() || (response_code != 200)) | 89 if (!source->status().is_success() || (source->response_code() != 200)) |
| 94 return; | 90 return; |
| 95 | 91 |
| 92 std::string data; |
| 93 CHECK(source->GetResponseAsString(&data)); |
| 96 web_resource_service_->UpdateResourceCache(data); | 94 web_resource_service_->UpdateResourceCache(data); |
| 97 web_resource_service_->Release(); | 95 web_resource_service_->Release(); |
| 98 } | 96 } |
| 99 | 97 |
| 100 private: | 98 private: |
| 101 // So that we can delay our start so as not to affect start-up time; also, | 99 // So that we can delay our start so as not to affect start-up time; also, |
| 102 // so that we can schedule future cache updates. | 100 // so that we can schedule future cache updates. |
| 103 ScopedRunnableMethodFactory<WebResourceFetcher> fetcher_factory_; | 101 ScopedRunnableMethodFactory<WebResourceFetcher> fetcher_factory_; |
| 104 | 102 |
| 105 // The tool that fetches the url data from the server. | 103 // The tool that fetches the url data from the server. |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 } | 293 } |
| 296 | 294 |
| 297 void WebResourceService::UpdateResourceCache(const std::string& json_data) { | 295 void WebResourceService::UpdateResourceCache(const std::string& json_data) { |
| 298 UnpackerClient* client = new UnpackerClient(this, json_data); | 296 UnpackerClient* client = new UnpackerClient(this, json_data); |
| 299 client->Start(); | 297 client->Start(); |
| 300 | 298 |
| 301 // Set cache update time in preferences. | 299 // Set cache update time in preferences. |
| 302 prefs_->SetString(last_update_time_pref_name_, | 300 prefs_->SetString(last_update_time_pref_name_, |
| 303 base::DoubleToString(base::Time::Now().ToDoubleT())); | 301 base::DoubleToString(base::Time::Now().ToDoubleT())); |
| 304 } | 302 } |
| OLD | NEW |