Skip to content

Commit

Permalink
Move permission origin lifetime monitor to components.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodov committed Apr 5, 2021
1 parent 0324989 commit 43f2ed6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions browser/permissions/permission_lifetime_manager_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <memory>
#include <utility>

#include "brave/browser/permissions/brave_permission_origin_lifetime_monitor.h"
#include "brave/components/permissions/permission_lifetime_manager.h"
#include "brave/components/permissions/permission_origin_lifetime_monitor_impl.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/permissions/permission_manager_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h"
Expand Down Expand Up @@ -52,7 +52,7 @@ KeyedService* PermissionLifetimeManagerFactory::BuildServiceInstanceFor(
permission_origin_lifetime_monitor;
if (base::FeatureList::IsEnabled(net::features::kBraveEphemeralStorage)) {
permission_origin_lifetime_monitor =
std::make_unique<permissions::BravePermissionOriginLifetimeMonitor>(
std::make_unique<permissions::PermissionOriginLifetimeMonitorImpl>(
context);
}
auto* profile = Profile::FromBrowserContext(context);
Expand Down
2 changes: 0 additions & 2 deletions browser/permissions/sources.gni
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
brave_browser_permissions_sources = [
"//brave/browser/permissions/brave_permission_origin_lifetime_monitor.cc",
"//brave/browser/permissions/brave_permission_origin_lifetime_monitor.h",
"//brave/browser/permissions/permission_lifetime_manager_factory.cc",
"//brave/browser/permissions/permission_lifetime_manager_factory.h",
]
Expand Down
1 change: 1 addition & 0 deletions components/permissions/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ include_rules = [
"+components/keyed_service/core",
"+components/permissions",
"+components/pref_registry",
"+content/public/browser",
"+services/preferences/public/cpp",
]
6 changes: 3 additions & 3 deletions components/permissions/permission_origin_lifetime_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class PermissionOriginLifetimeMonitor {
virtual void SetOnPermissionOriginDestroyedCallback(
base::RepeatingCallback<void(const std::string&)> callback) = 0;

// Subscribe to a permission origin destruction. Returns a string key which
// will be used in a callback. An empty key can be returned if a permission
// origin can not be observed.
// Subscribe to a permission origin destruction. Returns an ephemeral storage
// domain or an empty string if a storage partition for |requesting_origin|
// doesn't exist. Returned string will be used in a callback.
virtual std::string SubscribeToPermissionOriginDestruction(
const GURL& requesting_origin) = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* 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/brave_permission_origin_lifetime_monitor.h"
#include "brave/components/permissions/permission_origin_lifetime_monitor_impl.h"

#include <utility>

Expand All @@ -14,46 +14,47 @@

namespace permissions {

BravePermissionOriginLifetimeMonitor::BravePermissionOriginLifetimeMonitor(
PermissionOriginLifetimeMonitorImpl::PermissionOriginLifetimeMonitorImpl(
content::BrowserContext* browser_context)
: browser_context_(browser_context) {
DCHECK(browser_context_);
DCHECK(base::FeatureList::IsEnabled(net::features::kBraveEphemeralStorage));
}

BravePermissionOriginLifetimeMonitor::~BravePermissionOriginLifetimeMonitor() =
PermissionOriginLifetimeMonitorImpl::~PermissionOriginLifetimeMonitorImpl() =
default;

void BravePermissionOriginLifetimeMonitor::
void PermissionOriginLifetimeMonitorImpl::
SetOnPermissionOriginDestroyedCallback(
base::RepeatingCallback<void(const std::string&)> callback) {
permission_destroyed_callback_ = std::move(callback);
}

std::string
BravePermissionOriginLifetimeMonitor::SubscribeToPermissionOriginDestruction(
PermissionOriginLifetimeMonitorImpl::SubscribeToPermissionOriginDestruction(
const GURL& requesting_origin) {
DCHECK(permission_destroyed_callback_);
std::string storage_domain =
net::URLToEphemeralStorageDomain(requesting_origin);
auto* tld_ephemeral_lifetime =
content::TLDEphemeralLifetime::Get(browser_context_, storage_domain);
if (!tld_ephemeral_lifetime) {
DCHECK(!base::Contains(active_subscriptions_, storage_domain));
// If an ephemeral lifetime object doesn't exist, treat a permission origin
// as an already destroyed one.
return std::string();
}

if (!base::Contains(active_subscriptions_, storage_domain)) {
tld_ephemeral_lifetime->RegisterOnDestroyCallback(base::BindOnce(
&BravePermissionOriginLifetimeMonitor::OnEphemeralTLDDestroyed,
&PermissionOriginLifetimeMonitorImpl::OnEphemeralTLDDestroyed,
weak_ptr_factory_.GetWeakPtr()));
active_subscriptions_.insert(storage_domain);
}
return storage_domain;
}

void BravePermissionOriginLifetimeMonitor::OnEphemeralTLDDestroyed(
void PermissionOriginLifetimeMonitorImpl::OnEphemeralTLDDestroyed(
const std::string& storage_domain) {
DCHECK(base::Contains(active_subscriptions_, storage_domain));
active_subscriptions_.erase(storage_domain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* 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_BRAVE_PERMISSION_ORIGIN_LIFETIME_MONITOR_H_
#define BRAVE_BROWSER_PERMISSIONS_BRAVE_PERMISSION_ORIGIN_LIFETIME_MONITOR_H_
#ifndef BRAVE_COMPONENTS_PERMISSIONS_PERMISSION_ORIGIN_LIFETIME_MONITOR_IMPL_H_
#define BRAVE_COMPONENTS_PERMISSIONS_PERMISSION_ORIGIN_LIFETIME_MONITOR_IMPL_H_

#include <string>

Expand All @@ -19,21 +19,19 @@ class BrowserContext;
namespace permissions {

// Uses TLDEphemeralLifetime to observe a permission origin destruction.
class BravePermissionOriginLifetimeMonitor
class PermissionOriginLifetimeMonitorImpl
: public PermissionOriginLifetimeMonitor {
public:
BravePermissionOriginLifetimeMonitor(
explicit PermissionOriginLifetimeMonitorImpl(
content::BrowserContext* browser_context);
BravePermissionOriginLifetimeMonitor(
const BravePermissionOriginLifetimeMonitor&) = delete;
BravePermissionOriginLifetimeMonitor& operator=(
const BravePermissionOriginLifetimeMonitor&) = delete;
~BravePermissionOriginLifetimeMonitor() override;
PermissionOriginLifetimeMonitorImpl(
const PermissionOriginLifetimeMonitorImpl&) = delete;
PermissionOriginLifetimeMonitorImpl& operator=(
const PermissionOriginLifetimeMonitorImpl&) = delete;
~PermissionOriginLifetimeMonitorImpl() override;

void SetOnPermissionOriginDestroyedCallback(
base::RepeatingCallback<void(const std::string&)> callback) override;
// Returns an ephemeral storage domain or an empty string if a storage
// partition for |requesting_origin| doesn't exist.
std::string SubscribeToPermissionOriginDestruction(
const GURL& requesting_origin) override;

Expand All @@ -46,10 +44,10 @@ class BravePermissionOriginLifetimeMonitor
permission_destroyed_callback_;
base::flat_set<std::string> active_subscriptions_;

base::WeakPtrFactory<BravePermissionOriginLifetimeMonitor> weak_ptr_factory_{
base::WeakPtrFactory<PermissionOriginLifetimeMonitorImpl> weak_ptr_factory_{
this};
};

} // namespace permissions

#endif // BRAVE_BROWSER_PERMISSIONS_BRAVE_PERMISSION_ORIGIN_LIFETIME_MONITOR_H_
#endif // BRAVE_COMPONENTS_PERMISSIONS_PERMISSION_ORIGIN_LIFETIME_MONITOR_IMPL_H_
2 changes: 2 additions & 0 deletions components/permissions/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ brave_components_permissions_sources = [
"//brave/components/permissions/permission_lifetime_utils.cc",
"//brave/components/permissions/permission_lifetime_utils.h",
"//brave/components/permissions/permission_origin_lifetime_monitor.h",
"//brave/components/permissions/permission_origin_lifetime_monitor_impl.cc",
"//brave/components/permissions/permission_origin_lifetime_monitor_impl.h",
"//brave/components/permissions/permission_origins.cc",
"//brave/components/permissions/permission_origins.h",
]
Expand Down

0 comments on commit 43f2ed6

Please sign in to comment.