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 "chrome/common/content_settings.h"
12 #include "content/public/renderer/render_frame_observer.h"
13 #include "content/public/renderer/render_frame_observer_tracker.h"
14 #include "extensions/common/permissions/api_permission.h"
15 #include "third_party/WebKit/public/web/WebPermissionClient.h"
21 class WebSecurityOrigin
;
25 namespace extensions
{
30 // Handles blocking content per content settings for each RenderFrame.
31 class ContentSettingsObserver
32 : public content::RenderFrameObserver
,
33 public content::RenderFrameObserverTracker
<ContentSettingsObserver
>,
34 public blink::WebPermissionClient
{
36 ContentSettingsObserver(content::RenderFrame
* render_frame
,
37 extensions::Dispatcher
* extension_dispatcher
);
38 virtual ~ContentSettingsObserver();
40 // Sets the content setting rules which back |AllowImage()|, |AllowScript()|,
41 // and |AllowScriptFromSource()|. |content_setting_rules| must outlive this
42 // |ContentSettingsObserver|.
43 void SetContentSettingRules(
44 const RendererContentSettingRules
* content_setting_rules
);
46 bool IsPluginTemporarilyAllowed(const std::string
& identifier
);
48 // Sends an IPC notification that the specified content type was blocked.
49 void DidBlockContentType(ContentSettingsType settings_type
);
51 // blink::WebPermissionClient implementation.
52 virtual bool allowDatabase(const blink::WebString
& name
,
53 const blink::WebString
& display_name
,
54 unsigned long estimated_size
);
55 virtual bool allowFileSystem();
56 virtual bool allowImage(bool enabled_per_settings
,
57 const blink::WebURL
& image_url
);
58 virtual bool allowIndexedDB(const blink::WebString
& name
,
59 const blink::WebSecurityOrigin
& origin
);
60 virtual bool allowPlugins(bool enabled_per_settings
);
61 virtual bool allowScript(bool enabled_per_settings
);
62 virtual bool allowScriptFromSource(bool enabled_per_settings
,
63 const blink::WebURL
& script_url
);
64 virtual bool allowStorage(bool local
);
65 virtual bool allowReadFromClipboard(bool default_value
);
66 virtual bool allowWriteToClipboard(bool default_value
);
67 virtual bool allowWebComponents(bool default_value
);
68 virtual bool allowMutationEvents(bool default_value
);
69 virtual bool allowPushState();
70 virtual void didNotAllowPlugins();
71 virtual void didNotAllowScript();
72 virtual bool allowDisplayingInsecureContent(
73 bool allowed_per_settings
,
74 const blink::WebSecurityOrigin
& context
,
75 const blink::WebURL
& url
);
76 virtual bool allowRunningInsecureContent(
77 bool allowed_per_settings
,
78 const blink::WebSecurityOrigin
& context
,
79 const blink::WebURL
& url
);
81 // This is used for cases when the NPAPI plugins malfunction if used.
82 bool AreNPAPIPluginsBlocked() const;
85 FRIEND_TEST_ALL_PREFIXES(ContentSettingsObserverTest
, WhitelistedSchemes
);
86 FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest
,
87 ContentSettingsInterstitialPages
);
88 FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest
, PluginsTemporarilyAllowed
);
90 // RenderFrameObserver implementation.
91 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
92 virtual void DidCommitProvisionalLoad(bool is_new_navigation
) OVERRIDE
;
95 void OnLoadBlockedPlugins(const std::string
& identifier
);
96 void OnSetAsInterstitial();
97 void OnNPAPINotSupported();
98 void OnSetAllowDisplayingInsecureContent(bool allow
);
99 void OnSetAllowRunningInsecureContent(bool allow
);
100 void OnReloadFrame();
102 // Resets the |content_blocked_| array.
103 void ClearBlockedContentSettings();
105 // If |origin| corresponds to an installed extension, returns that extension.
106 // Otherwise returns NULL.
107 const extensions::Extension
* GetExtension(
108 const blink::WebSecurityOrigin
& origin
) const;
111 // True if |frame| contains content that is white-listed for content settings.
112 static bool IsWhitelistedForContentSettings(blink::WebFrame
* frame
);
113 static bool IsWhitelistedForContentSettings(
114 const blink::WebSecurityOrigin
& origin
,
115 const GURL
& document_url
);
117 // Owned by ChromeContentRendererClient and outlive us.
118 extensions::Dispatcher
* extension_dispatcher_
;
120 // Insecure content may be permitted for the duration of this render view.
121 bool allow_displaying_insecure_content_
;
122 bool allow_running_insecure_content_
;
124 // A pointer to content setting rules stored by the renderer. Normally, the
125 // |RendererContentSettingRules| object is owned by
126 // |ChromeRenderProcessObserver|. In the tests it is owned by the caller of
127 // |SetContentSettingRules|.
128 const RendererContentSettingRules
* content_setting_rules_
;
130 // Stores if images, scripts, and plugins have actually been blocked.
131 bool content_blocked_
[CONTENT_SETTINGS_NUM_TYPES
];
133 // Caches the result of AllowStorage.
134 typedef std::pair
<GURL
, bool> StoragePermissionsKey
;
135 std::map
<StoragePermissionsKey
, bool> cached_storage_permissions_
;
137 // Caches the result of |AllowScript|.
138 std::map
<blink::WebFrame
*, bool> cached_script_permissions_
;
140 std::set
<std::string
> temporarily_allowed_plugins_
;
141 bool is_interstitial_page_
;
142 bool npapi_plugins_blocked_
;
144 DISALLOW_COPY_AND_ASSIGN(ContentSettingsObserver
);
147 #endif // CHROME_RENDERER_CONTENT_SETTINGS_OBSERVER_H_