1 // Copyright 2013 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 "chrome/browser/ui/extensions/extension_installed_bubble.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/extensions/api/commands/command_service.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/common/extensions/api/extension_action/action_info.h"
18 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h"
19 #include "chrome/common/extensions/command.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_source.h"
23 #include "extensions/browser/extension_registry.h"
24 #include "extensions/common/extension.h"
25 #include "ui/base/l10n/l10n_util.h"
27 using extensions::Extension
;
31 // How long to wait for browser action animations to complete before retrying.
32 const int kAnimationWaitMs
= 50;
33 // How often we retry when waiting for browser action animation to end.
34 const int kAnimationWaitRetries
= 10;
36 // Returns the keybinding for an extension command, or a null if none exists.
37 scoped_ptr
<extensions::Command
> GetCommand(
38 const std::string
& extension_id
,
40 ExtensionInstalledBubble::BubbleType type
) {
41 scoped_ptr
<extensions::Command
> result
;
42 extensions::Command command
;
43 extensions::CommandService
* command_service
=
44 extensions::CommandService::Get(profile
);
45 bool has_command
= false;
46 if (type
== ExtensionInstalledBubble::BROWSER_ACTION
) {
47 has_command
= command_service
->GetBrowserActionCommand(
48 extension_id
, extensions::CommandService::ACTIVE
, &command
, nullptr);
49 } else if (type
== ExtensionInstalledBubble::PAGE_ACTION
) {
50 has_command
= command_service
->GetPageActionCommand(
51 extension_id
, extensions::CommandService::ACTIVE
, &command
, nullptr);
54 result
.reset(new extensions::Command(command
));
60 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate
* delegate
,
61 const Extension
* extension
,
64 : delegate_(delegate
),
65 extension_(extension
),
68 extension_registry_observer_(this),
69 animation_wait_retries_(0),
71 if (!extensions::OmniboxInfo::GetKeyword(extension
).empty())
72 type_
= OMNIBOX_KEYWORD
;
73 else if (extensions::ActionInfo::GetBrowserActionInfo(extension
))
74 type_
= BROWSER_ACTION
;
75 else if (extensions::ActionInfo::GetPageActionInfo(extension
) &&
76 extensions::ActionInfo::IsVerboseInstallMessage(extension
))
81 // |extension| has been initialized but not loaded at this point. We need
82 // to wait on showing the Bubble until not only the EXTENSION_LOADED gets
83 // fired, but all of the EXTENSION_LOADED Observers have run. Only then can we
84 // be sure that a BrowserAction or PageAction has had views created which we
85 // can inspect for the purpose of previewing of pointing to them.
86 extension_registry_observer_
.Add(
87 extensions::ExtensionRegistry::Get(browser
->profile()));
89 registrar_
.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING
,
90 content::Source
<Browser
>(browser
));
93 ExtensionInstalledBubble::~ExtensionInstalledBubble() {}
95 void ExtensionInstalledBubble::IgnoreBrowserClosing() {
96 registrar_
.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING
,
97 content::Source
<Browser
>(browser_
));
100 base::string16
ExtensionInstalledBubble::GetHowToUseDescription() const {
102 base::string16 extra
;
104 extra
= action_command_
->accelerator().GetShortcutText();
108 message_id
= extra
.empty() ? IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO
:
109 IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO_WITH_SHORTCUT
;
112 message_id
= extra
.empty() ? IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO
:
113 IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO_WITH_SHORTCUT
;
115 case OMNIBOX_KEYWORD
:
117 base::UTF8ToUTF16(extensions::OmniboxInfo::GetKeyword(extension_
));
118 message_id
= IDS_EXTENSION_INSTALLED_OMNIBOX_KEYWORD_INFO
;
125 return base::string16();
126 return extra
.empty() ? l10n_util::GetStringUTF16(message_id
) :
127 l10n_util::GetStringFUTF16(message_id
, extra
);
130 void ExtensionInstalledBubble::ShowInternal() {
131 if (delegate_
->MaybeShowNow())
133 if (animation_wait_retries_
++ < kAnimationWaitRetries
) {
134 base::MessageLoopForUI::current()->PostDelayedTask(
136 base::Bind(&ExtensionInstalledBubble::ShowInternal
,
137 weak_factory_
.GetWeakPtr()),
138 base::TimeDelta::FromMilliseconds(kAnimationWaitMs
));
142 void ExtensionInstalledBubble::OnExtensionLoaded(
143 content::BrowserContext
* browser_context
,
144 const extensions::Extension
* extension
) {
145 if (extension
== extension_
) {
146 // Parse the extension command, if one exists.
147 action_command_
= GetCommand(extension_
->id(), browser_
->profile(), type_
);
149 animation_wait_retries_
= 0;
150 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run.
151 base::MessageLoopForUI::current()->PostTask(
153 base::Bind(&ExtensionInstalledBubble::ShowInternal
,
154 weak_factory_
.GetWeakPtr()));
158 void ExtensionInstalledBubble::OnExtensionUnloaded(
159 content::BrowserContext
* browser_context
,
160 const extensions::Extension
* extension
,
161 extensions::UnloadedExtensionInfo::Reason reason
) {
162 if (extension
== extension_
) {
163 // Extension is going away, make sure ShowInternal won't be called.
164 weak_factory_
.InvalidateWeakPtrs();
169 void ExtensionInstalledBubble::Observe(
171 const content::NotificationSource
& source
,
172 const content::NotificationDetails
& details
) {
173 DCHECK_EQ(type
, chrome::NOTIFICATION_BROWSER_CLOSING
)
174 << "Received unexpected notification";