Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 default: | |
| 55 NOTREACHED(); | |
| 56 } | |
| 57 // It's possible that several PageCapture requests come before user resolves | |
| 58 // the permission dialog, so setup a callback that gets called when pref | |
| 59 // changes (dialog is resolved). | |
| 60 pref_change_registrar_ = base::MakeUnique<PrefChangeRegistrar>(); | |
| 61 pref_change_registrar_->Init(prefs_); | |
| 62 // base::Unretained is safe here because PrefChangeRegistrar will unregister | |
| 63 // callback on destruction of this object. | |
| 64 pref_change_registrar_->Add( | |
| 65 prefs::kPublicSessionPermissionsUserChoiceCache, | |
| 66 base::Bind(&PageCapturePermissionHelper::ResolvePermissionRequest, | |
| 67 base::Unretained(this))); | |
| 68 } | |
| 69 | |
| 70 void PageCapturePermissionHelper::ShowPermissionPrompt( | |
| 71 content::WebContents* web_contents) { | |
| 72 if (!web_contents) { | |
| 73 failure_callback_.Run(kNoAvailableBrowser); | |
| 74 return; | |
| 75 } | |
| 76 APIPermissionSet new_apis; | |
| 77 new_apis.insert(APIPermission::kPageCapture); | |
| 78 auto permission_set = base::MakeUnique<PermissionSet>( | |
| 79 new_apis, ManifestPermissionSet(), URLPatternSet(), URLPatternSet()); | |
| 80 | |
| 81 prompt_ = base::MakeUnique<ExtensionInstallPrompt>(web_contents); | |
| 82 prompt_->ShowDialog( | |
| 83 base::Bind(&PageCapturePermissionHelper::ResolvePermissionPrompt, | |
| 84 base::Unretained(this)), | |
| 85 extension_, | |
| 86 nullptr, // Uses the extension icon. | |
| 87 base::MakeUnique<ExtensionInstallPrompt::Prompt>( | |
| 88 ExtensionInstallPrompt::PERMISSIONS_PROMPT), | |
| 89 std::move(permission_set), | |
| 90 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); | |
| 91 | |
| 92 SetUserChoice(PermissionState::SHOWN_PROMPT); | |
| 93 } | |
| 94 | |
| 95 void PageCapturePermissionHelper::ResolvePermissionPrompt( | |
| 96 ExtensionInstallPrompt::Result prompt_result) { | |
| 97 // Dispose of the prompt as it's not needed anymore. | |
| 98 prompt_.reset(); | |
| 99 | |
| 100 bool allowed = prompt_result == ExtensionInstallPrompt::Result::ACCEPTED; | |
| 101 SetUserChoice(allowed ? PermissionState::ALLOWED : PermissionState::DENIED); | |
| 102 } | |
| 103 | |
| 104 void PageCapturePermissionHelper::ResolvePermissionRequest() { | |
| 105 switch (GetUserChoice()) { | |
| 106 case PermissionState::SHOWN_PROMPT: | |
| 107 // Permission was resolved for some other extension, continue waiting. | |
| 108 return; | |
| 109 case PermissionState::DENIED: | |
| 110 failure_callback_.Run(kUserDenied); | |
| 111 break; | |
| 112 case PermissionState::ALLOWED: | |
| 113 success_callback_.Run(); | |
| 114 break; | |
| 115 case PermissionState::NOT_PROMPTED: | |
| 116 default: | |
|
Devlin
2017/01/09 18:05:07
We shouldn't need this default, right?
Ivan Šandrk
2017/01/09 18:33:51
Right, forgot that the compiler throws an error if
| |
| 117 NOTREACHED(); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void PageCapturePermissionHelper::SetUserChoice(PermissionState value) { | |
| 122 DictionaryPrefUpdate update(prefs_, | |
| 123 prefs::kPublicSessionPermissionsUserChoiceCache); | |
| 124 update->SetInteger(extension_->id(), value); | |
| 125 } | |
| 126 | |
| 127 PageCapturePermissionHelper::PermissionState | |
| 128 PageCapturePermissionHelper::GetUserChoice() { | |
| 129 const base::DictionaryValue* dictionary = | |
| 130 prefs_->GetDictionary(prefs::kPublicSessionPermissionsUserChoiceCache); | |
| 131 int value = PermissionState::NOT_PROMPTED; | |
| 132 // In case no value was set previously, we return the default value | |
| 133 // (PermissionState::NOT_PROMPTED). | |
| 134 dictionary->GetInteger(extension_->id(), &value); | |
| 135 return static_cast<PermissionState>(value); | |
| 136 } | |
| 137 | |
| 138 } // namespace extensions | |
| OLD | NEW |