0e40e65fc420458d39bdd0c7a7f81b43c02be41c
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_action_icon_factory.cc
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/extension_action_icon_factory.h"
6
7 #include "chrome/browser/extensions/extension_action.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/common/extensions/extension_icon_set.h"
10 #include "extensions/common/extension.h"
11 #include "grit/theme_resources.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/gfx/image/image_skia.h"
14
15 using extensions::Extension;
16 using extensions::IconImage;
17
18 namespace {
19
20 gfx::ImageSkia GetDefaultIcon() {
21   return *ui::ResourceBundle::GetSharedInstance().GetImageNamed(
22       IDR_EXTENSIONS_FAVICON).ToImageSkia();
23 }
24
25 }  // namespace
26
27 ExtensionActionIconFactory::ExtensionActionIconFactory(
28     Profile* profile,
29     const Extension* extension,
30     const ExtensionAction* action,
31     Observer* observer)
32     : extension_(extension),
33       action_(action),
34       observer_(observer) {
35   if (action_->default_icon()) {
36     default_icon_.reset(new IconImage(
37         profile,
38         extension_,
39         *action_->default_icon(),
40         ExtensionAction::GetIconSizeForType(action_->action_type()),
41         GetDefaultIcon(),
42         this));
43   }
44 }
45
46 ExtensionActionIconFactory::~ExtensionActionIconFactory() {}
47
48 // extensions::IconImage::Observer overrides.
49 void ExtensionActionIconFactory::OnExtensionIconImageChanged(IconImage* image) {
50   if (observer_)
51     observer_->OnIconUpdated();
52 }
53
54 gfx::Image ExtensionActionIconFactory::GetIcon(int tab_id) {
55   gfx::ImageSkia base_icon = GetBaseIconFromAction(tab_id);
56   return action_->ApplyAttentionAndAnimation(base_icon, tab_id);
57 }
58
59 gfx::ImageSkia ExtensionActionIconFactory::GetBaseIconFromAction(int tab_id) {
60   gfx::ImageSkia icon = action_->GetExplicitlySetIcon(tab_id);
61   if (!icon.isNull())
62     return icon;
63
64   if (default_icon_)
65     return default_icon_->image_skia();
66
67   return GetDefaultIcon();
68 }