Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / extensions / api / developer_private / show_permissions_dialog_helper.cc
blob65d80897c0313c0b26a124fe89bd91f3a5cfa530
1 // Copyright 2015 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.
5 #include "chrome/browser/extensions/api/developer_private/show_permissions_dialog_helper.h"
7 #include "apps/app_load_service.h"
8 #include "apps/saved_files_service.h"
9 #include "base/metrics/histogram.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/apps/app_info_dialog.h"
12 #include "content/public/browser/web_contents.h"
13 #include "extensions/browser/api/device_permissions_manager.h"
14 #include "extensions/browser/extension_registry.h"
15 #include "extensions/common/extension.h"
16 #include "extensions/common/permissions/permissions_data.h"
18 namespace extensions {
20 ShowPermissionsDialogHelper::ShowPermissionsDialogHelper(
21 Profile* profile,
22 const base::Closure& on_complete)
23 : profile_(profile),
24 on_complete_(on_complete) {
27 ShowPermissionsDialogHelper::~ShowPermissionsDialogHelper() {
30 // static
31 void ShowPermissionsDialogHelper::Show(content::BrowserContext* browser_context,
32 content::WebContents* web_contents,
33 const Extension* extension,
34 bool from_webui,
35 const base::Closure& on_complete) {
36 Profile* profile = Profile::FromBrowserContext(browser_context);
38 // Show the new-style extensions dialog when it is available. It is currently
39 // unavailable by default on Mac.
40 if (CanShowAppInfoDialog()) {
41 if (from_webui) {
42 UMA_HISTOGRAM_ENUMERATION("Apps.AppInfoDialog.Launches",
43 AppInfoLaunchSource::FROM_EXTENSIONS_PAGE,
44 AppInfoLaunchSource::NUM_LAUNCH_SOURCES);
47 ShowAppInfoInNativeDialog(
48 web_contents->GetTopLevelNativeWindow(),
49 GetAppInfoNativeDialogSize(),
50 profile,
51 extension,
52 on_complete);
54 return; // All done.
57 // ShowPermissionsDialogHelper manages its own lifetime.
58 ShowPermissionsDialogHelper* helper =
59 new ShowPermissionsDialogHelper(profile, on_complete);
60 helper->ShowPermissionsDialog(web_contents, extension);
63 void ShowPermissionsDialogHelper::ShowPermissionsDialog(
64 content::WebContents* web_contents,
65 const Extension* extension) {
66 extension_id_ = extension->id();
67 prompt_.reset(new ExtensionInstallPrompt(web_contents));
68 std::vector<base::FilePath> retained_file_paths;
69 if (extension->permissions_data()->HasAPIPermission(
70 APIPermission::kFileSystem)) {
71 std::vector<apps::SavedFileEntry> retained_file_entries =
72 apps::SavedFilesService::Get(profile_)
73 ->GetAllFileEntries(extension_id_);
74 for (const apps::SavedFileEntry& entry : retained_file_entries)
75 retained_file_paths.push_back(entry.path);
77 std::vector<base::string16> retained_device_messages;
78 if (extension->permissions_data()->HasAPIPermission(APIPermission::kUsb)) {
79 retained_device_messages =
80 DevicePermissionsManager::Get(profile_)
81 ->GetPermissionMessageStrings(extension_id_);
83 prompt_->ReviewPermissions(
84 this, extension, retained_file_paths, retained_device_messages);
87 // This is called when the user clicks "Revoke File Access."
88 void ShowPermissionsDialogHelper::InstallUIProceed() {
89 const Extension* extension =
90 ExtensionRegistry::Get(profile_)->GetExtensionById(
91 extension_id_, ExtensionRegistry::EVERYTHING);
93 if (extension)
94 apps::SavedFilesService::Get(profile_)->ClearQueue(extension);
95 apps::AppLoadService::Get(profile_)
96 ->RestartApplicationIfRunning(extension_id_);
98 on_complete_.Run();
100 delete this;
103 void ShowPermissionsDialogHelper::InstallUIAbort(bool user_initiated) {
104 on_complete_.Run();
106 delete this;
109 } // namespace extensions