1 // Copyright 2016 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/ui/extensions/extension_installed_notification.h"
6 
7 #include "ash/public/cpp/notification_utils.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/app/vector_icons/vector_icons.h"
10 #include "chrome/browser/apps/app_service/app_service_proxy.h"
11 #include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
12 #include "chrome/browser/apps/app_service/launch_utils.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/extensions/extension_util.h"
15 #include "chrome/browser/notifications/notification_common.h"
16 #include "chrome/browser/notifications/notification_display_service.h"
17 #include "chrome/browser/ui/extensions/app_launch_params.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/vector_icons/vector_icons.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/extension_urls.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/display/types/display_constants.h"
26 #include "ui/gfx/color_palette.h"
27 #include "ui/message_center/public/cpp/notification.h"
28 
29 namespace {
30 const char kNotifierId[] = "app.downloaded-notification";
31 }  // namespace
32 
33 using content::BrowserThread;
34 
35 // static
Show(const extensions::Extension * extension,Profile * profile)36 void ExtensionInstalledNotification::Show(
37     const extensions::Extension* extension, Profile* profile) {
38   DCHECK_CURRENTLY_ON(BrowserThread::UI);
39 
40   // It's lifetime is managed by the parent class NotificationDelegate.
41   new ExtensionInstalledNotification(extension, profile);
42 }
43 
ExtensionInstalledNotification(const extensions::Extension * extension,Profile * profile)44 ExtensionInstalledNotification::ExtensionInstalledNotification(
45     const extensions::Extension* extension, Profile* profile)
46     : extension_id_(extension->id()), profile_(profile) {
47   std::unique_ptr<message_center::Notification> notification =
48       ash::CreateSystemNotification(
49           message_center::NOTIFICATION_TYPE_SIMPLE, extension_id_,
50           base::UTF8ToUTF16(extension->name()),
51           l10n_util::GetStringUTF16(IDS_EXTENSION_NOTIFICATION_INSTALLED),
52           l10n_util::GetStringUTF16(IDS_EXTENSION_NOTIFICATION_DISPLAY_SOURCE),
53           GURL(extension_urls::kChromeWebstoreBaseURL) /* origin_url */,
54           message_center::NotifierId(
55               message_center::NotifierType::SYSTEM_COMPONENT, kNotifierId),
56           {}, this, kNotificationInstalledIcon,
57           message_center::SystemNotificationWarningLevel::NORMAL);
58 
59   NotificationDisplayService::GetForProfile(profile_)->Display(
60       NotificationHandler::Type::TRANSIENT, *notification,
61       /*metadata=*/nullptr);
62 }
63 
~ExtensionInstalledNotification()64 ExtensionInstalledNotification::~ExtensionInstalledNotification() {}
65 
Click(const base::Optional<int> & button_index,const base::Optional<base::string16> & reply)66 void ExtensionInstalledNotification::Click(
67     const base::Optional<int>& button_index,
68     const base::Optional<base::string16>& reply) {
69   if (!extensions::util::IsAppLaunchable(extension_id_, profile_))
70     return;
71 
72   apps::AppServiceProxy* proxy =
73       apps::AppServiceProxyFactory::GetForProfile(profile_);
74   if (proxy->AppRegistryCache().GetAppType(extension_id_) ==
75       apps::mojom::AppType::kUnknown) {
76     return;
77   }
78 
79   proxy->Launch(
80       extension_id_,
81       apps::GetEventFlags(apps::mojom::LaunchContainer::kLaunchContainerNone,
82                           WindowOpenDisposition::NEW_FOREGROUND_TAB,
83                           true /* preferred_containner */),
84       apps::mojom::LaunchSource::kFromInstalledNotification,
85       display::kInvalidDisplayId);
86 }
87