Chromium Code Reviews| 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(Delegate* delegate, | |
| 32 const Extension* extension, | |
| 33 Browser *browser, | |
| 34 const SkBitmap& icon) | |
| 35 : delegate_(delegate), | |
| 36 extension_(extension), | |
| 37 browser_(browser), | |
| 38 icon_(icon), | |
| 39 animation_wait_retries_(0), | |
| 40 weak_factory_(this) { | |
| 41 if (!extensions::OmniboxInfo::GetKeyword(extension).empty()) | |
| 42 type_ = OMNIBOX_KEYWORD; | |
| 43 else if (extensions::ActionInfo::GetBrowserActionInfo(extension)) | |
| 44 type_ = BROWSER_ACTION; | |
| 45 else if (extensions::ActionInfo::GetPageActionInfo(extension) && | |
| 46 extensions::ActionInfo::IsVerboseInstallMessage(extension)) | |
| 47 type_ = PAGE_ACTION; | |
| 48 else | |
| 49 type_ = GENERIC; | |
| 50 | |
| 51 // |extension| has been initialized but not loaded at this point. We need | |
| 52 // to wait on showing the Bubble until not only the EXTENSION_LOADED gets | |
| 53 // fired, but all of the EXTENSION_LOADED Observers have run. Only then can we | |
| 54 // be sure that a BrowserAction or PageAction has had views created which we | |
| 55 // can inspect for the purpose of previewing of pointing to them. | |
| 56 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | |
| 57 content::Source<Profile>(browser->profile())); | |
| 58 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
| 59 content::Source<Profile>(browser->profile())); | |
| 60 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | |
| 61 content::Source<Browser>(browser)); | |
| 62 } | |
| 63 | |
| 64 ExtensionInstalledBubble::~ExtensionInstalledBubble() {} | |
| 65 | |
| 66 void ExtensionInstalledBubble::MaybeShowLater() { | |
| 67 if (animation_wait_retries_++ < kAnimationWaitRetries) { | |
| 68 base::MessageLoopForUI::current()->PostDelayedTask( | |
| 69 FROM_HERE, | |
| 70 base::Bind(&ExtensionInstalledBubble::ShowInternal, | |
| 71 weak_factory_.GetWeakPtr()), | |
| 72 base::TimeDelta::FromMilliseconds(kAnimationWaitMs)); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 void ExtensionInstalledBubble::IgnoreBrowserClosing() { | |
| 77 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, | |
| 78 content::Source<Browser>(browser_)); | |
| 79 } | |
| 80 | |
| 81 void ExtensionInstalledBubble::ShowInternal() { | |
| 82 delegate_->ShowInternal(); | |
| 83 } | |
| 84 | |
| 85 void ExtensionInstalledBubble::Observe( | |
| 86 int type, | |
| 87 const content::NotificationSource& source, | |
| 88 const content::NotificationDetails& details) { | |
| 89 if (type == chrome::NOTIFICATION_EXTENSION_LOADED) { | |
|
sky
2013/09/04 00:04:06
nit: IMO you've got enough ifs to warrant a switch
Yoyo Zhou
2013/09/05 00:12:21
Done.
| |
| 90 const Extension* extension = | |
| 91 content::Details<const Extension>(details).ptr(); | |
| 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 } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { | |
| 101 const Extension* extension = | |
| 102 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | |
| 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 } else if (type == chrome::NOTIFICATION_BROWSER_CLOSING) { | |
| 109 delegate_->OnBrowserClosing(); | |
| 110 } else { | |
| 111 NOTREACHED() << "Received unexpected notification"; | |
| 112 } | |
| 113 } | |
| OLD | NEW |