[Android] Added UMA for search by image context menu.
[chromium-blink-merge.git] / chrome / renderer / content_settings_observer.h
blob92cf5b8164ddb8973cd47e48f86d61b0983283ce
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_
8 #include <map>
9 #include <set>
11 #include "chrome/common/content_settings.h"
12 #include "content/public/renderer/render_view_observer.h"
13 #include "content/public/renderer/render_view_observer_tracker.h"
15 class GURL;
17 namespace WebKit {
18 class WebFrame;
19 class WebSecurityOrigin;
20 class WebURL;
23 // Handles blocking content per content settings for each RenderView.
24 class ContentSettingsObserver
25 : public content::RenderViewObserver,
26 public content::RenderViewObserverTracker<ContentSettingsObserver> {
27 public:
28 explicit ContentSettingsObserver(content::RenderView* render_view);
29 virtual ~ContentSettingsObserver();
31 // Sets the content setting rules which back |AllowImage()|, |AllowScript()|,
32 // and |AllowScriptFromSource()|. |content_setting_rules| must outlive this
33 // |ContentSettingsObserver|.
34 void SetContentSettingRules(
35 const RendererContentSettingRules* content_setting_rules);
37 bool IsPluginTemporarilyAllowed(const std::string& identifier);
39 // Sends an IPC notification that the specified content type was blocked.
40 // If the content type requires it, |resource_identifier| names the specific
41 // resource that was blocked (the plugin path in the case of plugins),
42 // otherwise it's the empty string.
43 void DidBlockContentType(ContentSettingsType settings_type,
44 const std::string& resource_identifier);
46 // These correspond to WebKit::WebPermissionClient methods.
47 bool AllowDatabase(WebKit::WebFrame* frame,
48 const WebKit::WebString& name,
49 const WebKit::WebString& display_name,
50 unsigned long estimated_size);
51 bool AllowFileSystem(WebKit::WebFrame* frame);
52 bool AllowImage(WebKit::WebFrame* frame,
53 bool enabled_per_settings,
54 const WebKit::WebURL& image_url);
55 bool AllowIndexedDB(WebKit::WebFrame* frame,
56 const WebKit::WebString& name,
57 const WebKit::WebSecurityOrigin& origin);
58 bool AllowPlugins(WebKit::WebFrame* frame, bool enabled_per_settings);
59 bool AllowScript(WebKit::WebFrame* frame, bool enabled_per_settings);
60 bool AllowScriptFromSource(WebKit::WebFrame* frame, bool enabled_per_settings,
61 const WebKit::WebURL& script_url);
62 bool AllowStorage(WebKit::WebFrame* frame, bool local);
64 void DidNotAllowPlugins();
65 void DidNotAllowScript();
66 void DidNotAllowMixedScript();
68 // These two methods are not related to content settings, they are used
69 // for cases when the NPAPI plugins malfunction if used.
70 void BlockNPAPIPlugins();
71 bool AreNPAPIPluginsBlocked() const;
73 private:
74 FRIEND_TEST_ALL_PREFIXES(ContentSettingsObserverTest, WhitelistedSchemes);
75 FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest,
76 ContentSettingsInterstitialPages);
78 // RenderViewObserver implementation.
79 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
80 virtual void DidCommitProvisionalLoad(WebKit::WebFrame* frame,
81 bool is_new_navigation) OVERRIDE;
83 // Message handlers.
84 void OnLoadBlockedPlugins(const std::string& identifier);
85 void OnSetAsInterstitial();
87 // Resets the |content_blocked_| array.
88 void ClearBlockedContentSettings();
90 // Helpers.
91 // True if |frame| contains content that is white-listed for content settings.
92 static bool IsWhitelistedForContentSettings(WebKit::WebFrame* frame);
93 static bool IsWhitelistedForContentSettings(
94 const WebKit::WebSecurityOrigin& origin,
95 const GURL& document_url);
97 // A pointer to content setting rules stored by the renderer. Normally, the
98 // |RendererContentSettingRules| object is owned by
99 // |ChromeRenderProcessObserver|. In the tests it is owned by the caller of
100 // |SetContentSettingRules|.
101 const RendererContentSettingRules* content_setting_rules_;
103 // Stores if images, scripts, and plugins have actually been blocked.
104 bool content_blocked_[CONTENT_SETTINGS_NUM_TYPES];
106 // Caches the result of AllowStorage.
107 typedef std::pair<GURL, bool> StoragePermissionsKey;
108 std::map<StoragePermissionsKey, bool> cached_storage_permissions_;
110 // Caches the result of |AllowScript|.
111 std::map<WebKit::WebFrame*, bool> cached_script_permissions_;
113 std::set<std::string> temporarily_allowed_plugins_;
114 bool is_interstitial_page_;
115 bool npapi_plugins_blocked_;
117 DISALLOW_COPY_AND_ASSIGN(ContentSettingsObserver);
120 #endif // CHROME_RENDERER_CONTENT_SETTINGS_OBSERVER_H_