Commit 6166a3d2 authored by Ramin Halavati's avatar Ramin Halavati Committed by Commit Bot

Move flash permissions to a new Ephemeral Provider.

A new content settings provider for ephemeral permissions is added and
flash permissions are moved to it. This provider keeps the permissions
as long as the current session is not closed.

Bug: 850062
Change-Id: Ib0825eeb39dbf7a341ac47a537f0f0bdfaf7848d
Reviewed-on: https://chromium-review.googlesource.com/1107988Reviewed-by: default avatarMartin Šrámek <msramek@chromium.org>
Commit-Queue: Ramin Halavati <rhalavati@chromium.org>
Cr-Commit-Position: refs/heads/master@{#572117}
parent 02f28d69
......@@ -11,6 +11,8 @@ static_library("browser") {
"content_settings_default_provider.h",
"content_settings_details.cc",
"content_settings_details.h",
"content_settings_ephemeral_provider.cc",
"content_settings_ephemeral_provider.h",
"content_settings_global_value_map.cc",
"content_settings_global_value_map.h",
"content_settings_info.cc",
......
// Copyright 2018 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 "components/content_settings/core/browser/content_settings_ephemeral_provider.h"
#include "base/stl_util.h"
#include "base/time/default_clock.h"
#include "components/content_settings/core/browser/content_settings_registry.h"
#include "components/content_settings/core/browser/website_settings_info.h"
#include "components/content_settings/core/browser/website_settings_registry.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
namespace content_settings {
// ////////////////////////////////////////////////////////////////////////////
// EphemeralProvider:
//
EphemeralProvider::EphemeralProvider(bool store_last_modified)
: store_last_modified_(store_last_modified),
clock_(base::DefaultClock::GetInstance()) {
ContentSettingsRegistry* content_settings =
ContentSettingsRegistry::GetInstance();
WebsiteSettingsRegistry* website_settings =
WebsiteSettingsRegistry::GetInstance();
for (const WebsiteSettingsInfo* info : *website_settings) {
const ContentSettingsInfo* content_type_info =
content_settings->Get(info->type());
// If this is an ephemeral content setting, handle it in this class.
if (content_type_info && content_type_info->storage_behavior() ==
ContentSettingsInfo::EPHEMERAL) {
supported_types_.insert(info->type());
}
}
}
EphemeralProvider::~EphemeralProvider() {}
std::unique_ptr<RuleIterator> EphemeralProvider::GetRuleIterator(
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
bool incognito) const {
return content_settings_rules_.GetRuleIterator(content_type,
resource_identifier, nullptr);
}
bool EphemeralProvider::SetWebsiteSetting(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
base::Value* in_value) {
DCHECK(CalledOnValidThread());
if (!base::ContainsKey(supported_types_, content_type))
return false;
// Default settings are set using a wildcard pattern for both
// |primary_pattern| and |secondary_pattern|. Don't store default settings in
// the EphemeralProvider. The EphemeralProvider handles settings for
// specific sites/origins defined by the |primary_pattern| and the
// |secondary_pattern|. Default settings are handled by the DefaultProvider.
if (primary_pattern == ContentSettingsPattern::Wildcard() &&
secondary_pattern == ContentSettingsPattern::Wildcard() &&
resource_identifier.empty()) {
return false;
}
content_settings_rules_.SetValue(
primary_pattern, secondary_pattern, content_type, resource_identifier,
store_last_modified_ ? clock_->Now() : base::Time(), in_value);
NotifyObservers(primary_pattern, secondary_pattern, content_type,
resource_identifier);
return true;
}
base::Time EphemeralProvider::GetWebsiteSettingLastModified(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier) {
DCHECK(CalledOnValidThread());
return content_settings_rules_.GetLastModified(
primary_pattern, secondary_pattern, content_type, resource_identifier);
}
void EphemeralProvider::ClearAllContentSettingsRules(
ContentSettingsType content_type) {
DCHECK(CalledOnValidThread());
// Get all resource identifiers for this |content_type|.
std::set<ResourceIdentifier> resource_identifiers;
for (OriginIdentifierValueMap::EntryMap::const_iterator entry =
content_settings_rules_.begin();
entry != content_settings_rules_.end(); entry++) {
if (entry->first.content_type == content_type)
resource_identifiers.insert(entry->first.resource_identifier);
}
for (const ResourceIdentifier& resource_identifier : resource_identifiers)
content_settings_rules_.DeleteValues(content_type, resource_identifier);
if (!resource_identifiers.empty()) {
NotifyObservers(ContentSettingsPattern(), ContentSettingsPattern(),
content_type, ResourceIdentifier());
}
}
void EphemeralProvider::ShutdownOnUIThread() {
DCHECK(CalledOnValidThread());
RemoveAllObservers();
}
void EphemeralProvider::SetClockForTesting(base::Clock* clock) {
clock_ = clock;
}
} // namespace content_settings
// Copyright 2018 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.
#ifndef COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_EPHEMERAL_PROVIDER_H_
#define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_EPHEMERAL_PROVIDER_H_
#include <set>
#include "components/content_settings/core/browser/content_settings_origin_identifier_value_map.h"
#include "components/content_settings/core/browser/user_modifiable_provider.h"
namespace base {
class Clock;
}
namespace content_settings {
// A user-modifiable content settings provider that doesn't store its settings
// on disk.
class EphemeralProvider : public UserModifiableProvider {
public:
EphemeralProvider(bool store_last_modified);
~EphemeralProvider() override;
// UserModifiableProvider implementations.
std::unique_ptr<RuleIterator> GetRuleIterator(
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
bool incognito) const override;
bool SetWebsiteSetting(const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
base::Value* value) override;
void ClearAllContentSettingsRules(ContentSettingsType content_type) override;
void ShutdownOnUIThread() override;
base::Time GetWebsiteSettingLastModified(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier) override;
void SetClockForTesting(base::Clock* clock);
private:
bool store_last_modified_;
OriginIdentifierValueMap content_settings_rules_;
std::set<ContentSettingsType> supported_types_;
base::Clock* clock_;
DISALLOW_COPY_AND_ASSIGN(EphemeralProvider);
};
} // namespace content_settings
#endif // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_EPHEMERAL_PROVIDER_H_
......@@ -14,11 +14,13 @@ ContentSettingsInfo::ContentSettingsInfo(
const WebsiteSettingsInfo* website_settings_info,
const std::vector<std::string>& whitelisted_schemes,
const std::set<ContentSetting>& valid_settings,
IncognitoBehavior incognito_behavior)
IncognitoBehavior incognito_behavior,
StorageBehavior storage_behavior)
: website_settings_info_(website_settings_info),
whitelisted_schemes_(whitelisted_schemes),
valid_settings_(valid_settings),
incognito_behavior_(incognito_behavior) {}
incognito_behavior_(incognito_behavior),
storage_behavior_(storage_behavior) {}
ContentSettingsInfo::~ContentSettingsInfo() {}
......
......@@ -32,11 +32,19 @@ class ContentSettingsInfo {
INHERIT_IF_LESS_PERMISSIVE
};
enum StorageBehavior {
// The setting is stored and used in future sessions.
PERSISTENT,
// The setting is only valid throughout the current session.
EPHEMERAL,
};
// This object does not take ownership of |website_settings_info|.
ContentSettingsInfo(const WebsiteSettingsInfo* website_settings_info,
const std::vector<std::string>& whitelisted_schemes,
const std::set<ContentSetting>& valid_settings,
IncognitoBehavior incognito_behavior);
IncognitoBehavior incognito_behavior,
StorageBehavior storage_behavior);
~ContentSettingsInfo();
const WebsiteSettingsInfo* website_settings_info() const {
......@@ -53,12 +61,14 @@ class ContentSettingsInfo {
bool IsDefaultSettingValid(ContentSetting setting) const;
IncognitoBehavior incognito_behavior() const { return incognito_behavior_; }
StorageBehavior storage_behavior() const { return storage_behavior_; }
private:
const WebsiteSettingsInfo* website_settings_info_;
const std::vector<std::string> whitelisted_schemes_;
const std::set<ContentSetting> valid_settings_;
const IncognitoBehavior incognito_behavior_;
const StorageBehavior storage_behavior_;
DISALLOW_COPY_AND_ASSIGN(ContentSettingsInfo);
};
......
......@@ -15,11 +15,12 @@
#include "base/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/time/default_clock.h"
#include "components/content_settings/core/browser/content_settings_info.h"
#include "components/content_settings/core/browser/content_settings_pref.h"
#include "components/content_settings/core/browser/content_settings_registry.h"
#include "components/content_settings/core/browser/content_settings_rule.h"
#include "components/content_settings/core/browser/content_settings_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/browser/website_settings_info.h"
#include "components/content_settings/core/browser/website_settings_registry.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
......@@ -107,15 +108,24 @@ PrefProvider::PrefProvider(PrefService* prefs,
pref_change_registrar_.Init(prefs_);
ContentSettingsRegistry* content_settings =
ContentSettingsRegistry::GetInstance();
WebsiteSettingsRegistry* website_settings =
WebsiteSettingsRegistry::GetInstance();
for (const WebsiteSettingsInfo* info : *website_settings) {
content_settings_prefs_.insert(std::make_pair(
info->type(),
std::make_unique<ContentSettingsPref>(
info->type(), prefs_, &pref_change_registrar_, info->pref_name(),
is_incognito_,
base::Bind(&PrefProvider::Notify, base::Unretained(this)))));
const ContentSettingsInfo* content_type_info =
content_settings->Get(info->type());
// If it's not a content setting, or it's persistent, handle it in this
// class.
if (!content_type_info || content_type_info->storage_behavior() ==
ContentSettingsInfo::PERSISTENT) {
content_settings_prefs_.insert(std::make_pair(
info->type(),
std::make_unique<ContentSettingsPref>(
info->type(), prefs_, &pref_change_registrar_, info->pref_name(),
is_incognito_,
base::Bind(&PrefProvider::Notify, base::Unretained(this)))));
}
}
if (!is_incognito_) {
......@@ -136,6 +146,9 @@ std::unique_ptr<RuleIterator> PrefProvider::GetRuleIterator(
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
bool incognito) const {
if (!supports_type(content_type))
return nullptr;
return GetPref(content_type)->GetRuleIterator(resource_identifier, incognito);
}
......@@ -148,6 +161,9 @@ bool PrefProvider::SetWebsiteSetting(
DCHECK(CalledOnValidThread());
DCHECK(prefs_);
if (!supports_type(content_type))
return false;
// Default settings are set using a wildcard pattern for both
// |primary_pattern| and |secondary_pattern|. Don't store default settings in
// the |PrefProvider|. The |PrefProvider| handles settings for specific
......@@ -175,6 +191,9 @@ base::Time PrefProvider::GetWebsiteSettingLastModified(
DCHECK(CalledOnValidThread());
DCHECK(prefs_);
if (!supports_type(content_type))
return base::Time();
return GetPref(content_type)
->GetWebsiteSettingLastModified(primary_pattern, secondary_pattern,
resource_identifier);
......@@ -185,7 +204,8 @@ void PrefProvider::ClearAllContentSettingsRules(
DCHECK(CalledOnValidThread());
DCHECK(prefs_);
GetPref(content_type)->ClearAllContentSettingsRules();
if (supports_type(content_type))
GetPref(content_type)->ClearAllContentSettingsRules();
}
void PrefProvider::ShutdownOnUIThread() {
......
......@@ -74,6 +74,12 @@ class PrefProvider : public UserModifiableProvider {
// Clean up the obsolete preferences from the user's profile.
void DiscardObsoletePreferences();
// Returns true if this provider supports the given |content_type|.
bool supports_type(ContentSettingsType content_type) const {
return content_settings_prefs_.find(content_type) !=
content_settings_prefs_.end();
}
// Weak; owned by the Profile and reset in ShutdownOnUIThread.
PrefService* prefs_;
......
......@@ -66,7 +66,8 @@ class ContentSettingsRegistry {
const std::set<ContentSetting>& valid_settings,
WebsiteSettingsInfo::ScopingType scoping_type,
Platforms platforms,
ContentSettingsInfo::IncognitoBehavior incognito_behavior);
ContentSettingsInfo::IncognitoBehavior incognito_behavior,
ContentSettingsInfo::StorageBehavior storage_behavior);
Map content_settings_info_;
WebsiteSettingsRegistry* website_settings_registry_;
......
......@@ -22,6 +22,7 @@
#include "build/build_config.h"
#include "components/content_settings/core/browser/content_settings_default_provider.h"
#include "components/content_settings/core/browser/content_settings_details.h"
#include "components/content_settings/core/browser/content_settings_ephemeral_provider.h"
#include "components/content_settings/core/browser/content_settings_info.h"
#include "components/content_settings/core/browser/content_settings_observable_provider.h"
#include "components/content_settings/core/browser/content_settings_policy_provider.h"
......@@ -65,6 +66,7 @@ constexpr ProviderNamesSourceMapEntry kProviderNamesSourceMap[] = {
{"supervised_user", content_settings::SETTING_SOURCE_SUPERVISED},
{"extension", content_settings::SETTING_SOURCE_EXTENSION},
{"notification_android", content_settings::SETTING_SOURCE_USER},
{"ephemeral", content_settings::SETTING_SOURCE_USER},
{"preference", content_settings::SETTING_SOURCE_USER},
{"default", content_settings::SETTING_SOURCE_USER},
{"tests", content_settings::SETTING_SOURCE_USER},
......@@ -219,6 +221,13 @@ HostContentSettingsMap::HostContentSettingsMap(PrefService* prefs,
if (is_guest_profile)
pref_provider_->ClearPrefs();
content_settings::EphemeralProvider* ephemeral_provider =
new content_settings::EphemeralProvider(store_last_modified_);
content_settings_providers_[EPHEMERAL_PROVIDER] =
base::WrapUnique(ephemeral_provider);
user_modifiable_providers_.push_back(ephemeral_provider);
ephemeral_provider->AddObserver(this);
auto default_provider = std::make_unique<content_settings::DefaultProvider>(
prefs_, is_incognito_);
default_provider->AddObserver(this);
......
......@@ -59,6 +59,7 @@ class HostContentSettingsMap : public content_settings::Observer,
SUPERVISED_PROVIDER,
CUSTOM_EXTENSION_PROVIDER,
NOTIFICATION_ANDROID_PROVIDER,
EPHEMERAL_PROVIDER,
PREF_PROVIDER,
DEFAULT_PROVIDER,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment