Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/extensions/app_icon_loader_impl.h"
      6 
      7 #include "base/stl_util.h"
      8 #include "chrome/browser/extensions/extension_service.h"
      9 #include "chrome/browser/extensions/extension_system.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/common/extensions/extension.h"
     12 #include "chrome/common/extensions/extension_constants.h"
     13 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
     14 #include "ui/gfx/color_utils.h"
     15 #include "ui/gfx/image/image_skia_operations.h"
     16 
     17 namespace {
     18 
     19 const extensions::Extension* GetExtensionByID(Profile* profile,
     20                                               const std::string& id) {
     21   ExtensionService* service =
     22       extensions::ExtensionSystem::Get(profile)->extension_service();
     23   if (!service)
     24     return NULL;
     25   return service->GetInstalledExtension(id);
     26 }
     27 
     28 }  // namespace
     29 
     30 namespace extensions {
     31 
     32 AppIconLoaderImpl::AppIconLoaderImpl(
     33     Profile* profile,
     34     int icon_size,
     35     AppIconLoader::Delegate* delegate)
     36     : profile_(profile),
     37       delegate_(delegate),
     38       icon_size_(icon_size) {
     39 
     40 }
     41 
     42 AppIconLoaderImpl::~AppIconLoaderImpl() {
     43   STLDeleteContainerPairFirstPointers(map_.begin(), map_.end());
     44 }
     45 
     46 void AppIconLoaderImpl::FetchImage(const std::string& id) {
     47   for (ImageToExtensionIDMap::const_iterator i = map_.begin();
     48        i != map_.end(); ++i) {
     49     if (i->second == id)
     50       return;  // Already loading the image.
     51   }
     52 
     53   const extensions::Extension* extension = GetExtensionByID(profile_, id);
     54   if (!extension)
     55     return;
     56 
     57   extensions::IconImage* image = new extensions::IconImage(
     58       profile_,
     59       extension,
     60       extensions::IconsInfo::GetIcons(extension),
     61       icon_size_,
     62       extensions::IconsInfo::GetDefaultAppIcon(),
     63       this);
     64   // |map_| takes ownership of |image|.
     65   map_[image] = id;
     66 
     67   // Triggers image loading now instead of depending on paint message. This
     68   // makes the temp blank image be shown for shorter time and improves user
     69   // experience. See http://crbug.com/146114.
     70   image->image_skia().EnsureRepsForSupportedScaleFactors();
     71 }
     72 
     73 void AppIconLoaderImpl::ClearImage(const std::string& id) {
     74   for (ImageToExtensionIDMap::iterator i = map_.begin();
     75        i != map_.end(); ++i) {
     76     if (i->second == id) {
     77       delete i->first;
     78       map_.erase(i);
     79       break;
     80     }
     81   }
     82 }
     83 
     84 void AppIconLoaderImpl::UpdateImage(const std::string& id) {
     85   for (ImageToExtensionIDMap::iterator i = map_.begin();
     86        i != map_.end(); ++i) {
     87     if (i->second == id) {
     88       BuildImage(i->second, i->first->image_skia());
     89       break;
     90     }
     91   }
     92 }
     93 
     94 void AppIconLoaderImpl::OnExtensionIconImageChanged(
     95     extensions::IconImage* image) {
     96   ImageToExtensionIDMap::iterator i = map_.find(image);
     97   if (i == map_.end())
     98     return;  // The image has been removed, do nothing.
     99 
    100   BuildImage(i->second, i->first->image_skia());
    101 }
    102 
    103 void AppIconLoaderImpl::BuildImage(const std::string& id,
    104                                    const gfx::ImageSkia& icon) {
    105   gfx::ImageSkia image = icon;
    106 
    107   const ExtensionService* service =
    108       extensions::ExtensionSystem::Get(profile_)->extension_service();
    109   const bool enabled = service->IsExtensionEnabledForLauncher(id);
    110   if (!enabled) {
    111     const color_utils::HSL shift = {-1, 0, 0.6};
    112     image = gfx::ImageSkiaOperations::CreateHSLShiftedImage(image, shift);
    113   }
    114 
    115   delegate_->SetAppImage(id, image);
    116 }
    117 
    118 }  // namespace extensions
    119