OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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/ntp_overridden_bubble_controller.h" |
| 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/url_constants.h" |
| 11 #include "extensions/browser/extension_registry.h" |
| 12 #include "extensions/browser/extension_system.h" |
| 13 #include "grit/generated_resources.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 |
| 16 using extensions::ExtensionMessageBubbleController; |
| 17 using extensions::NtpOverriddenBubbleController; |
| 18 |
| 19 namespace { |
| 20 |
| 21 //////////////////////////////////////////////////////////////////////////////// |
| 22 // NtpOverriddenBubbleDelegate |
| 23 |
| 24 class NtpOverriddenBubbleDelegate |
| 25 : public extensions::ExtensionMessageBubbleController::Delegate { |
| 26 public: |
| 27 NtpOverriddenBubbleDelegate(ExtensionService* service, Profile* profile); |
| 28 virtual ~NtpOverriddenBubbleDelegate(); |
| 29 |
| 30 // ExtensionMessageBubbleController::Delegate methods. |
| 31 virtual bool ShouldIncludeExtension(const std::string& extension_id) OVERRIDE; |
| 32 virtual void AcknowledgeExtension( |
| 33 const std::string& extension_id, |
| 34 extensions::ExtensionMessageBubbleController::BubbleAction |
| 35 user_action) OVERRIDE; |
| 36 virtual void PerformAction(const extensions::ExtensionIdList& list) OVERRIDE; |
| 37 virtual base::string16 GetTitle() const OVERRIDE; |
| 38 virtual base::string16 GetMessageBody() const OVERRIDE; |
| 39 virtual base::string16 GetOverflowText( |
| 40 const base::string16& overflow_count) const OVERRIDE; |
| 41 virtual base::string16 GetLearnMoreLabel() const OVERRIDE; |
| 42 virtual GURL GetLearnMoreUrl() const OVERRIDE; |
| 43 virtual base::string16 GetActionButtonLabel() const OVERRIDE; |
| 44 virtual base::string16 GetDismissButtonLabel() const OVERRIDE; |
| 45 virtual bool ShouldShowExtensionList() const OVERRIDE; |
| 46 virtual void RestrictToSingleExtension( |
| 47 const std::string& extension_id) OVERRIDE; |
| 48 virtual void LogExtensionCount(size_t count) OVERRIDE; |
| 49 virtual void LogAction( |
| 50 extensions::ExtensionMessageBubbleController::BubbleAction |
| 51 action) OVERRIDE; |
| 52 |
| 53 private: |
| 54 // Our extension service. Weak, not owned by us. |
| 55 ExtensionService* service_; |
| 56 |
| 57 // A weak pointer to the profile we are associated with. Not owned by us. |
| 58 Profile* profile_; |
| 59 |
| 60 // The ID of the extension we are showing the bubble for. |
| 61 std::string extension_id_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(NtpOverriddenBubbleDelegate); |
| 64 }; |
| 65 |
| 66 NtpOverriddenBubbleDelegate::NtpOverriddenBubbleDelegate( |
| 67 ExtensionService* service, |
| 68 Profile* profile) |
| 69 : service_(service), profile_(profile) {} |
| 70 |
| 71 NtpOverriddenBubbleDelegate::~NtpOverriddenBubbleDelegate() {} |
| 72 |
| 73 bool NtpOverriddenBubbleDelegate::ShouldIncludeExtension( |
| 74 const std::string& extension_id) { |
| 75 if (!extension_id_.empty() && extension_id_ != extension_id) |
| 76 return false; |
| 77 |
| 78 using extensions::ExtensionRegistry; |
| 79 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
| 80 const extensions::Extension* extension = |
| 81 registry->GetExtensionById(extension_id, ExtensionRegistry::ENABLED); |
| 82 if (!extension) |
| 83 return false; // The extension provided is no longer enabled. |
| 84 |
| 85 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_); |
| 86 if (prefs->HasNtpOverriddenBubbleBeenAcknowledged(extension_id)) |
| 87 return false; |
| 88 |
| 89 return true; |
| 90 } |
| 91 |
| 92 void NtpOverriddenBubbleDelegate::AcknowledgeExtension( |
| 93 const std::string& extension_id, |
| 94 ExtensionMessageBubbleController::BubbleAction user_action) { |
| 95 if (user_action != ExtensionMessageBubbleController::ACTION_EXECUTE) { |
| 96 extensions::ExtensionPrefs* prefs = |
| 97 extensions::ExtensionPrefs::Get(profile_); |
| 98 prefs->SetNtpOverriddenBubbleBeenAcknowledged(extension_id, true); |
| 99 } |
| 100 } |
| 101 |
| 102 void NtpOverriddenBubbleDelegate::PerformAction( |
| 103 const extensions::ExtensionIdList& list) { |
| 104 for (size_t i = 0; i < list.size(); ++i) { |
| 105 service_->DisableExtension(list[i], |
| 106 extensions::Extension::DISABLE_USER_ACTION); |
| 107 } |
| 108 } |
| 109 |
| 110 base::string16 NtpOverriddenBubbleDelegate::GetTitle() const { |
| 111 return l10n_util::GetStringUTF16( |
| 112 IDS_EXTENSIONS_NTP_CONTROLLED_TITLE_HOME_PAGE_BUBBLE); |
| 113 } |
| 114 |
| 115 base::string16 NtpOverriddenBubbleDelegate::GetMessageBody() const { |
| 116 base::string16 body = |
| 117 l10n_util::GetStringUTF16(IDS_EXTENSIONS_NTP_CONTROLLED_FIRST_LINE); |
| 118 body += l10n_util::GetStringUTF16( |
| 119 IDS_EXTENSIONS_SETTINGS_API_THIRD_LINE_CONFIRMATION); |
| 120 return body; |
| 121 } |
| 122 |
| 123 base::string16 NtpOverriddenBubbleDelegate::GetOverflowText( |
| 124 const base::string16& overflow_count) const { |
| 125 // Does not have more than one extension in the list at a time. |
| 126 NOTREACHED(); |
| 127 return base::string16(); |
| 128 } |
| 129 |
| 130 base::string16 NtpOverriddenBubbleDelegate::GetLearnMoreLabel() const { |
| 131 return l10n_util::GetStringUTF16(IDS_LEARN_MORE); |
| 132 } |
| 133 |
| 134 GURL NtpOverriddenBubbleDelegate::GetLearnMoreUrl() const { |
| 135 // TODO(finnur): Rename the const when things settle down (since it is used in |
| 136 // more places than for the Settings API bubble now). |
| 137 return GURL(chrome::kSettingsApiLearnMoreURL); |
| 138 } |
| 139 |
| 140 base::string16 NtpOverriddenBubbleDelegate::GetActionButtonLabel() const { |
| 141 // TODO(finnur): Rename the const when things settle down (since it is used in |
| 142 // more places than for the Settings API bubble now). |
| 143 return l10n_util::GetStringUTF16( |
| 144 IDS_EXTENSIONS_SETTINGS_API_RESTORE_SETTINGS); |
| 145 } |
| 146 |
| 147 base::string16 NtpOverriddenBubbleDelegate::GetDismissButtonLabel() const { |
| 148 // TODO(finnur): Rename the const when things settle down (since it is used in |
| 149 // more places than for the Settings API bubble now). |
| 150 return l10n_util::GetStringUTF16(IDS_EXTENSIONS_SETTINGS_API_KEEP_CHANGES); |
| 151 } |
| 152 |
| 153 bool NtpOverriddenBubbleDelegate::ShouldShowExtensionList() const { |
| 154 return false; |
| 155 } |
| 156 |
| 157 void NtpOverriddenBubbleDelegate::RestrictToSingleExtension( |
| 158 const std::string& extension_id) { |
| 159 extension_id_ = extension_id; |
| 160 } |
| 161 |
| 162 void NtpOverriddenBubbleDelegate::LogExtensionCount(size_t count) { |
| 163 UMA_HISTOGRAM_COUNTS_100("NtpOverriddenBubble.ExtensionCount", count); |
| 164 } |
| 165 |
| 166 void NtpOverriddenBubbleDelegate::LogAction( |
| 167 ExtensionMessageBubbleController::BubbleAction action) { |
| 168 UMA_HISTOGRAM_ENUMERATION("NtpOverriddenBubble.UserSelection", |
| 169 action, |
| 170 ExtensionMessageBubbleController::ACTION_BOUNDARY); |
| 171 } |
| 172 |
| 173 } // namespace |
| 174 |
| 175 namespace extensions { |
| 176 |
| 177 //////////////////////////////////////////////////////////////////////////////// |
| 178 // NtpOverriddenBubbleController |
| 179 |
| 180 NtpOverriddenBubbleController::NtpOverriddenBubbleController(Profile* profile) |
| 181 : ExtensionMessageBubbleController( |
| 182 new NtpOverriddenBubbleDelegate( |
| 183 ExtensionSystem::Get(profile)->extension_service(), |
| 184 profile), |
| 185 profile), |
| 186 profile_(profile) {} |
| 187 |
| 188 NtpOverriddenBubbleController::~NtpOverriddenBubbleController() {} |
| 189 |
| 190 bool NtpOverriddenBubbleController::ShouldShow( |
| 191 const std::string& extension_id) { |
| 192 if (!delegate()->ShouldIncludeExtension(extension_id)) |
| 193 return false; |
| 194 |
| 195 delegate()->RestrictToSingleExtension(extension_id); |
| 196 return true; |
| 197 } |
| 198 |
| 199 bool NtpOverriddenBubbleController::CloseOnDeactivate() { |
| 200 return true; |
| 201 } |
| 202 |
| 203 } // namespace extensions |
OLD | NEW |