Chromium Code Reviews| Index: chrome/browser/extensions/api/page_capture/page_capture_permission_helper.cc |
| diff --git a/chrome/browser/extensions/api/page_capture/page_capture_permission_helper.cc b/chrome/browser/extensions/api/page_capture/page_capture_permission_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bad60e0e9bf1cf845eb480165ea6e69711459534 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/page_capture/page_capture_permission_helper.cc |
| @@ -0,0 +1,138 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/api/page_capture/page_capture_permission_helper.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chromeos/login/login_state.h" |
| +#include "components/prefs/pref_change_registrar.h" |
| +#include "components/prefs/scoped_user_pref_update.h" |
| +#include "extensions/common/extension.h" |
| +#include "extensions/common/permissions/api_permission.h" |
| +#include "extensions/common/permissions/api_permission_set.h" |
| +#include "extensions/common/permissions/manifest_permission_set.h" |
| +#include "extensions/common/permissions/permission_set.h" |
| +#include "extensions/common/url_pattern_set.h" |
| + |
| +namespace { |
| + |
| +const char kNoAvailableBrowser[] = |
| + "No available browser window to show the prompt."; |
| +const char kUserDenied[] = "User denied request."; |
| + |
| +} // namespace |
| + |
| +namespace extensions { |
| + |
| +PageCapturePermissionHelper::PageCapturePermissionHelper( |
| + const Extension* extension, |
| + PrefService* prefs, |
| + const base::Closure& success_callback, |
| + const base::Callback<void(const std::string&)>& failure_callback) |
| + : extension_(extension), |
| + prefs_(prefs), |
| + success_callback_(success_callback), |
| + failure_callback_(failure_callback) {} |
| + |
| +PageCapturePermissionHelper::~PageCapturePermissionHelper() {} |
| + |
| +void PageCapturePermissionHelper::HandlePermissionRequest( |
| + content::WebContents* web_contents) { |
| + switch (GetUserChoice()) { |
| + case PermissionState::ALLOWED: |
| + case PermissionState::DENIED: |
| + ResolvePermissionRequest(); |
| + return; |
| + case PermissionState::NOT_PROMPTED: |
| + ShowPermissionPrompt(web_contents); |
| + case PermissionState::SHOWN_PROMPT: |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + // It's possible that several PageCapture requests come before user resolves |
| + // the permission dialog, so setup a callback that gets called when pref |
| + // changes (dialog is resolved). |
| + pref_change_registrar_ = base::MakeUnique<PrefChangeRegistrar>(); |
| + pref_change_registrar_->Init(prefs_); |
| + // base::Unretained is safe here because PrefChangeRegistrar will unregister |
| + // callback on destruction of this object. |
| + pref_change_registrar_->Add( |
| + prefs::kPublicSessionPermissionsUserChoiceCache, |
| + base::Bind(&PageCapturePermissionHelper::ResolvePermissionRequest, |
| + base::Unretained(this))); |
| +} |
| + |
| +void PageCapturePermissionHelper::ShowPermissionPrompt( |
| + content::WebContents* web_contents) { |
| + if (!web_contents) { |
| + failure_callback_.Run(kNoAvailableBrowser); |
| + return; |
| + } |
| + APIPermissionSet new_apis; |
| + new_apis.insert(APIPermission::kPageCapture); |
| + auto permission_set = base::MakeUnique<PermissionSet>( |
| + new_apis, ManifestPermissionSet(), URLPatternSet(), URLPatternSet()); |
| + |
| + prompt_ = base::MakeUnique<ExtensionInstallPrompt>(web_contents); |
| + prompt_->ShowDialog( |
| + base::Bind(&PageCapturePermissionHelper::ResolvePermissionPrompt, |
| + base::Unretained(this)), |
| + extension_, |
| + nullptr, // Uses the extension icon. |
| + base::MakeUnique<ExtensionInstallPrompt::Prompt>( |
| + ExtensionInstallPrompt::PERMISSIONS_PROMPT), |
| + std::move(permission_set), |
| + ExtensionInstallPrompt::GetDefaultShowDialogCallback()); |
| + |
| + SetUserChoice(PermissionState::SHOWN_PROMPT); |
| +} |
| + |
| +void PageCapturePermissionHelper::ResolvePermissionPrompt( |
| + ExtensionInstallPrompt::Result prompt_result) { |
| + // Dispose of the prompt as it's not needed anymore. |
| + prompt_.reset(); |
| + |
| + bool allowed = prompt_result == ExtensionInstallPrompt::Result::ACCEPTED; |
| + SetUserChoice(allowed ? PermissionState::ALLOWED : PermissionState::DENIED); |
| +} |
| + |
| +void PageCapturePermissionHelper::ResolvePermissionRequest() { |
| + switch (GetUserChoice()) { |
| + case PermissionState::SHOWN_PROMPT: |
| + // Permission was resolved for some other extension, continue waiting. |
| + return; |
| + case PermissionState::DENIED: |
| + failure_callback_.Run(kUserDenied); |
| + break; |
| + case PermissionState::ALLOWED: |
| + success_callback_.Run(); |
| + break; |
| + case PermissionState::NOT_PROMPTED: |
| + 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
|
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +void PageCapturePermissionHelper::SetUserChoice(PermissionState value) { |
| + DictionaryPrefUpdate update(prefs_, |
| + prefs::kPublicSessionPermissionsUserChoiceCache); |
| + update->SetInteger(extension_->id(), value); |
| +} |
| + |
| +PageCapturePermissionHelper::PermissionState |
| +PageCapturePermissionHelper::GetUserChoice() { |
| + const base::DictionaryValue* dictionary = |
| + prefs_->GetDictionary(prefs::kPublicSessionPermissionsUserChoiceCache); |
| + int value = PermissionState::NOT_PROMPTED; |
| + // In case no value was set previously, we return the default value |
| + // (PermissionState::NOT_PROMPTED). |
| + dictionary->GetInteger(extension_->id(), &value); |
| + return static_cast<PermissionState>(value); |
| +} |
| + |
| +} // namespace extensions |