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 using extensions::PageCapturePermissionHelper; | |
|
Devlin
2017/01/06 20:45:49
instead, just put this file in the extensions:: na
Ivan Šandrk
2017/01/09 13:14:55
Done.
| |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const char kNoAvailableBrowser[] = | |
| 26 "No available browser window to show the prompt."; | |
| 27 const char kUserDenied[] = "User denied request."; | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 PageCapturePermissionHelper::PageCapturePermissionHelper( | |
| 32 const extensions::Extension* extension, | |
| 33 PrefService* prefs, | |
| 34 base::Callback<void()> success_callback, | |
| 35 base::Callback<void(const std::string&)> failure_callback) | |
| 36 : extension_(extension), prefs_(prefs), success_callback_(success_callback), | |
|
Devlin
2017/01/06 20:45:49
nit: indentation off
Ivan Šandrk
2017/01/09 13:14:56
Done.
| |
| 37 failure_callback_(failure_callback) {} | |
| 38 | |
| 39 PageCapturePermissionHelper::~PageCapturePermissionHelper() {} | |
| 40 | |
| 41 void PageCapturePermissionHelper::HandlePermissionRequest( | |
| 42 content::WebContents* web_contents) { | |
| 43 auto user_choice = UserChoiceGet(); | |
| 44 if (user_choice == PermissionState::ALLOWED || | |
|
Devlin
2017/01/06 20:45:49
I'd prefer we use a switch here
Ivan Šandrk
2017/01/09 13:14:56
Done.
| |
| 45 user_choice == PermissionState::DENIED) { | |
| 46 ResolvePermissionRequest(); | |
| 47 return; | |
| 48 } | |
| 49 if (user_choice == PermissionState::NOT_PROMPTED) { | |
| 50 ShowPermissionPrompt(web_contents); | |
| 51 } | |
| 52 // It's possible that several PageCapture requests come before user resolves | |
| 53 // the permission dialog, so setup a callback that gets called when pref | |
| 54 // changes (dialog is resolved). | |
| 55 pref_change_registrar_.reset(new PrefChangeRegistrar); | |
|
Devlin
2017/01/06 20:45:49
prefer base::MakeUnique
Ivan Šandrk
2017/01/09 13:14:56
Done.
| |
| 56 pref_change_registrar_->Init(prefs_); | |
| 57 // base::Unretained is safe here because PrefChangeRegistrar will unregister | |
| 58 // callback on destruction of this object. | |
| 59 pref_change_registrar_->Add( | |
| 60 prefs::kPublicSessionPermissionsUserChoiceCache, | |
| 61 base::Bind(&PageCapturePermissionHelper::ResolvePermissionRequest, | |
| 62 base::Unretained(this))); | |
| 63 } | |
| 64 | |
| 65 void PageCapturePermissionHelper::ShowPermissionPrompt( | |
| 66 content::WebContents* web_contents) { | |
| 67 extensions::APIPermissionSet new_apis; | |
| 68 new_apis.insert(extensions::APIPermission::kPageCapture); | |
| 69 auto permission_set = base::MakeUnique<extensions::PermissionSet>( | |
| 70 new_apis, extensions::ManifestPermissionSet(), | |
| 71 extensions::URLPatternSet(), extensions::URLPatternSet()); | |
| 72 | |
| 73 if (!web_contents) { | |
|
Devlin
2017/01/06 20:45:49
Can we put this first (before creating the permiss
Ivan Šandrk
2017/01/09 13:14:56
Done.
| |
| 74 failure_callback_.Run(kNoAvailableBrowser); | |
| 75 return; | |
| 76 } | |
| 77 prompt_ = base::MakeUnique<ExtensionInstallPrompt>(web_contents); | |
| 78 prompt_->ShowDialog( | |
| 79 base::Bind(&PageCapturePermissionHelper::ResolvePermissionPrompt, | |
| 80 base::Unretained(this)), | |
| 81 extension_, | |
| 82 nullptr, // Uses the extension icon. | |
| 83 base::MakeUnique<ExtensionInstallPrompt::Prompt>( | |
| 84 ExtensionInstallPrompt::PERMISSIONS_PROMPT), | |
| 85 std::move(permission_set), | |
| 86 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); | |
| 87 | |
| 88 UserChoiceSet(PermissionState::SHOWN_PROMPT); | |
| 89 } | |
| 90 | |
| 91 void PageCapturePermissionHelper::ResolvePermissionPrompt( | |
| 92 ExtensionInstallPrompt::Result prompt_result) { | |
| 93 // Dispose of the prompt as it's not needed anymore. | |
| 94 prompt_.reset(); | |
| 95 | |
| 96 bool allowed = prompt_result == ExtensionInstallPrompt::Result::ACCEPTED; | |
| 97 UserChoiceSet(allowed ? PermissionState::ALLOWED : PermissionState::DENIED); | |
| 98 } | |
| 99 | |
| 100 void PageCapturePermissionHelper::ResolvePermissionRequest() { | |
| 101 auto user_choice = UserChoiceGet(); | |
| 102 // Permission was allowed/denied for some other extension, continue sleeping. | |
|
Devlin
2017/01/06 20:45:49
nit: s/sleeping/waiting. sleep often has a specif
Ivan Šandrk
2017/01/09 13:14:55
Done.
| |
| 103 if (user_choice == PermissionState::SHOWN_PROMPT) { | |
|
Devlin
2017/01/06 20:45:49
preferably a switch here too
Ivan Šandrk
2017/01/09 13:14:56
Done.
| |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 if (user_choice == PermissionState::DENIED) { | |
| 108 failure_callback_.Run(kUserDenied); | |
| 109 } else { | |
| 110 DCHECK(user_choice == PermissionState::ALLOWED); | |
| 111 success_callback_.Run(); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void PageCapturePermissionHelper::UserChoiceSet(PermissionState value) { | |
| 116 DictionaryPrefUpdate update(prefs_, | |
| 117 prefs::kPublicSessionPermissionsUserChoiceCache); | |
| 118 update->SetInteger(extension_->id(), value); | |
| 119 } | |
| 120 | |
| 121 PageCapturePermissionHelper::PermissionState | |
| 122 PageCapturePermissionHelper::UserChoiceGet() { | |
| 123 auto dictionary = prefs_->GetDictionary( | |
|
Devlin
2017/01/06 20:45:49
here, let's not use auto. It's unclear what the r
Ivan Šandrk
2017/01/09 13:14:55
Done.
Is this because of the compiler or because
Devlin
2017/01/09 18:05:07
TL;DR: This is just for the reader's benefit. auto
| |
| 124 prefs::kPublicSessionPermissionsUserChoiceCache); | |
| 125 int value = PermissionState::NOT_PROMPTED; | |
| 126 // In case no value was set previously, we return the default value | |
| 127 // (PermissionState::NOT_PROMPTED). | |
| 128 dictionary->GetInteger(extension_->id(), &value); | |
| 129 return static_cast<PermissionState>(value); | |
| 130 } | |
| OLD | NEW |