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