blob: b5e98b4791a9f9c44faa5d1626d1988c46d05a71 [file] [log] [blame]
[email protected]ff7e68e2013-09-06 05:59:071// 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.
4
5#include "chrome/browser/ui/extensions/extension_installed_bubble.h"
6
7#include <string>
8
9#include "base/bind.h"
skyostil380bb2222015-06-12 12:07:0510#include "base/location.h"
11#include "base/single_thread_task_runner.h"
rdevlin.croninf8a6bf62015-02-23 19:43:1412#include "base/strings/utf_string_conversions.h"
skyostil380bb2222015-06-12 12:07:0513#include "base/thread_task_runner_handle.h"
[email protected]ff7e68e2013-09-06 05:59:0714#include "base/time/time.h"
15#include "chrome/browser/chrome_notification_types.h"
rdevlin.croninf8a6bf62015-02-23 19:43:1416#include "chrome/browser/extensions/api/commands/command_service.h"
[email protected]a97883a882014-05-13 11:49:2517#include "chrome/browser/profiles/profile.h"
[email protected]ff7e68e2013-09-06 05:59:0718#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"
rdevlin.croninf8a6bf62015-02-23 19:43:1421#include "chrome/common/extensions/command.h"
22#include "chrome/grit/generated_resources.h"
[email protected]ff7e68e2013-09-06 05:59:0723#include "content/public/browser/notification_details.h"
24#include "content/public/browser/notification_source.h"
[email protected]a97883a882014-05-13 11:49:2525#include "extensions/browser/extension_registry.h"
[email protected]e4452d32013-11-15 23:07:4126#include "extensions/common/extension.h"
rdevlin.croninf8a6bf62015-02-23 19:43:1427#include "ui/base/l10n/l10n_util.h"
[email protected]ff7e68e2013-09-06 05:59:0728
[email protected]ff7e68e2013-09-06 05:59:0729using extensions::Extension;
30
31namespace {
32
33// How long to wait for browser action animations to complete before retrying.
34const int kAnimationWaitMs = 50;
35// How often we retry when waiting for browser action animation to end.
36const int kAnimationWaitRetries = 10;
37
rdevlin.croninf8a6bf62015-02-23 19:43:1438// Returns the keybinding for an extension command, or a null if none exists.
39scoped_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);
54 }
55 if (has_command)
56 result.reset(new extensions::Command(command));
57 return result.Pass();
58}
59
[email protected]ff7e68e2013-09-06 05:59:0760} // namespace
61
hcarmonadb25c2a2015-10-02 00:16:3762ExtensionInstalledBubble::ExtensionInstalledBubble(
63 ExtensionInstalledBubbleUi* bubble_ui,
64 const Extension* extension,
65 Browser* browser,
66 const SkBitmap& icon)
67 : bubble_ui_(bubble_ui),
[email protected]ff7e68e2013-09-06 05:59:0768 extension_(extension),
69 browser_(browser),
70 icon_(icon),
[email protected]a97883a882014-05-13 11:49:2571 extension_registry_observer_(this),
[email protected]ff7e68e2013-09-06 05:59:0772 animation_wait_retries_(0),
73 weak_factory_(this) {
74 if (!extensions::OmniboxInfo::GetKeyword(extension).empty())
75 type_ = OMNIBOX_KEYWORD;
76 else if (extensions::ActionInfo::GetBrowserActionInfo(extension))
77 type_ = BROWSER_ACTION;
78 else if (extensions::ActionInfo::GetPageActionInfo(extension) &&
79 extensions::ActionInfo::IsVerboseInstallMessage(extension))
80 type_ = PAGE_ACTION;
81 else
82 type_ = GENERIC;
83
84 // |extension| has been initialized but not loaded at this point. We need
85 // to wait on showing the Bubble until not only the EXTENSION_LOADED gets
86 // fired, but all of the EXTENSION_LOADED Observers have run. Only then can we
87 // be sure that a BrowserAction or PageAction has had views created which we
88 // can inspect for the purpose of previewing of pointing to them.
[email protected]a97883a882014-05-13 11:49:2589 extension_registry_observer_.Add(
90 extensions::ExtensionRegistry::Get(browser->profile()));
91
[email protected]ff7e68e2013-09-06 05:59:0792 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING,
93 content::Source<Browser>(browser));
94}
95
96ExtensionInstalledBubble::~ExtensionInstalledBubble() {}
97
98void ExtensionInstalledBubble::IgnoreBrowserClosing() {
99 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING,
100 content::Source<Browser>(browser_));
101}
102
rdevlin.croninf8a6bf62015-02-23 19:43:14103base::string16 ExtensionInstalledBubble::GetHowToUseDescription() const {
104 int message_id = 0;
105 base::string16 extra;
106 if (action_command_)
107 extra = action_command_->accelerator().GetShortcutText();
108
109 switch (type_) {
110 case BROWSER_ACTION:
111 message_id = extra.empty() ? IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO :
112 IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO_WITH_SHORTCUT;
113 break;
114 case PAGE_ACTION:
115 message_id = extra.empty() ? IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO :
116 IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO_WITH_SHORTCUT;
117 break;
118 case OMNIBOX_KEYWORD:
119 extra =
120 base::UTF8ToUTF16(extensions::OmniboxInfo::GetKeyword(extension_));
121 message_id = IDS_EXTENSION_INSTALLED_OMNIBOX_KEYWORD_INFO;
122 break;
123 case GENERIC:
124 break;
125 }
126
127 if (message_id == 0)
128 return base::string16();
129 return extra.empty() ? l10n_util::GetStringUTF16(message_id) :
130 l10n_util::GetStringFUTF16(message_id, extra);
131}
132
[email protected]ff7e68e2013-09-06 05:59:07133void ExtensionInstalledBubble::ShowInternal() {
hcarmonadb25c2a2015-10-02 00:16:37134 if (bubble_ui_->MaybeShowNow())
[email protected]ff7e68e2013-09-06 05:59:07135 return;
136 if (animation_wait_retries_++ < kAnimationWaitRetries) {
skyostil380bb2222015-06-12 12:07:05137 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
138 FROM_HERE, base::Bind(&ExtensionInstalledBubble::ShowInternal,
139 weak_factory_.GetWeakPtr()),
[email protected]ff7e68e2013-09-06 05:59:07140 base::TimeDelta::FromMilliseconds(kAnimationWaitMs));
141 }
142}
143
[email protected]a97883a882014-05-13 11:49:25144void ExtensionInstalledBubble::OnExtensionLoaded(
145 content::BrowserContext* browser_context,
146 const extensions::Extension* extension) {
147 if (extension == extension_) {
rdevlin.croninf8a6bf62015-02-23 19:43:14148 // Parse the extension command, if one exists.
149 action_command_ = GetCommand(extension_->id(), browser_->profile(), type_);
150
[email protected]a97883a882014-05-13 11:49:25151 animation_wait_retries_ = 0;
152 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run.
skyostil380bb2222015-06-12 12:07:05153 base::ThreadTaskRunnerHandle::Get()->PostTask(
154 FROM_HERE, base::Bind(&ExtensionInstalledBubble::ShowInternal,
155 weak_factory_.GetWeakPtr()));
[email protected]a97883a882014-05-13 11:49:25156 }
157}
158
159void ExtensionInstalledBubble::OnExtensionUnloaded(
160 content::BrowserContext* browser_context,
161 const extensions::Extension* extension,
162 extensions::UnloadedExtensionInfo::Reason reason) {
163 if (extension == extension_) {
164 // Extension is going away, make sure ShowInternal won't be called.
165 weak_factory_.InvalidateWeakPtrs();
166 extension_ = NULL;
167 }
168}
169
[email protected]ff7e68e2013-09-06 05:59:07170void ExtensionInstalledBubble::Observe(
171 int type,
172 const content::NotificationSource& source,
173 const content::NotificationDetails& details) {
[email protected]a97883a882014-05-13 11:49:25174 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_CLOSING)
175 << "Received unexpected notification";
hcarmonadb25c2a2015-10-02 00:16:37176 delete bubble_ui_;
[email protected]ff7e68e2013-09-06 05:59:07177}