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 "xwalk/runtime/renderer/xwalk_render_process_observer_generic.h"
9 #include "content/renderer/render_thread_impl.h"
10 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
11 #include "ipc/ipc_message_macros.h"
12 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14 #include "xwalk/runtime/common/xwalk_common_messages.h"
19 struct AccessWhitelistItem {
21 const GURL& source, const GURL& dest, bool allow_subdomains);
24 bool allow_subdomains_;
27 AccessWhitelistItem::AccessWhitelistItem(
28 const GURL& source, const GURL& dest, bool allow_subdomains)
31 allow_subdomains_(allow_subdomains) {
34 std::vector<AccessWhitelistItem> access_whitelist;
36 void AddAccessWhiteListEntry(
37 const GURL& source, const GURL& dest, bool allow_subdomains) {
38 blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
40 blink::WebString::fromUTF8(dest.scheme()),
41 blink::WebString::fromUTF8(dest.HostNoBrackets()),
46 XWalkRenderProcessObserver::XWalkRenderProcessObserver()
47 : is_webkit_initialized_(false),
49 security_mode_(application::SecurityPolicy::NoSecurity) {
52 XWalkRenderProcessObserver::~XWalkRenderProcessObserver() {
55 bool XWalkRenderProcessObserver::OnControlMessageReceived(
56 const IPC::Message& message) {
58 IPC_BEGIN_MESSAGE_MAP(XWalkRenderProcessObserver, message)
59 IPC_MESSAGE_HANDLER(ViewMsg_SetAccessWhiteList, OnSetAccessWhiteList)
60 IPC_MESSAGE_HANDLER(ViewMsg_EnableSecurityMode, OnEnableSecurityMode)
61 IPC_MESSAGE_HANDLER(ViewMsg_SuspendJSEngine, OnSuspendJSEngine)
62 IPC_MESSAGE_UNHANDLED(handled = false)
67 void XWalkRenderProcessObserver::WebKitInitialized() {
68 is_webkit_initialized_ = true;
69 for (std::vector<AccessWhitelistItem>::iterator it = access_whitelist.begin();
70 it != access_whitelist.end(); ++it)
71 AddAccessWhiteListEntry(it->source_, it->dest_, it->allow_subdomains_);
73 access_whitelist.clear();
76 void XWalkRenderProcessObserver::OnRenderProcessShutdown() {
77 is_webkit_initialized_ = false;
80 void XWalkRenderProcessObserver::OnSetAccessWhiteList(const GURL& source,
82 bool allow_subdomains) {
83 if (is_webkit_initialized_)
84 AddAccessWhiteListEntry(source, dest, allow_subdomains);
86 access_whitelist.push_back(
87 AccessWhitelistItem(source, dest, allow_subdomains));
90 void XWalkRenderProcessObserver::OnEnableSecurityMode(
91 const GURL& url, application::SecurityPolicy::SecurityMode mode) {
93 security_mode_ = mode;
96 void XWalkRenderProcessObserver::OnSuspendJSEngine(bool is_suspend) {
97 if (is_suspend == is_suspended_)
99 content::RenderThreadImpl* thread = content::RenderThreadImpl::current();
100 thread->EnsureWebKitInitialized();
102 thread->webkit_platform_support()->SuspendSharedTimer();
104 thread->webkit_platform_support()->ResumeSharedTimer();
105 is_suspended_ = is_suspend;