Support SizeClassIdiom on iOS7.
[chromium-blink-merge.git] / chrome / browser / ui / extensions / extension_installed_bubble.cc
blob2d5d7bd15a51444d5b7913987999a1a69e1da090
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/location.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/api/commands/command_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/common/extensions/api/extension_action/action_info.h"
20 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h"
21 #include "chrome/common/extensions/command.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "content/public/browser/notification_details.h"
24 #include "content/public/browser/notification_source.h"
25 #include "extensions/browser/extension_registry.h"
26 #include "extensions/common/extension.h"
27 #include "ui/base/l10n/l10n_util.h"
29 using extensions::Extension;
31 namespace {
33 // How long to wait for browser action animations to complete before retrying.
34 const int kAnimationWaitMs = 50;
35 // How often we retry when waiting for browser action animation to end.
36 const int kAnimationWaitRetries = 10;
38 // Returns the keybinding for an extension command, or a null if none exists.
39 scoped_ptr<extensions::Command> GetCommand(
40 const std::string& extension_id,
41 Profile* profile,
42 ExtensionInstalledBubble::BubbleType type) {
43 scoped_ptr<extensions::Command> result;
44 extensions::Command command;
45 extensions::CommandService* command_service =
46 extensions::CommandService::Get(profile);
47 bool has_command = false;
48 if (type == ExtensionInstalledBubble::BROWSER_ACTION) {
49 has_command = command_service->GetBrowserActionCommand(
50 extension_id, extensions::CommandService::ACTIVE, &command, nullptr);
51 } else if (type == ExtensionInstalledBubble::PAGE_ACTION) {
52 has_command = command_service->GetPageActionCommand(
53 extension_id, extensions::CommandService::ACTIVE, &command, nullptr);
55 if (has_command)
56 result.reset(new extensions::Command(command));
57 return result.Pass();
60 } // namespace
62 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate* delegate,
63 const Extension* extension,
64 Browser* browser,
65 const SkBitmap& icon)
66 : delegate_(delegate),
67 extension_(extension),
68 browser_(browser),
69 icon_(icon),
70 extension_registry_observer_(this),
71 animation_wait_retries_(0),
72 weak_factory_(this) {
73 if (!extensions::OmniboxInfo::GetKeyword(extension).empty())
74 type_ = OMNIBOX_KEYWORD;
75 else if (extensions::ActionInfo::GetBrowserActionInfo(extension))
76 type_ = BROWSER_ACTION;
77 else if (extensions::ActionInfo::GetPageActionInfo(extension) &&
78 extensions::ActionInfo::IsVerboseInstallMessage(extension))
79 type_ = PAGE_ACTION;
80 else
81 type_ = GENERIC;
83 // |extension| has been initialized but not loaded at this point. We need
84 // to wait on showing the Bubble until not only the EXTENSION_LOADED gets
85 // fired, but all of the EXTENSION_LOADED Observers have run. Only then can we
86 // be sure that a BrowserAction or PageAction has had views created which we
87 // can inspect for the purpose of previewing of pointing to them.
88 extension_registry_observer_.Add(
89 extensions::ExtensionRegistry::Get(browser->profile()));
91 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING,
92 content::Source<Browser>(browser));
95 ExtensionInstalledBubble::~ExtensionInstalledBubble() {}
97 void ExtensionInstalledBubble::IgnoreBrowserClosing() {
98 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING,
99 content::Source<Browser>(browser_));
102 base::string16 ExtensionInstalledBubble::GetHowToUseDescription() const {
103 int message_id = 0;
104 base::string16 extra;
105 if (action_command_)
106 extra = action_command_->accelerator().GetShortcutText();
108 switch (type_) {
109 case BROWSER_ACTION:
110 message_id = extra.empty() ? IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO :
111 IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO_WITH_SHORTCUT;
112 break;
113 case PAGE_ACTION:
114 message_id = extra.empty() ? IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO :
115 IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO_WITH_SHORTCUT;
116 break;
117 case OMNIBOX_KEYWORD:
118 extra =
119 base::UTF8ToUTF16(extensions::OmniboxInfo::GetKeyword(extension_));
120 message_id = IDS_EXTENSION_INSTALLED_OMNIBOX_KEYWORD_INFO;
121 break;
122 case GENERIC:
123 break;
126 if (message_id == 0)
127 return base::string16();
128 return extra.empty() ? l10n_util::GetStringUTF16(message_id) :
129 l10n_util::GetStringFUTF16(message_id, extra);
132 void ExtensionInstalledBubble::ShowInternal() {
133 if (delegate_->MaybeShowNow())
134 return;
135 if (animation_wait_retries_++ < kAnimationWaitRetries) {
136 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
137 FROM_HERE, base::Bind(&ExtensionInstalledBubble::ShowInternal,
138 weak_factory_.GetWeakPtr()),
139 base::TimeDelta::FromMilliseconds(kAnimationWaitMs));
143 void ExtensionInstalledBubble::OnExtensionLoaded(
144 content::BrowserContext* browser_context,
145 const extensions::Extension* extension) {
146 if (extension == extension_) {
147 // Parse the extension command, if one exists.
148 action_command_ = GetCommand(extension_->id(), browser_->profile(), type_);
150 animation_wait_retries_ = 0;
151 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run.
152 base::ThreadTaskRunnerHandle::Get()->PostTask(
153 FROM_HERE, 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_;