Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: chrome/renderer/content_settings_observer.cc

Issue 8498007: ContentSettingsObserver (+ related classes) cleanup. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removing struct ContentSettings, too. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/content_settings_observer.h" 5 #include "chrome/renderer/content_settings_observer.h"
6 6
7 #include "chrome/common/render_messages.h" 7 #include "chrome/common/render_messages.h"
8 #include "chrome/common/url_constants.h" 8 #include "chrome/common/url_constants.h"
9 #include "content/public/renderer/navigation_state.h" 9 #include "content/public/renderer/navigation_state.h"
10 #include "content/public/renderer/render_view.h" 10 #include "content/public/renderer/render_view.h"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 NOTREACHED(); 73 NOTREACHED();
74 return CONTENT_SETTING_DEFAULT; 74 return CONTENT_SETTING_DEFAULT;
75 } 75 }
76 76
77 } // namespace 77 } // namespace
78 78
79 ContentSettingsObserver::ContentSettingsObserver( 79 ContentSettingsObserver::ContentSettingsObserver(
80 content::RenderView* render_view) 80 content::RenderView* render_view)
81 : content::RenderViewObserver(render_view), 81 : content::RenderViewObserver(render_view),
82 content::RenderViewObserverTracker<ContentSettingsObserver>(render_view), 82 content::RenderViewObserverTracker<ContentSettingsObserver>(render_view),
83 default_content_settings_(NULL),
84 content_setting_rules_(NULL), 83 content_setting_rules_(NULL),
85 plugins_temporarily_allowed_(false) { 84 plugins_temporarily_allowed_(false) {
86 ClearBlockedContentSettings(); 85 ClearBlockedContentSettings();
87 } 86 }
88 87
89 ContentSettingsObserver::~ContentSettingsObserver() { 88 ContentSettingsObserver::~ContentSettingsObserver() {
90 } 89 }
91 90
92 void ContentSettingsObserver::SetContentSettings(
93 const ContentSettings& settings) {
94 current_content_settings_ = settings;
95 }
96
97 void ContentSettingsObserver::SetDefaultContentSettings(
98 const ContentSettings* settings) {
99 default_content_settings_ = settings;
100 }
101
102 void ContentSettingsObserver::SetContentSettingRules( 91 void ContentSettingsObserver::SetContentSettingRules(
103 const RendererContentSettingRules* content_setting_rules) { 92 const RendererContentSettingRules* content_setting_rules) {
104 content_setting_rules_ = content_setting_rules; 93 content_setting_rules_ = content_setting_rules;
105 } 94 }
106 95
107 ContentSetting ContentSettingsObserver::GetContentSetting(
108 ContentSettingsType type) {
109 // Don't call this for plug-ins.
110 DCHECK_NE(CONTENT_SETTINGS_TYPE_PLUGINS, type);
111 return current_content_settings_.settings[type];
112 }
113
114 void ContentSettingsObserver::DidBlockContentType( 96 void ContentSettingsObserver::DidBlockContentType(
115 ContentSettingsType settings_type, 97 ContentSettingsType settings_type,
116 const std::string& resource_identifier) { 98 const std::string& resource_identifier) {
117 // Always send a message when |resource_identifier| is not empty, to tell the 99 // Always send a message when |resource_identifier| is not empty, to tell the
118 // browser which resource was blocked (otherwise the browser will only show 100 // browser which resource was blocked (otherwise the browser will only show
119 // the first resource to be blocked, and none that are blocked at a later 101 // the first resource to be blocked, and none that are blocked at a later
120 // time). 102 // time).
121 if (!content_blocked_[settings_type] || !resource_identifier.empty()) { 103 if (!content_blocked_[settings_type] || !resource_identifier.empty()) {
122 content_blocked_[settings_type] = true; 104 content_blocked_[settings_type] = true;
123 Send(new ChromeViewHostMsg_ContentBlocked(routing_id(), settings_type, 105 Send(new ChromeViewHostMsg_ContentBlocked(routing_id(), settings_type,
124 resource_identifier)); 106 resource_identifier));
125 } 107 }
126 } 108 }
127 109
128 bool ContentSettingsObserver::OnMessageReceived(const IPC::Message& message) { 110 bool ContentSettingsObserver::OnMessageReceived(const IPC::Message& message) {
129 bool handled = true; 111 bool handled = true;
130 IPC_BEGIN_MESSAGE_MAP(ContentSettingsObserver, message) 112 IPC_BEGIN_MESSAGE_MAP(ContentSettingsObserver, message)
131 // Don't swallow LoadBlockedPlugins messages, as they're sent to every 113 // Don't swallow LoadBlockedPlugins messages, as they're sent to every
132 // blocked plugin. 114 // blocked plugin.
133 IPC_MESSAGE_HANDLER_GENERIC(ChromeViewMsg_LoadBlockedPlugins, 115 IPC_MESSAGE_HANDLER_GENERIC(ChromeViewMsg_LoadBlockedPlugins,
134 OnLoadBlockedPlugins(); handled = false) 116 OnLoadBlockedPlugins(); handled = false)
135 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetContentSettingsForLoadingURL,
136 OnSetContentSettingsForLoadingURL)
137 IPC_MESSAGE_UNHANDLED(handled = false) 117 IPC_MESSAGE_UNHANDLED(handled = false)
138 IPC_END_MESSAGE_MAP() 118 IPC_END_MESSAGE_MAP()
139 return handled; 119 return handled;
140 } 120 }
141 121
142 void ContentSettingsObserver::DidCommitProvisionalLoad( 122 void ContentSettingsObserver::DidCommitProvisionalLoad(
143 WebFrame* frame, bool is_new_navigation) { 123 WebFrame* frame, bool is_new_navigation) {
144 if (frame->parent()) 124 if (frame->parent())
145 return; // Not a top-level navigation. 125 return; // Not a top-level navigation.
146 126
147 NavigationState* state = NavigationState::FromDataSource(frame->dataSource()); 127 NavigationState* state = NavigationState::FromDataSource(frame->dataSource());
148 if (!state->was_within_same_page()) { 128 if (!state->was_within_same_page()) {
149 // Clear "block" flags for the new page. This needs to happen before any of 129 // Clear "block" flags for the new page. This needs to happen before any of
150 // |AllowScript()|, |AllowScriptFromSource()|, |AllowImage()|, or 130 // |AllowScript()|, |AllowScriptFromSource()|, |AllowImage()|, or
151 // |AllowPlugins()| is called for the new page so that these functions can 131 // |AllowPlugins()| is called for the new page so that these functions can
152 // correctly detect that a piece of content flipped from "not blocked" to 132 // correctly detect that a piece of content flipped from "not blocked" to
153 // "blocked". 133 // "blocked".
154 ClearBlockedContentSettings(); 134 ClearBlockedContentSettings();
155 plugins_temporarily_allowed_ = false; 135 plugins_temporarily_allowed_ = false;
156 } 136 }
157 137
158 GURL url = frame->document().url(); 138 GURL url = frame->document().url();
159
160 if (frame->document().securityOrigin().toString() == "null" &&
161 !url.SchemeIs(chrome::kFileScheme)) {
162 // The Frame has a unique security origin. Instead of granting the frame
163 // privileges based on it's URL, we fall back to the default content
164 // settings.
165
166 // We exempt file URLs here because we sandbox them by default, but folks
167 // might reasonably want to supply non-default content settings for various
168 // file URLs.
169 if (default_content_settings_)
170 SetContentSettings(*default_content_settings_);
171 return;
172 }
173
174 // If we start failing this DCHECK, please makes sure we don't regress 139 // If we start failing this DCHECK, please makes sure we don't regress
175 // this bug: http://code.google.com/p/chromium/issues/detail?id=79304 140 // this bug: http://code.google.com/p/chromium/issues/detail?id=79304
176 DCHECK(!url.SchemeIs(chrome::kDataScheme)); 141 DCHECK(frame->document().securityOrigin().toString() == "null" ||
177 142 !url.SchemeIs(chrome::kDataScheme));
178 // Set content settings. Default them from the parent window if one exists.
179 // This makes sure about:blank windows work as expected.
180 HostContentSettings::iterator host_content_settings =
181 host_content_settings_.find(url);
182 if (host_content_settings != host_content_settings_.end()) {
183 SetContentSettings(host_content_settings->second);
184
185 // These content settings were merely recorded transiently for this load.
186 // We can erase them now. If at some point we reload this page, the
187 // browser will send us new, up-to-date content settings.
188 host_content_settings_.erase(host_content_settings);
189 } else if (frame->opener()) {
190 // The opener's view is not guaranteed to be non-null (it could be
191 // detached from its page but not yet destructed).
192 if (WebView* opener_view = frame->opener()->view()) {
193 content::RenderView* opener =
194 content::RenderView::FromWebView(opener_view);
195 ContentSettingsObserver* observer = ContentSettingsObserver::Get(opener);
196 SetContentSettings(observer->current_content_settings_);
197 }
198 }
199 } 143 }
200 144
201 bool ContentSettingsObserver::AllowDatabase(WebFrame* frame, 145 bool ContentSettingsObserver::AllowDatabase(WebFrame* frame,
202 const WebString& name, 146 const WebString& name,
203 const WebString& display_name, 147 const WebString& display_name,
204 unsigned long estimated_size) { 148 unsigned long estimated_size) {
205 if (frame->document().securityOrigin().isEmpty() || 149 if (frame->document().securityOrigin().isEmpty() ||
206 frame->top()->document().securityOrigin().isEmpty()) 150 frame->top()->document().securityOrigin().isEmpty())
207 return false; // Uninitialized document. 151 return false; // Uninitialized document.
208 152
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 } 269 }
326 270
327 void ContentSettingsObserver::DidNotAllowPlugins(WebFrame* frame) { 271 void ContentSettingsObserver::DidNotAllowPlugins(WebFrame* frame) {
328 DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, std::string()); 272 DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, std::string());
329 } 273 }
330 274
331 void ContentSettingsObserver::DidNotAllowScript(WebFrame* frame) { 275 void ContentSettingsObserver::DidNotAllowScript(WebFrame* frame) {
332 DidBlockContentType(CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()); 276 DidBlockContentType(CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string());
333 } 277 }
334 278
335 void ContentSettingsObserver::OnSetContentSettingsForLoadingURL(
336 const GURL& url,
337 const ContentSettings& content_settings) {
338 host_content_settings_[url] = content_settings;
339 }
340
341 void ContentSettingsObserver::OnLoadBlockedPlugins() { 279 void ContentSettingsObserver::OnLoadBlockedPlugins() {
342 plugins_temporarily_allowed_ = true; 280 plugins_temporarily_allowed_ = true;
343 } 281 }
344 282
345 bool ContentSettingsObserver::AllowContentType(
346 ContentSettingsType settings_type) {
347 // CONTENT_SETTING_ASK is only valid for cookies.
348 return current_content_settings_.settings[settings_type] !=
349 CONTENT_SETTING_BLOCK;
350 }
351
352 void ContentSettingsObserver::ClearBlockedContentSettings() { 283 void ContentSettingsObserver::ClearBlockedContentSettings() {
353 for (size_t i = 0; i < arraysize(content_blocked_); ++i) 284 for (size_t i = 0; i < arraysize(content_blocked_); ++i)
354 content_blocked_[i] = false; 285 content_blocked_[i] = false;
355 cached_storage_permissions_.clear(); 286 cached_storage_permissions_.clear();
356 } 287 }
OLDNEW
« no previous file with comments | « chrome/renderer/content_settings_observer.h ('k') | chrome/renderer/content_settings_observer_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698