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.
6 #include "xwalk/runtime/renderer/xwalk_render_process_observer_generic.h"
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"
21 struct AccessWhitelistItem {
23 const GURL& source, const GURL& dest, bool allow_subdomains);
26 bool allow_subdomains_;
29 AccessWhitelistItem::AccessWhitelistItem(
30 const GURL& source, const GURL& dest, bool allow_subdomains)
33 allow_subdomains_(allow_subdomains) {
36 std::vector<AccessWhitelistItem> access_whitelist;
38 void AddAccessWhiteListEntry(
39 const GURL& source, const GURL& dest, bool allow_subdomains) {
40 blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
42 blink::WebString::fromUTF8(dest.scheme()),
43 blink::WebString::fromUTF8(dest.HostNoBrackets()),
48 XWalkRenderProcessObserver::XWalkRenderProcessObserver()
49 : is_webkit_initialized_(false),
51 security_mode_(application::SecurityPolicy::NoSecurity) {
54 XWalkRenderProcessObserver::~XWalkRenderProcessObserver() {
57 bool XWalkRenderProcessObserver::OnControlMessageReceived(
58 const IPC::Message& message) {
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)
65 IPC_MESSAGE_HANDLER(ViewMsg_UserAgentStringChanged, OnUserAgentChanged)
67 IPC_MESSAGE_UNHANDLED(handled = false)
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_);
78 access_whitelist.clear();
81 void XWalkRenderProcessObserver::OnRenderProcessShutdown() {
82 is_webkit_initialized_ = false;
85 void XWalkRenderProcessObserver::OnSetAccessWhiteList(const GURL& source,
87 bool allow_subdomains) {
88 if (is_webkit_initialized_)
89 AddAccessWhiteListEntry(source, dest, allow_subdomains);
91 access_whitelist.push_back(
92 AccessWhitelistItem(source, dest, allow_subdomains));
95 void XWalkRenderProcessObserver::OnEnableSecurityMode(
96 const GURL& url, application::SecurityPolicy::SecurityMode mode) {
98 security_mode_ = mode;
101 void XWalkRenderProcessObserver::OnSuspendJSEngine(bool is_suspend) {
102 if (is_suspend == is_suspended_)
104 content::RenderThreadImpl* thread = content::RenderThreadImpl::current();
105 thread->EnsureWebKitInitialized();
107 thread->webkit_platform_support()->SuspendSharedTimer();
109 thread->webkit_platform_support()->ResumeSharedTimer();
110 is_suspended_ = is_suspend;
113 #if defined(OS_TIZEN)
114 void XWalkRenderProcessObserver::OnUserAgentChanged(
115 const std::string& userAgentString) {
116 overriden_user_agent_ = userAgentString;
119 std::string XWalkRenderProcessObserver::GetOverridenUserAgent() const {
120 return overriden_user_agent_;