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 "ipc/ipc_message_macros.h"
10 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "xwalk/runtime/common/xwalk_common_messages.h"
17 struct AccessWhitelistItem {
19 const GURL& source, const GURL& dest, bool allow_subdomains);
22 bool allow_subdomains_;
25 AccessWhitelistItem::AccessWhitelistItem(
26 const GURL& source, const GURL& dest, bool allow_subdomains)
29 allow_subdomains_(allow_subdomains) {
32 std::vector<AccessWhitelistItem> access_whitelist;
34 void AddAccessWhiteListEntry(
35 const GURL& source, const GURL& dest, bool allow_subdomains) {
36 blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
38 blink::WebString::fromUTF8(dest.scheme()),
39 blink::WebString::fromUTF8(dest.HostNoBrackets()),
44 XWalkRenderProcessObserver::XWalkRenderProcessObserver()
45 : is_webkit_initialized_(false),
46 is_warp_mode_(false) {
49 XWalkRenderProcessObserver::~XWalkRenderProcessObserver() {
52 bool XWalkRenderProcessObserver::OnControlMessageReceived(
53 const IPC::Message& message) {
55 IPC_BEGIN_MESSAGE_MAP(XWalkRenderProcessObserver, message)
56 IPC_MESSAGE_HANDLER(ViewMsg_SetAccessWhiteList, OnSetAccessWhiteList)
57 IPC_MESSAGE_HANDLER(ViewMsg_EnableWarpMode, OnEnableWarpMode)
58 IPC_MESSAGE_UNHANDLED(handled = false)
63 void XWalkRenderProcessObserver::WebKitInitialized() {
64 is_webkit_initialized_ = true;
67 void XWalkRenderProcessObserver::OnRenderProcessShutdown() {
68 is_webkit_initialized_ = false;
71 void XWalkRenderProcessObserver::DidCreateScriptContext(
72 blink::WebFrame* frame, v8::Handle<v8::Context> context,
73 int extension_group, int world_id) {
74 for (std::vector<AccessWhitelistItem>::iterator it = access_whitelist.begin();
75 it != access_whitelist.end(); ++it)
76 AddAccessWhiteListEntry(it->source_, it->dest_, it->allow_subdomains_);
78 access_whitelist.clear();
81 void XWalkRenderProcessObserver::OnSetAccessWhiteList(const GURL& source,
83 bool allow_subdomains) {
84 if (is_webkit_initialized_)
85 AddAccessWhiteListEntry(source, dest, allow_subdomains);
87 access_whitelist.push_back(
88 AccessWhitelistItem(source, dest, allow_subdomains));
91 void XWalkRenderProcessObserver::OnEnableWarpMode() {