Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: chrome/browser/ui/extensions/extension_installed_bubble.cc

Issue 922523005: [Extensions UI] Share more code in ExtensionInstalledBubble (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 an empty string if none
Finnur 2015/02/18 09:40:41 Empty string?
Devlin 2015/02/18 18:08:41 Whoops, forgot to update that comment. Done.
37 // exists.
38 scoped_ptr<extensions::Command> GetCommand(
39 const std::string& extension_id,
40 Profile* profile,
41 ExtensionInstalledBubble::BubbleType type) {
42 scoped_ptr<extensions::Command> result;
43 extensions::Command command;
44 extensions::CommandService* command_service =
45 extensions::CommandService::Get(profile);
46 bool has_command = false;
47 if (type == ExtensionInstalledBubble::BROWSER_ACTION) {
48 has_command = command_service->GetBrowserActionCommand(
49 extension_id, extensions::CommandService::ACTIVE, &command, nullptr);
50 } else if (type == ExtensionInstalledBubble::PAGE_ACTION) {
51 has_command = command_service->GetPageActionCommand(
52 extension_id, extensions::CommandService::ACTIVE, &command, nullptr);
53 }
54 if (has_command)
55 result.reset(new extensions::Command(command));
56 return result.Pass();
57 }
58
32 } // namespace 59 } // namespace
33 60
34 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate* delegate, 61 ExtensionInstalledBubble::ExtensionInstalledBubble(Delegate* delegate,
35 const Extension* extension, 62 const Extension* extension,
36 Browser* browser, 63 Browser* browser,
37 const SkBitmap& icon) 64 const SkBitmap& icon)
38 : delegate_(delegate), 65 : delegate_(delegate),
39 extension_(extension), 66 extension_(extension),
40 browser_(browser), 67 browser_(browser),
41 icon_(icon), 68 icon_(icon),
(...skipping 22 matching lines...) Expand all
64 content::Source<Browser>(browser)); 91 content::Source<Browser>(browser));
65 } 92 }
66 93
67 ExtensionInstalledBubble::~ExtensionInstalledBubble() {} 94 ExtensionInstalledBubble::~ExtensionInstalledBubble() {}
68 95
69 void ExtensionInstalledBubble::IgnoreBrowserClosing() { 96 void ExtensionInstalledBubble::IgnoreBrowserClosing() {
70 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, 97 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING,
71 content::Source<Browser>(browser_)); 98 content::Source<Browser>(browser_));
72 } 99 }
73 100
101 base::string16 ExtensionInstalledBubble::GetHowToUseDescription() const {
102 int message_id = 0;
103 base::string16 extra;
104 if (action_command_)
105 extra = action_command_->accelerator().GetShortcutText();
106
107 switch (type_) {
108 case BROWSER_ACTION:
109 message_id = extra.empty() ? IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO :
110 IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO_WITH_SHORTCUT;
111 break;
112 case PAGE_ACTION:
113 message_id = extra.empty() ? IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO :
114 IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO_WITH_SHORTCUT;
115 break;
116 case OMNIBOX_KEYWORD:
117 extra =
118 base::UTF8ToUTF16(extensions::OmniboxInfo::GetKeyword(extension_));
119 message_id = IDS_EXTENSION_INSTALLED_OMNIBOX_KEYWORD_INFO;
120 break;
121 case GENERIC:
122 break;
123 }
124
125 if (message_id != 0) {
126 return extra.empty() ? l10n_util::GetStringUTF16(message_id) :
127 l10n_util::GetStringFUTF16(message_id, extra);
128 }
129 return base::string16();
Finnur 2015/02/18 09:40:41 nit: We generally prefer: if (message_id == 0)
Devlin 2015/02/18 18:08:41 Done.
130 }
131
74 void ExtensionInstalledBubble::ShowInternal() { 132 void ExtensionInstalledBubble::ShowInternal() {
75 if (delegate_->MaybeShowNow()) 133 if (delegate_->MaybeShowNow())
76 return; 134 return;
77 if (animation_wait_retries_++ < kAnimationWaitRetries) { 135 if (animation_wait_retries_++ < kAnimationWaitRetries) {
78 base::MessageLoopForUI::current()->PostDelayedTask( 136 base::MessageLoopForUI::current()->PostDelayedTask(
79 FROM_HERE, 137 FROM_HERE,
80 base::Bind(&ExtensionInstalledBubble::ShowInternal, 138 base::Bind(&ExtensionInstalledBubble::ShowInternal,
81 weak_factory_.GetWeakPtr()), 139 weak_factory_.GetWeakPtr()),
82 base::TimeDelta::FromMilliseconds(kAnimationWaitMs)); 140 base::TimeDelta::FromMilliseconds(kAnimationWaitMs));
83 } 141 }
84 } 142 }
85 143
86 void ExtensionInstalledBubble::OnExtensionLoaded( 144 void ExtensionInstalledBubble::OnExtensionLoaded(
87 content::BrowserContext* browser_context, 145 content::BrowserContext* browser_context,
88 const extensions::Extension* extension) { 146 const extensions::Extension* extension) {
89 if (extension == extension_) { 147 if (extension == extension_) {
148 // Parse the extension command, if one exists.
Robert Sesek 2015/02/18 15:03:00 Can the command change over time?
Devlin 2015/02/18 18:08:41 Technically, yes. A user could customize an exten
149 action_command_ = GetCommand(extension_->id(), browser_->profile(), type_);
150
90 animation_wait_retries_ = 0; 151 animation_wait_retries_ = 0;
91 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run. 152 // PostTask to ourself to allow all EXTENSION_LOADED Observers to run.
92 base::MessageLoopForUI::current()->PostTask( 153 base::MessageLoopForUI::current()->PostTask(
93 FROM_HERE, 154 FROM_HERE,
94 base::Bind(&ExtensionInstalledBubble::ShowInternal, 155 base::Bind(&ExtensionInstalledBubble::ShowInternal,
95 weak_factory_.GetWeakPtr())); 156 weak_factory_.GetWeakPtr()));
96 } 157 }
97 } 158 }
98 159
99 void ExtensionInstalledBubble::OnExtensionUnloaded( 160 void ExtensionInstalledBubble::OnExtensionUnloaded(
100 content::BrowserContext* browser_context, 161 content::BrowserContext* browser_context,
101 const extensions::Extension* extension, 162 const extensions::Extension* extension,
102 extensions::UnloadedExtensionInfo::Reason reason) { 163 extensions::UnloadedExtensionInfo::Reason reason) {
103 if (extension == extension_) { 164 if (extension == extension_) {
104 // Extension is going away, make sure ShowInternal won't be called. 165 // Extension is going away, make sure ShowInternal won't be called.
105 weak_factory_.InvalidateWeakPtrs(); 166 weak_factory_.InvalidateWeakPtrs();
106 extension_ = NULL; 167 extension_ = NULL;
107 } 168 }
108 } 169 }
109 170
110 void ExtensionInstalledBubble::Observe( 171 void ExtensionInstalledBubble::Observe(
111 int type, 172 int type,
112 const content::NotificationSource& source, 173 const content::NotificationSource& source,
113 const content::NotificationDetails& details) { 174 const content::NotificationDetails& details) {
114 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_CLOSING) 175 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_CLOSING)
115 << "Received unexpected notification"; 176 << "Received unexpected notification";
116 delete delegate_; 177 delete delegate_;
117 } 178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698