Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / renderer / android / xwalk_render_process_observer.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/android/xwalk_render_process_observer.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/values.h"
9 #include "content/public/common/url_constants.h"
10 #include "extensions/common/url_pattern.h"
11 #include "ipc/ipc_message_macros.h"
12 #include "third_party/WebKit/public/web/WebCache.h"
13 #include "third_party/WebKit/public/web/WebNetworkStateNotifier.h"
14 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
15 #include "xwalk/runtime/browser/android/net/url_constants.h"
16 #include "xwalk/runtime/common/android/xwalk_render_view_messages.h"
17
18 namespace xwalk {
19
20 XWalkRenderProcessObserver::XWalkRenderProcessObserver()
21   : webkit_initialized_(false) {
22 }
23
24 XWalkRenderProcessObserver::~XWalkRenderProcessObserver() {
25 }
26
27 bool XWalkRenderProcessObserver::OnControlMessageReceived(
28     const IPC::Message& message) {
29   bool handled = true;
30   IPC_BEGIN_MESSAGE_MAP(XWalkRenderProcessObserver, message)
31     IPC_MESSAGE_HANDLER(XWalkViewMsg_SetJsOnlineProperty, OnSetJsOnlineProperty)
32     IPC_MESSAGE_HANDLER(XWalkViewMsg_ClearCache, OnClearCache);
33     IPC_MESSAGE_HANDLER(XWalkViewMsg_SetOriginAccessWhitelist,
34                         OnSetOriginAccessWhitelist)
35     IPC_MESSAGE_UNHANDLED(handled = false)
36   IPC_END_MESSAGE_MAP()
37   return handled;
38 }
39
40 void XWalkRenderProcessObserver::WebKitInitialized() {
41   webkit_initialized_ = true;
42 }
43
44 void XWalkRenderProcessObserver::OnSetJsOnlineProperty(bool network_up) {
45   if (webkit_initialized_)
46     blink::WebNetworkStateNotifier::setOnLine(network_up);
47 }
48
49 void XWalkRenderProcessObserver::OnClearCache() {
50   if (webkit_initialized_)
51     blink::WebCache::clear();
52 }
53
54 void XWalkRenderProcessObserver::OnSetOriginAccessWhitelist(
55     std::string base_url,
56     std::string match_patterns) {
57   blink::WebSecurityPolicy::resetOriginAccessWhitelists();
58
59   DCHECK(!base_url.empty());
60   if (base_url.empty() || match_patterns.empty())
61     return;
62
63   base::Value* patterns = base::JSONReader::Read(match_patterns);
64   if (!patterns)
65     return;
66
67   base::ListValue* match_pattern_list = NULL;
68   if (!patterns->GetAsList(&match_pattern_list))
69     return;
70
71   const char* schemes[] = {
72     url::kHttpScheme,
73     url::kHttpsScheme,
74     content::kFileScheme,
75     xwalk::kAppScheme,
76   };
77   size_t size = match_pattern_list->GetSize();
78   for (size_t i = 0; i < size; i ++) {
79     std::string match_pattern;
80     if (!match_pattern_list->GetString(i, &match_pattern))
81       continue;
82
83     URLPattern allowedUrl(URLPattern::SCHEME_ALL);
84     if (allowedUrl.Parse(match_pattern) != URLPattern::PARSE_SUCCESS)
85       continue;
86
87     for (size_t j = 0; j < arraysize(schemes); ++j) {
88       if (allowedUrl.MatchesScheme(schemes[j])) {
89         blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
90               blink::WebURL(GURL(base_url)),
91               blink::WebString::fromUTF8(schemes[j]),
92               blink::WebString::fromUTF8(allowedUrl.host()),
93               allowedUrl.match_subdomains());
94       }
95     }
96   }
97 }
98
99 }  // namespace xwalk