OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/ui/extensions/extension_installed_bubble.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.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 "chrome/common/extensions/extension.h" | |
17 #include "content/public/browser/notification_details.h" | |
18 #include "content/public/browser/notification_source.h" | |
19 | |
20 using extensions::Extension; | |
21 | |
22 namespace { | |
23 | |
24 // How long to wait for browser action animations to complete before retrying. | |
25 const int kAnimationWaitMs = 50; | |
26 // How often we retry when waiting for browser action animation to end. | |
27 const int kAnimationWaitRetries = 10; | |
28 | |
29 } // namespace | |
30 | |
31 ExtensionInstalledBubble::ExtensionInstalledBubble(const Extension* extension, | |
32 Browser *browser, | |
33 const SkBitmap& icon) | |
34 : extension_(extension), | |
35 browser_(browser), | |
36 icon_(icon), | |
37 animation_wait_retries_(0), | |
38 weak_factory_(this) { | |
39 if (!extensions::OmniboxInfo::GetKeyword(extension).empty()) | |
40 type_ = OMNIBOX_KEYWORD; | |
41 else if (extensions::ActionInfo::GetBrowserActionInfo(extension)) | |
42 type_ = BROWSER_ACTION; | |
43 else if (extensions::ActionInfo::GetPageActionInfo(extension) && | |
44 extensions::ActionInfo::IsVerboseInstallMessage(extension)) | |
45 type_ = PAGE_ACTION; | |
46 else | |
47 type_ = GENERIC; | |
48 | |
49 // |extension| has been initialized but not loaded at this point. We need | |
50 // to wait on showing the Bubble until not only the EXTENSION_LOADED gets | |
51 // fired, but all of the EXTENSION_LOADED Observers have run. Only then can we | |
52 // be sure that a BrowserAction or PageAction has had views created which we | |
53 // can inspect for the purpose of previewing of pointing to them. | |
54 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | |
55 content::Source<Profile>(browser->profile())); | |
56 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
57 content::Source<Profile>(browser->profile())); | |
58 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | |
59 content::Source<Browser>(browser)); | |
60 } | |
61 | |
62 ExtensionInstalledBubble::~ExtensionInstalledBubble() {} | |
sky
2013/08/29 15:39:51
nit: make order match header.
Yoyo Zhou
2013/08/29 21:44:46
Done.
| |
63 | |
64 void ExtensionInstalledBubble::MaybeShowLater() { | |
65 if (animation_wait_retries_++ < kAnimationWaitRetries) { | |
66 base::MessageLoopForUI::current()->PostDelayedTask( | |
67 FROM_HERE, | |
68 base::Bind(&ExtensionInstalledBubble::ShowInternal, | |
69 weak_factory_.GetWeakPtr()), | |
70 base::TimeDelta::FromMilliseconds(kAnimationWaitMs)); | |
71 } | |
72 } | |
73 | |
74 void ExtensionInstalledBubble::Observe( | |
75 int type, | |
76 const content::NotificationSource& source, | |
77 const content::NotificationDetails& details) { | |
78 if (type == chrome::NOTIFICATION_EXTENSION_LOADED) { | |
79 const Extension* extension = | |
80 content::Details<const Extension>(details).ptr(); | |
81 if (extension == extension_) { | |
82 animation_wait_retries_ = 0; | |
83 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run. | |
84 base::MessageLoopForUI::current()->PostTask( | |
85 FROM_HERE, | |
86 base::Bind(&ExtensionInstalledBubble::ShowInternal, | |
87 weak_factory_.GetWeakPtr())); | |
88 } | |
89 } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { | |
90 const Extension* extension = | |
91 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | |
92 if (extension == extension_) { | |
93 // Extension is going away, make sure ShowInternal won't be called. | |
94 weak_factory_.InvalidateWeakPtrs(); | |
95 extension_ = NULL; | |
96 } | |
97 } else if (type == chrome::NOTIFICATION_BROWSER_CLOSING) { | |
98 delete this; | |
99 } else { | |
100 NOTREACHED() << "Received unexpected notification"; | |
101 } | |
102 } | |
OLD | NEW |