Upstream version 6.34.113.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / renderer / xwalk_render_process_observer_generic.cc
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.
4
5 #include "xwalk/runtime/renderer/xwalk_render_process_observer_generic.h"
6
7 #include <vector>
8
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"
13
14
15 namespace xwalk {
16 namespace {
17 struct AccessWhitelistItem {
18   AccessWhitelistItem(
19       const GURL& source, const GURL& dest, bool allow_subdomains);
20   GURL source_;
21   GURL dest_;
22   bool allow_subdomains_;
23 };
24
25 AccessWhitelistItem::AccessWhitelistItem(
26     const GURL& source, const GURL& dest, bool allow_subdomains)
27     : source_(source),
28       dest_(dest),
29       allow_subdomains_(allow_subdomains) {
30 }
31
32 std::vector<AccessWhitelistItem> access_whitelist;
33
34 void AddAccessWhiteListEntry(
35     const GURL& source, const GURL& dest, bool allow_subdomains) {
36   blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
37       source.GetOrigin(),
38       blink::WebString::fromUTF8(dest.scheme()),
39       blink::WebString::fromUTF8(dest.HostNoBrackets()),
40       allow_subdomains);
41 }
42 }  // namespace
43
44 XWalkRenderProcessObserver::XWalkRenderProcessObserver()
45     : is_webkit_initialized_(false),
46       is_warp_mode_(false) {
47 }
48
49 XWalkRenderProcessObserver::~XWalkRenderProcessObserver() {
50 }
51
52 bool XWalkRenderProcessObserver::OnControlMessageReceived(
53     const IPC::Message& message) {
54   bool handled = true;
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)
59   IPC_END_MESSAGE_MAP()
60   return handled;
61 }
62
63 void XWalkRenderProcessObserver::WebKitInitialized() {
64   is_webkit_initialized_ = true;
65 }
66
67 void XWalkRenderProcessObserver::OnRenderProcessShutdown() {
68   is_webkit_initialized_ = false;
69 }
70
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_);
77
78   access_whitelist.clear();
79 }
80
81 void XWalkRenderProcessObserver::OnSetAccessWhiteList(const GURL& source,
82                                                       const GURL& dest,
83                                                       bool allow_subdomains) {
84   if (is_webkit_initialized_)
85     AddAccessWhiteListEntry(source, dest, allow_subdomains);
86   else
87     access_whitelist.push_back(
88         AccessWhitelistItem(source, dest, allow_subdomains));
89 }
90
91 void XWalkRenderProcessObserver::OnEnableWarpMode() {
92   is_warp_mode_ = true;
93 }
94
95 }  // namespace xwalk