| 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/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/sync/sync_ui_util.h" | 18 #include "chrome/browser/sync/sync_ui_util.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/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
| 21 #include "chrome/common/web_resource/web_resource_unpacker.h" | 22 #include "chrome/common/web_resource/web_resource_unpacker.h" |
| 22 #include "content/browser/browser_thread.h" | 23 #include "content/browser/browser_thread.h" |
| 23 #include "content/common/notification_service.h" | 24 #include "content/common/notification_service.h" |
| 24 #include "content/common/url_fetcher.h" | 25 #include "content/common/url_fetcher.h" |
| 25 #include "googleurl/src/gurl.h" | 26 #include "googleurl/src/gurl.h" |
| 26 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
| 27 #include "net/url_request/url_request_status.h" | 28 #include "net/url_request/url_request_status.h" |
| 28 | 29 |
| 29 class WebResourceService::WebResourceFetcher | 30 class WebResourceService::WebResourceFetcher |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 // The tool that fetches the url data from the server. | 105 // The tool that fetches the url data from the server. |
| 105 scoped_ptr<URLFetcher> url_fetcher_; | 106 scoped_ptr<URLFetcher> url_fetcher_; |
| 106 | 107 |
| 107 // Our owner and creator. Ref counted. | 108 // Our owner and creator. Ref counted. |
| 108 WebResourceService* web_resource_service_; | 109 WebResourceService* web_resource_service_; |
| 109 }; | 110 }; |
| 110 | 111 |
| 111 // This class coordinates a web resource unpack and parse task which is run in | 112 // This class coordinates a web resource unpack and parse task which is run in |
| 112 // a separate process. Results are sent back to this class and routed to | 113 // a separate process. Results are sent back to this class and routed to |
| 113 // the WebResourceService. | 114 // the WebResourceService. |
| 114 class WebResourceService::UnpackerClient | 115 class WebResourceService::UnpackerClient : public UtilityProcessHost::Client { |
| 115 : public UtilityProcessHost::Client { | |
| 116 public: | 116 public: |
| 117 UnpackerClient(WebResourceService* web_resource_service, | 117 UnpackerClient(WebResourceService* web_resource_service, |
| 118 const std::string& json_data) | 118 const std::string& json_data) |
| 119 : web_resource_service_(web_resource_service), | 119 : web_resource_service_(web_resource_service), |
| 120 json_data_(json_data), got_response_(false) { | 120 json_data_(json_data), got_response_(false) { |
| 121 } | 121 } |
| 122 | 122 |
| 123 void Start() { | 123 void Start() { |
| 124 AddRef(); // balanced in Cleanup. | 124 AddRef(); // balanced in Cleanup. |
| 125 | 125 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 144 } else { | 144 } else { |
| 145 OnUnpackWebResourceFailed(unpacker.error_message()); | 145 OnUnpackWebResourceFailed(unpacker.error_message()); |
| 146 } | 146 } |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 | 149 |
| 150 private: | 150 private: |
| 151 ~UnpackerClient() {} | 151 ~UnpackerClient() {} |
| 152 | 152 |
| 153 // UtilityProcessHost::Client | 153 // UtilityProcessHost::Client |
| 154 virtual bool OnMessageReceived(const IPC::Message& message) { |
| 155 bool handled = true; |
| 156 IPC_BEGIN_MESSAGE_MAP(WebResourceService::UnpackerClient, message) |
| 157 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackWebResource_Succeeded, |
| 158 OnUnpackWebResourceSucceeded) |
| 159 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackWebResource_Failed, |
| 160 OnUnpackWebResourceFailed) |
| 161 IPC_MESSAGE_UNHANDLED(handled = false) |
| 162 IPC_END_MESSAGE_MAP_EX() |
| 163 return handled; |
| 164 } |
| 165 |
| 154 virtual void OnProcessCrashed(int exit_code) { | 166 virtual void OnProcessCrashed(int exit_code) { |
| 155 if (got_response_) | 167 if (got_response_) |
| 156 return; | 168 return; |
| 157 | 169 |
| 158 OnUnpackWebResourceFailed( | 170 OnUnpackWebResourceFailed( |
| 159 "Chrome crashed while trying to retrieve web resources."); | 171 "Chrome crashed while trying to retrieve web resources."); |
| 160 } | 172 } |
| 161 | 173 |
| 162 virtual void OnUnpackWebResourceSucceeded( | 174 void OnUnpackWebResourceSucceeded( |
| 163 const DictionaryValue& parsed_json) { | 175 const DictionaryValue& parsed_json) { |
| 164 web_resource_service_->OnWebResourceUnpacked(parsed_json); | 176 web_resource_service_->OnWebResourceUnpacked(parsed_json); |
| 165 Cleanup(); | 177 Cleanup(); |
| 166 } | 178 } |
| 167 | 179 |
| 168 virtual void OnUnpackWebResourceFailed(const std::string& error_message) { | 180 void OnUnpackWebResourceFailed(const std::string& error_message) { |
| 169 web_resource_service_->EndFetch(); | 181 web_resource_service_->EndFetch(); |
| 170 Cleanup(); | 182 Cleanup(); |
| 171 } | 183 } |
| 172 | 184 |
| 173 // Release reference and set got_response_. | 185 // Release reference and set got_response_. |
| 174 void Cleanup() { | 186 void Cleanup() { |
| 175 if (got_response_) | 187 if (got_response_) |
| 176 return; | 188 return; |
| 177 | 189 |
| 178 got_response_ = true; | 190 got_response_ = true; |
| 179 Release(); | 191 Release(); |
| 180 } | 192 } |
| 181 | 193 |
| 182 void StartProcessOnIOThread(BrowserThread::ID thread_id) { | 194 void StartProcessOnIOThread(BrowserThread::ID thread_id) { |
| 183 UtilityProcessHost* host = new UtilityProcessHost(this, thread_id); | 195 UtilityProcessHost* host = new UtilityProcessHost(this, thread_id); |
| 184 // TODO(mrc): get proper file path when we start using web resources | 196 // TODO(mrc): get proper file path when we start using web resources |
| 185 // that need to be unpacked. | 197 // that need to be unpacked. |
| 186 host->StartWebResourceUnpacker(json_data_); | 198 host->Send(new UtilityMsg_UnpackWebResource(json_data_)); |
| 187 } | 199 } |
| 188 | 200 |
| 189 scoped_refptr<WebResourceService> web_resource_service_; | 201 scoped_refptr<WebResourceService> web_resource_service_; |
| 190 | 202 |
| 191 // Holds raw JSON string. | 203 // Holds raw JSON string. |
| 192 const std::string& json_data_; | 204 const std::string& json_data_; |
| 193 | 205 |
| 194 // True if we got a response from the utility process and have cleaned up | 206 // True if we got a response from the utility process and have cleaned up |
| 195 // already. | 207 // already. |
| 196 bool got_response_; | 208 bool got_response_; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 } | 295 } |
| 284 | 296 |
| 285 void WebResourceService::UpdateResourceCache(const std::string& json_data) { | 297 void WebResourceService::UpdateResourceCache(const std::string& json_data) { |
| 286 UnpackerClient* client = new UnpackerClient(this, json_data); | 298 UnpackerClient* client = new UnpackerClient(this, json_data); |
| 287 client->Start(); | 299 client->Start(); |
| 288 | 300 |
| 289 // Set cache update time in preferences. | 301 // Set cache update time in preferences. |
| 290 prefs_->SetString(last_update_time_pref_name_, | 302 prefs_->SetString(last_update_time_pref_name_, |
| 291 base::DoubleToString(base::Time::Now().ToDoubleT())); | 303 base::DoubleToString(base::Time::Now().ToDoubleT())); |
| 292 } | 304 } |
| OLD | NEW |