1 // Copyright 2014 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.
5 #include "athena/extensions/public/extension_app_model_builder.h"
7 #include "athena/activity/public/activity_factory.h"
8 #include "athena/activity/public/activity_manager.h"
9 #include "athena/extensions/public/extensions_delegate.h"
10 #include "extensions/browser/extension_icon_image.h"
11 #include "extensions/browser/extension_registry.h"
12 #include "extensions/browser/extension_registry_factory.h"
13 #include "extensions/common/constants.h"
14 #include "extensions/common/extension_set.h"
15 #include "extensions/common/manifest_handlers/icons_handler.h"
16 #include "extensions/grit/extensions_browser_resources.h"
17 #include "ui/app_list/app_list_item.h"
18 #include "ui/app_list/app_list_model.h"
19 #include "ui/base/resource/resource_bundle.h"
25 // Copied from chrome/common/extensions/extension_constants.h
26 // TODO(mukai): move constants to src/extensions
27 const char kChromeAppId
[] = "mgndgikekgjfcpckkfioiadnlibdjbkf";
29 class AppItem
: public app_list::AppListItem
{
31 AppItem(scoped_refptr
<const extensions::Extension
> extension
,
32 content::BrowserContext
* browser_context
)
33 : app_list::AppListItem(extension
->id()),
34 browser_context_(browser_context
) {
38 void Reload(scoped_refptr
<const extensions::Extension
> extension
) {
39 extension_
= extension
;
40 icon_image_
.reset(new extensions::IconImage(
43 extensions::IconsInfo::GetIcons(extension
.get()),
44 extension_misc::EXTENSION_ICON_MEDIUM
,
45 *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
46 IDR_APP_DEFAULT_ICON
),
48 icon_image_
->image_skia().EnsureRepsForSupportedScales();
49 SetIcon(icon_image_
->image_skia(), false);
50 SetName(extension
->name());
54 // Overridden from app_list::AppListItem:
55 void Activate(int event_flags
) override
{
56 ExtensionsDelegate::Get(browser_context_
)->LaunchApp(extension_
->id());
59 scoped_refptr
<const extensions::Extension
> extension_
;
60 content::BrowserContext
* browser_context_
;
61 scoped_ptr
<extensions::IconImage
> icon_image_
;
63 DISALLOW_COPY_AND_ASSIGN(AppItem
);
68 ExtensionAppModelBuilder::ExtensionAppModelBuilder(
69 content::BrowserContext
* browser_context
)
70 : browser_context_(browser_context
), model_(nullptr) {
71 extensions::ExtensionRegistryFactory::GetForBrowserContext(browser_context_
)
75 ExtensionAppModelBuilder::~ExtensionAppModelBuilder() {
76 extensions::ExtensionRegistryFactory::GetForBrowserContext(browser_context_
)
77 ->RemoveObserver(this);
80 void ExtensionAppModelBuilder::RegisterAppListModel(
81 app_list::AppListModel
* model
) {
85 ExtensionsDelegate
* bridge
= ExtensionsDelegate::Get(browser_context_
);
86 const extensions::ExtensionSet
& extensions
= bridge
->GetInstalledExtensions();
87 for (extensions::ExtensionSet::const_iterator iter
= extensions
.begin();
88 iter
!= extensions
.end();
94 void ExtensionAppModelBuilder::AddItem(
95 scoped_refptr
<const extensions::Extension
> extension
) {
96 // Chrome icon is currently disabled for homecard since it's not meaningful.
97 // http://crbug.com/421677
98 // TODO(mukai): use chrome/browser/extension_ui_util.
99 if (extension
->ShouldDisplayInAppLauncher() &&
100 extension
->id() != kChromeAppId
) {
101 model_
->AddItem(make_scoped_ptr(new AppItem(extension
, browser_context_
)));
105 void ExtensionAppModelBuilder::OnExtensionInstalled(
106 content::BrowserContext
* browser_context
,
107 const extensions::Extension
* extension
,
109 app_list::AppListItem
* item
= model_
->FindItem(extension
->id());
111 static_cast<AppItem
*>(item
)->Reload(make_scoped_refptr(extension
));
113 AddItem(make_scoped_refptr(extension
));
116 void ExtensionAppModelBuilder::OnExtensionUninstalled(
117 content::BrowserContext
* browser_context
,
118 const extensions::Extension
* extension
,
119 extensions::UninstallReason reason
) {
120 model_
->DeleteItem(extension
->id());
123 } // namespace athena