Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / ui / extensions / extension_installed_bubble.cc
blob9e260ea2c2790fcbe4f004c40cf2c882c056fe2f
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.
5 #include "chrome/browser/ui/extensions/extension_installed_bubble.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/extensions/api/commands/command_service.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/common/extensions/api/extension_action/action_info.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"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_source.h"
23 #include "extensions/browser/extension_registry.h"
24 #include "extensions/common/extension.h"
25 #include "ui/base/l10n/l10n_util.h"
27 using extensions::Extension;
29 namespace {
31 // How long to wait for browser action animations to complete before retrying.
32 const int kAnimationWaitMs = 50;
33 // How often we retry when waiting for browser action animation to end.
34 const int kAnimationWaitRetries = 10;
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);
53 if (has_command)
54 result.reset(new extensions::Command(command));
55 return result.Pass();
58 } // namespace
60 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate* delegate,
61 const Extension* extension,
62 Browser* browser,
63 const SkBitmap& icon)
64 : delegate_(delegate),
65 extension_(extension),
66 browser_(browser),
67 icon_(icon),
68 extension_registry_observer_(this),
69 animation_wait_retries_(0),
70 weak_factory_(this) {
71 if (!extensions::OmniboxInfo::GetKeyword(extension).empty())
72 type_ = OMNIBOX_KEYWORD;
73 else if (extensions::ActionInfo::GetBrowserActionInfo(extension))
74 type_ = BROWSER_ACTION;
75 else if (extensions::ActionInfo::GetPageActionInfo(extension) &&
76 extensions::ActionInfo::IsVerboseInstallMessage(extension))
77 type_ = PAGE_ACTION;
78 else
79 type_ = GENERIC;
81 // |extension| has been initialized but not loaded at this point. We need
82 // to wait on showing the Bubble until not only the EXTENSION_LOADED gets
83 // fired, but all of the EXTENSION_LOADED Observers have run. Only then can we
84 // be sure that a BrowserAction or PageAction has had views created which we
85 // can inspect for the purpose of previewing of pointing to them.
86 extension_registry_observer_.Add(
87 extensions::ExtensionRegistry::Get(browser->profile()));
89 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING,
90 content::Source<Browser>(browser));
93 ExtensionInstalledBubble::~ExtensionInstalledBubble() {}
95 void ExtensionInstalledBubble::IgnoreBrowserClosing() {
96 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING,
97 content::Source<Browser>(browser_));
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();
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;
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);
130 void ExtensionInstalledBubble::ShowInternal() {
131 if (delegate_->MaybeShowNow())
132 return;
133 if (animation_wait_retries_++ < kAnimationWaitRetries) {
134 base::MessageLoopForUI::current()->PostDelayedTask(
135 FROM_HERE,
136 base::Bind(&ExtensionInstalledBubble::ShowInternal,
137 weak_factory_.GetWeakPtr()),
138 base::TimeDelta::FromMilliseconds(kAnimationWaitMs));
142 void ExtensionInstalledBubble::OnExtensionLoaded(
143 content::BrowserContext* browser_context,
144 const extensions::Extension* extension) {
145 if (extension == extension_) {
146 // Parse the extension command, if one exists.
147 action_command_ = GetCommand(extension_->id(), browser_->profile(), type_);
149 animation_wait_retries_ = 0;
150 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run.
151 base::MessageLoopForUI::current()->PostTask(
152 FROM_HERE,
153 base::Bind(&ExtensionInstalledBubble::ShowInternal,
154 weak_factory_.GetWeakPtr()));
158 void ExtensionInstalledBubble::OnExtensionUnloaded(
159 content::BrowserContext* browser_context,
160 const extensions::Extension* extension,
161 extensions::UnloadedExtensionInfo::Reason reason) {
162 if (extension == extension_) {
163 // Extension is going away, make sure ShowInternal won't be called.
164 weak_factory_.InvalidateWeakPtrs();
165 extension_ = NULL;
169 void ExtensionInstalledBubble::Observe(
170 int type,
171 const content::NotificationSource& source,
172 const content::NotificationDetails& details) {
173 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_CLOSING)
174 << "Received unexpected notification";
175 delete delegate_;