Push API: Add PUSH_DELIVERY_STATUS_INVALID_MESSAGE
[chromium-blink-merge.git] / athena / extensions / extension_app_model_builder.cc
blobe043ef25e07b3add1b3e910442ead7579d3af13c
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"
21 namespace athena {
23 namespace {
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 {
30 public:
31 AppItem(scoped_refptr<const extensions::Extension> extension,
32 content::BrowserContext* browser_context)
33 : app_list::AppListItem(extension->id()),
34 browser_context_(browser_context) {
35 Reload(extension);
38 void Reload(scoped_refptr<const extensions::Extension> extension) {
39 extension_ = extension;
40 icon_image_.reset(new extensions::IconImage(
41 browser_context_,
42 extension.get(),
43 extensions::IconsInfo::GetIcons(extension.get()),
44 extension_misc::EXTENSION_ICON_MEDIUM,
45 *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
46 IDR_APP_DEFAULT_ICON),
47 nullptr));
48 icon_image_->image_skia().EnsureRepsForSupportedScales();
49 SetIcon(icon_image_->image_skia(), false);
50 SetName(extension->name());
53 private:
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);
66 } // namespace
68 ExtensionAppModelBuilder::ExtensionAppModelBuilder(
69 content::BrowserContext* browser_context)
70 : browser_context_(browser_context), model_(nullptr) {
71 extensions::ExtensionRegistryFactory::GetForBrowserContext(browser_context_)
72 ->AddObserver(this);
75 ExtensionAppModelBuilder::~ExtensionAppModelBuilder() {
76 extensions::ExtensionRegistryFactory::GetForBrowserContext(browser_context_)
77 ->RemoveObserver(this);
80 void ExtensionAppModelBuilder::RegisterAppListModel(
81 app_list::AppListModel* model) {
82 DCHECK(!model_);
83 model_ = 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();
89 ++iter) {
90 AddItem(*iter);
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,
108 bool is_update) {
109 app_list::AppListItem* item = model_->FindItem(extension->id());
110 if (item)
111 static_cast<AppItem*>(item)->Reload(make_scoped_refptr(extension));
112 else
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