OLD | NEW |
| (Empty) |
1 // Copyright 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/external_install_error.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "chrome/app/chrome_command_ids.h" | |
10 #include "chrome/browser/extensions/extension_install_ui.h" | |
11 #include "chrome/browser/extensions/extension_service.h" | |
12 #include "chrome/browser/extensions/external_install_manager.h" | |
13 #include "chrome/browser/extensions/webstore_data_fetcher.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/browser/ui/browser.h" | |
16 #include "chrome/browser/ui/browser_finder.h" | |
17 #include "chrome/browser/ui/global_error/global_error.h" | |
18 #include "chrome/browser/ui/global_error/global_error_service.h" | |
19 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | |
20 #include "extensions/browser/extension_registry.h" | |
21 #include "extensions/browser/extension_system.h" | |
22 #include "extensions/common/constants.h" | |
23 #include "extensions/common/extension.h" | |
24 #include "grit/generated_resources.h" | |
25 #include "ui/base/l10n/l10n_util.h" | |
26 #include "ui/gfx/image/image.h" | |
27 #include "ui/gfx/image/image_skia_operations.h" | |
28 | |
29 namespace extensions { | |
30 | |
31 namespace { | |
32 | |
33 // Return the menu label for a global error. | |
34 base::string16 GetMenuItemLabel(const Extension* extension) { | |
35 if (!extension) | |
36 return base::string16(); | |
37 | |
38 int id = -1; | |
39 if (extension->is_app()) | |
40 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_APP; | |
41 else if (extension->is_theme()) | |
42 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_THEME; | |
43 else | |
44 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_EXTENSION; | |
45 | |
46 return l10n_util::GetStringFUTF16(id, base::UTF8ToUTF16(extension->name())); | |
47 } | |
48 | |
49 // A global error that spawns a dialog when the menu item is clicked. | |
50 class ExternalInstallMenuAlert : public GlobalError { | |
51 public: | |
52 explicit ExternalInstallMenuAlert(ExternalInstallError* error); | |
53 virtual ~ExternalInstallMenuAlert(); | |
54 | |
55 private: | |
56 // GlobalError implementation. | |
57 virtual Severity GetSeverity() OVERRIDE; | |
58 virtual bool HasMenuItem() OVERRIDE; | |
59 virtual int MenuItemCommandID() OVERRIDE; | |
60 virtual base::string16 MenuItemLabel() OVERRIDE; | |
61 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE; | |
62 virtual bool HasBubbleView() OVERRIDE; | |
63 virtual bool HasShownBubbleView() OVERRIDE; | |
64 virtual void ShowBubbleView(Browser* browser) OVERRIDE; | |
65 virtual GlobalErrorBubbleViewBase* GetBubbleView() OVERRIDE; | |
66 | |
67 // The owning ExternalInstallError. | |
68 ExternalInstallError* error_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(ExternalInstallMenuAlert); | |
71 }; | |
72 | |
73 // A global error that spawns a bubble when the menu item is clicked. | |
74 class ExternalInstallBubbleAlert : public GlobalErrorWithStandardBubble { | |
75 public: | |
76 explicit ExternalInstallBubbleAlert(ExternalInstallError* error, | |
77 ExtensionInstallPrompt::Prompt* prompt); | |
78 virtual ~ExternalInstallBubbleAlert(); | |
79 | |
80 private: | |
81 // GlobalError implementation. | |
82 virtual Severity GetSeverity() OVERRIDE; | |
83 virtual bool HasMenuItem() OVERRIDE; | |
84 virtual int MenuItemCommandID() OVERRIDE; | |
85 virtual base::string16 MenuItemLabel() OVERRIDE; | |
86 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE; | |
87 | |
88 // GlobalErrorWithStandardBubble implementation. | |
89 virtual gfx::Image GetBubbleViewIcon() OVERRIDE; | |
90 virtual base::string16 GetBubbleViewTitle() OVERRIDE; | |
91 virtual std::vector<base::string16> GetBubbleViewMessages() OVERRIDE; | |
92 virtual base::string16 GetBubbleViewAcceptButtonLabel() OVERRIDE; | |
93 virtual base::string16 GetBubbleViewCancelButtonLabel() OVERRIDE; | |
94 virtual void OnBubbleViewDidClose(Browser* browser) OVERRIDE; | |
95 virtual void BubbleViewAcceptButtonPressed(Browser* browser) OVERRIDE; | |
96 virtual void BubbleViewCancelButtonPressed(Browser* browser) OVERRIDE; | |
97 | |
98 // The owning ExternalInstallError. | |
99 ExternalInstallError* error_; | |
100 | |
101 // The Prompt with all information, which we then use to populate the bubble. | |
102 ExtensionInstallPrompt::Prompt* prompt_; | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(ExternalInstallBubbleAlert); | |
105 }; | |
106 | |
107 //////////////////////////////////////////////////////////////////////////////// | |
108 // ExternalInstallMenuAlert | |
109 | |
110 ExternalInstallMenuAlert::ExternalInstallMenuAlert(ExternalInstallError* error) | |
111 : error_(error) { | |
112 } | |
113 | |
114 ExternalInstallMenuAlert::~ExternalInstallMenuAlert() { | |
115 } | |
116 | |
117 GlobalError::Severity ExternalInstallMenuAlert::GetSeverity() { | |
118 return SEVERITY_LOW; | |
119 } | |
120 | |
121 bool ExternalInstallMenuAlert::HasMenuItem() { | |
122 return true; | |
123 } | |
124 | |
125 int ExternalInstallMenuAlert::MenuItemCommandID() { | |
126 return IDC_EXTERNAL_EXTENSION_ALERT; | |
127 } | |
128 | |
129 base::string16 ExternalInstallMenuAlert::MenuItemLabel() { | |
130 return GetMenuItemLabel(error_->GetExtension()); | |
131 } | |
132 | |
133 void ExternalInstallMenuAlert::ExecuteMenuItem(Browser* browser) { | |
134 error_->ShowDialog(); | |
135 } | |
136 | |
137 bool ExternalInstallMenuAlert::HasBubbleView() { | |
138 return false; | |
139 } | |
140 | |
141 bool ExternalInstallMenuAlert::HasShownBubbleView() { | |
142 NOTREACHED(); | |
143 return true; | |
144 } | |
145 | |
146 void ExternalInstallMenuAlert::ShowBubbleView(Browser* browser) { | |
147 NOTREACHED(); | |
148 } | |
149 | |
150 GlobalErrorBubbleViewBase* ExternalInstallMenuAlert::GetBubbleView() { | |
151 return NULL; | |
152 } | |
153 | |
154 //////////////////////////////////////////////////////////////////////////////// | |
155 // ExternalInstallBubbleAlert | |
156 | |
157 ExternalInstallBubbleAlert::ExternalInstallBubbleAlert( | |
158 ExternalInstallError* error, | |
159 ExtensionInstallPrompt::Prompt* prompt) | |
160 : error_(error), | |
161 prompt_(prompt) { | |
162 DCHECK(error_); | |
163 DCHECK(prompt_); | |
164 } | |
165 | |
166 ExternalInstallBubbleAlert::~ExternalInstallBubbleAlert() { | |
167 } | |
168 | |
169 GlobalError::Severity ExternalInstallBubbleAlert::GetSeverity() { | |
170 return SEVERITY_LOW; | |
171 } | |
172 | |
173 bool ExternalInstallBubbleAlert::HasMenuItem() { | |
174 return true; | |
175 } | |
176 | |
177 int ExternalInstallBubbleAlert::MenuItemCommandID() { | |
178 return IDC_EXTERNAL_EXTENSION_ALERT; | |
179 } | |
180 | |
181 base::string16 ExternalInstallBubbleAlert::MenuItemLabel() { | |
182 return GetMenuItemLabel(error_->GetExtension()); | |
183 } | |
184 | |
185 void ExternalInstallBubbleAlert::ExecuteMenuItem(Browser* browser) { | |
186 ShowBubbleView(browser); | |
187 } | |
188 | |
189 gfx::Image ExternalInstallBubbleAlert::GetBubbleViewIcon() { | |
190 if (prompt_->icon().IsEmpty()) | |
191 return GlobalErrorWithStandardBubble::GetBubbleViewIcon(); | |
192 // Scale icon to a reasonable size. | |
193 return gfx::Image(gfx::ImageSkiaOperations::CreateResizedImage( | |
194 *prompt_->icon().ToImageSkia(), | |
195 skia::ImageOperations::RESIZE_BEST, | |
196 gfx::Size(extension_misc::EXTENSION_ICON_SMALL, | |
197 extension_misc::EXTENSION_ICON_SMALL))); | |
198 } | |
199 | |
200 base::string16 ExternalInstallBubbleAlert::GetBubbleViewTitle() { | |
201 return prompt_->GetDialogTitle(); | |
202 } | |
203 | |
204 std::vector<base::string16> | |
205 ExternalInstallBubbleAlert::GetBubbleViewMessages() { | |
206 std::vector<base::string16> messages; | |
207 messages.push_back(prompt_->GetHeading()); | |
208 if (prompt_->GetPermissionCount()) { | |
209 messages.push_back(prompt_->GetPermissionsHeading()); | |
210 for (size_t i = 0; i < prompt_->GetPermissionCount(); ++i) { | |
211 messages.push_back(l10n_util::GetStringFUTF16( | |
212 IDS_EXTENSION_PERMISSION_LINE, prompt_->GetPermission(i))); | |
213 } | |
214 } | |
215 // TODO(yoz): OAuth issue advice? | |
216 return messages; | |
217 } | |
218 | |
219 base::string16 ExternalInstallBubbleAlert::GetBubbleViewAcceptButtonLabel() { | |
220 return prompt_->GetAcceptButtonLabel(); | |
221 } | |
222 | |
223 base::string16 ExternalInstallBubbleAlert::GetBubbleViewCancelButtonLabel() { | |
224 return prompt_->GetAbortButtonLabel(); | |
225 } | |
226 | |
227 void ExternalInstallBubbleAlert::OnBubbleViewDidClose(Browser* browser) { | |
228 } | |
229 | |
230 void ExternalInstallBubbleAlert::BubbleViewAcceptButtonPressed( | |
231 Browser* browser) { | |
232 error_->InstallUIProceed(); | |
233 } | |
234 | |
235 void ExternalInstallBubbleAlert::BubbleViewCancelButtonPressed( | |
236 Browser* browser) { | |
237 error_->InstallUIAbort(true); | |
238 } | |
239 | |
240 } // namespace | |
241 | |
242 //////////////////////////////////////////////////////////////////////////////// | |
243 // ExternalInstallError | |
244 | |
245 ExternalInstallError::ExternalInstallError( | |
246 content::BrowserContext* browser_context, | |
247 const std::string& extension_id, | |
248 AlertType alert_type, | |
249 ExternalInstallManager* manager) | |
250 : browser_context_(browser_context), | |
251 extension_id_(extension_id), | |
252 alert_type_(alert_type), | |
253 manager_(manager), | |
254 error_service_(GlobalErrorServiceFactory::GetForProfile( | |
255 Profile::FromBrowserContext(browser_context_))), | |
256 browser_(NULL), | |
257 weak_factory_(this) { | |
258 #if !defined(OS_ANDROID) | |
259 browser_ = | |
260 chrome::FindTabbedBrowser(Profile::FromBrowserContext(browser_context_), | |
261 true, | |
262 chrome::GetActiveDesktop()); | |
263 #endif | |
264 | |
265 prompt_.reset(new ExtensionInstallPrompt::Prompt( | |
266 ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT)); | |
267 | |
268 webstore_data_fetcher_.reset(new WebstoreDataFetcher( | |
269 this, browser_context_->GetRequestContext(), GURL(), extension_id_)); | |
270 webstore_data_fetcher_->Start(); | |
271 } | |
272 | |
273 ExternalInstallError::~ExternalInstallError() { | |
274 if (global_error_.get()) | |
275 error_service_->RemoveGlobalError(global_error_.get()); | |
276 } | |
277 | |
278 void ExternalInstallError::InstallUIProceed() { | |
279 const Extension* extension = GetExtension(); | |
280 if (extension) { | |
281 ExtensionSystem::Get(browser_context_) | |
282 ->extension_service() | |
283 ->GrantPermissionsAndEnableExtension(extension); | |
284 // Since the manager listens for the extension to be loaded, this will | |
285 // remove the error... | |
286 } else { | |
287 // ... Otherwise we have to do it explicitly. | |
288 manager_->RemoveExternalInstallError(); | |
289 } | |
290 } | |
291 | |
292 void ExternalInstallError::InstallUIAbort(bool user_initiated) { | |
293 if (user_initiated && GetExtension()) { | |
294 ExtensionSystem::Get(browser_context_) | |
295 ->extension_service() | |
296 ->UninstallExtension(extension_id_, | |
297 false, // Not externally uninstalled. | |
298 NULL); // Ignore error. | |
299 // Since the manager listens for the extension to be removed, this will | |
300 // remove the error... | |
301 } else { | |
302 // ... Otherwise we have to do it explicitly. | |
303 manager_->RemoveExternalInstallError(); | |
304 } | |
305 } | |
306 | |
307 void ExternalInstallError::AcknowledgeExtension() { | |
308 const Extension* extension = GetExtension(); | |
309 if (extension) { | |
310 ExtensionSystem::Get(browser_context_) | |
311 ->extension_service() | |
312 ->AcknowledgeExternalExtension(extension->id()); | |
313 } | |
314 } | |
315 | |
316 void ExternalInstallError::ShowDialog() { | |
317 DCHECK(install_ui_.get()); | |
318 DCHECK(prompt_.get()); | |
319 DCHECK(show_params_.get()); | |
320 ExtensionInstallPrompt::GetDefaultShowDialogCallback().Run( | |
321 *show_params_, this, *prompt_); | |
322 } | |
323 | |
324 const Extension* ExternalInstallError::GetExtension() const { | |
325 return ExtensionRegistry::Get(browser_context_) | |
326 ->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING); | |
327 } | |
328 | |
329 void ExternalInstallError::OnWebstoreRequestFailure() { | |
330 OnFetchComplete(); | |
331 } | |
332 | |
333 void ExternalInstallError::OnWebstoreResponseParseSuccess( | |
334 scoped_ptr<base::DictionaryValue> webstore_data) { | |
335 std::string localized_user_count; | |
336 double average_rating; | |
337 int rating_count; | |
338 if (!webstore_data->GetString(kUsersKey, &localized_user_count) || | |
339 !webstore_data->GetDouble(kAverageRatingKey, &average_rating) || | |
340 !webstore_data->GetInteger(kRatingCountKey, &rating_count)) { | |
341 // If we don't get a valid webstore response, short circuit, and continue | |
342 // to show a prompt without webstore data. | |
343 OnFetchComplete(); | |
344 return; | |
345 } | |
346 | |
347 bool show_user_count = true; | |
348 webstore_data->GetBoolean(kShowUserCountKey, &show_user_count); | |
349 | |
350 prompt_->SetWebstoreData( | |
351 localized_user_count, show_user_count, average_rating, rating_count); | |
352 OnFetchComplete(); | |
353 } | |
354 | |
355 void ExternalInstallError::OnWebstoreResponseParseFailure( | |
356 const std::string& error) { | |
357 OnFetchComplete(); | |
358 } | |
359 | |
360 void ExternalInstallError::OnFetchComplete() { | |
361 install_ui_.reset( | |
362 ExtensionInstallUI::CreateInstallPromptWithBrowser(browser_)); | |
363 | |
364 install_ui_->ConfirmExternalInstall( | |
365 this, | |
366 GetExtension(), | |
367 base::Bind(&ExternalInstallError::OnDialogReady, | |
368 weak_factory_.GetWeakPtr()), | |
369 *prompt_); | |
370 } | |
371 | |
372 void ExternalInstallError::OnDialogReady( | |
373 const ExtensionInstallPrompt::ShowParams& show_params, | |
374 ExtensionInstallPrompt::Delegate* prompt_delegate, | |
375 const ExtensionInstallPrompt::Prompt& prompt) { | |
376 DCHECK_EQ(this, prompt_delegate); | |
377 show_params_.reset(new ExtensionInstallPrompt::ShowParams(show_params)); | |
378 prompt_.reset(new ExtensionInstallPrompt::Prompt(prompt)); | |
379 | |
380 if (alert_type_ == BUBBLE_ALERT) { | |
381 global_error_.reset(new ExternalInstallBubbleAlert(this, prompt_.get())); | |
382 error_service_->AddGlobalError(global_error_.get()); | |
383 if (browser_) | |
384 global_error_->ShowBubbleView(browser_); | |
385 } else { | |
386 DCHECK(alert_type_ == MENU_ALERT); | |
387 global_error_.reset(new ExternalInstallMenuAlert(this)); | |
388 error_service_->AddGlobalError(global_error_.get()); | |
389 } | |
390 } | |
391 | |
392 } // namespace extensions | |
OLD | NEW |