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