Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / chrome / renderer / content_settings_observer.cc
blob9fb675ea8109a64270d31b6db12564fc7c5b1f82
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 #include "chrome/renderer/content_settings_observer.h"
7 #include "base/command_line.h"
8 #include "base/metrics/histogram.h"
9 #include "chrome/common/render_messages.h"
10 #include "content/public/common/url_constants.h"
11 #include "content/public/renderer/document_state.h"
12 #include "content/public/renderer/render_frame.h"
13 #include "content/public/renderer/render_view.h"
14 #include "third_party/WebKit/public/platform/WebPermissionCallbacks.h"
15 #include "third_party/WebKit/public/platform/WebURL.h"
16 #include "third_party/WebKit/public/web/WebDataSource.h"
17 #include "third_party/WebKit/public/web/WebDocument.h"
18 #include "third_party/WebKit/public/web/WebFrameClient.h"
19 #include "third_party/WebKit/public/web/WebLocalFrame.h"
20 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
21 #include "third_party/WebKit/public/web/WebView.h"
22 #include "url/url_constants.h"
24 #if defined(ENABLE_EXTENSIONS)
25 #include "chrome/common/extensions/chrome_extension_messages.h"
26 #include "extensions/common/constants.h"
27 #include "extensions/common/extension.h"
28 #include "extensions/common/permissions/api_permission.h"
29 #include "extensions/common/permissions/permissions_data.h"
30 #include "extensions/renderer/dispatcher.h"
31 #endif
33 using blink::WebDataSource;
34 using blink::WebDocument;
35 using blink::WebFrame;
36 using blink::WebPermissionCallbacks;
37 using blink::WebSecurityOrigin;
38 using blink::WebString;
39 using blink::WebURL;
40 using blink::WebView;
41 using content::DocumentState;
42 using content::NavigationState;
44 namespace {
46 enum {
47 INSECURE_CONTENT_DISPLAY = 0,
48 INSECURE_CONTENT_DISPLAY_HOST_GOOGLE,
49 INSECURE_CONTENT_DISPLAY_HOST_WWW_GOOGLE,
50 INSECURE_CONTENT_DISPLAY_HTML,
51 INSECURE_CONTENT_RUN,
52 INSECURE_CONTENT_RUN_HOST_GOOGLE,
53 INSECURE_CONTENT_RUN_HOST_WWW_GOOGLE,
54 INSECURE_CONTENT_RUN_TARGET_YOUTUBE,
55 INSECURE_CONTENT_RUN_JS,
56 INSECURE_CONTENT_RUN_CSS,
57 INSECURE_CONTENT_RUN_SWF,
58 INSECURE_CONTENT_DISPLAY_HOST_YOUTUBE,
59 INSECURE_CONTENT_RUN_HOST_YOUTUBE,
60 INSECURE_CONTENT_RUN_HOST_GOOGLEUSERCONTENT,
61 INSECURE_CONTENT_DISPLAY_HOST_MAIL_GOOGLE,
62 INSECURE_CONTENT_RUN_HOST_MAIL_GOOGLE,
63 INSECURE_CONTENT_DISPLAY_HOST_PLUS_GOOGLE,
64 INSECURE_CONTENT_RUN_HOST_PLUS_GOOGLE,
65 INSECURE_CONTENT_DISPLAY_HOST_DOCS_GOOGLE,
66 INSECURE_CONTENT_RUN_HOST_DOCS_GOOGLE,
67 INSECURE_CONTENT_DISPLAY_HOST_SITES_GOOGLE,
68 INSECURE_CONTENT_RUN_HOST_SITES_GOOGLE,
69 INSECURE_CONTENT_DISPLAY_HOST_PICASAWEB_GOOGLE,
70 INSECURE_CONTENT_RUN_HOST_PICASAWEB_GOOGLE,
71 INSECURE_CONTENT_DISPLAY_HOST_GOOGLE_READER,
72 INSECURE_CONTENT_RUN_HOST_GOOGLE_READER,
73 INSECURE_CONTENT_DISPLAY_HOST_CODE_GOOGLE,
74 INSECURE_CONTENT_RUN_HOST_CODE_GOOGLE,
75 INSECURE_CONTENT_DISPLAY_HOST_GROUPS_GOOGLE,
76 INSECURE_CONTENT_RUN_HOST_GROUPS_GOOGLE,
77 INSECURE_CONTENT_DISPLAY_HOST_MAPS_GOOGLE,
78 INSECURE_CONTENT_RUN_HOST_MAPS_GOOGLE,
79 INSECURE_CONTENT_DISPLAY_HOST_GOOGLE_SUPPORT,
80 INSECURE_CONTENT_RUN_HOST_GOOGLE_SUPPORT,
81 INSECURE_CONTENT_DISPLAY_HOST_GOOGLE_INTL,
82 INSECURE_CONTENT_RUN_HOST_GOOGLE_INTL,
83 INSECURE_CONTENT_NUM_EVENTS
86 // Constants for UMA statistic collection.
87 static const char kWWWDotGoogleDotCom[] = "www.google.com";
88 static const char kMailDotGoogleDotCom[] = "mail.google.com";
89 static const char kPlusDotGoogleDotCom[] = "plus.google.com";
90 static const char kDocsDotGoogleDotCom[] = "docs.google.com";
91 static const char kSitesDotGoogleDotCom[] = "sites.google.com";
92 static const char kPicasawebDotGoogleDotCom[] = "picasaweb.google.com";
93 static const char kCodeDotGoogleDotCom[] = "code.google.com";
94 static const char kGroupsDotGoogleDotCom[] = "groups.google.com";
95 static const char kMapsDotGoogleDotCom[] = "maps.google.com";
96 static const char kWWWDotYoutubeDotCom[] = "www.youtube.com";
97 static const char kDotGoogleUserContentDotCom[] = ".googleusercontent.com";
98 static const char kGoogleReaderPathPrefix[] = "/reader/";
99 static const char kGoogleSupportPathPrefix[] = "/support/";
100 static const char kGoogleIntlPathPrefix[] = "/intl/";
101 static const char kDotJS[] = ".js";
102 static const char kDotCSS[] = ".css";
103 static const char kDotSWF[] = ".swf";
104 static const char kDotHTML[] = ".html";
106 // Constants for mixed-content blocking.
107 static const char kGoogleDotCom[] = "google.com";
109 static bool IsHostInDomain(const std::string& host, const std::string& domain) {
110 return (EndsWith(host, domain, false) &&
111 (host.length() == domain.length() ||
112 (host.length() > domain.length() &&
113 host[host.length() - domain.length() - 1] == '.')));
116 GURL GetOriginOrURL(const WebFrame* frame) {
117 WebString top_origin = frame->top()->securityOrigin().toString();
118 // The |top_origin| is unique ("null") e.g., for file:// URLs. Use the
119 // document URL as the primary URL in those cases.
120 // TODO(alexmos): This is broken for --site-per-process, since top() can be a
121 // WebRemoteFrame which does not have a document(), and the WebRemoteFrame's
122 // URL is not replicated.
123 if (top_origin == "null")
124 return frame->top()->document().url();
125 return GURL(top_origin);
128 ContentSetting GetContentSettingFromRules(
129 const ContentSettingsForOneType& rules,
130 const WebFrame* frame,
131 const GURL& secondary_url) {
132 ContentSettingsForOneType::const_iterator it;
133 // If there is only one rule, it's the default rule and we don't need to match
134 // the patterns.
135 if (rules.size() == 1) {
136 DCHECK(rules[0].primary_pattern == ContentSettingsPattern::Wildcard());
137 DCHECK(rules[0].secondary_pattern == ContentSettingsPattern::Wildcard());
138 return rules[0].setting;
140 const GURL& primary_url = GetOriginOrURL(frame);
141 for (it = rules.begin(); it != rules.end(); ++it) {
142 if (it->primary_pattern.Matches(primary_url) &&
143 it->secondary_pattern.Matches(secondary_url)) {
144 return it->setting;
147 NOTREACHED();
148 return CONTENT_SETTING_DEFAULT;
151 } // namespace
153 ContentSettingsObserver::ContentSettingsObserver(
154 content::RenderFrame* render_frame,
155 extensions::Dispatcher* extension_dispatcher,
156 bool should_whitelist)
157 : content::RenderFrameObserver(render_frame),
158 content::RenderFrameObserverTracker<ContentSettingsObserver>(
159 render_frame),
160 #if defined(ENABLE_EXTENSIONS)
161 extension_dispatcher_(extension_dispatcher),
162 #endif
163 allow_displaying_insecure_content_(false),
164 allow_running_insecure_content_(false),
165 content_setting_rules_(NULL),
166 is_interstitial_page_(false),
167 npapi_plugins_blocked_(false),
168 current_request_id_(0),
169 should_whitelist_(should_whitelist) {
170 ClearBlockedContentSettings();
171 render_frame->GetWebFrame()->setContentSettingsClient(this);
173 if (render_frame->GetRenderView()->GetMainRenderFrame() != render_frame) {
174 // Copy all the settings from the main render frame to avoid race conditions
175 // when initializing this data. See http://crbug.com/333308.
176 ContentSettingsObserver* parent = ContentSettingsObserver::Get(
177 render_frame->GetRenderView()->GetMainRenderFrame());
178 allow_displaying_insecure_content_ =
179 parent->allow_displaying_insecure_content_;
180 allow_running_insecure_content_ = parent->allow_running_insecure_content_;
181 temporarily_allowed_plugins_ = parent->temporarily_allowed_plugins_;
182 is_interstitial_page_ = parent->is_interstitial_page_;
183 npapi_plugins_blocked_ = parent->npapi_plugins_blocked_;
187 ContentSettingsObserver::~ContentSettingsObserver() {
190 void ContentSettingsObserver::SetContentSettingRules(
191 const RendererContentSettingRules* content_setting_rules) {
192 content_setting_rules_ = content_setting_rules;
195 bool ContentSettingsObserver::IsPluginTemporarilyAllowed(
196 const std::string& identifier) {
197 // If the empty string is in here, it means all plug-ins are allowed.
198 // TODO(bauerb): Remove this once we only pass in explicit identifiers.
199 return (temporarily_allowed_plugins_.find(identifier) !=
200 temporarily_allowed_plugins_.end()) ||
201 (temporarily_allowed_plugins_.find(std::string()) !=
202 temporarily_allowed_plugins_.end());
205 void ContentSettingsObserver::DidBlockContentType(
206 ContentSettingsType settings_type) {
207 DidBlockContentType(settings_type, base::string16());
210 void ContentSettingsObserver::DidBlockContentType(
211 ContentSettingsType settings_type,
212 const base::string16& details) {
213 // Send multiple ContentBlocked messages if details are provided.
214 if (!content_blocked_[settings_type] || !details.empty()) {
215 content_blocked_[settings_type] = true;
216 Send(new ChromeViewHostMsg_ContentBlocked(routing_id(), settings_type,
217 details));
221 bool ContentSettingsObserver::OnMessageReceived(const IPC::Message& message) {
222 bool handled = true;
223 IPC_BEGIN_MESSAGE_MAP(ContentSettingsObserver, message)
224 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAsInterstitial, OnSetAsInterstitial)
225 IPC_MESSAGE_HANDLER(ChromeViewMsg_NPAPINotSupported, OnNPAPINotSupported)
226 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowDisplayingInsecureContent,
227 OnSetAllowDisplayingInsecureContent)
228 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAllowRunningInsecureContent,
229 OnSetAllowRunningInsecureContent)
230 IPC_MESSAGE_HANDLER(ChromeViewMsg_ReloadFrame, OnReloadFrame);
231 IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestFileSystemAccessAsyncResponse,
232 OnRequestFileSystemAccessAsyncResponse)
233 IPC_MESSAGE_UNHANDLED(handled = false)
234 IPC_END_MESSAGE_MAP()
235 if (handled)
236 return true;
238 // Don't swallow LoadBlockedPlugins messages, as they're sent to every
239 // blocked plugin.
240 IPC_BEGIN_MESSAGE_MAP(ContentSettingsObserver, message)
241 IPC_MESSAGE_HANDLER(ChromeViewMsg_LoadBlockedPlugins, OnLoadBlockedPlugins)
242 IPC_END_MESSAGE_MAP()
244 return false;
247 void ContentSettingsObserver::DidCommitProvisionalLoad(
248 bool is_new_navigation,
249 bool is_same_page_navigation) {
250 WebFrame* frame = render_frame()->GetWebFrame();
251 if (frame->parent())
252 return; // Not a top-level navigation.
254 if (!is_same_page_navigation) {
255 // Clear "block" flags for the new page. This needs to happen before any of
256 // |allowScript()|, |allowScriptFromSource()|, |allowImage()|, or
257 // |allowPlugins()| is called for the new page so that these functions can
258 // correctly detect that a piece of content flipped from "not blocked" to
259 // "blocked".
260 ClearBlockedContentSettings();
261 temporarily_allowed_plugins_.clear();
264 GURL url = frame->document().url();
265 // If we start failing this DCHECK, please makes sure we don't regress
266 // this bug: http://code.google.com/p/chromium/issues/detail?id=79304
267 DCHECK(frame->document().securityOrigin().toString() == "null" ||
268 !url.SchemeIs(url::kDataScheme));
271 bool ContentSettingsObserver::allowDatabase(const WebString& name,
272 const WebString& display_name,
273 unsigned long estimated_size) {
274 WebFrame* frame = render_frame()->GetWebFrame();
275 if (frame->securityOrigin().isUnique() ||
276 frame->top()->securityOrigin().isUnique())
277 return false;
279 bool result = false;
280 Send(new ChromeViewHostMsg_AllowDatabase(
281 routing_id(), GURL(frame->securityOrigin().toString()),
282 GURL(frame->top()->securityOrigin().toString()), name, display_name,
283 &result));
284 return result;
287 void ContentSettingsObserver::requestFileSystemAccessAsync(
288 const WebPermissionCallbacks& callbacks) {
289 WebFrame* frame = render_frame()->GetWebFrame();
290 if (frame->securityOrigin().isUnique() ||
291 frame->top()->securityOrigin().isUnique()) {
292 WebPermissionCallbacks permissionCallbacks(callbacks);
293 permissionCallbacks.doDeny();
294 return;
296 ++current_request_id_;
297 std::pair<PermissionRequestMap::iterator, bool> insert_result =
298 permission_requests_.insert(
299 std::make_pair(current_request_id_, callbacks));
301 // Verify there are no duplicate insertions.
302 DCHECK(insert_result.second);
304 Send(new ChromeViewHostMsg_RequestFileSystemAccessAsync(
305 routing_id(), current_request_id_,
306 GURL(frame->securityOrigin().toString()),
307 GURL(frame->top()->securityOrigin().toString())));
310 bool ContentSettingsObserver::allowImage(bool enabled_per_settings,
311 const WebURL& image_url) {
312 bool allow = enabled_per_settings;
313 if (enabled_per_settings) {
314 if (is_interstitial_page_)
315 return true;
317 if (IsWhitelistedForContentSettings())
318 return true;
320 if (content_setting_rules_) {
321 GURL secondary_url(image_url);
322 allow =
323 GetContentSettingFromRules(content_setting_rules_->image_rules,
324 render_frame()->GetWebFrame(),
325 secondary_url) != CONTENT_SETTING_BLOCK;
328 if (!allow)
329 DidBlockContentType(CONTENT_SETTINGS_TYPE_IMAGES);
330 return allow;
333 bool ContentSettingsObserver::allowIndexedDB(const WebString& name,
334 const WebSecurityOrigin& origin) {
335 WebFrame* frame = render_frame()->GetWebFrame();
336 if (frame->securityOrigin().isUnique() ||
337 frame->top()->securityOrigin().isUnique())
338 return false;
340 bool result = false;
341 Send(new ChromeViewHostMsg_AllowIndexedDB(
342 routing_id(), GURL(frame->securityOrigin().toString()),
343 GURL(frame->top()->securityOrigin().toString()), name, &result));
344 return result;
347 bool ContentSettingsObserver::allowPlugins(bool enabled_per_settings) {
348 return enabled_per_settings;
351 bool ContentSettingsObserver::allowScript(bool enabled_per_settings) {
352 if (!enabled_per_settings)
353 return false;
354 if (is_interstitial_page_)
355 return true;
357 WebFrame* frame = render_frame()->GetWebFrame();
358 std::map<WebFrame*, bool>::const_iterator it =
359 cached_script_permissions_.find(frame);
360 if (it != cached_script_permissions_.end())
361 return it->second;
363 // Evaluate the content setting rules before
364 // |IsWhitelistedForContentSettings|; if there is only the default rule
365 // allowing all scripts, it's quicker this way.
366 bool allow = true;
367 if (content_setting_rules_) {
368 ContentSetting setting = GetContentSettingFromRules(
369 content_setting_rules_->script_rules,
370 frame,
371 GURL(frame->document().securityOrigin().toString()));
372 allow = setting != CONTENT_SETTING_BLOCK;
374 allow = allow || IsWhitelistedForContentSettings();
376 cached_script_permissions_[frame] = allow;
377 return allow;
380 bool ContentSettingsObserver::allowScriptFromSource(
381 bool enabled_per_settings,
382 const blink::WebURL& script_url) {
383 if (!enabled_per_settings)
384 return false;
385 if (is_interstitial_page_)
386 return true;
388 bool allow = true;
389 if (content_setting_rules_) {
390 ContentSetting setting =
391 GetContentSettingFromRules(content_setting_rules_->script_rules,
392 render_frame()->GetWebFrame(),
393 GURL(script_url));
394 allow = setting != CONTENT_SETTING_BLOCK;
396 return allow || IsWhitelistedForContentSettings();
399 bool ContentSettingsObserver::allowStorage(bool local) {
400 WebFrame* frame = render_frame()->GetWebFrame();
401 if (frame->securityOrigin().isUnique() ||
402 frame->top()->securityOrigin().isUnique())
403 return false;
404 bool result = false;
406 StoragePermissionsKey key(
407 GURL(frame->document().securityOrigin().toString()), local);
408 std::map<StoragePermissionsKey, bool>::const_iterator permissions =
409 cached_storage_permissions_.find(key);
410 if (permissions != cached_storage_permissions_.end())
411 return permissions->second;
413 Send(new ChromeViewHostMsg_AllowDOMStorage(
414 routing_id(), GURL(frame->securityOrigin().toString()),
415 GURL(frame->top()->securityOrigin().toString()), local, &result));
416 cached_storage_permissions_[key] = result;
417 return result;
420 bool ContentSettingsObserver::allowReadFromClipboard(bool default_value) {
421 bool allowed = default_value;
422 #if defined(ENABLE_EXTENSIONS)
423 extensions::ScriptContext* calling_context =
424 extension_dispatcher_->script_context_set().GetCalling();
425 if (calling_context) {
426 allowed |= calling_context->HasAPIPermission(
427 extensions::APIPermission::kClipboardRead);
429 #endif
430 return allowed;
433 bool ContentSettingsObserver::allowWriteToClipboard(bool default_value) {
434 bool allowed = default_value;
435 #if defined(ENABLE_EXTENSIONS)
436 // All blessed extension pages could historically write to the clipboard, so
437 // preserve that for compatibility.
438 extensions::ScriptContext* calling_context =
439 extension_dispatcher_->script_context_set().GetCalling();
440 if (calling_context) {
441 if (calling_context->effective_context_type() ==
442 extensions::Feature::BLESSED_EXTENSION_CONTEXT) {
443 allowed = true;
444 } else {
445 allowed |= calling_context->HasAPIPermission(
446 extensions::APIPermission::kClipboardWrite);
449 #endif
450 return allowed;
453 bool ContentSettingsObserver::allowMutationEvents(bool default_value) {
454 return IsPlatformApp() ? false : default_value;
457 bool ContentSettingsObserver::allowPushState() {
458 return !IsPlatformApp();
461 static void SendInsecureContentSignal(int signal) {
462 UMA_HISTOGRAM_ENUMERATION("SSL.InsecureContent", signal,
463 INSECURE_CONTENT_NUM_EVENTS);
466 bool ContentSettingsObserver::allowDisplayingInsecureContent(
467 bool allowed_per_settings,
468 const blink::WebSecurityOrigin& origin,
469 const blink::WebURL& resource_url) {
470 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY);
472 std::string origin_host(origin.host().utf8());
473 WebFrame* frame = render_frame()->GetWebFrame();
474 GURL frame_gurl(frame->document().url());
475 if (IsHostInDomain(origin_host, kGoogleDotCom)) {
476 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_GOOGLE);
477 if (StartsWithASCII(frame_gurl.path(), kGoogleSupportPathPrefix, false)) {
478 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_GOOGLE_SUPPORT);
479 } else if (StartsWithASCII(frame_gurl.path(),
480 kGoogleIntlPathPrefix,
481 false)) {
482 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_GOOGLE_INTL);
486 if (origin_host == kWWWDotGoogleDotCom) {
487 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_WWW_GOOGLE);
488 if (StartsWithASCII(frame_gurl.path(), kGoogleReaderPathPrefix, false))
489 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_GOOGLE_READER);
490 } else if (origin_host == kMailDotGoogleDotCom) {
491 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_MAIL_GOOGLE);
492 } else if (origin_host == kPlusDotGoogleDotCom) {
493 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_PLUS_GOOGLE);
494 } else if (origin_host == kDocsDotGoogleDotCom) {
495 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_DOCS_GOOGLE);
496 } else if (origin_host == kSitesDotGoogleDotCom) {
497 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_SITES_GOOGLE);
498 } else if (origin_host == kPicasawebDotGoogleDotCom) {
499 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_PICASAWEB_GOOGLE);
500 } else if (origin_host == kCodeDotGoogleDotCom) {
501 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_CODE_GOOGLE);
502 } else if (origin_host == kGroupsDotGoogleDotCom) {
503 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_GROUPS_GOOGLE);
504 } else if (origin_host == kMapsDotGoogleDotCom) {
505 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_MAPS_GOOGLE);
506 } else if (origin_host == kWWWDotYoutubeDotCom) {
507 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HOST_YOUTUBE);
510 GURL resource_gurl(resource_url);
511 if (EndsWith(resource_gurl.path(), kDotHTML, false))
512 SendInsecureContentSignal(INSECURE_CONTENT_DISPLAY_HTML);
514 if (allowed_per_settings || allow_displaying_insecure_content_)
515 return true;
517 Send(new ChromeViewHostMsg_DidBlockDisplayingInsecureContent(routing_id()));
519 return false;
522 bool ContentSettingsObserver::allowRunningInsecureContent(
523 bool allowed_per_settings,
524 const blink::WebSecurityOrigin& origin,
525 const blink::WebURL& resource_url) {
526 std::string origin_host(origin.host().utf8());
527 WebFrame* frame = render_frame()->GetWebFrame();
528 GURL frame_gurl(frame->document().url());
529 DCHECK_EQ(frame_gurl.host(), origin_host);
531 bool is_google = IsHostInDomain(origin_host, kGoogleDotCom);
532 if (is_google) {
533 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_GOOGLE);
534 if (StartsWithASCII(frame_gurl.path(), kGoogleSupportPathPrefix, false)) {
535 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_GOOGLE_SUPPORT);
536 } else if (StartsWithASCII(frame_gurl.path(),
537 kGoogleIntlPathPrefix,
538 false)) {
539 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_GOOGLE_INTL);
543 if (origin_host == kWWWDotGoogleDotCom) {
544 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_WWW_GOOGLE);
545 if (StartsWithASCII(frame_gurl.path(), kGoogleReaderPathPrefix, false))
546 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_GOOGLE_READER);
547 } else if (origin_host == kMailDotGoogleDotCom) {
548 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_MAIL_GOOGLE);
549 } else if (origin_host == kPlusDotGoogleDotCom) {
550 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_PLUS_GOOGLE);
551 } else if (origin_host == kDocsDotGoogleDotCom) {
552 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_DOCS_GOOGLE);
553 } else if (origin_host == kSitesDotGoogleDotCom) {
554 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_SITES_GOOGLE);
555 } else if (origin_host == kPicasawebDotGoogleDotCom) {
556 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_PICASAWEB_GOOGLE);
557 } else if (origin_host == kCodeDotGoogleDotCom) {
558 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_CODE_GOOGLE);
559 } else if (origin_host == kGroupsDotGoogleDotCom) {
560 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_GROUPS_GOOGLE);
561 } else if (origin_host == kMapsDotGoogleDotCom) {
562 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_MAPS_GOOGLE);
563 } else if (origin_host == kWWWDotYoutubeDotCom) {
564 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_YOUTUBE);
565 } else if (EndsWith(origin_host, kDotGoogleUserContentDotCom, false)) {
566 SendInsecureContentSignal(INSECURE_CONTENT_RUN_HOST_GOOGLEUSERCONTENT);
569 GURL resource_gurl(resource_url);
570 if (resource_gurl.host() == kWWWDotYoutubeDotCom)
571 SendInsecureContentSignal(INSECURE_CONTENT_RUN_TARGET_YOUTUBE);
573 if (EndsWith(resource_gurl.path(), kDotJS, false))
574 SendInsecureContentSignal(INSECURE_CONTENT_RUN_JS);
575 else if (EndsWith(resource_gurl.path(), kDotCSS, false))
576 SendInsecureContentSignal(INSECURE_CONTENT_RUN_CSS);
577 else if (EndsWith(resource_gurl.path(), kDotSWF, false))
578 SendInsecureContentSignal(INSECURE_CONTENT_RUN_SWF);
580 if (!allow_running_insecure_content_ && !allowed_per_settings) {
581 DidBlockContentType(CONTENT_SETTINGS_TYPE_MIXEDSCRIPT, origin.host());
582 return false;
585 return true;
588 void ContentSettingsObserver::didNotAllowPlugins() {
589 DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS);
592 void ContentSettingsObserver::didNotAllowScript() {
593 DidBlockContentType(CONTENT_SETTINGS_TYPE_JAVASCRIPT);
596 bool ContentSettingsObserver::AreNPAPIPluginsBlocked() const {
597 return npapi_plugins_blocked_;
600 void ContentSettingsObserver::OnLoadBlockedPlugins(
601 const std::string& identifier) {
602 temporarily_allowed_plugins_.insert(identifier);
605 void ContentSettingsObserver::OnSetAsInterstitial() {
606 is_interstitial_page_ = true;
609 void ContentSettingsObserver::OnNPAPINotSupported() {
610 npapi_plugins_blocked_ = true;
613 void ContentSettingsObserver::OnSetAllowDisplayingInsecureContent(bool allow) {
614 allow_displaying_insecure_content_ = allow;
617 void ContentSettingsObserver::OnSetAllowRunningInsecureContent(bool allow) {
618 allow_running_insecure_content_ = allow;
619 OnSetAllowDisplayingInsecureContent(allow);
622 void ContentSettingsObserver::OnReloadFrame() {
623 DCHECK(!render_frame()->GetWebFrame()->parent()) <<
624 "Should only be called on the main frame";
625 render_frame()->GetWebFrame()->reload();
628 void ContentSettingsObserver::OnRequestFileSystemAccessAsyncResponse(
629 int request_id,
630 bool allowed) {
631 PermissionRequestMap::iterator it = permission_requests_.find(request_id);
632 if (it == permission_requests_.end())
633 return;
635 WebPermissionCallbacks callbacks = it->second;
636 permission_requests_.erase(it);
638 if (allowed) {
639 callbacks.doAllow();
640 return;
642 callbacks.doDeny();
645 void ContentSettingsObserver::ClearBlockedContentSettings() {
646 for (size_t i = 0; i < arraysize(content_blocked_); ++i)
647 content_blocked_[i] = false;
648 cached_storage_permissions_.clear();
649 cached_script_permissions_.clear();
652 bool ContentSettingsObserver::IsPlatformApp() {
653 #if defined(ENABLE_EXTENSIONS)
654 WebFrame* frame = render_frame()->GetWebFrame();
655 WebSecurityOrigin origin = frame->document().securityOrigin();
656 const extensions::Extension* extension = GetExtension(origin);
657 return extension && extension->is_platform_app();
658 #else
659 return false;
660 #endif
663 #if defined(ENABLE_EXTENSIONS)
664 const extensions::Extension* ContentSettingsObserver::GetExtension(
665 const WebSecurityOrigin& origin) const {
666 if (!EqualsASCII(origin.protocol(), extensions::kExtensionScheme))
667 return NULL;
669 const std::string extension_id = origin.host().utf8().data();
670 if (!extension_dispatcher_->IsExtensionActive(extension_id))
671 return NULL;
673 return extension_dispatcher_->extensions()->GetByID(extension_id);
675 #endif
677 bool ContentSettingsObserver::IsWhitelistedForContentSettings() const {
678 if (should_whitelist_)
679 return true;
681 // Whitelist ftp directory listings, as they require JavaScript to function
682 // properly.
683 if (render_frame()->IsFTPDirectoryListing())
684 return true;
686 WebFrame* web_frame = render_frame()->GetWebFrame();
687 return IsWhitelistedForContentSettings(web_frame->document().securityOrigin(),
688 web_frame->document().url());
691 bool ContentSettingsObserver::IsWhitelistedForContentSettings(
692 const WebSecurityOrigin& origin,
693 const GURL& document_url) {
694 if (document_url == GURL(content::kUnreachableWebDataURL))
695 return true;
697 if (origin.isUnique())
698 return false; // Uninitialized document?
700 if (EqualsASCII(origin.protocol(), content::kChromeUIScheme))
701 return true; // Browser UI elements should still work.
703 if (EqualsASCII(origin.protocol(), content::kChromeDevToolsScheme))
704 return true; // DevTools UI elements should still work.
706 #if defined(ENABLE_EXTENSIONS)
707 if (EqualsASCII(origin.protocol(), extensions::kExtensionScheme))
708 return true;
709 #endif
711 // TODO(creis, fsamuel): Remove this once the concept of swapped out
712 // RenderFrames goes away.
713 if (document_url == GURL(content::kSwappedOutURL))
714 return true;
716 // If the scheme is file:, an empty file name indicates a directory listing,
717 // which requires JavaScript to function properly.
718 if (EqualsASCII(origin.protocol(), url::kFileScheme)) {
719 return document_url.SchemeIs(url::kFileScheme) &&
720 document_url.ExtractFileName().empty();
723 return false;