Upstream version 10.38.210.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 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "xwalk/runtime/renderer/xwalk_render_process_observer_generic.h"
7
8 #include <vector>
9
10 #include "content/renderer/render_thread_impl.h"
11 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
12 #include "ipc/ipc_message_macros.h"
13 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
14 #include "third_party/WebKit/public/platform/WebString.h"
15 #include "xwalk/runtime/common/xwalk_common_messages.h"
16 #include "xwalk/runtime/common/xwalk_content_client.h"
17
18
19 namespace xwalk {
20 namespace {
21 struct AccessWhitelistItem {
22   AccessWhitelistItem(
23       const GURL& source, const GURL& dest, bool allow_subdomains);
24   GURL source_;
25   GURL dest_;
26   bool allow_subdomains_;
27 };
28
29 AccessWhitelistItem::AccessWhitelistItem(
30     const GURL& source, const GURL& dest, bool allow_subdomains)
31     : source_(source),
32       dest_(dest),
33       allow_subdomains_(allow_subdomains) {
34 }
35
36 std::vector<AccessWhitelistItem> access_whitelist;
37
38 void AddAccessWhiteListEntry(
39     const GURL& source, const GURL& dest, bool allow_subdomains) {
40   blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
41       source.GetOrigin(),
42       blink::WebString::fromUTF8(dest.scheme()),
43       blink::WebString::fromUTF8(dest.HostNoBrackets()),
44       allow_subdomains);
45 }
46 }  // namespace
47
48 XWalkRenderProcessObserver::XWalkRenderProcessObserver()
49     : is_webkit_initialized_(false),
50       is_suspended_(false),
51       security_mode_(application::SecurityPolicy::NoSecurity) {
52 }
53
54 XWalkRenderProcessObserver::~XWalkRenderProcessObserver() {
55 }
56
57 bool XWalkRenderProcessObserver::OnControlMessageReceived(
58     const IPC::Message& message) {
59   bool handled = true;
60   IPC_BEGIN_MESSAGE_MAP(XWalkRenderProcessObserver, message)
61     IPC_MESSAGE_HANDLER(ViewMsg_SetAccessWhiteList, OnSetAccessWhiteList)
62     IPC_MESSAGE_HANDLER(ViewMsg_EnableSecurityMode, OnEnableSecurityMode)
63     IPC_MESSAGE_HANDLER(ViewMsg_SuspendJSEngine, OnSuspendJSEngine)
64 #if defined(OS_TIZEN)
65     IPC_MESSAGE_HANDLER(ViewMsg_UserAgentStringChanged, OnUserAgentChanged)
66 #endif
67     IPC_MESSAGE_UNHANDLED(handled = false)
68   IPC_END_MESSAGE_MAP()
69   return handled;
70 }
71
72 void XWalkRenderProcessObserver::WebKitInitialized() {
73   is_webkit_initialized_ = true;
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::OnRenderProcessShutdown() {
82   is_webkit_initialized_ = false;
83 }
84
85 void XWalkRenderProcessObserver::OnSetAccessWhiteList(const GURL& source,
86                                                       const GURL& dest,
87                                                       bool allow_subdomains) {
88   if (is_webkit_initialized_)
89     AddAccessWhiteListEntry(source, dest, allow_subdomains);
90   else
91     access_whitelist.push_back(
92         AccessWhitelistItem(source, dest, allow_subdomains));
93 }
94
95 void XWalkRenderProcessObserver::OnEnableSecurityMode(
96     const GURL& url, application::SecurityPolicy::SecurityMode mode) {
97   app_url_ = url;
98   security_mode_ = mode;
99 }
100
101 void XWalkRenderProcessObserver::OnSuspendJSEngine(bool is_suspend) {
102   if (is_suspend == is_suspended_)
103     return;
104   content::RenderThreadImpl* thread = content::RenderThreadImpl::current();
105   thread->EnsureWebKitInitialized();
106   if (is_suspend)
107     thread->webkit_platform_support()->SuspendSharedTimer();
108   else
109     thread->webkit_platform_support()->ResumeSharedTimer();
110   is_suspended_ = is_suspend;
111 }
112
113 #if defined(OS_TIZEN)
114 void XWalkRenderProcessObserver::OnUserAgentChanged(
115     const std::string& userAgentString) {
116   overriden_user_agent_ = userAgentString;
117 }
118
119 std::string XWalkRenderProcessObserver::GetOverridenUserAgent() const {
120   return overriden_user_agent_;
121 }
122 #endif
123
124 }  // namespace xwalk