1 // Copyright (c) 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/extensions/dev_mode_bubble_controller.h"
7 #include "base/lazy_instance.h"
8 #include "base/metrics/histogram.h"
9 #include "chrome/browser/extensions/extension_action_manager.h"
10 #include "chrome/browser/extensions/extension_message_bubble.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_toolbar_model.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/common/chrome_version_info.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/common/feature_switch.h"
21 #include "grit/components_strings.h"
22 #include "ui/base/l10n/l10n_util.h"
24 namespace extensions
{
28 base::LazyInstance
<std::set
<Profile
*> > g_shown_for_profiles
=
29 LAZY_INSTANCE_INITIALIZER
;
31 ////////////////////////////////////////////////////////////////////////////////
32 // DevModeBubbleDelegate
34 class DevModeBubbleDelegate
35 : public ExtensionMessageBubbleController::Delegate
{
37 explicit DevModeBubbleDelegate(Profile
* profile
);
38 ~DevModeBubbleDelegate() override
;
40 // ExtensionMessageBubbleController::Delegate methods.
41 bool ShouldIncludeExtension(const std::string
& extension_id
) override
;
42 void AcknowledgeExtension(
43 const std::string
& extension_id
,
44 ExtensionMessageBubbleController::BubbleAction user_action
) override
;
45 void PerformAction(const ExtensionIdList
& list
) override
;
46 void OnClose() override
;
47 base::string16
GetTitle() const override
;
48 base::string16
GetMessageBody(bool anchored_to_browser_action
) const override
;
49 base::string16
GetOverflowText(
50 const base::string16
& overflow_count
) const override
;
51 GURL
GetLearnMoreUrl() const override
;
52 base::string16
GetActionButtonLabel() const override
;
53 base::string16
GetDismissButtonLabel() const override
;
54 bool ShouldShowExtensionList() const override
;
55 bool ShouldHighlightExtensions() const override
;
56 void LogExtensionCount(size_t count
) override
;
58 ExtensionMessageBubbleController::BubbleAction action
) override
;
61 // Our extension service. Weak, not owned by us.
62 ExtensionService
* service_
;
64 DISALLOW_COPY_AND_ASSIGN(DevModeBubbleDelegate
);
67 DevModeBubbleDelegate::DevModeBubbleDelegate(Profile
* profile
)
68 : ExtensionMessageBubbleController::Delegate(profile
),
69 service_(ExtensionSystem::Get(profile
)->extension_service()) {
72 DevModeBubbleDelegate::~DevModeBubbleDelegate() {
75 bool DevModeBubbleDelegate::ShouldIncludeExtension(
76 const std::string
& extension_id
) {
77 const Extension
* extension
= service_
->GetExtensionById(extension_id
, false);
80 return DevModeBubbleController::IsDevModeExtension(extension
);
83 void DevModeBubbleDelegate::AcknowledgeExtension(
84 const std::string
& extension_id
,
85 ExtensionMessageBubbleController::BubbleAction user_action
) {
88 void DevModeBubbleDelegate::PerformAction(const ExtensionIdList
& list
) {
89 for (size_t i
= 0; i
< list
.size(); ++i
)
90 service_
->DisableExtension(list
[i
], Extension::DISABLE_USER_ACTION
);
93 void DevModeBubbleDelegate::OnClose() {
94 ExtensionToolbarModel
* toolbar_model
= ExtensionToolbarModel::Get(profile());
96 toolbar_model
->StopHighlighting();
99 base::string16
DevModeBubbleDelegate::GetTitle() const {
100 return l10n_util::GetStringUTF16(IDS_EXTENSIONS_DISABLE_DEVELOPER_MODE_TITLE
);
103 base::string16
DevModeBubbleDelegate::GetMessageBody(
104 bool anchored_to_browser_action
) const {
105 return l10n_util::GetStringUTF16(IDS_EXTENSIONS_DISABLE_DEVELOPER_MODE_BODY
);
108 base::string16
DevModeBubbleDelegate::GetOverflowText(
109 const base::string16
& overflow_count
) const {
110 return l10n_util::GetStringFUTF16(
111 IDS_EXTENSIONS_DISABLED_AND_N_MORE
,
115 GURL
DevModeBubbleDelegate::GetLearnMoreUrl() const {
116 return GURL(chrome::kChromeUIExtensionsURL
);
119 base::string16
DevModeBubbleDelegate::GetActionButtonLabel() const {
120 return l10n_util::GetStringUTF16(IDS_DISABLE
);
123 base::string16
DevModeBubbleDelegate::GetDismissButtonLabel() const {
124 return l10n_util::GetStringUTF16(IDS_CANCEL
);
127 bool DevModeBubbleDelegate::ShouldShowExtensionList() const {
131 bool DevModeBubbleDelegate::ShouldHighlightExtensions() const {
135 void DevModeBubbleDelegate::LogExtensionCount(size_t count
) {
136 UMA_HISTOGRAM_COUNTS_100(
137 "ExtensionBubble.ExtensionsInDevModeCount", count
);
140 void DevModeBubbleDelegate::LogAction(
141 ExtensionMessageBubbleController::BubbleAction action
) {
142 UMA_HISTOGRAM_ENUMERATION(
143 "ExtensionBubble.DevModeUserSelection",
144 action
, ExtensionMessageBubbleController::ACTION_BOUNDARY
);
149 ////////////////////////////////////////////////////////////////////////////////
150 // DevModeBubbleController
153 void DevModeBubbleController::ClearProfileListForTesting() {
154 g_shown_for_profiles
.Get().clear();
158 bool DevModeBubbleController::IsDevModeExtension(
159 const Extension
* extension
) {
160 if (!FeatureSwitch::force_dev_mode_highlighting()->IsEnabled()) {
161 if (chrome::VersionInfo::GetChannel() < chrome::VersionInfo::CHANNEL_BETA
)
164 return extension
->location() == Manifest::UNPACKED
||
165 extension
->location() == Manifest::COMMAND_LINE
;
168 DevModeBubbleController::DevModeBubbleController(Profile
* profile
)
169 : ExtensionMessageBubbleController(new DevModeBubbleDelegate(profile
),
173 DevModeBubbleController::~DevModeBubbleController() {
176 bool DevModeBubbleController::ShouldShow() {
177 return !g_shown_for_profiles
.Get().count(profile_
->GetOriginalProfile()) &&
178 !GetExtensionList().empty();
181 void DevModeBubbleController::Show(ExtensionMessageBubble
* bubble
) {
182 g_shown_for_profiles
.Get().insert(profile_
->GetOriginalProfile());
183 ExtensionMessageBubbleController::Show(bubble
);
186 } // namespace extensions