Upstream version 9.37.195.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/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"
15
16
17 namespace xwalk {
18 namespace {
19 struct AccessWhitelistItem {
20   AccessWhitelistItem(
21       const GURL& source, const GURL& dest, bool allow_subdomains);
22   GURL source_;
23   GURL dest_;
24   bool allow_subdomains_;
25 };
26
27 AccessWhitelistItem::AccessWhitelistItem(
28     const GURL& source, const GURL& dest, bool allow_subdomains)
29     : source_(source),
30       dest_(dest),
31       allow_subdomains_(allow_subdomains) {
32 }
33
34 std::vector<AccessWhitelistItem> access_whitelist;
35
36 void AddAccessWhiteListEntry(
37     const GURL& source, const GURL& dest, bool allow_subdomains) {
38   blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
39       source.GetOrigin(),
40       blink::WebString::fromUTF8(dest.scheme()),
41       blink::WebString::fromUTF8(dest.HostNoBrackets()),
42       allow_subdomains);
43 }
44 }  // namespace
45
46 XWalkRenderProcessObserver::XWalkRenderProcessObserver()
47     : is_webkit_initialized_(false),
48       is_suspended_(false),
49       security_mode_(application::SecurityPolicy::NoSecurity) {
50 }
51
52 XWalkRenderProcessObserver::~XWalkRenderProcessObserver() {
53 }
54
55 bool XWalkRenderProcessObserver::OnControlMessageReceived(
56     const IPC::Message& message) {
57   bool handled = true;
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)
63   IPC_END_MESSAGE_MAP()
64   return handled;
65 }
66
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_);
72
73   access_whitelist.clear();
74 }
75
76 void XWalkRenderProcessObserver::OnRenderProcessShutdown() {
77   is_webkit_initialized_ = false;
78 }
79
80 void XWalkRenderProcessObserver::OnSetAccessWhiteList(const GURL& source,
81                                                       const GURL& dest,
82                                                       bool allow_subdomains) {
83   if (is_webkit_initialized_)
84     AddAccessWhiteListEntry(source, dest, allow_subdomains);
85   else
86     access_whitelist.push_back(
87         AccessWhitelistItem(source, dest, allow_subdomains));
88 }
89
90 void XWalkRenderProcessObserver::OnEnableSecurityMode(
91     const GURL& url, application::SecurityPolicy::SecurityMode mode) {
92   app_url_ = url;
93   security_mode_ = mode;
94 }
95
96 void XWalkRenderProcessObserver::OnSuspendJSEngine(bool is_suspend) {
97   if (is_suspend == is_suspended_)
98     return;
99   content::RenderThreadImpl* thread = content::RenderThreadImpl::current();
100   thread->EnsureWebKitInitialized();
101   if (is_suspend)
102     thread->webkit_platform_support()->SuspendSharedTimer();
103   else
104     thread->webkit_platform_support()->ResumeSharedTimer();
105   is_suspended_ = is_suspend;
106 }
107
108 }  // namespace xwalk