Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add permission lifetime backend. #8275

Merged
merged 6 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ source_set("browser_process") {
"//brave/components/ntp_background_images/browser",
"//brave/components/ntp_tiles",
"//brave/components/p3a",
"//brave/components/permissions",
"//brave/components/resources",
"//brave/components/sidebar/buildflags",
"//brave/components/speedreader:buildflags",
Expand Down
2 changes: 2 additions & 0 deletions browser/browser_context_keyed_service_factories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "brave/browser/brave_shields/ad_block_pref_service_factory.h"
#include "brave/browser/brave_shields/cookie_pref_service_factory.h"
#include "brave/browser/ntp_background_images/view_counter_service_factory.h"
#include "brave/browser/permissions/permission_lifetime_manager_factory.h"
#include "brave/browser/search_engines/search_engine_provider_service_factory.h"
#include "brave/browser/search_engines/search_engine_tracker.h"
#include "brave/components/brave_ads/browser/ads_service_factory.h"
Expand Down Expand Up @@ -69,6 +70,7 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
#if BUILDFLAG(IPFS_ENABLED)
ipfs::IpfsServiceFactory::GetInstance();
#endif
PermissionLifetimeManagerFactory::GetInstance();
}

} // namespace brave
44 changes: 44 additions & 0 deletions browser/permissions/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
if (!is_android) {
source_set("browser_tests") {
testonly = true
defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]

sources = [
"mock_permission_lifetime_prompt.cc",
"mock_permission_lifetime_prompt.h",
"mock_permission_lifetime_prompt_factory.cc",
"mock_permission_lifetime_prompt_factory.h",
"permission_lifetime_manager_browsertest.cc",
]

deps = [
"//brave/browser:browser_process",
"//brave/components/permissions",
"//chrome/browser",
"//chrome/browser/ui",
"//chrome/test:test_support",
"//chrome/test:test_support_ui",
"//components/permissions",
"//content/test:test_support",
]
}
}

source_set("unit_tests") {
testonly = true
defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]

sources = [ "permission_lifetime_manager_unittest.cc" ]

deps = [
"//brave/components/permissions",
"//chrome/browser",
"//chrome/test:test_support",
"//components/content_settings/core/browser",
"//components/permissions",
"//components/pref_registry:pref_registry",
"//components/sync_preferences:test_support",
"//content/test:test_support",
"//testing/gtest",
]
}
38 changes: 38 additions & 0 deletions browser/permissions/mock_permission_lifetime_prompt.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/permissions/mock_permission_lifetime_prompt.h"

#include "brave/browser/permissions/mock_permission_lifetime_prompt_factory.h"

namespace permissions {

MockPermissionLifetimePrompt::MockPermissionLifetimePrompt(
MockPermissionLifetimePromptFactory* factory,
Delegate* delegate)
: factory_(factory), delegate_(delegate) {}

MockPermissionLifetimePrompt::~MockPermissionLifetimePrompt() {
if (factory_)
factory_->HideView(this);
}

void MockPermissionLifetimePrompt::UpdateAnchorPosition() {}

PermissionPrompt::TabSwitchingBehavior
MockPermissionLifetimePrompt::GetTabSwitchingBehavior() {
return TabSwitchingBehavior::kDestroyPromptButKeepRequestPending;
}

PermissionPromptDisposition MockPermissionLifetimePrompt::GetPromptDisposition()
const {
return PermissionPromptDisposition::ANCHORED_BUBBLE;
}

void MockPermissionLifetimePrompt::ResetFactory() {
factory_ = nullptr;
}

} // namespace permissions
35 changes: 35 additions & 0 deletions browser/permissions/mock_permission_lifetime_prompt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_PERMISSIONS_MOCK_PERMISSION_LIFETIME_PROMPT_H_
#define BRAVE_BROWSER_PERMISSIONS_MOCK_PERMISSION_LIFETIME_PROMPT_H_

#include "components/permissions/permission_prompt.h"

namespace permissions {
class MockPermissionLifetimePromptFactory;

class MockPermissionLifetimePrompt : public PermissionPrompt {
public:
MockPermissionLifetimePrompt(MockPermissionLifetimePromptFactory* factory,
Delegate* delegate);
~MockPermissionLifetimePrompt() override;

// PermissionPrompt:
void UpdateAnchorPosition() override;
TabSwitchingBehavior GetTabSwitchingBehavior() override;
PermissionPromptDisposition GetPromptDisposition() const override;

Delegate* delegate() { return delegate_; }
void ResetFactory();

private:
MockPermissionLifetimePromptFactory* factory_;
Delegate* delegate_;
};

} // namespace permissions

#endif // BRAVE_BROWSER_PERMISSIONS_MOCK_PERMISSION_LIFETIME_PROMPT_H_
71 changes: 71 additions & 0 deletions browser/permissions/mock_permission_lifetime_prompt_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/permissions/mock_permission_lifetime_prompt_factory.h"

#include "base/bind.h"
#include "base/containers/contains.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/request_type.h"
#include "components/permissions/test/mock_permission_prompt.h"
#include "content/public/browser/web_contents.h"

namespace permissions {

MockPermissionLifetimePromptFactory::MockPermissionLifetimePromptFactory(
PermissionRequestManager* manager)
: show_count_(0),
response_type_(PermissionRequestManager::NONE),
manager_(manager) {
manager->set_view_factory_for_testing(base::BindRepeating(
&MockPermissionLifetimePromptFactory::Create, base::Unretained(this)));
}

MockPermissionLifetimePromptFactory::~MockPermissionLifetimePromptFactory() {
for (auto* prompt : prompts_)
prompt->ResetFactory();
prompts_.clear();
}

std::unique_ptr<PermissionPrompt> MockPermissionLifetimePromptFactory::Create(
content::WebContents* web_contents,
PermissionPrompt::Delegate* delegate) {
auto prompt = std::make_unique<MockPermissionLifetimePrompt>(this, delegate);

prompts_.push_back(prompt.get());
show_count_++;

if (!show_bubble_quit_closure_.is_null())
show_bubble_quit_closure_.Run();

manager_->set_auto_response_for_test(response_type_);

OnPermissionPromptCreated(prompt.get());
return prompt;
}

bool MockPermissionLifetimePromptFactory::IsVisible() const {
return !prompts_.empty();
}

void MockPermissionLifetimePromptFactory::WaitForPermissionBubble() {
if (IsVisible())
return;
DCHECK(show_bubble_quit_closure_.is_null());
base::RunLoop loop;
show_bubble_quit_closure_ = loop.QuitClosure();
loop.Run();
show_bubble_quit_closure_ = base::RepeatingClosure();
}

void MockPermissionLifetimePromptFactory::HideView(
MockPermissionLifetimePrompt* prompt) {
base::Erase(prompts_, prompt);
}

} // namespace permissions
71 changes: 71 additions & 0 deletions browser/permissions/mock_permission_lifetime_prompt_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_PERMISSIONS_MOCK_PERMISSION_LIFETIME_PROMPT_FACTORY_H_
#define BRAVE_BROWSER_PERMISSIONS_MOCK_PERMISSION_LIFETIME_PROMPT_FACTORY_H_

#include <memory>
#include <vector>

#include "brave/browser/permissions/mock_permission_lifetime_prompt.h"
#include "components/permissions/permission_prompt.h"
#include "components/permissions/permission_request.h"
#include "components/permissions/permission_request_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "url/gurl.h"

namespace content {
class WebContents;
}

namespace permissions {
enum class RequestType;

class MockPermissionLifetimePromptFactory {
public:
explicit MockPermissionLifetimePromptFactory(
PermissionRequestManager* manager);
MockPermissionLifetimePromptFactory(
const MockPermissionLifetimePromptFactory&) = delete;
MockPermissionLifetimePromptFactory& operator=(
const MockPermissionLifetimePromptFactory&) = delete;
~MockPermissionLifetimePromptFactory();

// Create method called by the PRM to show a bubble.
std::unique_ptr<PermissionPrompt> Create(
content::WebContents* web_contents,
PermissionPrompt::Delegate* delegate);

void set_response_type(PermissionRequestManager::AutoResponseType type) {
response_type_ = type;
}

PermissionRequestManager::AutoResponseType response_type() {
return response_type_;
}

// Number of times |Show| was called on any bubble.
int show_count() { return show_count_; }
bool IsVisible() const;
void WaitForPermissionBubble();
void HideView(MockPermissionLifetimePrompt* view);

MOCK_METHOD(void, OnPermissionPromptCreated, (MockPermissionLifetimePrompt*));

private:
int show_count_;

std::vector<MockPermissionLifetimePrompt*> prompts_;
PermissionRequestManager::AutoResponseType response_type_;

base::RepeatingClosure show_bubble_quit_closure_;

// The bubble manager that will be associated with this factory.
PermissionRequestManager* manager_;
};

} // namespace permissions

#endif // BRAVE_BROWSER_PERMISSIONS_MOCK_PERMISSION_LIFETIME_PROMPT_FACTORY_H_
Loading