OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chromeos/app_mode/kiosk_app_data.h" | 5 #include "chrome/browser/chromeos/app_mode/kiosk_app_data.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
14 #include "base/task_scheduler/post_task.h" | 14 #include "base/task_scheduler/post_task.h" |
15 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/threading/sequenced_worker_pool.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/chromeos/app_mode/kiosk_app_data_delegate.h" | 18 #include "chrome/browser/chromeos/app_mode/kiosk_app_data_delegate.h" |
19 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" | 19 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" |
20 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
21 #include "chrome/browser/extensions/extension_util.h" | 21 #include "chrome/browser/extensions/extension_util.h" |
22 #include "chrome/browser/extensions/webstore_data_fetcher.h" | 22 #include "chrome/browser/extensions/webstore_data_fetcher.h" |
23 #include "chrome/browser/extensions/webstore_install_helper.h" | 23 #include "chrome/browser/extensions/webstore_install_helper.h" |
24 #include "chrome/browser/image_decoder.h" | |
25 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
26 #include "components/prefs/pref_service.h" | 25 #include "components/prefs/pref_service.h" |
27 #include "components/prefs/scoped_user_pref_update.h" | 26 #include "components/prefs/scoped_user_pref_update.h" |
28 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
29 #include "extensions/browser/extension_system.h" | 28 #include "extensions/browser/extension_system.h" |
30 #include "extensions/browser/image_loader.h" | 29 #include "extensions/browser/image_loader.h" |
31 #include "extensions/browser/sandboxed_unpacker.h" | 30 #include "extensions/browser/sandboxed_unpacker.h" |
32 #include "extensions/common/constants.h" | 31 #include "extensions/common/constants.h" |
33 #include "extensions/common/extension_urls.h" | 32 #include "extensions/common/extension_urls.h" |
34 #include "extensions/common/manifest.h" | 33 #include "extensions/common/manifest.h" |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 195 |
197 // Extracted meta data. | 196 // Extracted meta data. |
198 std::string name_; | 197 std::string name_; |
199 SkBitmap icon_; | 198 SkBitmap icon_; |
200 std::string required_platform_version_; | 199 std::string required_platform_version_; |
201 | 200 |
202 DISALLOW_COPY_AND_ASSIGN(CrxLoader); | 201 DISALLOW_COPY_AND_ASSIGN(CrxLoader); |
203 }; | 202 }; |
204 | 203 |
205 //////////////////////////////////////////////////////////////////////////////// | 204 //////////////////////////////////////////////////////////////////////////////// |
206 // KioskAppData::IconLoader | |
207 // Loads locally stored icon data and decode it. | |
208 | |
209 class KioskAppData::IconLoader { | |
210 public: | |
211 enum LoadResult { | |
212 SUCCESS, | |
213 FAILED_TO_LOAD, | |
214 FAILED_TO_DECODE, | |
215 }; | |
216 | |
217 IconLoader(const base::WeakPtr<KioskAppData>& client, | |
218 const base::FilePath& icon_path) | |
219 : client_(client), | |
220 icon_path_(icon_path), | |
221 load_result_(SUCCESS) {} | |
222 | |
223 void Start() { | |
224 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | |
225 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); | |
226 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
227 token, | |
228 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
229 task_runner_->PostTask(FROM_HERE, | |
230 base::Bind(&IconLoader::LoadOnBlockingPool, | |
231 base::Unretained(this))); | |
232 } | |
233 | |
234 private: | |
235 friend class base::RefCountedThreadSafe<IconLoader>; | |
236 | |
237 ~IconLoader() {} | |
238 | |
239 class IconImageRequest : public ImageDecoder::ImageRequest { | |
240 public: | |
241 IconImageRequest( | |
242 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
243 IconLoader* icon_loader) | |
244 : ImageRequest(task_runner), icon_loader_(icon_loader) {} | |
245 | |
246 void OnImageDecoded(const SkBitmap& decoded_image) override { | |
247 icon_loader_->icon_ = gfx::ImageSkia::CreateFrom1xBitmap(decoded_image); | |
248 icon_loader_->icon_.MakeThreadSafe(); | |
249 icon_loader_->ReportResultOnBlockingPool(SUCCESS); | |
250 delete this; | |
251 } | |
252 | |
253 void OnDecodeImageFailed() override { | |
254 icon_loader_->ReportResultOnBlockingPool(FAILED_TO_DECODE); | |
255 delete this; | |
256 } | |
257 | |
258 private: | |
259 ~IconImageRequest() override {} | |
260 IconLoader* icon_loader_; | |
261 }; | |
262 | |
263 // Loads the icon from locally stored |icon_path_| on the blocking pool | |
264 void LoadOnBlockingPool() { | |
265 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
266 | |
267 std::string data; | |
268 if (!base::ReadFileToString(base::FilePath(icon_path_), &data)) { | |
269 ReportResultOnBlockingPool(FAILED_TO_LOAD); | |
270 return; | |
271 } | |
272 raw_icon_ = base::RefCountedString::TakeString(&data); | |
273 | |
274 IconImageRequest* image_request = new IconImageRequest(task_runner_, this); | |
275 ImageDecoder::Start(image_request, raw_icon_->data()); | |
276 } | |
277 | |
278 void ReportResultOnBlockingPool(LoadResult result) { | |
279 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
280 | |
281 load_result_ = result; | |
282 BrowserThread::PostTask( | |
283 BrowserThread::UI, | |
284 FROM_HERE, | |
285 base::Bind(&IconLoader::ReportResultOnUIThread, | |
286 base::Unretained(this))); | |
287 } | |
288 | |
289 void NotifyClient() { | |
290 if (!client_) | |
291 return; | |
292 | |
293 if (load_result_ == SUCCESS) | |
294 client_->OnIconLoadSuccess(icon_); | |
295 else | |
296 client_->OnIconLoadFailure(); | |
297 } | |
298 | |
299 void ReportResultOnUIThread() { | |
300 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
301 | |
302 NotifyClient(); | |
303 delete this; | |
304 } | |
305 | |
306 base::WeakPtr<KioskAppData> client_; | |
307 base::FilePath icon_path_; | |
308 | |
309 LoadResult load_result_; | |
310 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
311 | |
312 gfx::ImageSkia icon_; | |
313 scoped_refptr<base::RefCountedString> raw_icon_; | |
314 | |
315 DISALLOW_COPY_AND_ASSIGN(IconLoader); | |
316 }; | |
317 | |
318 //////////////////////////////////////////////////////////////////////////////// | |
319 // KioskAppData::WebstoreDataParser | 205 // KioskAppData::WebstoreDataParser |
320 // Use WebstoreInstallHelper to parse the manifest and decode the icon. | 206 // Use WebstoreInstallHelper to parse the manifest and decode the icon. |
321 | 207 |
322 class KioskAppData::WebstoreDataParser | 208 class KioskAppData::WebstoreDataParser |
323 : public extensions::WebstoreInstallHelper::Delegate { | 209 : public extensions::WebstoreInstallHelper::Delegate { |
324 public: | 210 public: |
325 explicit WebstoreDataParser(const base::WeakPtr<KioskAppData>& client) | 211 explicit WebstoreDataParser(const base::WeakPtr<KioskAppData>& client) |
326 : client_(client) {} | 212 : client_(client) {} |
327 | 213 |
328 void Start(const std::string& app_id, | 214 void Start(const std::string& app_id, |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 icon_path_.clear(); | 420 icon_path_.clear(); |
535 std::string icon_path_string; | 421 std::string icon_path_string; |
536 if (!dict->GetString(name_key, &name_) || | 422 if (!dict->GetString(name_key, &name_) || |
537 !dict->GetString(icon_path_key, &icon_path_string) || | 423 !dict->GetString(icon_path_key, &icon_path_string) || |
538 !dict->GetString(required_platform_version_key, | 424 !dict->GetString(required_platform_version_key, |
539 &required_platform_version_)) { | 425 &required_platform_version_)) { |
540 return false; | 426 return false; |
541 } | 427 } |
542 icon_path_ = base::FilePath(icon_path_string); | 428 icon_path_ = base::FilePath(icon_path_string); |
543 | 429 |
544 // IconLoader deletes itself when done. | 430 // KioskAppIconLoader deletes itself when done. |
545 (new IconLoader(AsWeakPtr(), icon_path_))->Start(); | 431 (new KioskAppIconLoader(AsWeakPtr(), icon_path_))->Start(); |
546 return true; | 432 return true; |
547 } | 433 } |
548 | 434 |
549 void KioskAppData::SetCache(const std::string& name, | 435 void KioskAppData::SetCache(const std::string& name, |
550 const base::FilePath& icon_path, | 436 const base::FilePath& icon_path, |
551 const std::string& required_platform_version) { | 437 const std::string& required_platform_version) { |
552 name_ = name; | 438 name_ = name; |
553 icon_path_ = icon_path; | 439 icon_path_ = icon_path; |
554 required_platform_version_ = required_platform_version; | 440 required_platform_version_ = required_platform_version; |
555 | 441 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
720 | 606 |
721 SkBitmap icon = crx_loader->icon(); | 607 SkBitmap icon = crx_loader->icon(); |
722 if (icon.empty()) | 608 if (icon.empty()) |
723 icon = *extensions::util::GetDefaultAppIcon().bitmap(); | 609 icon = *extensions::util::GetDefaultAppIcon().bitmap(); |
724 SetCache(crx_loader->name(), icon, crx_loader->required_platform_version()); | 610 SetCache(crx_loader->name(), icon, crx_loader->required_platform_version()); |
725 | 611 |
726 SetStatus(STATUS_LOADED); | 612 SetStatus(STATUS_LOADED); |
727 } | 613 } |
728 | 614 |
729 } // namespace chromeos | 615 } // namespace chromeos |
OLD | NEW |