Upstream version 6.35.131.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 "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"
14
15
16 namespace xwalk {
17 namespace {
18 struct AccessWhitelistItem {
19   AccessWhitelistItem(
20       const GURL& source, const GURL& dest, bool allow_subdomains);
21   GURL source_;
22   GURL dest_;
23   bool allow_subdomains_;
24 };
25
26 AccessWhitelistItem::AccessWhitelistItem(
27     const GURL& source, const GURL& dest, bool allow_subdomains)
28     : source_(source),
29       dest_(dest),
30       allow_subdomains_(allow_subdomains) {
31 }
32
33 std::vector<AccessWhitelistItem> access_whitelist;
34
35 void AddAccessWhiteListEntry(
36     const GURL& source, const GURL& dest, bool allow_subdomains) {
37   blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
38       source.GetOrigin(),
39       blink::WebString::fromUTF8(dest.scheme()),
40       blink::WebString::fromUTF8(dest.HostNoBrackets()),
41       allow_subdomains);
42 }
43 }  // namespace
44
45 XWalkRenderProcessObserver::XWalkRenderProcessObserver()
46     : is_webkit_initialized_(false),
47       security_mode_(application::SecurityPolicy::NoSecurity) {
48 }
49
50 XWalkRenderProcessObserver::~XWalkRenderProcessObserver() {
51 }
52
53 bool XWalkRenderProcessObserver::OnControlMessageReceived(
54     const IPC::Message& message) {
55   bool handled = true;
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)
60   IPC_END_MESSAGE_MAP()
61   return handled;
62 }
63
64 void XWalkRenderProcessObserver::WebKitInitialized() {
65   is_webkit_initialized_ = true;
66 }
67
68 void XWalkRenderProcessObserver::OnRenderProcessShutdown() {
69   is_webkit_initialized_ = false;
70 }
71
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_);
78
79   access_whitelist.clear();
80 }
81
82 void XWalkRenderProcessObserver::OnSetAccessWhiteList(const GURL& source,
83                                                       const GURL& dest,
84                                                       bool allow_subdomains) {
85   if (is_webkit_initialized_)
86     AddAccessWhiteListEntry(source, dest, allow_subdomains);
87   else
88     access_whitelist.push_back(
89         AccessWhitelistItem(source, dest, allow_subdomains));
90 }
91
92 void XWalkRenderProcessObserver::OnEnableSecurityMode(
93     const GURL& url, application::SecurityPolicy::SecurityMode mode) {
94   app_url_ = url;
95   security_mode_ = mode;
96 }
97
98 }  // namespace xwalk