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" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
| 126 // TODO(willchan): Look for a better signal of whether we're in a unit test |
| 127 // or not. Using |resource_dispatcher_host_| for this is pretty lame. |
126 // If we don't have a resource_dispatcher_host_, assume we're in | 128 // If we don't have a resource_dispatcher_host_, assume we're in |
127 // a test and run the unpacker directly in-process. | 129 // a test and run the unpacker directly in-process. |
128 bool use_utility_process = | 130 bool use_utility_process = |
129 web_resource_service_->resource_dispatcher_host_ != NULL && | 131 web_resource_service_->resource_dispatcher_host_ != NULL && |
130 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess); | 132 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess); |
131 if (use_utility_process) { | 133 if (use_utility_process) { |
132 BrowserThread::ID thread_id; | 134 BrowserThread::ID thread_id; |
133 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); | 135 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); |
134 BrowserThread::PostTask( | 136 BrowserThread::PostTask( |
135 BrowserThread::IO, FROM_HERE, | 137 BrowserThread::IO, FROM_HERE, |
136 NewRunnableMethod(this, &UnpackerClient::StartProcessOnIOThread, | 138 NewRunnableMethod(this, &UnpackerClient::StartProcessOnIOThread, |
137 web_resource_service_->resource_dispatcher_host_, | |
138 thread_id)); | 139 thread_id)); |
139 } else { | 140 } else { |
140 WebResourceUnpacker unpacker(json_data_); | 141 WebResourceUnpacker unpacker(json_data_); |
141 if (unpacker.Run()) { | 142 if (unpacker.Run()) { |
142 OnUnpackWebResourceSucceeded(*unpacker.parsed_json()); | 143 OnUnpackWebResourceSucceeded(*unpacker.parsed_json()); |
143 } else { | 144 } else { |
144 OnUnpackWebResourceFailed(unpacker.error_message()); | 145 OnUnpackWebResourceFailed(unpacker.error_message()); |
145 } | 146 } |
146 } | 147 } |
147 } | 148 } |
(...skipping 23 matching lines...) Expand all Loading... |
171 | 172 |
172 // Release reference and set got_response_. | 173 // Release reference and set got_response_. |
173 void Cleanup() { | 174 void Cleanup() { |
174 if (got_response_) | 175 if (got_response_) |
175 return; | 176 return; |
176 | 177 |
177 got_response_ = true; | 178 got_response_ = true; |
178 Release(); | 179 Release(); |
179 } | 180 } |
180 | 181 |
181 void StartProcessOnIOThread(ResourceDispatcherHost* rdh, | 182 void StartProcessOnIOThread(BrowserThread::ID thread_id) { |
182 BrowserThread::ID thread_id) { | 183 UtilityProcessHost* host = new UtilityProcessHost(this, thread_id); |
183 UtilityProcessHost* host = new UtilityProcessHost(rdh, this, thread_id); | |
184 // TODO(mrc): get proper file path when we start using web resources | 184 // TODO(mrc): get proper file path when we start using web resources |
185 // that need to be unpacked. | 185 // that need to be unpacked. |
186 host->StartWebResourceUnpacker(json_data_); | 186 host->StartWebResourceUnpacker(json_data_); |
187 } | 187 } |
188 | 188 |
189 scoped_refptr<WebResourceService> web_resource_service_; | 189 scoped_refptr<WebResourceService> web_resource_service_; |
190 | 190 |
191 // Holds raw JSON string. | 191 // Holds raw JSON string. |
192 const std::string& json_data_; | 192 const std::string& json_data_; |
193 | 193 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 } | 281 } |
282 | 282 |
283 void WebResourceService::UpdateResourceCache(const std::string& json_data) { | 283 void WebResourceService::UpdateResourceCache(const std::string& json_data) { |
284 UnpackerClient* client = new UnpackerClient(this, json_data); | 284 UnpackerClient* client = new UnpackerClient(this, json_data); |
285 client->Start(); | 285 client->Start(); |
286 | 286 |
287 // Set cache update time in preferences. | 287 // Set cache update time in preferences. |
288 prefs_->SetString(last_update_time_pref_name_, | 288 prefs_->SetString(last_update_time_pref_name_, |
289 base::DoubleToString(base::Time::Now().ToDoubleT())); | 289 base::DoubleToString(base::Time::Now().ToDoubleT())); |
290 } | 290 } |
OLD | NEW |