Upstream version 11.39.256.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / common / xwalk_paths.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/common/xwalk_paths.h"
6
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h"
12
13 #if defined(OS_WIN)
14 #include "base/base_paths_win.h"
15 #elif defined(OS_TIZEN)
16 #include <tzplatform_config.h>
17 #elif defined(OS_LINUX)
18 #include "base/environment.h"
19 #include "base/nix/xdg_util.h"
20 #elif defined(OS_MACOSX) && !defined(OS_IOS)
21 #import "base/mac/mac_util.h"
22 #endif
23
24 namespace xwalk {
25
26 namespace {
27
28 #if defined(OS_MACOSX) && !defined(OS_IOS)
29
30 base::FilePath GetVersionedDirectory() {
31   // Start out with the path to the running executable.
32   base::FilePath path;
33   PathService::Get(base::FILE_EXE, &path);
34
35   // One step up to MacOS, another to Contents.
36   path = path.DirName().DirName();
37   DCHECK_EQ(path.BaseName().value(), "Contents");
38
39   if (base::mac::IsBackgroundOnlyProcess()) {
40     // path identifies the helper .app's Contents directory in the browser
41     // .app's versioned directory.  Go up two steps to get to the browser
42     // .app's versioned directory.
43     path = path.DirName().DirName();
44   }
45
46   return path;
47 }
48
49 base::FilePath GetFrameworkBundlePath() {
50   // It's tempting to use +[NSBundle bundleWithIdentifier:], but it's really
51   // slow (about 30ms on 10.5 and 10.6), despite Apple's documentation stating
52   // that it may be more efficient than +bundleForClass:.  +bundleForClass:
53   // itself takes 1-2ms.  Getting an NSBundle from a path, on the other hand,
54   // essentially takes no time at all, at least when the bundle has already
55   // been loaded as it will have been in this case.  The FilePath operations
56   // needed to compute the framework's path are also effectively free, so that
57   // is the approach that is used here.  NSBundle is also documented as being
58   // not thread-safe, and thread safety may be a concern here.
59
60   // The framework bundle is at a known path and name from the browser .app's
61   // versioned directory.
62   return GetVersionedDirectory().Append("XWalk");
63 }
64 #endif
65
66 // File name of the internal NaCl plugin on different platforms.
67 const base::FilePath::CharType kInternalNaClPluginFileName[] =
68     FILE_PATH_LITERAL("internal-nacl-plugin");
69
70 #if defined(OS_TIZEN)
71 base::FilePath GetAppPath() {
72   const char* app_path = getuid() != tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) ?
73                          tzplatform_getenv(TZ_USER_APP) :
74                          tzplatform_getenv(TZ_SYS_RW_APP);
75   return base::FilePath(app_path);
76 }
77
78 #elif defined(OS_LINUX)
79 base::FilePath GetConfigPath() {
80   scoped_ptr<base::Environment> env(base::Environment::Create());
81   return base::nix::GetXDGDirectory(
82       env.get(), base::nix::kXdgConfigHomeEnvVar, base::nix::kDotConfigDir);
83 }
84 #endif
85
86 bool GetXWalkDataPath(base::FilePath* path) {
87   base::FilePath::StringType xwalk_suffix;
88 #if defined(SHARED_PROCESS_MODE)
89   xwalk_suffix = FILE_PATH_LITERAL("xwalk-service");
90 #else
91   xwalk_suffix = FILE_PATH_LITERAL("xwalk");
92 #endif
93   base::FilePath cur;
94
95 #if defined(OS_WIN)
96   CHECK(PathService::Get(base::DIR_LOCAL_APP_DATA, &cur));
97   cur = cur.Append(xwalk_suffix);
98
99 #elif defined(OS_TIZEN)
100   cur = GetAppPath().Append(xwalk_suffix);
101
102 #elif defined(OS_LINUX)
103   cur = GetConfigPath().Append(xwalk_suffix);
104
105 #elif defined(OS_MACOSX)
106   CHECK(PathService::Get(base::DIR_APP_DATA, &cur));
107   cur = cur.Append(xwalk_suffix);
108
109 #else
110   NOTIMPLEMENTED() << "Unsupported OS platform.";
111   return false;
112 #endif
113
114   *path = cur;
115   return true;
116 }
117
118 bool GetInternalPluginsDirectory(base::FilePath* result) {
119 #if defined(OS_MACOSX) && !defined(OS_IOS)
120   // If called from Chrome, get internal plugins from a subdirectory of the
121   // framework.
122   if (base::mac::AmIBundled()) {
123     *result = xwalk::GetFrameworkBundlePath();
124     DCHECK(!result->empty());
125     *result = result->Append("Internet Plug-Ins");
126     return true;
127   }
128   // In tests, just look in the module directory (below).
129 #endif
130
131   // The rest of the world expects plugins in the module directory.
132   return PathService::Get(base::DIR_MODULE, result);
133 }
134
135 }  // namespace
136
137 bool PathProvider(int key, base::FilePath* path) {
138   base::FilePath cur;
139   switch (key) {
140     case xwalk::DIR_DATA_PATH:
141       return GetXWalkDataPath(path);
142       break;
143     case xwalk::DIR_INTERNAL_PLUGINS:
144       if (!GetInternalPluginsDirectory(&cur))
145         return false;
146       break;
147     case xwalk::FILE_NACL_PLUGIN:
148       if (!GetInternalPluginsDirectory(&cur))
149         return false;
150       cur = cur.Append(kInternalNaClPluginFileName);
151       break;
152     // Where PNaCl files are ultimately located.  The default finds the files
153     // inside the InternalPluginsDirectory / build directory, as if it
154     // was shipped along with xwalk.  The value can be overridden
155     // if it is installed via component updater.
156     case xwalk::DIR_PNACL_COMPONENT:
157 #if defined(OS_MACOSX)
158       // PNaCl really belongs in the InternalPluginsDirectory but actually
159       // copying it there would result in the files also being shipped, which
160       // we don't want yet. So for now, just find them in the directory where
161       // they get built.
162       if (!PathService::Get(base::DIR_EXE, &cur))
163         return false;
164       if (base::mac::AmIBundled()) {
165         // If we're called from xwalk, it's beside the app (outside the
166         // app bundle), if we're called from a unittest, we'll already be
167         // outside the bundle so use the exe dir.
168         // exe_dir gave us .../Chromium.app/Contents/MacOS/Chromium.
169         cur = cur.DirName();
170         cur = cur.DirName();
171         cur = cur.DirName();
172       }
173 #else
174       if (!GetInternalPluginsDirectory(&cur))
175         return false;
176 #endif
177       cur = cur.Append(FILE_PATH_LITERAL("pnacl"));
178       break;
179     case xwalk::DIR_TEST_DATA:
180       if (!PathService::Get(base::DIR_SOURCE_ROOT, &cur))
181         return false;
182       cur = cur.Append(FILE_PATH_LITERAL("xwalk"));
183       cur = cur.Append(FILE_PATH_LITERAL("test"));
184       cur = cur.Append(FILE_PATH_LITERAL("data"));
185       break;
186     case xwalk::DIR_WGT_STORAGE_PATH:
187       if (!GetXWalkDataPath(&cur))
188         return false;
189       cur = cur.Append(FILE_PATH_LITERAL("Widget Storage"));
190       break;
191     case xwalk::DIR_APPLICATION_PATH:
192       if (!GetXWalkDataPath(&cur))
193         return false;
194       cur = cur.Append(FILE_PATH_LITERAL("applications"));
195       break;
196     default:
197       return false;
198   }
199   *path = cur;
200   return true;
201 }
202
203 void RegisterPathProvider() {
204   PathService::RegisterProvider(PathProvider, PATH_START, PATH_END);
205 }
206
207 }  // namespace xwalk