1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_RENDERER_CONTENT_SETTINGS_OBSERVER_H_
6 #define CHROME_RENDERER_CONTENT_SETTINGS_OBSERVER_H_
11 #include "components/content_settings/core/common/content_settings.h"
12 #include "components/content_settings/core/common/content_settings_types.h"
13 #include "content/public/renderer/render_frame_observer.h"
14 #include "content/public/renderer/render_frame_observer_tracker.h"
15 #include "third_party/WebKit/public/platform/WebPermissionCallbacks.h"
16 #include "third_party/WebKit/public/web/WebContentSettingsClient.h"
22 class WebSecurityOrigin
;
26 namespace extensions
{
31 // Handles blocking content per content settings for each RenderFrame.
32 class ContentSettingsObserver
33 : public content::RenderFrameObserver
,
34 public content::RenderFrameObserverTracker
<ContentSettingsObserver
>,
35 public blink::WebContentSettingsClient
{
37 // Set |should_whitelist| to true if |render_frame()| contains content that
38 // should be whitelisted for content settings.
39 ContentSettingsObserver(content::RenderFrame
* render_frame
,
40 extensions::Dispatcher
* extension_dispatcher
,
41 bool should_whitelist
);
42 ~ContentSettingsObserver() override
;
44 // Sets the content setting rules which back |AllowImage()|, |AllowScript()|,
45 // and |AllowScriptFromSource()|. |content_setting_rules| must outlive this
46 // |ContentSettingsObserver|.
47 void SetContentSettingRules(
48 const RendererContentSettingRules
* content_setting_rules
);
50 bool IsPluginTemporarilyAllowed(const std::string
& identifier
);
52 // Sends an IPC notification that the specified content type was blocked.
53 void DidBlockContentType(ContentSettingsType settings_type
);
55 // Sends an IPC notification that the specified content type was blocked
56 // with additional metadata.
57 void DidBlockContentType(ContentSettingsType settings_type
,
58 const base::string16
& details
);
60 // blink::WebContentSettingsClient implementation.
61 virtual bool allowDatabase(const blink::WebString
& name
,
62 const blink::WebString
& display_name
,
63 unsigned long estimated_size
);
64 virtual void requestFileSystemAccessAsync(
65 const blink::WebContentSettingCallbacks
& callbacks
);
66 virtual void requestFileSystemAccessAsync(
67 const blink::WebPermissionCallbacks
& callbacks
);
68 virtual bool allowImage(bool enabled_per_settings
,
69 const blink::WebURL
& image_url
);
70 virtual bool allowIndexedDB(const blink::WebString
& name
,
71 const blink::WebSecurityOrigin
& origin
);
72 virtual bool allowPlugins(bool enabled_per_settings
);
73 virtual bool allowScript(bool enabled_per_settings
);
74 virtual bool allowScriptFromSource(bool enabled_per_settings
,
75 const blink::WebURL
& script_url
);
76 virtual bool allowStorage(bool local
);
77 virtual bool allowReadFromClipboard(bool default_value
);
78 virtual bool allowWriteToClipboard(bool default_value
);
79 virtual bool allowMutationEvents(bool default_value
);
80 virtual void didNotAllowPlugins();
81 virtual void didNotAllowScript();
82 virtual bool allowDisplayingInsecureContent(
83 bool allowed_per_settings
,
84 const blink::WebSecurityOrigin
& context
,
85 const blink::WebURL
& url
);
86 virtual bool allowRunningInsecureContent(
87 bool allowed_per_settings
,
88 const blink::WebSecurityOrigin
& context
,
89 const blink::WebURL
& url
);
91 // This is used for cases when the NPAPI plugins malfunction if used.
92 bool AreNPAPIPluginsBlocked() const;
95 FRIEND_TEST_ALL_PREFIXES(ContentSettingsObserverTest
, WhitelistedSchemes
);
96 FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest
,
97 ContentSettingsInterstitialPages
);
98 FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest
, PluginsTemporarilyAllowed
);
100 // RenderFrameObserver implementation.
101 bool OnMessageReceived(const IPC::Message
& message
) override
;
102 void DidCommitProvisionalLoad(bool is_new_navigation
,
103 bool is_same_page_navigation
) override
;
106 void OnLoadBlockedPlugins(const std::string
& identifier
);
107 void OnSetAsInterstitial();
108 void OnNPAPINotSupported();
109 void OnSetAllowDisplayingInsecureContent(bool allow
);
110 void OnSetAllowRunningInsecureContent(bool allow
);
111 void OnReloadFrame();
112 void OnRequestFileSystemAccessAsyncResponse(int request_id
, bool allowed
);
114 // Resets the |content_blocked_| array.
115 void ClearBlockedContentSettings();
117 // Whether the observed RenderFrame is for a platform app.
118 bool IsPlatformApp();
120 #if defined(ENABLE_EXTENSIONS)
121 // If |origin| corresponds to an installed extension, returns that extension.
122 // Otherwise returns NULL.
123 const extensions::Extension
* GetExtension(
124 const blink::WebSecurityOrigin
& origin
) const;
128 // True if |render_frame()| contains content that is white-listed for content
130 bool IsWhitelistedForContentSettings() const;
131 static bool IsWhitelistedForContentSettings(
132 const blink::WebSecurityOrigin
& origin
,
133 const GURL
& document_url
);
135 #if defined(ENABLE_EXTENSIONS)
136 // Owned by ChromeContentRendererClient and outlive us.
137 extensions::Dispatcher
* extension_dispatcher_
;
140 // Insecure content may be permitted for the duration of this render view.
141 bool allow_displaying_insecure_content_
;
142 bool allow_running_insecure_content_
;
144 // A pointer to content setting rules stored by the renderer. Normally, the
145 // |RendererContentSettingRules| object is owned by
146 // |ChromeRenderProcessObserver|. In the tests it is owned by the caller of
147 // |SetContentSettingRules|.
148 const RendererContentSettingRules
* content_setting_rules_
;
150 // Stores if images, scripts, and plugins have actually been blocked.
151 bool content_blocked_
[CONTENT_SETTINGS_NUM_TYPES
];
153 // Caches the result of AllowStorage.
154 typedef std::pair
<GURL
, bool> StoragePermissionsKey
;
155 std::map
<StoragePermissionsKey
, bool> cached_storage_permissions_
;
157 // Caches the result of |AllowScript|.
158 std::map
<blink::WebFrame
*, bool> cached_script_permissions_
;
160 std::set
<std::string
> temporarily_allowed_plugins_
;
161 bool is_interstitial_page_
;
162 bool npapi_plugins_blocked_
;
164 int current_request_id_
;
165 typedef std::map
<int, blink::WebContentSettingCallbacks
> PermissionRequestMap
;
166 PermissionRequestMap permission_requests_
;
168 // If true, IsWhitelistedForContentSettings will always return true.
169 const bool should_whitelist_
;
171 DISALLOW_COPY_AND_ASSIGN(ContentSettingsObserver
);
174 #endif // CHROME_RENDERER_CONTENT_SETTINGS_OBSERVER_H_