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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
// 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/external_install_error.h"
#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/extension_install_prompt_show_params.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/external_install_manager.h"
#include "chrome/browser/extensions/webstore_data_fetcher.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/global_error/global_error.h"
#include "chrome/browser/ui/global_error/global_error_service.h"
#include "chrome/browser/ui/global_error/global_error_service_factory.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/grit/generated_resources.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/uninstall_reason.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_operations.h"
namespace extensions {
namespace {
// Return the menu label for a global error.
base::string16 GetMenuItemLabel(const Extension* extension) {
if (!extension)
return base::string16();
int id = -1;
if (extension->is_app())
id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_APP;
else if (extension->is_theme())
id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_THEME;
else
id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_EXTENSION;
return l10n_util::GetStringFUTF16(id, base::UTF8ToUTF16(extension->name()));
}
// A global error that spawns a dialog when the menu item is clicked.
class ExternalInstallMenuAlert : public GlobalError {
public:
explicit ExternalInstallMenuAlert(ExternalInstallError* error);
~ExternalInstallMenuAlert() override;
private:
// GlobalError implementation.
Severity GetSeverity() override;
bool HasMenuItem() override;
int MenuItemCommandID() override;
base::string16 MenuItemLabel() override;
void ExecuteMenuItem(Browser* browser) override;
bool HasBubbleView() override;
bool HasShownBubbleView() override;
void ShowBubbleView(Browser* browser) override;
GlobalErrorBubbleViewBase* GetBubbleView() override;
// The owning ExternalInstallError.
ExternalInstallError* error_;
DISALLOW_COPY_AND_ASSIGN(ExternalInstallMenuAlert);
};
// A global error that spawns a bubble when the menu item is clicked.
class ExternalInstallBubbleAlert : public GlobalErrorWithStandardBubble {
public:
explicit ExternalInstallBubbleAlert(ExternalInstallError* error,
ExtensionInstallPrompt::Prompt* prompt);
~ExternalInstallBubbleAlert() override;
private:
// GlobalError implementation.
Severity GetSeverity() override;
bool HasMenuItem() override;
int MenuItemCommandID() override;
base::string16 MenuItemLabel() override;
void ExecuteMenuItem(Browser* browser) override;
// GlobalErrorWithStandardBubble implementation.
gfx::Image GetBubbleViewIcon() override;
base::string16 GetBubbleViewTitle() override;
std::vector<base::string16> GetBubbleViewMessages() override;
base::string16 GetBubbleViewAcceptButtonLabel() override;
base::string16 GetBubbleViewCancelButtonLabel() override;
void OnBubbleViewDidClose(Browser* browser) override;
void BubbleViewAcceptButtonPressed(Browser* browser) override;
void BubbleViewCancelButtonPressed(Browser* browser) override;
// The owning ExternalInstallError.
ExternalInstallError* error_;
// The Prompt with all information, which we then use to populate the bubble.
ExtensionInstallPrompt::Prompt* prompt_;
DISALLOW_COPY_AND_ASSIGN(ExternalInstallBubbleAlert);
};
////////////////////////////////////////////////////////////////////////////////
// ExternalInstallMenuAlert
ExternalInstallMenuAlert::ExternalInstallMenuAlert(ExternalInstallError* error)
: error_(error) {
}
ExternalInstallMenuAlert::~ExternalInstallMenuAlert() {
}
GlobalError::Severity ExternalInstallMenuAlert::GetSeverity() {
return SEVERITY_LOW;
}
bool ExternalInstallMenuAlert::HasMenuItem() {
return true;
}
int ExternalInstallMenuAlert::MenuItemCommandID() {
return IDC_EXTERNAL_EXTENSION_ALERT;
}
base::string16 ExternalInstallMenuAlert::MenuItemLabel() {
return GetMenuItemLabel(error_->GetExtension());
}
void ExternalInstallMenuAlert::ExecuteMenuItem(Browser* browser) {
error_->ShowDialog(browser);
}
bool ExternalInstallMenuAlert::HasBubbleView() {
return false;
}
bool ExternalInstallMenuAlert::HasShownBubbleView() {
NOTREACHED();
return true;
}
void ExternalInstallMenuAlert::ShowBubbleView(Browser* browser) {
NOTREACHED();
}
GlobalErrorBubbleViewBase* ExternalInstallMenuAlert::GetBubbleView() {
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
// ExternalInstallBubbleAlert
ExternalInstallBubbleAlert::ExternalInstallBubbleAlert(
ExternalInstallError* error,
ExtensionInstallPrompt::Prompt* prompt)
: error_(error), prompt_(prompt) {
DCHECK(error_);
DCHECK(prompt_);
}
ExternalInstallBubbleAlert::~ExternalInstallBubbleAlert() {
}
GlobalError::Severity ExternalInstallBubbleAlert::GetSeverity() {
return SEVERITY_LOW;
}
bool ExternalInstallBubbleAlert::HasMenuItem() {
return true;
}
int ExternalInstallBubbleAlert::MenuItemCommandID() {
return IDC_EXTERNAL_EXTENSION_ALERT;
}
base::string16 ExternalInstallBubbleAlert::MenuItemLabel() {
return GetMenuItemLabel(error_->GetExtension());
}
void ExternalInstallBubbleAlert::ExecuteMenuItem(Browser* browser) {
ShowBubbleView(browser);
}
gfx::Image ExternalInstallBubbleAlert::GetBubbleViewIcon() {
if (prompt_->icon().IsEmpty())
return GlobalErrorWithStandardBubble::GetBubbleViewIcon();
// Scale icon to a reasonable size.
return gfx::Image(gfx::ImageSkiaOperations::CreateResizedImage(
*prompt_->icon().ToImageSkia(),
skia::ImageOperations::RESIZE_BEST,
gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
extension_misc::EXTENSION_ICON_SMALL)));
}
base::string16 ExternalInstallBubbleAlert::GetBubbleViewTitle() {
return prompt_->GetDialogTitle();
}
std::vector<base::string16>
ExternalInstallBubbleAlert::GetBubbleViewMessages() {
ExtensionInstallPrompt::PermissionsType regular_permissions =
ExtensionInstallPrompt::PermissionsType::REGULAR_PERMISSIONS;
ExtensionInstallPrompt::PermissionsType withheld_permissions =
ExtensionInstallPrompt::PermissionsType::WITHHELD_PERMISSIONS;
std::vector<base::string16> messages;
messages.push_back(prompt_->GetHeading());
if (prompt_->GetPermissionCount(regular_permissions)) {
messages.push_back(prompt_->GetPermissionsHeading(regular_permissions));
for (size_t i = 0; i < prompt_->GetPermissionCount(regular_permissions);
++i) {
messages.push_back(l10n_util::GetStringFUTF16(
IDS_EXTENSION_PERMISSION_LINE,
prompt_->GetPermission(i, regular_permissions)));
}
}
if (prompt_->GetPermissionCount(withheld_permissions)) {
messages.push_back(prompt_->GetPermissionsHeading(withheld_permissions));
for (size_t i = 0; i < prompt_->GetPermissionCount(withheld_permissions);
++i) {
messages.push_back(l10n_util::GetStringFUTF16(
IDS_EXTENSION_PERMISSION_LINE,
prompt_->GetPermission(i, withheld_permissions)));
}
}
// TODO(yoz): OAuth issue advice?
return messages;
}
base::string16 ExternalInstallBubbleAlert::GetBubbleViewAcceptButtonLabel() {
return prompt_->GetAcceptButtonLabel();
}
base::string16 ExternalInstallBubbleAlert::GetBubbleViewCancelButtonLabel() {
return prompt_->GetAbortButtonLabel();
}
void ExternalInstallBubbleAlert::OnBubbleViewDidClose(Browser* browser) {
}
void ExternalInstallBubbleAlert::BubbleViewAcceptButtonPressed(
Browser* browser) {
error_->InstallUIProceed();
}
void ExternalInstallBubbleAlert::BubbleViewCancelButtonPressed(
Browser* browser) {
error_->InstallUIAbort(true);
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// ExternalInstallError
ExternalInstallError::ExternalInstallError(
content::BrowserContext* browser_context,
const std::string& extension_id,
AlertType alert_type,
ExternalInstallManager* manager)
: browser_context_(browser_context),
extension_id_(extension_id),
alert_type_(alert_type),
manager_(manager),
error_service_(GlobalErrorServiceFactory::GetForProfile(
Profile::FromBrowserContext(browser_context_))),
weak_factory_(this) {
prompt_ = new ExtensionInstallPrompt::Prompt(
ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT);
webstore_data_fetcher_.reset(new WebstoreDataFetcher(
this, browser_context_->GetRequestContext(), GURL(), extension_id_));
webstore_data_fetcher_->Start();
}
ExternalInstallError::~ExternalInstallError() {
if (global_error_.get())
error_service_->RemoveGlobalError(global_error_.get());
}
void ExternalInstallError::InstallUIProceed() {
const Extension* extension = GetExtension();
if (extension) {
ExtensionSystem::Get(browser_context_)
->extension_service()
->GrantPermissionsAndEnableExtension(extension);
// Since the manager listens for the extension to be loaded, this will
// remove the error...
} else {
// ... Otherwise we have to do it explicitly.
manager_->RemoveExternalInstallError();
}
}
void ExternalInstallError::InstallUIAbort(bool user_initiated) {
if (user_initiated && GetExtension()) {
ExtensionSystem::Get(browser_context_)
->extension_service()
->UninstallExtension(extension_id_,
extensions::UNINSTALL_REASON_INSTALL_CANCELED,
base::Bind(&base::DoNothing),
NULL); // Ignore error.
// Since the manager listens for the extension to be removed, this will
// remove the error...
} else {
// ... Otherwise we have to do it explicitly.
manager_->RemoveExternalInstallError();
}
}
void ExternalInstallError::ShowDialog(Browser* browser) {
DCHECK(install_ui_.get());
DCHECK(prompt_.get());
DCHECK(browser);
content::WebContents* web_contents = NULL;
web_contents = browser->tab_strip_model()->GetActiveWebContents();
install_ui_show_params_.reset(
new ExtensionInstallPromptShowParams(web_contents));
ExtensionInstallPrompt::GetDefaultShowDialogCallback().Run(
install_ui_show_params_.get(), this, prompt_);
}
const Extension* ExternalInstallError::GetExtension() const {
return ExtensionRegistry::Get(browser_context_)
->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING);
}
void ExternalInstallError::OnWebstoreRequestFailure() {
OnFetchComplete();
}
void ExternalInstallError::OnWebstoreResponseParseSuccess(
scoped_ptr<base::DictionaryValue> webstore_data) {
std::string localized_user_count;
double average_rating = 0;
int rating_count = 0;
if (!webstore_data->GetString(kUsersKey, &localized_user_count) ||
!webstore_data->GetDouble(kAverageRatingKey, &average_rating) ||
!webstore_data->GetInteger(kRatingCountKey, &rating_count)) {
// If we don't get a valid webstore response, short circuit, and continue
// to show a prompt without webstore data.
OnFetchComplete();
return;
}
bool show_user_count = true;
webstore_data->GetBoolean(kShowUserCountKey, &show_user_count);
prompt_->SetWebstoreData(
localized_user_count, show_user_count, average_rating, rating_count);
OnFetchComplete();
}
void ExternalInstallError::OnWebstoreResponseParseFailure(
const std::string& error) {
OnFetchComplete();
}
void ExternalInstallError::OnFetchComplete() {
// Create a new ExtensionInstallPrompt. We pass in NULL for the UI
// components because we display at a later point, and don't want
// to pass ones which may be invalidated.
install_ui_.reset(
new ExtensionInstallPrompt(Profile::FromBrowserContext(browser_context_),
NULL)); // NULL native window.
install_ui_->ConfirmExternalInstall(
this,
GetExtension(),
base::Bind(&ExternalInstallError::OnDialogReady,
weak_factory_.GetWeakPtr()),
prompt_);
}
void ExternalInstallError::OnDialogReady(
ExtensionInstallPromptShowParams* show_params,
ExtensionInstallPrompt::Delegate* prompt_delegate,
scoped_refptr<ExtensionInstallPrompt::Prompt> prompt) {
DCHECK_EQ(this, prompt_delegate);
prompt_ = prompt;
if (alert_type_ == BUBBLE_ALERT) {
global_error_.reset(new ExternalInstallBubbleAlert(this, prompt_.get()));
error_service_->AddGlobalError(global_error_.get());
Browser* browser =
chrome::FindTabbedBrowser(Profile::FromBrowserContext(browser_context_),
true,
chrome::GetActiveDesktop());
if (browser)
global_error_->ShowBubbleView(browser);
} else {
DCHECK(alert_type_ == MENU_ALERT);
global_error_.reset(new ExternalInstallMenuAlert(this));
error_service_->AddGlobalError(global_error_.get());
}
}
} // namespace extensions
|