OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/extensions/extension_installed_bubble.h" | 5 #include "chrome/browser/ui/extensions/extension_installed_bubble.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/common/extensions/api/extension_action/action_info.h" | 15 #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/api/omnibox/omnibox_handler.h" |
16 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
17 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
| 19 #include "extensions/browser/extension_registry.h" |
18 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
19 | 21 |
20 using content::Details; | 22 using content::Details; |
21 using extensions::Extension; | 23 using extensions::Extension; |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 // How long to wait for browser action animations to complete before retrying. | 27 // How long to wait for browser action animations to complete before retrying. |
26 const int kAnimationWaitMs = 50; | 28 const int kAnimationWaitMs = 50; |
27 // How often we retry when waiting for browser action animation to end. | 29 // How often we retry when waiting for browser action animation to end. |
28 const int kAnimationWaitRetries = 10; | 30 const int kAnimationWaitRetries = 10; |
29 | 31 |
30 } // namespace | 32 } // namespace |
31 | 33 |
32 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate* delegate, | 34 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate* delegate, |
33 const Extension* extension, | 35 const Extension* extension, |
34 Browser *browser, | 36 Browser* browser, |
35 const SkBitmap& icon) | 37 const SkBitmap& icon) |
36 : delegate_(delegate), | 38 : delegate_(delegate), |
37 extension_(extension), | 39 extension_(extension), |
38 browser_(browser), | 40 browser_(browser), |
39 icon_(icon), | 41 icon_(icon), |
| 42 extension_registry_observer_(this), |
40 animation_wait_retries_(0), | 43 animation_wait_retries_(0), |
41 weak_factory_(this) { | 44 weak_factory_(this) { |
42 if (!extensions::OmniboxInfo::GetKeyword(extension).empty()) | 45 if (!extensions::OmniboxInfo::GetKeyword(extension).empty()) |
43 type_ = OMNIBOX_KEYWORD; | 46 type_ = OMNIBOX_KEYWORD; |
44 else if (extensions::ActionInfo::GetBrowserActionInfo(extension)) | 47 else if (extensions::ActionInfo::GetBrowserActionInfo(extension)) |
45 type_ = BROWSER_ACTION; | 48 type_ = BROWSER_ACTION; |
46 else if (extensions::ActionInfo::GetPageActionInfo(extension) && | 49 else if (extensions::ActionInfo::GetPageActionInfo(extension) && |
47 extensions::ActionInfo::IsVerboseInstallMessage(extension)) | 50 extensions::ActionInfo::IsVerboseInstallMessage(extension)) |
48 type_ = PAGE_ACTION; | 51 type_ = PAGE_ACTION; |
49 else | 52 else |
50 type_ = GENERIC; | 53 type_ = GENERIC; |
51 | 54 |
52 // |extension| has been initialized but not loaded at this point. We need | 55 // |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 | 56 // 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 | 57 // 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 | 58 // 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. | 59 // can inspect for the purpose of previewing of pointing to them. |
57 registrar_.Add(this, | 60 extension_registry_observer_.Add( |
58 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 61 extensions::ExtensionRegistry::Get(browser->profile())); |
59 content::Source<Profile>(browser->profile())); | 62 |
60 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
61 content::Source<Profile>(browser->profile())); | |
62 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 63 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, |
63 content::Source<Browser>(browser)); | 64 content::Source<Browser>(browser)); |
64 } | 65 } |
65 | 66 |
66 ExtensionInstalledBubble::~ExtensionInstalledBubble() {} | 67 ExtensionInstalledBubble::~ExtensionInstalledBubble() {} |
67 | 68 |
68 void ExtensionInstalledBubble::IgnoreBrowserClosing() { | 69 void ExtensionInstalledBubble::IgnoreBrowserClosing() { |
69 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 70 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, |
70 content::Source<Browser>(browser_)); | 71 content::Source<Browser>(browser_)); |
71 } | 72 } |
72 | 73 |
73 void ExtensionInstalledBubble::ShowInternal() { | 74 void ExtensionInstalledBubble::ShowInternal() { |
74 if (delegate_->MaybeShowNow()) | 75 if (delegate_->MaybeShowNow()) |
75 return; | 76 return; |
76 if (animation_wait_retries_++ < kAnimationWaitRetries) { | 77 if (animation_wait_retries_++ < kAnimationWaitRetries) { |
77 base::MessageLoopForUI::current()->PostDelayedTask( | 78 base::MessageLoopForUI::current()->PostDelayedTask( |
78 FROM_HERE, | 79 FROM_HERE, |
79 base::Bind(&ExtensionInstalledBubble::ShowInternal, | 80 base::Bind(&ExtensionInstalledBubble::ShowInternal, |
80 weak_factory_.GetWeakPtr()), | 81 weak_factory_.GetWeakPtr()), |
81 base::TimeDelta::FromMilliseconds(kAnimationWaitMs)); | 82 base::TimeDelta::FromMilliseconds(kAnimationWaitMs)); |
82 } | 83 } |
83 } | 84 } |
84 | 85 |
| 86 void ExtensionInstalledBubble::OnExtensionLoaded( |
| 87 content::BrowserContext* browser_context, |
| 88 const extensions::Extension* extension) { |
| 89 if (extension == extension_) { |
| 90 animation_wait_retries_ = 0; |
| 91 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run. |
| 92 base::MessageLoopForUI::current()->PostTask( |
| 93 FROM_HERE, |
| 94 base::Bind(&ExtensionInstalledBubble::ShowInternal, |
| 95 weak_factory_.GetWeakPtr())); |
| 96 } |
| 97 } |
| 98 |
| 99 void ExtensionInstalledBubble::OnExtensionUnloaded( |
| 100 content::BrowserContext* browser_context, |
| 101 const extensions::Extension* extension, |
| 102 extensions::UnloadedExtensionInfo::Reason reason) { |
| 103 if (extension == extension_) { |
| 104 // Extension is going away, make sure ShowInternal won't be called. |
| 105 weak_factory_.InvalidateWeakPtrs(); |
| 106 extension_ = NULL; |
| 107 } |
| 108 } |
| 109 |
85 void ExtensionInstalledBubble::Observe( | 110 void ExtensionInstalledBubble::Observe( |
86 int type, | 111 int type, |
87 const content::NotificationSource& source, | 112 const content::NotificationSource& source, |
88 const content::NotificationDetails& details) { | 113 const content::NotificationDetails& details) { |
89 switch (type) { | 114 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_CLOSING) |
90 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { | 115 << "Received unexpected notification"; |
91 const Extension* extension = Details<const Extension>(details).ptr(); | 116 delete delegate_; |
92 if (extension == extension_) { | |
93 animation_wait_retries_ = 0; | |
94 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run. | |
95 base::MessageLoopForUI::current()->PostTask( | |
96 FROM_HERE, | |
97 base::Bind(&ExtensionInstalledBubble::ShowInternal, | |
98 weak_factory_.GetWeakPtr())); | |
99 } | |
100 break; | |
101 } | |
102 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | |
103 const Extension* extension = | |
104 Details<extensions::UnloadedExtensionInfo>(details)->extension; | |
105 if (extension == extension_) { | |
106 // Extension is going away, make sure ShowInternal won't be called. | |
107 weak_factory_.InvalidateWeakPtrs(); | |
108 extension_ = NULL; | |
109 } | |
110 break; | |
111 } | |
112 case chrome::NOTIFICATION_BROWSER_CLOSING: | |
113 delete delegate_; | |
114 break; | |
115 | |
116 default: | |
117 NOTREACHED() << "Received unexpected notification"; | |
118 } | |
119 } | 117 } |
OLD | NEW |