[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/extensions/dev_mode_bubble_controller.h" |
| 6 | |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 7 | #include "base/lazy_instance.h" |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 8 | #include "base/metrics/histogram.h" |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_action_manager.h" |
| 10 | #include "chrome/browser/extensions/extension_message_bubble.h" |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | a74569b | 2014-03-25 02:56:30 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_toolbar_model.h" |
[email protected] | f845448465 | 2014-02-27 04:20:42 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 15 | #include "chrome/common/chrome_version_info.h" |
| 16 | #include "chrome/common/url_constants.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 17 | #include "chrome/grit/generated_resources.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 18 | #include "extensions/browser/extension_prefs.h" |
[email protected] | d46c050 | 2014-02-14 13:33:36 | [diff] [blame] | 19 | #include "extensions/browser/extension_system.h" |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 20 | #include "extensions/common/feature_switch.h" |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 21 | #include "ui/base/l10n/l10n_util.h" |
| 22 | |
[email protected] | a74569b | 2014-03-25 02:56:30 | [diff] [blame] | 23 | namespace extensions { |
| 24 | |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 25 | namespace { |
| 26 | |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 27 | base::LazyInstance<std::set<Profile*> > g_shown_for_profiles = |
| 28 | LAZY_INSTANCE_INITIALIZER; |
| 29 | |
| 30 | //////////////////////////////////////////////////////////////////////////////// |
| 31 | // DevModeBubbleDelegate |
| 32 | |
[email protected] | 3d4d9337 | 2014-05-20 06:25:31 | [diff] [blame] | 33 | class DevModeBubbleDelegate |
| 34 | : public ExtensionMessageBubbleController::Delegate { |
| 35 | public: |
| 36 | explicit DevModeBubbleDelegate(Profile* profile); |
| 37 | virtual ~DevModeBubbleDelegate(); |
| 38 | |
| 39 | // ExtensionMessageBubbleController::Delegate methods. |
| 40 | virtual bool ShouldIncludeExtension(const std::string& extension_id) OVERRIDE; |
| 41 | virtual void AcknowledgeExtension( |
| 42 | const std::string& extension_id, |
| 43 | ExtensionMessageBubbleController::BubbleAction user_action) OVERRIDE; |
| 44 | virtual void PerformAction(const ExtensionIdList& list) OVERRIDE; |
| 45 | virtual void OnClose() OVERRIDE; |
| 46 | virtual base::string16 GetTitle() const OVERRIDE; |
[email protected] | 8dc56d0 | 2014-06-07 00:44:23 | [diff] [blame] | 47 | virtual base::string16 GetMessageBody( |
| 48 | bool anchored_to_browser_action) const OVERRIDE; |
[email protected] | 3d4d9337 | 2014-05-20 06:25:31 | [diff] [blame] | 49 | virtual base::string16 GetOverflowText( |
| 50 | const base::string16& overflow_count) const OVERRIDE; |
| 51 | virtual base::string16 GetLearnMoreLabel() const OVERRIDE; |
| 52 | virtual GURL GetLearnMoreUrl() const OVERRIDE; |
| 53 | virtual base::string16 GetActionButtonLabel() const OVERRIDE; |
| 54 | virtual base::string16 GetDismissButtonLabel() const OVERRIDE; |
| 55 | virtual bool ShouldShowExtensionList() const OVERRIDE; |
| 56 | virtual void LogExtensionCount(size_t count) OVERRIDE; |
| 57 | virtual void LogAction( |
| 58 | ExtensionMessageBubbleController::BubbleAction action) OVERRIDE; |
| 59 | |
| 60 | private: |
| 61 | // The associated profile (weak). |
| 62 | Profile* profile_; |
| 63 | |
| 64 | // Our extension service. Weak, not owned by us. |
| 65 | ExtensionService* service_; |
| 66 | |
| 67 | DISALLOW_COPY_AND_ASSIGN(DevModeBubbleDelegate); |
| 68 | }; |
| 69 | |
[email protected] | a74569b | 2014-03-25 02:56:30 | [diff] [blame] | 70 | DevModeBubbleDelegate::DevModeBubbleDelegate(Profile* profile) |
| 71 | : profile_(profile), |
| 72 | service_(ExtensionSystem::Get(profile)->extension_service()) {} |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 73 | |
| 74 | DevModeBubbleDelegate::~DevModeBubbleDelegate() { |
| 75 | } |
| 76 | |
| 77 | bool DevModeBubbleDelegate::ShouldIncludeExtension( |
| 78 | const std::string& extension_id) { |
[email protected] | a74569b | 2014-03-25 02:56:30 | [diff] [blame] | 79 | const Extension* extension = service_->GetExtensionById(extension_id, false); |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 80 | if (!extension) |
| 81 | return false; |
[email protected] | a74569b | 2014-03-25 02:56:30 | [diff] [blame] | 82 | return DevModeBubbleController::IsDevModeExtension(extension); |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void DevModeBubbleDelegate::AcknowledgeExtension( |
| 86 | const std::string& extension_id, |
| 87 | ExtensionMessageBubbleController::BubbleAction user_action) { |
| 88 | } |
| 89 | |
[email protected] | a74569b | 2014-03-25 02:56:30 | [diff] [blame] | 90 | void DevModeBubbleDelegate::PerformAction(const ExtensionIdList& list) { |
| 91 | for (size_t i = 0; i < list.size(); ++i) |
| 92 | service_->DisableExtension(list[i], Extension::DISABLE_USER_ACTION); |
| 93 | } |
| 94 | |
| 95 | void DevModeBubbleDelegate::OnClose() { |
| 96 | ExtensionToolbarModel* toolbar_model = ExtensionToolbarModel::Get(profile_); |
| 97 | if (toolbar_model) |
| 98 | toolbar_model->StopHighlighting(); |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | base::string16 DevModeBubbleDelegate::GetTitle() const { |
| 102 | return l10n_util::GetStringUTF16(IDS_EXTENSIONS_DISABLE_DEVELOPER_MODE_TITLE); |
| 103 | } |
| 104 | |
[email protected] | 8dc56d0 | 2014-06-07 00:44:23 | [diff] [blame] | 105 | base::string16 DevModeBubbleDelegate::GetMessageBody( |
| 106 | bool anchored_to_browser_action) const { |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 107 | return l10n_util::GetStringUTF16(IDS_EXTENSIONS_DISABLE_DEVELOPER_MODE_BODY); |
| 108 | } |
| 109 | |
| 110 | base::string16 DevModeBubbleDelegate::GetOverflowText( |
| 111 | const base::string16& overflow_count) const { |
| 112 | return l10n_util::GetStringFUTF16( |
[email protected] | d246924 | 2014-05-09 11:56:58 | [diff] [blame] | 113 | IDS_EXTENSIONS_DISABLED_AND_N_MORE, |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 114 | overflow_count); |
| 115 | } |
| 116 | |
| 117 | base::string16 DevModeBubbleDelegate::GetLearnMoreLabel() const { |
| 118 | return l10n_util::GetStringUTF16(IDS_LEARN_MORE); |
| 119 | } |
| 120 | |
| 121 | GURL DevModeBubbleDelegate::GetLearnMoreUrl() const { |
| 122 | return GURL(chrome::kChromeUIExtensionsURL); |
| 123 | } |
| 124 | |
| 125 | base::string16 DevModeBubbleDelegate::GetActionButtonLabel() const { |
| 126 | return l10n_util::GetStringUTF16(IDS_DISABLE); |
| 127 | } |
| 128 | |
| 129 | base::string16 DevModeBubbleDelegate::GetDismissButtonLabel() const { |
| 130 | return l10n_util::GetStringUTF16(IDS_CANCEL); |
| 131 | } |
| 132 | |
| 133 | bool DevModeBubbleDelegate::ShouldShowExtensionList() const { |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | void DevModeBubbleDelegate::LogExtensionCount(size_t count) { |
| 138 | UMA_HISTOGRAM_COUNTS_100( |
[email protected] | b61b05ce8d | 2014-05-14 15:33:01 | [diff] [blame] | 139 | "ExtensionBubble.ExtensionsInDevModeCount", count); |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void DevModeBubbleDelegate::LogAction( |
| 143 | ExtensionMessageBubbleController::BubbleAction action) { |
| 144 | UMA_HISTOGRAM_ENUMERATION( |
[email protected] | b61b05ce8d | 2014-05-14 15:33:01 | [diff] [blame] | 145 | "ExtensionBubble.DevModeUserSelection", |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 146 | action, ExtensionMessageBubbleController::ACTION_BOUNDARY); |
| 147 | } |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 148 | |
| 149 | } // namespace |
| 150 | |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 151 | //////////////////////////////////////////////////////////////////////////////// |
| 152 | // DevModeBubbleController |
| 153 | |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 154 | // static |
| 155 | void DevModeBubbleController::ClearProfileListForTesting() { |
| 156 | g_shown_for_profiles.Get().clear(); |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // static |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 160 | bool DevModeBubbleController::IsDevModeExtension( |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 161 | const Extension* extension) { |
[email protected] | d46c050 | 2014-02-14 13:33:36 | [diff] [blame] | 162 | if (!FeatureSwitch::force_dev_mode_highlighting()->IsEnabled()) { |
[email protected] | 3d4d9337 | 2014-05-20 06:25:31 | [diff] [blame] | 163 | if (chrome::VersionInfo::GetChannel() < chrome::VersionInfo::CHANNEL_BETA) |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | return extension->location() == Manifest::UNPACKED || |
| 167 | extension->location() == Manifest::COMMAND_LINE; |
| 168 | } |
| 169 | |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 170 | DevModeBubbleController::DevModeBubbleController(Profile* profile) |
[email protected] | a74569b | 2014-03-25 02:56:30 | [diff] [blame] | 171 | : ExtensionMessageBubbleController(new DevModeBubbleDelegate(profile), |
| 172 | profile), |
[email protected] | d46c050 | 2014-02-14 13:33:36 | [diff] [blame] | 173 | profile_(profile) {} |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 174 | |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 175 | DevModeBubbleController::~DevModeBubbleController() { |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 178 | bool DevModeBubbleController::ShouldShow() { |
[email protected] | dbc165c | 2014-04-18 19:59:12 | [diff] [blame] | 179 | return !g_shown_for_profiles.Get().count(profile_->GetOriginalProfile()) && |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 180 | !GetExtensionList().empty(); |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 183 | void DevModeBubbleController::Show(ExtensionMessageBubble* bubble) { |
[email protected] | dbc165c | 2014-04-18 19:59:12 | [diff] [blame] | 184 | g_shown_for_profiles.Get().insert(profile_->GetOriginalProfile()); |
[email protected] | 4f2f353d | 2014-01-14 11:21:09 | [diff] [blame] | 185 | ExtensionMessageBubbleController::Show(bubble); |
[email protected] | c72ebfe | 2013-12-13 21:57:53 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } // namespace extensions |