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/time/time.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/common/extensions/api/extension_action/action_info.h"
15 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h"
16 #include "content/public/browser/notification_details.h"
17 #include "content/public/browser/notification_source.h"
18 #include "extensions/common/extension.h"
20 using content::Details
;
21 using extensions::Extension
;
25 // How long to wait for browser action animations to complete before retrying.
26 const int kAnimationWaitMs
= 50;
27 // How often we retry when waiting for browser action animation to end.
28 const int kAnimationWaitRetries
= 10;
32 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate
* delegate
,
33 const Extension
* extension
,
36 : delegate_(delegate
),
37 extension_(extension
),
40 animation_wait_retries_(0),
42 if (!extensions::OmniboxInfo::GetKeyword(extension
).empty())
43 type_
= OMNIBOX_KEYWORD
;
44 else if (extensions::ActionInfo::GetBrowserActionInfo(extension
))
45 type_
= BROWSER_ACTION
;
46 else if (extensions::ActionInfo::GetPageActionInfo(extension
) &&
47 extensions::ActionInfo::IsVerboseInstallMessage(extension
))
52 // |extension| has been initialized but not loaded at this point. We need
53 // to wait on showing the Bubble until not only the EXTENSION_LOADED gets
54 // fired, but all of the EXTENSION_LOADED Observers have run. Only then can we
55 // be sure that a BrowserAction or PageAction has had views created which we
56 // can inspect for the purpose of previewing of pointing to them.
57 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED
,
58 content::Source
<Profile
>(browser
->profile()));
59 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED
,
60 content::Source
<Profile
>(browser
->profile()));
61 registrar_
.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING
,
62 content::Source
<Browser
>(browser
));
65 ExtensionInstalledBubble::~ExtensionInstalledBubble() {}
67 void ExtensionInstalledBubble::IgnoreBrowserClosing() {
68 registrar_
.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING
,
69 content::Source
<Browser
>(browser_
));
72 void ExtensionInstalledBubble::ShowInternal() {
73 if (delegate_
->MaybeShowNow())
75 if (animation_wait_retries_
++ < kAnimationWaitRetries
) {
76 base::MessageLoopForUI::current()->PostDelayedTask(
78 base::Bind(&ExtensionInstalledBubble::ShowInternal
,
79 weak_factory_
.GetWeakPtr()),
80 base::TimeDelta::FromMilliseconds(kAnimationWaitMs
));
84 void ExtensionInstalledBubble::Observe(
86 const content::NotificationSource
& source
,
87 const content::NotificationDetails
& details
) {
89 case chrome::NOTIFICATION_EXTENSION_LOADED
: {
90 const Extension
* extension
= Details
<const Extension
>(details
).ptr();
91 if (extension
== extension_
) {
92 animation_wait_retries_
= 0;
93 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run.
94 base::MessageLoopForUI::current()->PostTask(
96 base::Bind(&ExtensionInstalledBubble::ShowInternal
,
97 weak_factory_
.GetWeakPtr()));
101 case chrome::NOTIFICATION_EXTENSION_UNLOADED
: {
102 const Extension
* extension
=
103 Details
<extensions::UnloadedExtensionInfo
>(details
)->extension
;
104 if (extension
== extension_
) {
105 // Extension is going away, make sure ShowInternal won't be called.
106 weak_factory_
.InvalidateWeakPtrs();
111 case chrome::NOTIFICATION_BROWSER_CLOSING
:
116 NOTREACHED() << "Received unexpected notification";