| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/scoped_observer.h" | 14 #include "base/scoped_observer.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "chrome/browser/chrome_notification_types.h" | 18 #include "chrome/browser/chrome_notification_types.h" |
| 19 #include "chrome/browser/extensions/api/commands/command_service.h" | 19 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/browser_list.h" |
| 23 #include "chrome/browser/ui/browser_list_observer.h" |
| 22 #include "chrome/browser/ui/sync/sync_promo_ui.h" | 24 #include "chrome/browser/ui/sync/sync_promo_ui.h" |
| 23 #include "chrome/common/extensions/api/extension_action/action_info.h" | 25 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 24 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" | 26 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" |
| 25 #include "chrome/common/extensions/command.h" | 27 #include "chrome/common/extensions/command.h" |
| 26 #include "chrome/common/extensions/sync_helper.h" | 28 #include "chrome/common/extensions/sync_helper.h" |
| 27 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
| 28 #include "content/public/browser/notification_observer.h" | 30 #include "content/public/browser/notification_observer.h" |
| 29 #include "content/public/browser/notification_registrar.h" | 31 #include "content/public/browser/notification_registrar.h" |
| 30 #include "content/public/browser/notification_source.h" | 32 #include "content/public/browser/notification_source.h" |
| 31 #include "extensions/browser/extension_registry.h" | 33 #include "extensions/browser/extension_registry.h" |
| 32 #include "extensions/browser/extension_registry_observer.h" | 34 #include "extensions/browser/extension_registry_observer.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
| 34 | 36 |
| 35 using extensions::Extension; | 37 using extensions::Extension; |
| 36 | 38 |
| 37 namespace { | 39 namespace { |
| 38 | 40 |
| 39 // How long to wait for browser action animations to complete before retrying. | 41 // How long to wait for browser action animations to complete before retrying. |
| 40 const int kAnimationWaitMs = 50; | 42 const int kAnimationWaitMs = 50; |
| 41 // How often we retry when waiting for browser action animation to end. | 43 // How often we retry when waiting for browser action animation to end. |
| 42 const int kAnimationWaitRetries = 10; | 44 const int kAnimationWaitRetries = 10; |
| 43 | 45 |
| 44 // Class responsible for showing the bubble after it's installed. Owns itself. | 46 // Class responsible for showing the bubble after it's installed. Owns itself. |
| 45 class ExtensionInstalledBubbleObserver | 47 class ExtensionInstalledBubbleObserver |
| 46 : public content::NotificationObserver, | 48 : public chrome::BrowserListObserver, |
| 47 public extensions::ExtensionRegistryObserver { | 49 public extensions::ExtensionRegistryObserver { |
| 48 public: | 50 public: |
| 49 explicit ExtensionInstalledBubbleObserver( | 51 explicit ExtensionInstalledBubbleObserver( |
| 50 std::unique_ptr<ExtensionInstalledBubble> bubble) | 52 std::unique_ptr<ExtensionInstalledBubble> bubble) |
| 51 : bubble_(std::move(bubble)), | 53 : bubble_(std::move(bubble)), |
| 52 extension_registry_observer_(this), | 54 extension_registry_observer_(this), |
| 53 animation_wait_retries_(0), | 55 animation_wait_retries_(0), |
| 54 weak_factory_(this) { | 56 weak_factory_(this) { |
| 55 // |extension| has been initialized but not loaded at this point. We need to | 57 // |extension| has been initialized but not loaded at this point. We need to |
| 56 // wait on showing the Bubble until the EXTENSION_LOADED gets fired. | 58 // wait on showing the Bubble until the EXTENSION_LOADED gets fired. |
| 57 extension_registry_observer_.Add( | 59 extension_registry_observer_.Add( |
| 58 extensions::ExtensionRegistry::Get(bubble_->browser()->profile())); | 60 extensions::ExtensionRegistry::Get(bubble_->browser()->profile())); |
| 59 | 61 BrowserList::AddObserver(this); |
| 60 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | |
| 61 content::Source<Browser>(bubble_->browser())); | |
| 62 } | 62 } |
| 63 | 63 |
| 64 void Run() { OnExtensionLoaded(nullptr, bubble_->extension()); } | 64 void Run() { OnExtensionLoaded(nullptr, bubble_->extension()); } |
| 65 | 65 |
| 66 private: | 66 private: |
| 67 ~ExtensionInstalledBubbleObserver() override {} | 67 ~ExtensionInstalledBubbleObserver() override {} |
| 68 | 68 |
| 69 // content::NotificationObserver: | 69 // chrome::BrowserListObserver: |
| 70 void Observe(int type, | 70 void OnBrowserCloseStarted(Browser* browser) override { |
| 71 const content::NotificationSource& source, | 71 if (bubble_->browser() == browser) { |
| 72 const content::NotificationDetails& details) override { | 72 // Browser is closing before the bubble was shown. |
| 73 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_CLOSING) | 73 // TODO(hcarmona): Look into logging this with the BubbleManager. |
| 74 << "Received unexpected notification"; | 74 delete this; |
| 75 // Browser is closing before the bubble was shown. | 75 } |
| 76 // TODO(hcarmona): Look into logging this with the BubbleManager. | |
| 77 delete this; | |
| 78 } | 76 } |
| 79 | 77 |
| 80 // extensions::ExtensionRegistryObserver: | 78 // extensions::ExtensionRegistryObserver: |
| 81 void OnExtensionLoaded(content::BrowserContext* browser_context, | 79 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 82 const extensions::Extension* extension) override { | 80 const extensions::Extension* extension) override { |
| 83 if (extension == bubble_->extension()) { | 81 if (extension == bubble_->extension()) { |
| 84 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run. | 82 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run. |
| 85 // Only then can we be sure that a BrowserAction or PageAction has had | 83 // Only then can we be sure that a BrowserAction or PageAction has had |
| 86 // views created which we can inspect for the purpose of previewing of | 84 // views created which we can inspect for the purpose of previewing of |
| 87 // pointing to them. | 85 // pointing to them. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 } | 130 } |
| 133 } | 131 } |
| 134 | 132 |
| 135 // The bubble that will be shown when the extension has finished installing. | 133 // The bubble that will be shown when the extension has finished installing. |
| 136 std::unique_ptr<ExtensionInstalledBubble> bubble_; | 134 std::unique_ptr<ExtensionInstalledBubble> bubble_; |
| 137 | 135 |
| 138 ScopedObserver<extensions::ExtensionRegistry, | 136 ScopedObserver<extensions::ExtensionRegistry, |
| 139 extensions::ExtensionRegistryObserver> | 137 extensions::ExtensionRegistryObserver> |
| 140 extension_registry_observer_; | 138 extension_registry_observer_; |
| 141 | 139 |
| 142 content::NotificationRegistrar registrar_; | |
| 143 | |
| 144 // The number of times to retry showing the bubble if the bubble_->browser() | 140 // The number of times to retry showing the bubble if the bubble_->browser() |
| 145 // action toolbar is animating. | 141 // action toolbar is animating. |
| 146 int animation_wait_retries_; | 142 int animation_wait_retries_; |
| 147 | 143 |
| 148 base::WeakPtrFactory<ExtensionInstalledBubbleObserver> weak_factory_; | 144 base::WeakPtrFactory<ExtensionInstalledBubbleObserver> weak_factory_; |
| 149 | 145 |
| 150 DISALLOW_COPY_AND_ASSIGN(ExtensionInstalledBubbleObserver); | 146 DISALLOW_COPY_AND_ASSIGN(ExtensionInstalledBubbleObserver); |
| 151 }; | 147 }; |
| 152 | 148 |
| 153 // Returns the keybinding for an extension command, or a null if none exists. | 149 // Returns the keybinding for an extension command, or a null if none exists. |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 break; | 282 break; |
| 287 case OMNIBOX_KEYWORD: | 283 case OMNIBOX_KEYWORD: |
| 288 options_ |= HOW_TO_USE | HOW_TO_MANAGE; | 284 options_ |= HOW_TO_USE | HOW_TO_MANAGE; |
| 289 anchor_position_ = ANCHOR_OMNIBOX; | 285 anchor_position_ = ANCHOR_OMNIBOX; |
| 290 break; | 286 break; |
| 291 case GENERIC: | 287 case GENERIC: |
| 292 anchor_position_ = ANCHOR_APP_MENU; | 288 anchor_position_ = ANCHOR_APP_MENU; |
| 293 break; | 289 break; |
| 294 } | 290 } |
| 295 } | 291 } |
| OLD | NEW |