/chrome/renderer/content_settings_observer.h

https://gitlab.com/0072016/Facebook-SDK- · C Header · 166 lines · 104 code · 30 blank · 32 comment · 0 complexity · 5d6f402bbcfae3ff5ab7bc81f546b639 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 "base/gtest_prod_util.h"
  9. #include "base/macros.h"
  10. #include "components/content_settings/core/common/content_settings.h"
  11. #include "components/content_settings/core/common/content_settings_types.h"
  12. #include "content/public/renderer/render_frame_observer.h"
  13. #include "content/public/renderer/render_frame_observer_tracker.h"
  14. #include "third_party/WebKit/public/web/WebContentSettingsClient.h"
  15. class GURL;
  16. namespace blink {
  17. class WebFrame;
  18. class WebSecurityOrigin;
  19. class WebURL;
  20. }
  21. namespace extensions {
  22. class Dispatcher;
  23. class Extension;
  24. }
  25. // Handles blocking content per content settings for each RenderFrame.
  26. class ContentSettingsObserver
  27. : public content::RenderFrameObserver,
  28. public content::RenderFrameObserverTracker<ContentSettingsObserver>,
  29. public blink::WebContentSettingsClient {
  30. public:
  31. // Set |should_whitelist| to true if |render_frame()| contains content that
  32. // should be whitelisted for content settings.
  33. ContentSettingsObserver(content::RenderFrame* render_frame,
  34. extensions::Dispatcher* extension_dispatcher,
  35. bool should_whitelist);
  36. ~ContentSettingsObserver() override;
  37. // Sets the content setting rules which back |AllowImage()|, |AllowScript()|,
  38. // and |AllowScriptFromSource()|. |content_setting_rules| must outlive this
  39. // |ContentSettingsObserver|.
  40. void SetContentSettingRules(
  41. const RendererContentSettingRules* content_setting_rules);
  42. bool IsPluginTemporarilyAllowed(const std::string& identifier);
  43. // Sends an IPC notification that the specified content type was blocked.
  44. void DidBlockContentType(ContentSettingsType settings_type);
  45. // Sends an IPC notification that the specified content type was blocked
  46. // with additional metadata.
  47. void DidBlockContentType(ContentSettingsType settings_type,
  48. const base::string16& details);
  49. // blink::WebContentSettingsClient implementation.
  50. bool allowDatabase(const blink::WebString& name,
  51. const blink::WebString& display_name,
  52. unsigned long estimated_size) override;
  53. void requestFileSystemAccessAsync(
  54. const blink::WebContentSettingCallbacks& callbacks) override;
  55. bool allowImage(bool enabled_per_settings,
  56. const blink::WebURL& image_url) override;
  57. bool allowIndexedDB(const blink::WebString& name,
  58. const blink::WebSecurityOrigin& origin) override;
  59. bool allowPlugins(bool enabled_per_settings) override;
  60. bool allowScript(bool enabled_per_settings) override;
  61. bool allowScriptFromSource(bool enabled_per_settings,
  62. const blink::WebURL& script_url) override;
  63. bool allowStorage(bool local) override;
  64. bool allowReadFromClipboard(bool default_value) override;
  65. bool allowWriteToClipboard(bool default_value) override;
  66. bool allowMutationEvents(bool default_value) override;
  67. void didNotAllowPlugins() override;
  68. void didNotAllowScript() override;
  69. void didUseKeygen() override;
  70. bool allowDisplayingInsecureContent(bool allowed_per_settings,
  71. const blink::WebURL& url) override;
  72. bool allowRunningInsecureContent(bool allowed_per_settings,
  73. const blink::WebSecurityOrigin& context,
  74. const blink::WebURL& url) override;
  75. private:
  76. FRIEND_TEST_ALL_PREFIXES(ContentSettingsObserverTest, WhitelistedSchemes);
  77. FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest,
  78. ContentSettingsInterstitialPages);
  79. FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest, PluginsTemporarilyAllowed);
  80. // RenderFrameObserver implementation.
  81. bool OnMessageReceived(const IPC::Message& message) override;
  82. void DidCommitProvisionalLoad(bool is_new_navigation,
  83. bool is_same_page_navigation) override;
  84. // Message handlers.
  85. void OnLoadBlockedPlugins(const std::string& identifier);
  86. void OnSetAsInterstitial();
  87. void OnSetAllowDisplayingInsecureContent(bool allow);
  88. void OnSetAllowRunningInsecureContent(bool allow);
  89. void OnReloadFrame();
  90. void OnRequestFileSystemAccessAsyncResponse(int request_id, bool allowed);
  91. // Resets the |content_blocked_| array.
  92. void ClearBlockedContentSettings();
  93. // Whether the observed RenderFrame is for a platform app.
  94. bool IsPlatformApp();
  95. #if defined(ENABLE_EXTENSIONS)
  96. // If |origin| corresponds to an installed extension, returns that extension.
  97. // Otherwise returns NULL.
  98. const extensions::Extension* GetExtension(
  99. const blink::WebSecurityOrigin& origin) const;
  100. #endif
  101. // Helpers.
  102. // True if |render_frame()| contains content that is white-listed for content
  103. // settings.
  104. bool IsWhitelistedForContentSettings() const;
  105. static bool IsWhitelistedForContentSettings(
  106. const blink::WebSecurityOrigin& origin,
  107. const GURL& document_url);
  108. #if defined(ENABLE_EXTENSIONS)
  109. // Owned by ChromeContentRendererClient and outlive us.
  110. extensions::Dispatcher* extension_dispatcher_;
  111. #endif
  112. // Insecure content may be permitted for the duration of this render view.
  113. bool allow_displaying_insecure_content_;
  114. bool allow_running_insecure_content_;
  115. // A pointer to content setting rules stored by the renderer. Normally, the
  116. // |RendererContentSettingRules| object is owned by
  117. // |ChromeRenderProcessObserver|. In the tests it is owned by the caller of
  118. // |SetContentSettingRules|.
  119. const RendererContentSettingRules* content_setting_rules_;
  120. // Stores if images, scripts, and plugins have actually been blocked.
  121. std::map<ContentSettingsType, bool> content_blocked_;
  122. // Caches the result of AllowStorage.
  123. typedef std::pair<GURL, bool> StoragePermissionsKey;
  124. std::map<StoragePermissionsKey, bool> cached_storage_permissions_;
  125. // Caches the result of AllowScript.
  126. std::map<blink::WebFrame*, bool> cached_script_permissions_;
  127. std::set<std::string> temporarily_allowed_plugins_;
  128. bool is_interstitial_page_;
  129. int current_request_id_;
  130. typedef std::map<int, blink::WebContentSettingCallbacks> PermissionRequestMap;
  131. PermissionRequestMap permission_requests_;
  132. // If true, IsWhitelistedForContentSettings will always return true.
  133. const bool should_whitelist_;
  134. DISALLOW_COPY_AND_ASSIGN(ContentSettingsObserver);
  135. };
  136. #endif // CHROME_RENDERER_CONTENT_SETTINGS_OBSERVER_H_