08350495eaf2d4ff05140c320a70186b46d9d0d1
[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 "net/base/filename_util.h"
11 #include "url/gurl.h"
12 #include "xwalk/application/common/constants.h"
13 #include "third_party/WebKit/public/web/WebFrame.h"
14 #include "third_party/WebKit/public/web/WebDocument.h"
15
16 namespace xwalk {
17
18 namespace {
19
20 const base::FilePath kTizenWebUiFwPath("/usr/share/tizen-web-ui-fw/");
21 const std::string kTizenWebUiFw = "/tizen-web-ui-fw/";
22
23 bool HasKnownExtension(const base::FilePath& path) {
24   if (path.MatchesExtension(".js"))
25     return true;
26   if (path.MatchesExtension(".css"))
27     return true;
28   if (path.MatchesExtension(".png"))
29     return true;
30   return false;
31 }
32
33 size_t GetRootPathLength(const GURL& first_party_for_cookies) {
34   const std::string& path = first_party_for_cookies.PathForRequest();
35   return path.rfind('/');
36 }
37
38 bool URLHasAppOrFileScheme(const GURL& url) {
39   if (url.SchemeIs("app"))
40     return true;
41   if (url.SchemeIsFile())
42     return true;
43   return false;
44 }
45
46 };  // namespace
47
48 bool XWalkContentRendererClientTizen::WillSendRequest(
49     blink::WebFrame* frame, content::PageTransition transition_type,
50     const GURL& url, const GURL& first_party_for_cookies, GURL* new_url) {
51   DCHECK(new_url);
52
53   if (XWalkContentRendererClient::WillSendRequest(
54           frame, transition_type, url, first_party_for_cookies, new_url))
55     return true;
56
57   if (!URLHasAppOrFileScheme(first_party_for_cookies))
58     return false;
59
60   const size_t root_path_length = GetRootPathLength(first_party_for_cookies);
61   if (root_path_length == std::string::npos)
62     return false;
63
64   const std::string& relative_path = url.path();
65   size_t tizen_web_ui_fw_pos = relative_path.find(kTizenWebUiFw,
66       root_path_length);
67   if (tizen_web_ui_fw_pos == std::string::npos)
68     return false;
69
70   tizen_web_ui_fw_pos += kTizenWebUiFw.length();
71   const base::FilePath& resource_path = kTizenWebUiFwPath.Append(
72       relative_path.substr(tizen_web_ui_fw_pos));
73
74   // FIXME(leandro): base::NormalizeFilePath(resource_path) should be called
75   // here to make sure files are really beneath kTizenWebUiFwPath, but the
76   // sandbox prevents the Render process from calling realpath().
77   if (!kTizenWebUiFwPath.IsParent(resource_path))
78     return false;
79
80   if (!HasKnownExtension(resource_path))
81     return false;
82
83   GURL replacement_url = net::FilePathToFileURL(resource_path);
84   if (!replacement_url.is_valid())
85     return false;
86
87   new_url->Swap(&replacement_url);
88   return true;
89 }
90
91 }  // namespace xwalk