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/strings/utf_string_conversions.h" |
11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
| 14 #include "chrome/browser/extensions/api/commands/command_service.h" |
13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/common/extensions/api/extension_action/action_info.h" | 17 #include "chrome/common/extensions/api/extension_action/action_info.h" |
16 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" | 18 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" |
| 19 #include "chrome/common/extensions/command.h" |
| 20 #include "chrome/grit/generated_resources.h" |
17 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
18 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
19 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" |
20 #include "extensions/common/extension.h" | 24 #include "extensions/common/extension.h" |
| 25 #include "ui/base/l10n/l10n_util.h" |
21 | 26 |
22 using content::Details; | |
23 using extensions::Extension; | 27 using extensions::Extension; |
24 | 28 |
25 namespace { | 29 namespace { |
26 | 30 |
27 // How long to wait for browser action animations to complete before retrying. | 31 // How long to wait for browser action animations to complete before retrying. |
28 const int kAnimationWaitMs = 50; | 32 const int kAnimationWaitMs = 50; |
29 // How often we retry when waiting for browser action animation to end. | 33 // How often we retry when waiting for browser action animation to end. |
30 const int kAnimationWaitRetries = 10; | 34 const int kAnimationWaitRetries = 10; |
31 | 35 |
| 36 // Returns the keybinding for an extension command, or a null if none exists. |
| 37 scoped_ptr<extensions::Command> GetCommand( |
| 38 const std::string& extension_id, |
| 39 Profile* profile, |
| 40 ExtensionInstalledBubble::BubbleType type) { |
| 41 scoped_ptr<extensions::Command> result; |
| 42 extensions::Command command; |
| 43 extensions::CommandService* command_service = |
| 44 extensions::CommandService::Get(profile); |
| 45 bool has_command = false; |
| 46 if (type == ExtensionInstalledBubble::BROWSER_ACTION) { |
| 47 has_command = command_service->GetBrowserActionCommand( |
| 48 extension_id, extensions::CommandService::ACTIVE, &command, nullptr); |
| 49 } else if (type == ExtensionInstalledBubble::PAGE_ACTION) { |
| 50 has_command = command_service->GetPageActionCommand( |
| 51 extension_id, extensions::CommandService::ACTIVE, &command, nullptr); |
| 52 } |
| 53 if (has_command) |
| 54 result.reset(new extensions::Command(command)); |
| 55 return result.Pass(); |
| 56 } |
| 57 |
32 } // namespace | 58 } // namespace |
33 | 59 |
34 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate* delegate, | 60 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate* delegate, |
35 const Extension* extension, | 61 const Extension* extension, |
36 Browser* browser, | 62 Browser* browser, |
37 const SkBitmap& icon) | 63 const SkBitmap& icon) |
38 : delegate_(delegate), | 64 : delegate_(delegate), |
39 extension_(extension), | 65 extension_(extension), |
40 browser_(browser), | 66 browser_(browser), |
41 icon_(icon), | 67 icon_(icon), |
(...skipping 22 matching lines...) Expand all Loading... |
64 content::Source<Browser>(browser)); | 90 content::Source<Browser>(browser)); |
65 } | 91 } |
66 | 92 |
67 ExtensionInstalledBubble::~ExtensionInstalledBubble() {} | 93 ExtensionInstalledBubble::~ExtensionInstalledBubble() {} |
68 | 94 |
69 void ExtensionInstalledBubble::IgnoreBrowserClosing() { | 95 void ExtensionInstalledBubble::IgnoreBrowserClosing() { |
70 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 96 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, |
71 content::Source<Browser>(browser_)); | 97 content::Source<Browser>(browser_)); |
72 } | 98 } |
73 | 99 |
| 100 base::string16 ExtensionInstalledBubble::GetHowToUseDescription() const { |
| 101 int message_id = 0; |
| 102 base::string16 extra; |
| 103 if (action_command_) |
| 104 extra = action_command_->accelerator().GetShortcutText(); |
| 105 |
| 106 switch (type_) { |
| 107 case BROWSER_ACTION: |
| 108 message_id = extra.empty() ? IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO : |
| 109 IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO_WITH_SHORTCUT; |
| 110 break; |
| 111 case PAGE_ACTION: |
| 112 message_id = extra.empty() ? IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO : |
| 113 IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO_WITH_SHORTCUT; |
| 114 break; |
| 115 case OMNIBOX_KEYWORD: |
| 116 extra = |
| 117 base::UTF8ToUTF16(extensions::OmniboxInfo::GetKeyword(extension_)); |
| 118 message_id = IDS_EXTENSION_INSTALLED_OMNIBOX_KEYWORD_INFO; |
| 119 break; |
| 120 case GENERIC: |
| 121 break; |
| 122 } |
| 123 |
| 124 if (message_id == 0) |
| 125 return base::string16(); |
| 126 return extra.empty() ? l10n_util::GetStringUTF16(message_id) : |
| 127 l10n_util::GetStringFUTF16(message_id, extra); |
| 128 } |
| 129 |
74 void ExtensionInstalledBubble::ShowInternal() { | 130 void ExtensionInstalledBubble::ShowInternal() { |
75 if (delegate_->MaybeShowNow()) | 131 if (delegate_->MaybeShowNow()) |
76 return; | 132 return; |
77 if (animation_wait_retries_++ < kAnimationWaitRetries) { | 133 if (animation_wait_retries_++ < kAnimationWaitRetries) { |
78 base::MessageLoopForUI::current()->PostDelayedTask( | 134 base::MessageLoopForUI::current()->PostDelayedTask( |
79 FROM_HERE, | 135 FROM_HERE, |
80 base::Bind(&ExtensionInstalledBubble::ShowInternal, | 136 base::Bind(&ExtensionInstalledBubble::ShowInternal, |
81 weak_factory_.GetWeakPtr()), | 137 weak_factory_.GetWeakPtr()), |
82 base::TimeDelta::FromMilliseconds(kAnimationWaitMs)); | 138 base::TimeDelta::FromMilliseconds(kAnimationWaitMs)); |
83 } | 139 } |
84 } | 140 } |
85 | 141 |
86 void ExtensionInstalledBubble::OnExtensionLoaded( | 142 void ExtensionInstalledBubble::OnExtensionLoaded( |
87 content::BrowserContext* browser_context, | 143 content::BrowserContext* browser_context, |
88 const extensions::Extension* extension) { | 144 const extensions::Extension* extension) { |
89 if (extension == extension_) { | 145 if (extension == extension_) { |
| 146 // Parse the extension command, if one exists. |
| 147 action_command_ = GetCommand(extension_->id(), browser_->profile(), type_); |
| 148 |
90 animation_wait_retries_ = 0; | 149 animation_wait_retries_ = 0; |
91 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run. | 150 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run. |
92 base::MessageLoopForUI::current()->PostTask( | 151 base::MessageLoopForUI::current()->PostTask( |
93 FROM_HERE, | 152 FROM_HERE, |
94 base::Bind(&ExtensionInstalledBubble::ShowInternal, | 153 base::Bind(&ExtensionInstalledBubble::ShowInternal, |
95 weak_factory_.GetWeakPtr())); | 154 weak_factory_.GetWeakPtr())); |
96 } | 155 } |
97 } | 156 } |
98 | 157 |
99 void ExtensionInstalledBubble::OnExtensionUnloaded( | 158 void ExtensionInstalledBubble::OnExtensionUnloaded( |
100 content::BrowserContext* browser_context, | 159 content::BrowserContext* browser_context, |
101 const extensions::Extension* extension, | 160 const extensions::Extension* extension, |
102 extensions::UnloadedExtensionInfo::Reason reason) { | 161 extensions::UnloadedExtensionInfo::Reason reason) { |
103 if (extension == extension_) { | 162 if (extension == extension_) { |
104 // Extension is going away, make sure ShowInternal won't be called. | 163 // Extension is going away, make sure ShowInternal won't be called. |
105 weak_factory_.InvalidateWeakPtrs(); | 164 weak_factory_.InvalidateWeakPtrs(); |
106 extension_ = NULL; | 165 extension_ = NULL; |
107 } | 166 } |
108 } | 167 } |
109 | 168 |
110 void ExtensionInstalledBubble::Observe( | 169 void ExtensionInstalledBubble::Observe( |
111 int type, | 170 int type, |
112 const content::NotificationSource& source, | 171 const content::NotificationSource& source, |
113 const content::NotificationDetails& details) { | 172 const content::NotificationDetails& details) { |
114 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_CLOSING) | 173 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_CLOSING) |
115 << "Received unexpected notification"; | 174 << "Received unexpected notification"; |
116 delete delegate_; | 175 delete delegate_; |
117 } | 176 } |
OLD | NEW |