/chromium-webcl/src/chrome/renderer/content_settings_observer.h

https://bitbucket.org/peixuan/chromium_r197479_base · C Header · 113 lines · 67 code · 22 blank · 24 comment · 0 complexity · 5df3d9d8dce762b9638487ae8c0ebafd MD5 · raw file

  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.
  4. #ifndef CHROME_RENDERER_CONTENT_SETTINGS_OBSERVER_H_
  5. #define CHROME_RENDERER_CONTENT_SETTINGS_OBSERVER_H_
  6. #include <map>
  7. #include <set>
  8. #include "chrome/common/content_settings.h"
  9. #include "content/public/renderer/render_view_observer.h"
  10. #include "content/public/renderer/render_view_observer_tracker.h"
  11. class GURL;
  12. namespace WebKit {
  13. class WebFrame;
  14. class WebSecurityOrigin;
  15. class WebURL;
  16. }
  17. // Handles blocking content per content settings for each RenderView.
  18. class ContentSettingsObserver
  19. : public content::RenderViewObserver,
  20. public content::RenderViewObserverTracker<ContentSettingsObserver> {
  21. public:
  22. explicit ContentSettingsObserver(content::RenderView* render_view);
  23. virtual ~ContentSettingsObserver();
  24. // Sets the content setting rules which back |AllowImage()|, |AllowScript()|,
  25. // and |AllowScriptFromSource()|. |content_setting_rules| must outlive this
  26. // |ContentSettingsObserver|.
  27. void SetContentSettingRules(
  28. const RendererContentSettingRules* content_setting_rules);
  29. bool IsPluginTemporarilyAllowed(const std::string& identifier);
  30. // Sends an IPC notification that the specified content type was blocked.
  31. // If the content type requires it, |resource_identifier| names the specific
  32. // resource that was blocked (the plugin path in the case of plugins),
  33. // otherwise it's the empty string.
  34. void DidBlockContentType(ContentSettingsType settings_type,
  35. const std::string& resource_identifier);
  36. // These correspond to WebKit::WebPermissionClient methods.
  37. bool AllowDatabase(WebKit::WebFrame* frame,
  38. const WebKit::WebString& name,
  39. const WebKit::WebString& display_name,
  40. unsigned long estimated_size);
  41. bool AllowFileSystem(WebKit::WebFrame* frame);
  42. bool AllowImage(WebKit::WebFrame* frame,
  43. bool enabled_per_settings,
  44. const WebKit::WebURL& image_url);
  45. bool AllowIndexedDB(WebKit::WebFrame* frame,
  46. const WebKit::WebString& name,
  47. const WebKit::WebSecurityOrigin& origin);
  48. bool AllowPlugins(WebKit::WebFrame* frame, bool enabled_per_settings);
  49. bool AllowScript(WebKit::WebFrame* frame, bool enabled_per_settings);
  50. bool AllowScriptFromSource(WebKit::WebFrame* frame, bool enabled_per_settings,
  51. const WebKit::WebURL& script_url);
  52. bool AllowStorage(WebKit::WebFrame* frame, bool local);
  53. void DidNotAllowPlugins();
  54. void DidNotAllowScript();
  55. void DidNotAllowMixedScript();
  56. private:
  57. FRIEND_TEST_ALL_PREFIXES(ContentSettingsObserverTest, WhitelistedSchemes);
  58. FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest,
  59. ContentSettingsInterstitialPages);
  60. // RenderViewObserver implementation.
  61. virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
  62. virtual void DidCommitProvisionalLoad(WebKit::WebFrame* frame,
  63. bool is_new_navigation) OVERRIDE;
  64. // Message handlers.
  65. void OnLoadBlockedPlugins(const std::string& identifier);
  66. void OnSetAsInterstitial();
  67. // Resets the |content_blocked_| array.
  68. void ClearBlockedContentSettings();
  69. // Helpers.
  70. // True if |frame| contains content that is white-listed for content settings.
  71. static bool IsWhitelistedForContentSettings(WebKit::WebFrame* frame);
  72. static bool IsWhitelistedForContentSettings(
  73. const WebKit::WebSecurityOrigin& origin,
  74. const GURL& document_url);
  75. // A pointer to content setting rules stored by the renderer. Normally, the
  76. // |RendererContentSettingRules| object is owned by
  77. // |ChromeRenderProcessObserver|. In the tests it is owned by the caller of
  78. // |SetContentSettingRules|.
  79. const RendererContentSettingRules* content_setting_rules_;
  80. // Stores if images, scripts, and plugins have actually been blocked.
  81. bool content_blocked_[CONTENT_SETTINGS_NUM_TYPES];
  82. // Caches the result of AllowStorage.
  83. typedef std::pair<GURL, bool> StoragePermissionsKey;
  84. std::map<StoragePermissionsKey, bool> cached_storage_permissions_;
  85. // Caches the result of |AllowScript|.
  86. std::map<WebKit::WebFrame*, bool> cached_script_permissions_;
  87. std::set<std::string> temporarily_allowed_plugins_;
  88. bool is_interstitial_page_;
  89. DISALLOW_COPY_AND_ASSIGN(ContentSettingsObserver);
  90. };
  91. #endif // CHROME_RENDERER_CONTENT_SETTINGS_OBSERVER_H_