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/public/renderer/render_thread.h"
10 #include "ipc/ipc_message_macros.h"
11 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
13 #include "xwalk/runtime/common/xwalk_common_messages.h"
18 struct AccessWhitelistItem {
20 const GURL& source, const GURL& dest, bool allow_subdomains);
23 bool allow_subdomains_;
26 AccessWhitelistItem::AccessWhitelistItem(
27 const GURL& source, const GURL& dest, bool allow_subdomains)
30 allow_subdomains_(allow_subdomains) {
33 std::vector<AccessWhitelistItem> access_whitelist;
35 void AddAccessWhiteListEntry(
36 const GURL& source, const GURL& dest, bool allow_subdomains) {
37 blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
39 blink::WebString::fromUTF8(dest.scheme()),
40 blink::WebString::fromUTF8(dest.HostNoBrackets()),
45 XWalkRenderProcessObserver::XWalkRenderProcessObserver()
46 : is_webkit_initialized_(false),
47 security_mode_(application::SecurityPolicy::NoSecurity) {
50 XWalkRenderProcessObserver::~XWalkRenderProcessObserver() {
53 bool XWalkRenderProcessObserver::OnControlMessageReceived(
54 const IPC::Message& message) {
56 IPC_BEGIN_MESSAGE_MAP(XWalkRenderProcessObserver, message)
57 IPC_MESSAGE_HANDLER(ViewMsg_SetAccessWhiteList, OnSetAccessWhiteList)
58 IPC_MESSAGE_HANDLER(ViewMsg_EnableSecurityMode, OnEnableSecurityMode)
59 IPC_MESSAGE_UNHANDLED(handled = false)
64 void XWalkRenderProcessObserver::WebKitInitialized() {
65 is_webkit_initialized_ = true;
68 void XWalkRenderProcessObserver::OnRenderProcessShutdown() {
69 is_webkit_initialized_ = false;
72 void XWalkRenderProcessObserver::DidCreateScriptContext(
73 blink::WebFrame* frame, v8::Handle<v8::Context> context,
74 int extension_group, int world_id) {
75 for (std::vector<AccessWhitelistItem>::iterator it = access_whitelist.begin();
76 it != access_whitelist.end(); ++it)
77 AddAccessWhiteListEntry(it->source_, it->dest_, it->allow_subdomains_);
79 access_whitelist.clear();
82 void XWalkRenderProcessObserver::OnSetAccessWhiteList(const GURL& source,
84 bool allow_subdomains) {
85 if (is_webkit_initialized_)
86 AddAccessWhiteListEntry(source, dest, allow_subdomains);
88 access_whitelist.push_back(
89 AccessWhitelistItem(source, dest, allow_subdomains));
92 void XWalkRenderProcessObserver::OnEnableSecurityMode(
93 const GURL& url, application::SecurityPolicy::SecurityMode mode) {
95 security_mode_ = mode;