| 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.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" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
| 24 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
| 25 | 25 |
| 26 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 | 27 |
| 28 // This class coordinates a web resource unpack and parse task which is run in | 28 // This class coordinates a web resource unpack and parse task which is run in |
| 29 // a separate process. Results are sent back to this class and routed to | 29 // a separate process. Results are sent back to this class and routed to |
| 30 // the WebResourceService. | 30 // the WebResourceService. |
| 31 class WebResourceService::UnpackerClient : public UtilityProcessHost::Client { | 31 class WebResourceService::UnpackerClient : public UtilityProcessHost::Client { |
| 32 public: | 32 public: |
| 33 explicit UnpackerClient(WebResourceService* web_resource_service) | 33 UnpackerClient(WebResourceService* web_resource_service, |
| 34 bool use_utility_process) |
| 34 : web_resource_service_(web_resource_service), | 35 : web_resource_service_(web_resource_service), |
| 35 resource_dispatcher_host_(g_browser_process->resource_dispatcher_host()), | 36 use_utility_process_(use_utility_process), |
| 36 got_response_(false) { | 37 got_response_(false) { |
| 37 } | 38 } |
| 38 | 39 |
| 39 void Start(const std::string& json_data) { | 40 void Start(const std::string& json_data) { |
| 40 AddRef(); // balanced in Cleanup. | 41 AddRef(); // balanced in Cleanup. |
| 41 | 42 |
| 42 // TODO(willchan): Look for a better signal of whether we're in a unit test | |
| 43 // or not. Using |resource_dispatcher_host_| for this is pretty lame. | |
| 44 // If we don't have a resource_dispatcher_host_, assume we're in | |
| 45 // a test and run the unpacker directly in-process. | |
| 46 bool use_utility_process = | 43 bool use_utility_process = |
| 47 resource_dispatcher_host_ != NULL && | 44 use_utility_process_ && |
| 48 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess); | 45 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess); |
| 49 if (use_utility_process) { | 46 if (use_utility_process) { |
| 50 BrowserThread::ID thread_id; | 47 BrowserThread::ID thread_id; |
| 51 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); | 48 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); |
| 52 BrowserThread::PostTask( | 49 BrowserThread::PostTask( |
| 53 BrowserThread::IO, FROM_HERE, | 50 BrowserThread::IO, FROM_HERE, |
| 54 base::Bind(&UnpackerClient::StartProcessOnIOThread, | 51 base::Bind(&UnpackerClient::StartProcessOnIOThread, |
| 55 this, thread_id, json_data)); | 52 this, thread_id, json_data)); |
| 56 } else { | 53 } else { |
| 57 WebResourceUnpacker unpacker(json_data); | 54 WebResourceUnpacker unpacker(json_data); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 const std::string& json_data) { | 108 const std::string& json_data) { |
| 112 UtilityProcessHost* host = new UtilityProcessHost(this, thread_id); | 109 UtilityProcessHost* host = new UtilityProcessHost(this, thread_id); |
| 113 host->set_use_linux_zygote(true); | 110 host->set_use_linux_zygote(true); |
| 114 // TODO(mrc): get proper file path when we start using web resources | 111 // TODO(mrc): get proper file path when we start using web resources |
| 115 // that need to be unpacked. | 112 // that need to be unpacked. |
| 116 host->Send(new ChromeUtilityMsg_UnpackWebResource(json_data)); | 113 host->Send(new ChromeUtilityMsg_UnpackWebResource(json_data)); |
| 117 } | 114 } |
| 118 | 115 |
| 119 scoped_refptr<WebResourceService> web_resource_service_; | 116 scoped_refptr<WebResourceService> web_resource_service_; |
| 120 | 117 |
| 121 // Owned by the global browser process. | 118 bool use_utility_process_; |
| 122 ResourceDispatcherHost* resource_dispatcher_host_; | |
| 123 | 119 |
| 124 // True if we got a response from the utility process and have cleaned up | 120 // True if we got a response from the utility process and have cleaned up |
| 125 // already. | 121 // already. |
| 126 bool got_response_; | 122 bool got_response_; |
| 127 }; | 123 }; |
| 128 | 124 |
| 129 WebResourceService::WebResourceService( | 125 WebResourceService::WebResourceService( |
| 130 PrefService* prefs, | 126 PrefService* prefs, |
| 131 const char* web_resource_server, | 127 const char* web_resource_server, |
| 132 bool apply_locale_to_url, | 128 bool apply_locale_to_url, |
| 133 const char* last_update_time_pref_name, | 129 const char* last_update_time_pref_name, |
| 134 int start_fetch_delay_ms, | 130 int start_fetch_delay_ms, |
| 135 int cache_update_delay_ms) | 131 int cache_update_delay_ms) |
| 136 : prefs_(prefs), | 132 : prefs_(prefs), |
| 137 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 133 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
| 138 in_fetch_(false), | 134 in_fetch_(false), |
| 139 web_resource_server_(web_resource_server), | 135 web_resource_server_(web_resource_server), |
| 140 apply_locale_to_url_(apply_locale_to_url), | 136 apply_locale_to_url_(apply_locale_to_url), |
| 141 last_update_time_pref_name_(last_update_time_pref_name), | 137 last_update_time_pref_name_(last_update_time_pref_name), |
| 142 start_fetch_delay_ms_(start_fetch_delay_ms), | 138 start_fetch_delay_ms_(start_fetch_delay_ms), |
| 143 cache_update_delay_ms_(cache_update_delay_ms) { | 139 cache_update_delay_ms_(cache_update_delay_ms), |
| 140 use_utility_process_(true) { |
| 144 DCHECK(prefs); | 141 DCHECK(prefs); |
| 145 } | 142 } |
| 146 | 143 |
| 147 WebResourceService::~WebResourceService() { } | 144 WebResourceService::~WebResourceService() { } |
| 148 | 145 |
| 149 void WebResourceService::EndFetch() { | 146 void WebResourceService::EndFetch() { |
| 150 in_fetch_ = false; | 147 in_fetch_ = false; |
| 151 } | 148 } |
| 152 | 149 |
| 153 void WebResourceService::StartAfterDelay() { | 150 void WebResourceService::StartAfterDelay() { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 | 221 |
| 225 // Don't parse data if attempt to download was unsuccessful. | 222 // Don't parse data if attempt to download was unsuccessful. |
| 226 // Stop loading new web resource data, and silently exit. | 223 // Stop loading new web resource data, and silently exit. |
| 227 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) | 224 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) |
| 228 return; | 225 return; |
| 229 | 226 |
| 230 std::string data; | 227 std::string data; |
| 231 source->GetResponseAsString(&data); | 228 source->GetResponseAsString(&data); |
| 232 | 229 |
| 233 // UnpackerClient releases itself. | 230 // UnpackerClient releases itself. |
| 234 UnpackerClient* client = new UnpackerClient(this); | 231 UnpackerClient* client = new UnpackerClient(this, use_utility_process_); |
| 235 client->Start(data); | 232 client->Start(data); |
| 236 | 233 |
| 237 Release(); | 234 Release(); |
| 238 } | 235 } |
| OLD | NEW |