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/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/prefs/scoped_user_pref_update.h" | 14 #include "base/prefs/scoped_user_pref_update.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" |
| 21 #include "chrome/browser/extensions/extension_system.h" |
| 22 #include "chrome/browser/extensions/image_loader.h" |
20 #include "chrome/browser/extensions/webstore_data_fetcher.h" | 23 #include "chrome/browser/extensions/webstore_data_fetcher.h" |
21 #include "chrome/browser/extensions/webstore_install_helper.h" | 24 #include "chrome/browser/extensions/webstore_install_helper.h" |
22 #include "chrome/browser/image_decoder.h" | 25 #include "chrome/browser/image_decoder.h" |
| 26 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/common/extensions/extension_constants.h" | 27 #include "chrome/common/extensions/extension_constants.h" |
| 28 #include "chrome/common/extensions/manifest_handlers/icons_handler.h" |
24 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
25 #include "extensions/common/manifest.h" | 30 #include "extensions/common/manifest.h" |
26 #include "extensions/common/manifest_constants.h" | 31 #include "extensions/common/manifest_constants.h" |
27 #include "ui/gfx/codec/png_codec.h" | 32 #include "ui/gfx/codec/png_codec.h" |
| 33 #include "ui/gfx/image/image.h" |
28 | 34 |
29 using content::BrowserThread; | 35 using content::BrowserThread; |
30 | 36 |
31 namespace chromeos { | 37 namespace chromeos { |
32 | 38 |
33 namespace { | 39 namespace { |
34 | 40 |
35 // Keys for local state data. See sample layout in KioskAppManager. | 41 // Keys for local state data. See sample layout in KioskAppManager. |
36 const char kKeyName[] = "name"; | 42 const char kKeyName[] = "name"; |
37 const char kKeyIcon[] = "icon"; | 43 const char kKeyIcon[] = "icon"; |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 std::string app_key = std::string(KioskAppManager::kKeyApps) + '.' + app_id_; | 287 std::string app_key = std::string(KioskAppManager::kKeyApps) + '.' + app_id_; |
282 dict_update->Remove(app_key, NULL); | 288 dict_update->Remove(app_key, NULL); |
283 | 289 |
284 if (!icon_path_.empty()) { | 290 if (!icon_path_.empty()) { |
285 BrowserThread::PostBlockingPoolTask( | 291 BrowserThread::PostBlockingPoolTask( |
286 FROM_HERE, | 292 FROM_HERE, |
287 base::Bind(base::IgnoreResult(&base::DeleteFile), icon_path_, false)); | 293 base::Bind(base::IgnoreResult(&base::DeleteFile), icon_path_, false)); |
288 } | 294 } |
289 } | 295 } |
290 | 296 |
| 297 void KioskAppData::LoadFromInstalledApp(Profile* profile, |
| 298 const extensions::Extension* app) { |
| 299 SetStatus(STATUS_LOADING); |
| 300 |
| 301 if (!app) { |
| 302 app = extensions::ExtensionSystem::Get(profile) |
| 303 ->extension_service() |
| 304 ->GetInstalledExtension(app_id_); |
| 305 } |
| 306 |
| 307 DCHECK_EQ(app_id_, app->id()); |
| 308 |
| 309 name_ = app->name(); |
| 310 |
| 311 const int kIconSize = extension_misc::EXTENSION_ICON_LARGE; |
| 312 extensions::ExtensionResource image = extensions::IconsInfo::GetIconResource( |
| 313 app, kIconSize, ExtensionIconSet::MATCH_BIGGER); |
| 314 extensions::ImageLoader::Get(profile)->LoadImageAsync( |
| 315 app, image, gfx::Size(kIconSize, kIconSize), |
| 316 base::Bind(&KioskAppData::OnExtensionIconLoaded, AsWeakPtr())); |
| 317 } |
| 318 |
291 bool KioskAppData::IsLoading() const { | 319 bool KioskAppData::IsLoading() const { |
292 return status_ == STATUS_LOADING; | 320 return status_ == STATUS_LOADING; |
293 } | 321 } |
294 | 322 |
295 void KioskAppData::SetStatus(Status status) { | 323 void KioskAppData::SetStatus(Status status) { |
296 if (status_ == status) | 324 if (status_ == status) |
297 return; | 325 return; |
298 | 326 |
299 status_ = status; | 327 status_ = status; |
300 | 328 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 std::string icon_path_key = app_key + '.' + kKeyIcon; | 375 std::string icon_path_key = app_key + '.' + kKeyIcon; |
348 | 376 |
349 PrefService* local_state = g_browser_process->local_state(); | 377 PrefService* local_state = g_browser_process->local_state(); |
350 DictionaryPrefUpdate dict_update(local_state, | 378 DictionaryPrefUpdate dict_update(local_state, |
351 KioskAppManager::kKioskDictionaryName); | 379 KioskAppManager::kKioskDictionaryName); |
352 dict_update->SetString(name_key, name); | 380 dict_update->SetString(name_key, name); |
353 dict_update->SetString(icon_path_key, icon_path.value()); | 381 dict_update->SetString(icon_path_key, icon_path.value()); |
354 icon_path_ = icon_path; | 382 icon_path_ = icon_path; |
355 } | 383 } |
356 | 384 |
357 void KioskAppData::OnIconLoadSuccess( | 385 void KioskAppData::SetCache(const std::string& name, const SkBitmap& icon) { |
358 const scoped_refptr<base::RefCountedString>& raw_icon, | |
359 const gfx::ImageSkia& icon) { | |
360 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
361 raw_icon_ = raw_icon; | |
362 icon_ = icon; | |
363 SetStatus(STATUS_LOADED); | |
364 } | |
365 | |
366 void KioskAppData::OnIconLoadFailure() { | |
367 // Re-fetch data from web store when failed to load cached data. | |
368 StartFetch(); | |
369 } | |
370 | |
371 void KioskAppData::OnWebstoreParseSuccess(const SkBitmap& icon) { | |
372 icon_ = gfx::ImageSkia::CreateFrom1xBitmap(icon); | 386 icon_ = gfx::ImageSkia::CreateFrom1xBitmap(icon); |
373 icon_.MakeThreadSafe(); | 387 icon_.MakeThreadSafe(); |
374 | 388 |
375 std::vector<unsigned char> image_data; | 389 std::vector<unsigned char> image_data; |
376 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &image_data)); | 390 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &image_data)); |
377 raw_icon_ = new base::RefCountedString; | 391 raw_icon_ = new base::RefCountedString; |
378 raw_icon_->data().assign(image_data.begin(), image_data.end()); | 392 raw_icon_->data().assign(image_data.begin(), image_data.end()); |
379 | 393 |
380 base::FilePath cache_dir; | 394 base::FilePath cache_dir; |
381 if (delegate_) | 395 if (delegate_) |
382 delegate_->GetKioskAppIconCacheDir(&cache_dir); | 396 delegate_->GetKioskAppIconCacheDir(&cache_dir); |
383 | 397 |
384 base::FilePath icon_path = | 398 base::FilePath icon_path = |
385 cache_dir.AppendASCII(app_id_).AddExtension(kIconFileExtension); | 399 cache_dir.AppendASCII(app_id_).AddExtension(kIconFileExtension); |
386 BrowserThread::GetBlockingPool()->PostTask( | 400 BrowserThread::GetBlockingPool()->PostTask( |
387 FROM_HERE, | 401 FROM_HERE, |
388 base::Bind(&SaveIconToLocalOnBlockingPool, icon_path, raw_icon_)); | 402 base::Bind(&SaveIconToLocalOnBlockingPool, icon_path, raw_icon_)); |
389 | 403 |
390 SetCache(name_, icon_path); | 404 SetCache(name, icon_path); |
| 405 } |
| 406 |
| 407 void KioskAppData::OnExtensionIconLoaded(const gfx::Image& icon) { |
| 408 if (icon.IsEmpty()) { |
| 409 LOG(WARNING) << "Failed to load icon from installed app" |
| 410 << ", id=" << app_id_; |
| 411 SetCache(name_, *extensions::IconsInfo::GetDefaultAppIcon().bitmap()); |
| 412 } else { |
| 413 SetCache(name_, icon.AsBitmap()); |
| 414 } |
| 415 |
391 SetStatus(STATUS_LOADED); | 416 SetStatus(STATUS_LOADED); |
392 } | 417 } |
393 | 418 |
| 419 void KioskAppData::OnIconLoadSuccess( |
| 420 const scoped_refptr<base::RefCountedString>& raw_icon, |
| 421 const gfx::ImageSkia& icon) { |
| 422 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 423 raw_icon_ = raw_icon; |
| 424 icon_ = icon; |
| 425 SetStatus(STATUS_LOADED); |
| 426 } |
| 427 |
| 428 void KioskAppData::OnIconLoadFailure() { |
| 429 // Re-fetch data from web store when failed to load cached data. |
| 430 StartFetch(); |
| 431 } |
| 432 |
| 433 void KioskAppData::OnWebstoreParseSuccess(const SkBitmap& icon) { |
| 434 SetCache(name_, icon); |
| 435 SetStatus(STATUS_LOADED); |
| 436 } |
| 437 |
394 void KioskAppData::OnWebstoreParseFailure() { | 438 void KioskAppData::OnWebstoreParseFailure() { |
395 SetStatus(STATUS_ERROR); | 439 SetStatus(STATUS_ERROR); |
396 } | 440 } |
397 | 441 |
398 void KioskAppData::StartFetch() { | 442 void KioskAppData::StartFetch() { |
399 webstore_fetcher_.reset(new extensions::WebstoreDataFetcher( | 443 webstore_fetcher_.reset(new extensions::WebstoreDataFetcher( |
400 this, | 444 this, |
401 GetRequestContextGetter(), | 445 GetRequestContextGetter(), |
402 GURL(), | 446 GURL(), |
403 app_id_)); | 447 app_id_)); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 if (!response->GetString(key, value)) { | 498 if (!response->GetString(key, value)) { |
455 LOG(ERROR) << "Webstore response error (" << key | 499 LOG(ERROR) << "Webstore response error (" << key |
456 << "): " << ValueToString(response); | 500 << "): " << ValueToString(response); |
457 OnWebstoreResponseParseFailure(kInvalidWebstoreResponseError); | 501 OnWebstoreResponseParseFailure(kInvalidWebstoreResponseError); |
458 return false; | 502 return false; |
459 } | 503 } |
460 return true; | 504 return true; |
461 } | 505 } |
462 | 506 |
463 } // namespace chromeos | 507 } // namespace chromeos |
OLD | NEW |