1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_CONTENT_SETTINGS_RENDERER_CONTENT_SETTINGS_AGENT_IMPL_H_
#define COMPONENTS_CONTENT_SETTINGS_RENDERER_CONTENT_SETTINGS_AGENT_IMPL_H_
#include <string>
#include <utility>
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/functional/callback.h"
#include "base/gtest_prod_util.h"
#include "components/content_settings/common/content_settings_agent.mojom.h"
#include "components/content_settings/common/content_settings_manager.mojom.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/render_frame_observer_tracker.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/associated_receiver_set.h"
#include "third_party/blink/public/platform/web_content_settings_client.h"
#include "url/origin.h"
namespace blink {
class WebFrame;
class WebURL;
} // namespace blink
namespace content_settings {
// This class serves as an agent of the browser-side content settings machinery
// to implement browser-specified rules directly within the renderer process.
// In some cases it forwards requests on to the browser to determine policy.
// An instance of this class is associated w/ each RenderFrame in the process.
class ContentSettingsAgentImpl
: public content::RenderFrameObserver,
public content::RenderFrameObserverTracker<ContentSettingsAgentImpl>,
public blink::WebContentSettingsClient,
public mojom::ContentSettingsAgent {
public:
class Delegate {
public:
virtual ~Delegate();
// Return true if this frame should be allowlisted for accessing storage.
virtual bool IsFrameAllowlistedForStorageAccess(
blink::WebFrame* frame) const;
// Return true if this scheme should be allowlisted for content settings.
virtual bool IsSchemeAllowlisted(const std::string& scheme);
// Allows the delegate to override logic for various
// blink::WebContentSettingsClient methods.
virtual bool AllowReadFromClipboard();
virtual bool AllowWriteToClipboard();
};
ContentSettingsAgentImpl(content::RenderFrame* render_frame,
std::unique_ptr<Delegate> delegate);
ContentSettingsAgentImpl(const ContentSettingsAgentImpl&) = delete;
ContentSettingsAgentImpl& operator=(const ContentSettingsAgentImpl&) = delete;
~ContentSettingsAgentImpl() override;
// Sends an IPC notification that the specified content type was blocked.
void DidBlockContentType(ContentSettingsType settings_type);
// Helper to convert StorageType to its Mojo counterpart.
static mojom::ContentSettingsManager::StorageType ConvertToMojoStorageType(
StorageType storage_type);
// blink::WebContentSettingsClient:
void AllowStorageAccess(StorageType storage_type,
base::OnceCallback<void(bool)> callback) override;
bool AllowStorageAccessSync(StorageType type) override;
bool AllowReadFromClipboard() override;
bool AllowWriteToClipboard() override;
void DidNotAllowImage() override;
void DidNotAllowScript() override;
bool AllowRunningInsecureContent(bool allowed_per_settings,
const blink::WebURL& url) override;
bool ShouldAutoupgradeMixedContent() override;
bool allow_running_insecure_content() const {
return allow_running_insecure_content_;
}
void SetContentSettingsManager(
mojo::Remote<mojom::ContentSettingsManager> manager) {
content_settings_manager_ = std::move(manager);
}
RendererContentSettingRules* GetRendererContentSettingRules();
void SetRendererContentSettingRulesForTest(
const RendererContentSettingRules& rules);
protected:
// Allow this to be overridden by tests.
virtual void BindContentSettingsManager(
mojo::Remote<mojom::ContentSettingsManager>* manager);
private:
FRIEND_TEST_ALL_PREFIXES(ContentSettingsAgentImplBrowserTest,
AllowlistedSchemes);
FRIEND_TEST_ALL_PREFIXES(ContentSettingsAgentImplBrowserTest,
ContentSettingsInterstitialPages);
// RenderFrameObserver implementation.
void DidCommitProvisionalLoad(ui::PageTransition transition) override;
void OnDestruct() override;
// mojom::ContentSettingsAgent:
void SetAllowRunningInsecureContent() override;
void SendRendererContentSettingRules(
const RendererContentSettingRules& renderer_settings) override;
void OnContentSettingsAgentRequest(
mojo::PendingAssociatedReceiver<mojom::ContentSettingsAgent> receiver);
// Resets the `content_blocked_` array.
void ClearBlockedContentSettings();
// A getter for `content_settings_manager_` that ensures it is bound.
mojom::ContentSettingsManager& GetContentSettingsManager();
mojo::Remote<mojom::ContentSettingsManager> content_settings_manager_;
// Insecure content may be permitted for the duration of this render view.
bool allow_running_insecure_content_ = false;
std::unique_ptr<RendererContentSettingRules> content_setting_rules_ = nullptr;
// Stores if images, scripts, and plugins have actually been blocked.
base::flat_set<ContentSettingsType> content_blocked_;
// Caches the result of AllowStorageAccess.
using StoragePermissionsKey = std::pair<url::Origin, StorageType>;
base::flat_map<StoragePermissionsKey, bool> cached_storage_permissions_;
std::unique_ptr<Delegate> delegate_;
mojo::AssociatedReceiverSet<mojom::ContentSettingsAgent> receivers_;
};
} // namespace content_settings
#endif // COMPONENTS_CONTENT_SETTINGS_RENDERER_CONTENT_SETTINGS_AGENT_IMPL_H_
|