Upstream version 10.38.210.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / renderer / tizen / xwalk_content_renderer_client_tizen.cc
1 // Copyright (c) 2013 Intel Corporation. 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/tizen/xwalk_content_renderer_client_tizen.h"
7
8 #include <string>
9
10 #include "base/files/file_path.h"
11 #include "base/strings/stringprintf.h"
12 #include "net/base/filename_util.h"
13 #include "net/base/net_errors.h"
14 #include "url/gurl.h"
15 #include "xwalk/application/common/constants.h"
16 #include "third_party/WebKit/public/platform/WebURLError.h"
17 #include "third_party/WebKit/public/web/WebFrame.h"
18 #include "third_party/WebKit/public/web/WebDocument.h"
19
20 namespace xwalk {
21
22 namespace {
23
24 const base::FilePath kTizenWebUiFwPath("/usr/share/tizen-web-ui-fw/");
25 const std::string kTizenWebUiFw = "/tizen-web-ui-fw/";
26
27 bool HasKnownExtension(const base::FilePath& path) {
28   if (path.MatchesExtension(".js"))
29     return true;
30   if (path.MatchesExtension(".css"))
31     return true;
32   if (path.MatchesExtension(".png"))
33     return true;
34   return false;
35 }
36
37 size_t GetRootPathLength(const GURL& first_party_for_cookies) {
38   const std::string& path = first_party_for_cookies.PathForRequest();
39   return path.rfind('/');
40 }
41
42 bool URLHasAppOrFileScheme(const GURL& url) {
43   if (url.SchemeIs("app"))
44     return true;
45   if (url.SchemeIsFile())
46     return true;
47   return false;
48 }
49
50 };  // namespace
51
52 bool XWalkContentRendererClientTizen::WillSendRequest(
53     blink::WebFrame* frame, content::PageTransition transition_type,
54     const GURL& url, const GURL& first_party_for_cookies, GURL* new_url) {
55   DCHECK(new_url);
56
57   if (XWalkContentRendererClient::WillSendRequest(
58           frame, transition_type, url, first_party_for_cookies, new_url))
59     return true;
60
61   if (!URLHasAppOrFileScheme(first_party_for_cookies))
62     return false;
63
64   const size_t root_path_length = GetRootPathLength(first_party_for_cookies);
65   if (root_path_length == std::string::npos)
66     return false;
67
68   const std::string& relative_path = url.path();
69   size_t tizen_web_ui_fw_pos = relative_path.find(kTizenWebUiFw,
70       root_path_length);
71   if (tizen_web_ui_fw_pos == std::string::npos)
72     return false;
73
74   tizen_web_ui_fw_pos += kTizenWebUiFw.length();
75   const base::FilePath& resource_path = kTizenWebUiFwPath.Append(
76       relative_path.substr(tizen_web_ui_fw_pos));
77
78   // FIXME(leandro): base::NormalizeFilePath(resource_path) should be called
79   // here to make sure files are really beneath kTizenWebUiFwPath, but the
80   // sandbox prevents the Render process from calling realpath().
81   if (!kTizenWebUiFwPath.IsParent(resource_path))
82     return false;
83
84   if (!HasKnownExtension(resource_path))
85     return false;
86
87   GURL replacement_url = net::FilePathToFileURL(resource_path);
88   if (!replacement_url.is_valid())
89     return false;
90
91   new_url->Swap(&replacement_url);
92   return true;
93 }
94
95 bool XWalkContentRendererClientTizen::HasErrorPage(int http_status_code,
96                                                    std::string* error_domain) {
97   return true;
98 }
99
100 void XWalkContentRendererClientTizen::GetNavigationErrorStrings(
101     content::RenderView* render_view,
102     blink::WebFrame* frame,
103     const blink::WebURLRequest& failed_request,
104     const blink::WebURLError& error,
105     std::string* error_html,
106     base::string16* error_description) {
107   if (error_html) {
108     *error_html =
109         base::StringPrintf("<html><body style=\"text-align: center;\">"
110                            "<h1>NET ERROR : %s</h1></body></html>",
111                            net::ErrorToString(error.reason).c_str());
112   }
113 }
114
115 std::string XWalkContentRendererClientTizen::GetOverridenUserAgent() const {
116   if (!xwalk_render_process_observer_)
117     return "";
118   return xwalk_render_process_observer_->GetOverridenUserAgent();
119 }
120
121 }  // namespace xwalk