| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/api/page_capture/page_capture_permission_hel
per.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "chrome/common/pref_names.h" |
| 11 #include "chromeos/login/login_state.h" |
| 12 #include "components/prefs/pref_change_registrar.h" |
| 13 #include "components/prefs/scoped_user_pref_update.h" |
| 14 #include "extensions/common/extension.h" |
| 15 #include "extensions/common/permissions/api_permission.h" |
| 16 #include "extensions/common/permissions/api_permission_set.h" |
| 17 #include "extensions/common/permissions/manifest_permission_set.h" |
| 18 #include "extensions/common/permissions/permission_set.h" |
| 19 #include "extensions/common/url_pattern_set.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 const char kNoAvailableBrowser[] = |
| 24 "No available browser window to show the prompt."; |
| 25 const char kUserDenied[] = "User denied request."; |
| 26 |
| 27 } // namespace |
| 28 |
| 29 namespace extensions { |
| 30 |
| 31 PageCapturePermissionHelper::PageCapturePermissionHelper( |
| 32 const Extension* extension, |
| 33 PrefService* prefs, |
| 34 const base::Closure& success_callback, |
| 35 const base::Callback<void(const std::string&)>& failure_callback) |
| 36 : extension_(extension), |
| 37 prefs_(prefs), |
| 38 success_callback_(success_callback), |
| 39 failure_callback_(failure_callback) {} |
| 40 |
| 41 PageCapturePermissionHelper::~PageCapturePermissionHelper() {} |
| 42 |
| 43 void PageCapturePermissionHelper::HandlePermissionRequest( |
| 44 content::WebContents* web_contents) { |
| 45 switch (GetUserChoice()) { |
| 46 case PermissionState::ALLOWED: |
| 47 case PermissionState::DENIED: |
| 48 ResolvePermissionRequest(); |
| 49 return; |
| 50 case PermissionState::NOT_PROMPTED: |
| 51 ShowPermissionPrompt(web_contents); |
| 52 case PermissionState::SHOWN_PROMPT: |
| 53 break; |
| 54 } |
| 55 // It's possible that several PageCapture requests come before user resolves |
| 56 // the permission dialog, so setup a callback that gets called when pref |
| 57 // changes (dialog is resolved). |
| 58 pref_change_registrar_ = base::MakeUnique<PrefChangeRegistrar>(); |
| 59 pref_change_registrar_->Init(prefs_); |
| 60 // base::Unretained is safe here because PrefChangeRegistrar will unregister |
| 61 // callback on destruction of this object. |
| 62 pref_change_registrar_->Add( |
| 63 prefs::kExtensionsPublicSessionPermissions, |
| 64 base::Bind(&PageCapturePermissionHelper::ResolvePermissionRequest, |
| 65 base::Unretained(this))); |
| 66 } |
| 67 |
| 68 void PageCapturePermissionHelper::ShowPermissionPrompt( |
| 69 content::WebContents* web_contents) { |
| 70 if (!web_contents) { |
| 71 failure_callback_.Run(kNoAvailableBrowser); |
| 72 return; |
| 73 } |
| 74 APIPermissionSet new_apis; |
| 75 new_apis.insert(APIPermission::kPageCapture); |
| 76 auto permission_set = base::MakeUnique<PermissionSet>( |
| 77 new_apis, ManifestPermissionSet(), URLPatternSet(), URLPatternSet()); |
| 78 |
| 79 prompt_ = base::MakeUnique<ExtensionInstallPrompt>(web_contents); |
| 80 prompt_->ShowDialog( |
| 81 base::Bind(&PageCapturePermissionHelper::ResolvePermissionPrompt, |
| 82 base::Unretained(this)), |
| 83 extension_, |
| 84 nullptr, // Uses the extension icon. |
| 85 base::MakeUnique<ExtensionInstallPrompt::Prompt>( |
| 86 ExtensionInstallPrompt::PERMISSIONS_PROMPT), |
| 87 std::move(permission_set), |
| 88 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); |
| 89 |
| 90 SetUserChoice(PermissionState::SHOWN_PROMPT); |
| 91 } |
| 92 |
| 93 void PageCapturePermissionHelper::ResolvePermissionPrompt( |
| 94 ExtensionInstallPrompt::Result prompt_result) { |
| 95 // Dispose of the prompt as it's not needed anymore. |
| 96 prompt_.reset(); |
| 97 |
| 98 bool allowed = prompt_result == ExtensionInstallPrompt::Result::ACCEPTED; |
| 99 SetUserChoice(allowed ? PermissionState::ALLOWED : PermissionState::DENIED); |
| 100 } |
| 101 |
| 102 void PageCapturePermissionHelper::ResolvePermissionRequest() { |
| 103 switch (GetUserChoice()) { |
| 104 case PermissionState::SHOWN_PROMPT: |
| 105 // Permission was resolved for some other extension, continue waiting. |
| 106 return; |
| 107 case PermissionState::DENIED: |
| 108 failure_callback_.Run(kUserDenied); |
| 109 break; |
| 110 case PermissionState::ALLOWED: |
| 111 success_callback_.Run(); |
| 112 break; |
| 113 case PermissionState::NOT_PROMPTED: |
| 114 NOTREACHED(); |
| 115 } |
| 116 } |
| 117 |
| 118 void PageCapturePermissionHelper::SetUserChoice(PermissionState value) { |
| 119 DictionaryPrefUpdate update(prefs_, |
| 120 prefs::kExtensionsPublicSessionPermissions); |
| 121 update->SetInteger(extension_->id(), value); |
| 122 } |
| 123 |
| 124 PageCapturePermissionHelper::PermissionState |
| 125 PageCapturePermissionHelper::GetUserChoice() { |
| 126 const base::DictionaryValue* dictionary = |
| 127 prefs_->GetDictionary(prefs::kExtensionsPublicSessionPermissions); |
| 128 int value = PermissionState::NOT_PROMPTED; |
| 129 // In case no value was set previously, we return the default value |
| 130 // (PermissionState::NOT_PROMPTED). |
| 131 dictionary->GetInteger(extension_->id(), &value); |
| 132 return static_cast<PermissionState>(value); |
| 133 } |
| 134 |
| 135 } // namespace extensions |
| OLD | NEW |