1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/settings_api_bubble_controller.h"
#include "base/metrics/histogram.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/settings_api_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/startup/startup_browser_creator.h"
#include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
namespace extensions {
namespace {
// Whether the user has been notified about extension taking over some aspect of
// the user's settings (homepage, startup pages, or search engine).
const char kSettingsBubbleAcknowledged[] = "ack_settings_bubble";
////////////////////////////////////////////////////////////////////////////////
// SettingsApiBubbleDelegate
class SettingsApiBubbleDelegate
: public ExtensionMessageBubbleController::Delegate {
public:
SettingsApiBubbleDelegate(Profile* profile, SettingsApiOverrideType type);
~SettingsApiBubbleDelegate() override;
// ExtensionMessageBubbleController::Delegate methods.
bool ShouldIncludeExtension(const std::string& extension_id) override;
void AcknowledgeExtension(
const std::string& extension_id,
ExtensionMessageBubbleController::BubbleAction user_action) override;
void PerformAction(const ExtensionIdList& list) override;
base::string16 GetTitle() const override;
base::string16 GetMessageBody(bool anchored_to_browser_action,
int extension_count) const override;
base::string16 GetOverflowText(
const base::string16& overflow_count) const override;
GURL GetLearnMoreUrl() const override;
base::string16 GetActionButtonLabel() const override;
base::string16 GetDismissButtonLabel() const override;
bool ShouldShowExtensionList() const override;
bool ShouldHighlightExtensions() const override;
void LogExtensionCount(size_t count) override;
void LogAction(
ExtensionMessageBubbleController::BubbleAction action) override;
private:
// The type of settings override this bubble will report on. This can be, for
// example, a bubble to notify the user that the search engine has been
// changed by an extension (or homepage/startup pages/etc).
SettingsApiOverrideType type_;
// The ID of the extension we are showing the bubble for.
std::string extension_id_;
DISALLOW_COPY_AND_ASSIGN(SettingsApiBubbleDelegate);
};
SettingsApiBubbleDelegate::SettingsApiBubbleDelegate(
Profile* profile,
SettingsApiOverrideType type)
: ExtensionMessageBubbleController::Delegate(profile),
type_(type) {
set_acknowledged_flag_pref_name(kSettingsBubbleAcknowledged);
}
SettingsApiBubbleDelegate::~SettingsApiBubbleDelegate() {}
bool SettingsApiBubbleDelegate::ShouldIncludeExtension(
const std::string& extension_id) {
const Extension* extension =
registry()->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
if (!extension)
return false; // The extension provided is no longer enabled.
if (HasBubbleInfoBeenAcknowledged(extension_id))
return false;
const Extension* override = NULL;
switch (type_) {
case extensions::BUBBLE_TYPE_HOME_PAGE:
override = extensions::GetExtensionOverridingHomepage(profile());
break;
case extensions::BUBBLE_TYPE_STARTUP_PAGES:
override = extensions::GetExtensionOverridingStartupPages(profile());
break;
case extensions::BUBBLE_TYPE_SEARCH_ENGINE:
override = extensions::GetExtensionOverridingSearchEngine(profile());
break;
}
if (!override || override->id() != extension->id())
return false;
extension_id_ = extension_id;
return true;
}
void SettingsApiBubbleDelegate::AcknowledgeExtension(
const std::string& extension_id,
ExtensionMessageBubbleController::BubbleAction user_action) {
if (user_action != ExtensionMessageBubbleController::ACTION_EXECUTE)
SetBubbleInfoBeenAcknowledged(extension_id, true);
}
void SettingsApiBubbleDelegate::PerformAction(const ExtensionIdList& list) {
for (size_t i = 0; i < list.size(); ++i) {
service()->DisableExtension(list[i], Extension::DISABLE_USER_ACTION);
}
}
base::string16 SettingsApiBubbleDelegate::GetTitle() const {
switch (type_) {
case BUBBLE_TYPE_HOME_PAGE:
return l10n_util::GetStringUTF16(
IDS_EXTENSIONS_SETTINGS_API_TITLE_HOME_PAGE_BUBBLE);
case BUBBLE_TYPE_STARTUP_PAGES:
return l10n_util::GetStringUTF16(
IDS_EXTENSIONS_SETTINGS_API_TITLE_STARTUP_PAGES_BUBBLE);
case BUBBLE_TYPE_SEARCH_ENGINE:
return l10n_util::GetStringUTF16(
IDS_EXTENSIONS_SETTINGS_API_TITLE_SEARCH_ENGINE_BUBBLE);
}
NOTREACHED();
return base::string16();
}
base::string16 SettingsApiBubbleDelegate::GetMessageBody(
bool anchored_to_browser_action,
int extension_count) const {
const Extension* extension =
registry()->GetExtensionById(extension_id_, ExtensionRegistry::ENABLED);
const SettingsOverrides* settings =
extension ? SettingsOverrides::Get(extension) : NULL;
if (!extension || !settings) {
NOTREACHED();
return base::string16();
}
bool home_change = settings->homepage != NULL;
bool startup_change = !settings->startup_pages.empty();
bool search_change = settings->search_engine != NULL;
int first_line_id = 0;
int second_line_id = 0;
base::string16 body;
switch (type_) {
case BUBBLE_TYPE_HOME_PAGE:
first_line_id = anchored_to_browser_action ?
IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_HOME_PAGE_SPECIFIC :
IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_HOME_PAGE;
if (startup_change && search_change) {
second_line_id =
IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_START_AND_SEARCH;
} else if (startup_change) {
second_line_id = IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_START_PAGES;
} else if (search_change) {
second_line_id = IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_SEARCH_ENGINE;
}
break;
case BUBBLE_TYPE_STARTUP_PAGES:
first_line_id = anchored_to_browser_action ?
IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_START_PAGES_SPECIFIC :
IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_START_PAGES;
if (home_change && search_change) {
second_line_id =
IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_HOME_AND_SEARCH;
} else if (home_change) {
second_line_id = IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_HOME_PAGE;
} else if (search_change) {
second_line_id = IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_SEARCH_ENGINE;
}
break;
case BUBBLE_TYPE_SEARCH_ENGINE:
first_line_id = anchored_to_browser_action ?
IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_SEARCH_ENGINE_SPECIFIC :
IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_SEARCH_ENGINE;
if (startup_change && home_change)
second_line_id = IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_START_AND_HOME;
else if (startup_change)
second_line_id = IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_START_PAGES;
else if (home_change)
second_line_id = IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_HOME_PAGE;
break;
}
DCHECK_NE(0, first_line_id);
body = anchored_to_browser_action ?
l10n_util::GetStringUTF16(first_line_id) :
l10n_util::GetStringFUTF16(first_line_id,
base::UTF8ToUTF16(extension->name()));
if (second_line_id)
body += l10n_util::GetStringUTF16(second_line_id);
body += l10n_util::GetStringUTF16(
IDS_EXTENSIONS_SETTINGS_API_THIRD_LINE_CONFIRMATION);
return body;
}
base::string16 SettingsApiBubbleDelegate::GetOverflowText(
const base::string16& overflow_count) const {
// Does not have more than one extension in the list at a time.
NOTREACHED();
return base::string16();
}
GURL SettingsApiBubbleDelegate::GetLearnMoreUrl() const {
return GURL(chrome::kExtensionControlledSettingLearnMoreURL);
}
base::string16 SettingsApiBubbleDelegate::GetActionButtonLabel() const {
return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_RESTORE_SETTINGS);
}
base::string16 SettingsApiBubbleDelegate::GetDismissButtonLabel() const {
return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_KEEP_CHANGES);
}
bool SettingsApiBubbleDelegate::ShouldShowExtensionList() const {
return false;
}
bool SettingsApiBubbleDelegate::ShouldHighlightExtensions() const {
return type_ == BUBBLE_TYPE_STARTUP_PAGES;
}
void SettingsApiBubbleDelegate::LogExtensionCount(size_t count) {
}
void SettingsApiBubbleDelegate::LogAction(
ExtensionMessageBubbleController::BubbleAction action) {
switch (type_) {
case BUBBLE_TYPE_HOME_PAGE:
UMA_HISTOGRAM_ENUMERATION(
"ExtensionOverrideBubble.SettingsApiUserSelectionHomePage",
action,
ExtensionMessageBubbleController::ACTION_BOUNDARY);
break;
case BUBBLE_TYPE_STARTUP_PAGES:
UMA_HISTOGRAM_ENUMERATION(
"ExtensionOverrideBubble.SettingsApiUserSelectionStartupPage",
action,
ExtensionMessageBubbleController::ACTION_BOUNDARY);
break;
case BUBBLE_TYPE_SEARCH_ENGINE:
UMA_HISTOGRAM_ENUMERATION(
"ExtensionOverrideBubble.SettingsApiUserSelectionSearchEngine",
action,
ExtensionMessageBubbleController::ACTION_BOUNDARY);
break;
}
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// SettingsApiBubbleController
SettingsApiBubbleController::SettingsApiBubbleController(
Browser* browser,
SettingsApiOverrideType type)
: ExtensionMessageBubbleController(
new SettingsApiBubbleDelegate(browser->profile(), type),
browser),
type_(type) {}
SettingsApiBubbleController::~SettingsApiBubbleController() {}
bool SettingsApiBubbleController::ShouldShow() {
const Extension* extension = nullptr;
switch (type_) {
case BUBBLE_TYPE_HOME_PAGE:
extension = GetExtensionOverridingHomepage(profile());
break;
case BUBBLE_TYPE_SEARCH_ENGINE:
extension = GetExtensionOverridingSearchEngine(profile());
break;
case BUBBLE_TYPE_STARTUP_PAGES:
extension = GetExtensionOverridingStartupPages(profile());
break;
}
if (!extension)
return false;
if (delegate()->HasBubbleInfoBeenAcknowledged(extension->id()))
return false;
if (!delegate()->ShouldIncludeExtension(extension->id()))
return false;
// If the browser is showing the 'Chrome crashed' infobar, it won't be showing
// the startup pages, so there's no point in showing the bubble now.
if (type_ == BUBBLE_TYPE_STARTUP_PAGES)
return profile()->GetLastSessionExitType() != Profile::EXIT_CRASHED;
return true;
}
bool SettingsApiBubbleController::CloseOnDeactivate() {
// Startup bubbles tend to get lost in the focus storm that happens on
// startup. Other types should dismiss on focus loss.
return type_ != BUBBLE_TYPE_STARTUP_PAGES;
}
} // namespace extensions
|