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.
5 #include "chrome/common/chrome_paths.h"
7 #include "base/files/file_util.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/mac/bundle_locations.h"
11 #include "base/path_service.h"
12 #include "base/strings/string_util.h"
13 #include "base/sys_info.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "base/version.h"
16 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/chrome_paths_internal.h"
18 #include "chrome/common/widevine_cdm_constants.h"
19 #include "ui/base/ui_base_paths.h"
21 #if defined(OS_ANDROID)
22 #include "base/android/path_utils.h"
23 #include "base/base_paths_android.h"
26 #if defined(OS_MACOSX)
27 #include "base/mac/foundation_util.h"
31 #include "base/win/registry.h"
34 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
39 const wchar_t kFlashRegistryRoot
[] = L
"SOFTWARE\\Macromedia\\FlashPlayerPepper";
41 const wchar_t kFlashPlayerPathValueName
[] = L
"PlayerPath";
44 // File name of the internal Flash plugin on different platforms.
45 const base::FilePath::CharType kInternalFlashPluginFileName
[] =
46 #if defined(OS_MACOSX)
47 FILE_PATH_LITERAL("Flash Player Plugin for Chrome.plugin");
49 FILE_PATH_LITERAL("gcswf32.dll");
50 #else // OS_LINUX, etc.
51 FILE_PATH_LITERAL("libgcflashplayer.so");
54 // The Pepper Flash plugins are in a directory with this name.
55 const base::FilePath::CharType kPepperFlashBaseDirectory
[] =
56 FILE_PATH_LITERAL("PepperFlash");
59 const base::FilePath::CharType kPepperFlashSystemBaseDirectory
[] =
60 FILE_PATH_LITERAL("Macromed\\Flash");
61 #elif defined(OS_MACOSX) && !defined(OS_IOS)
62 const base::FilePath::CharType kPepperFlashSystemBaseDirectory
[] =
63 FILE_PATH_LITERAL("Internet Plug-Ins/PepperFlashPlayer");
66 const base::FilePath::CharType kInternalNaClPluginFileName
[] =
67 FILE_PATH_LITERAL("internal-nacl-plugin");
70 // The path to the external extension <id>.json files.
71 // /usr/share seems like a good choice, see: http://www.pathname.com/fhs/
72 const base::FilePath::CharType kFilepathSinglePrefExtensions
[] =
73 #if defined(GOOGLE_CHROME_BUILD)
74 FILE_PATH_LITERAL("/usr/share/google-chrome/extensions");
76 FILE_PATH_LITERAL("/usr/share/chromium/extensions");
77 #endif // defined(GOOGLE_CHROME_BUILD)
78 #endif // defined(OS_LINUX)
80 static base::LazyInstance
<base::FilePath
>
81 g_invalid_specified_user_data_dir
= LAZY_INSTANCE_INITIALIZER
;
83 // Gets the path for internal plugins.
84 bool GetInternalPluginsDirectory(base::FilePath
* result
) {
85 #if defined(OS_MACOSX) && !defined(OS_IOS)
86 // If called from Chrome, get internal plugins from a subdirectory of the
88 if (base::mac::AmIBundled()) {
89 *result
= chrome::GetFrameworkBundlePath();
90 DCHECK(!result
->empty());
91 *result
= result
->Append("Internet Plug-Ins");
94 // In tests, just look in the module directory (below).
97 // The rest of the world expects plugins in the module directory.
98 return PathService::Get(base::DIR_MODULE
, result
);
102 // Gets the Flash path if installed on the system.
103 bool GetSystemFlashDirectory(base::FilePath
* out_path
) {
104 base::win::RegKey
path_key(HKEY_LOCAL_MACHINE
, kFlashRegistryRoot
, KEY_READ
);
105 base::string16 path_str
;
106 if (FAILED(path_key
.ReadValue(kFlashPlayerPathValueName
, &path_str
)))
108 base::FilePath plugin_path
= base::FilePath(path_str
).DirName();
110 *out_path
= plugin_path
;
119 bool PathProvider(int key
, base::FilePath
* result
) {
120 // Some keys are just aliases...
122 case chrome::DIR_APP
:
123 return PathService::Get(base::DIR_MODULE
, result
);
124 case chrome::DIR_LOGS
:
126 // Release builds write to the data dir
127 return PathService::Get(chrome::DIR_USER_DATA
, result
);
129 // Debug builds write next to the binary (in the build tree)
130 #if defined(OS_MACOSX)
131 if (!PathService::Get(base::DIR_EXE
, result
))
133 if (base::mac::AmIBundled()) {
134 // If we're called from chrome, dump it beside the app (outside the
135 // app bundle), if we're called from a unittest, we'll already
136 // outside the bundle so use the exe dir.
137 // exe_dir gave us .../Chromium.app/Contents/MacOS/Chromium.
138 *result
= result
->DirName();
139 *result
= result
->DirName();
140 *result
= result
->DirName();
144 return PathService::Get(base::DIR_EXE
, result
);
145 #endif // defined(OS_MACOSX)
147 case chrome::FILE_RESOURCE_MODULE
:
148 return PathService::Get(base::FILE_MODULE
, result
);
151 // Assume that we will not need to create the directory if it does not exist.
152 // This flag can be set to true for the cases where we want to create it.
153 bool create_dir
= false;
157 case chrome::DIR_USER_DATA
:
158 if (!GetDefaultUserDataDirectory(&cur
)) {
164 case chrome::DIR_USER_DOCUMENTS
:
165 if (!GetUserDocumentsDirectory(&cur
))
169 case chrome::DIR_USER_MUSIC
:
170 if (!GetUserMusicDirectory(&cur
))
173 case chrome::DIR_USER_PICTURES
:
174 if (!GetUserPicturesDirectory(&cur
))
177 case chrome::DIR_USER_VIDEOS
:
178 if (!GetUserVideosDirectory(&cur
))
181 case chrome::DIR_DEFAULT_DOWNLOADS_SAFE
:
182 #if defined(OS_WIN) || defined(OS_LINUX)
183 if (!GetUserDownloadsDirectorySafe(&cur
))
187 // Fall through for all other platforms.
189 case chrome::DIR_DEFAULT_DOWNLOADS
:
190 #if defined(OS_ANDROID)
191 if (!base::android::GetDownloadsDirectory(&cur
))
194 if (!GetUserDownloadsDirectory(&cur
))
196 // Do not create the download directory here, we have done it twice now
197 // and annoyed a lot of users.
200 case chrome::DIR_CRASH_DUMPS
:
201 #if defined(OS_CHROMEOS)
202 // ChromeOS uses a separate directory. See http://crosbug.com/25089
203 cur
= base::FilePath("/var/log/chrome");
204 #elif defined(OS_ANDROID)
205 if (!base::android::GetCacheDirectory(&cur
))
208 // The crash reports are always stored relative to the default user data
209 // directory. This avoids the problem of having to re-initialize the
210 // exception handler after parsing command line options, which may
211 // override the location of the app's profile directory.
212 if (!GetDefaultUserDataDirectory(&cur
))
215 #if defined(OS_MACOSX)
216 cur
= cur
.Append(FILE_PATH_LITERAL("Crashpad"));
218 cur
= cur
.Append(FILE_PATH_LITERAL("Crash Reports"));
223 case chrome::DIR_WATCHER_DATA
:
224 // The watcher data is always stored relative to the default user data
225 // directory. This allows the watcher to be initialized before
226 // command-line options have been parsed.
227 if (!GetDefaultUserDataDirectory(&cur
))
229 cur
= cur
.Append(FILE_PATH_LITERAL("Diagnostics"));
232 case chrome::DIR_RESOURCES
:
233 #if defined(OS_MACOSX)
234 cur
= base::mac::FrameworkBundlePath();
235 cur
= cur
.Append(FILE_PATH_LITERAL("Resources"));
237 if (!PathService::Get(chrome::DIR_APP
, &cur
))
239 cur
= cur
.Append(FILE_PATH_LITERAL("resources"));
242 case chrome::DIR_INSPECTOR
:
243 if (!PathService::Get(chrome::DIR_RESOURCES
, &cur
))
245 cur
= cur
.Append(FILE_PATH_LITERAL("inspector"));
247 case chrome::DIR_APP_DICTIONARIES
:
248 #if defined(OS_POSIX)
249 // We can't write into the EXE dir on Linux, so keep dictionaries
250 // alongside the safe browsing database in the user data dir.
251 // And we don't want to write into the bundle on the Mac, so push
252 // it to the user data dir there also.
253 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
256 if (!PathService::Get(base::DIR_EXE
, &cur
))
259 cur
= cur
.Append(FILE_PATH_LITERAL("Dictionaries"));
262 case chrome::DIR_INTERNAL_PLUGINS
:
263 if (!GetInternalPluginsDirectory(&cur
))
266 case chrome::DIR_PEPPER_FLASH_PLUGIN
:
267 if (!GetInternalPluginsDirectory(&cur
))
269 cur
= cur
.Append(kPepperFlashBaseDirectory
);
271 case chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
:
272 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
274 cur
= cur
.Append(kPepperFlashBaseDirectory
);
276 case chrome::DIR_PEPPER_FLASH_SYSTEM_PLUGIN
:
278 if (!GetSystemFlashDirectory(&cur
))
280 #elif defined(OS_MACOSX) && !defined(OS_IOS)
281 if (!GetLocalLibraryDirectory(&cur
))
283 cur
= cur
.Append(kPepperFlashSystemBaseDirectory
);
285 // Chrome on iOS does not supports PPAPI binaries, return false.
286 // TODO(wfh): If Adobe release PPAPI binaries for Linux, add support here.
290 case chrome::FILE_LOCAL_STATE
:
291 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
293 cur
= cur
.Append(chrome::kLocalStateFilename
);
295 case chrome::FILE_RECORDED_SCRIPT
:
296 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
298 cur
= cur
.Append(FILE_PATH_LITERAL("script.log"));
300 case chrome::FILE_FLASH_PLUGIN
:
301 if (!GetInternalPluginsDirectory(&cur
))
303 cur
= cur
.Append(kInternalFlashPluginFileName
);
305 case chrome::FILE_PEPPER_FLASH_PLUGIN
:
306 if (!PathService::Get(chrome::DIR_PEPPER_FLASH_PLUGIN
, &cur
))
308 cur
= cur
.Append(chrome::kPepperFlashPluginFilename
);
310 // TODO(teravest): Remove this case once the internal NaCl plugin is gone.
311 // We currently need a path here to look up whether the plugin is disabled
312 // and what its permissions are.
313 case chrome::FILE_NACL_PLUGIN
:
314 if (!GetInternalPluginsDirectory(&cur
))
316 cur
= cur
.Append(kInternalNaClPluginFileName
);
318 // PNaCl is currenly installable via the component updater or by being
319 // simply built-in. DIR_PNACL_BASE is used as the base directory for
320 // installation via component updater. DIR_PNACL_COMPONENT will be
321 // the final location of pnacl, which is a subdir of DIR_PNACL_BASE.
322 case chrome::DIR_PNACL_BASE
:
323 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
325 cur
= cur
.Append(FILE_PATH_LITERAL("pnacl"));
327 // Where PNaCl files are ultimately located. The default finds the files
328 // inside the InternalPluginsDirectory / build directory, as if it
329 // was shipped along with chrome. The value can be overridden
330 // if it is installed via component updater.
331 case chrome::DIR_PNACL_COMPONENT
:
332 #if defined(OS_MACOSX)
333 // PNaCl really belongs in the InternalPluginsDirectory but actually
334 // copying it there would result in the files also being shipped, which
335 // we don't want yet. So for now, just find them in the directory where
337 if (!PathService::Get(base::DIR_EXE
, &cur
))
339 if (base::mac::AmIBundled()) {
340 // If we're called from chrome, it's beside the app (outside the
341 // app bundle), if we're called from a unittest, we'll already be
342 // outside the bundle so use the exe dir.
343 // exe_dir gave us .../Chromium.app/Contents/MacOS/Chromium.
349 if (!GetInternalPluginsDirectory(&cur
))
352 cur
= cur
.Append(FILE_PATH_LITERAL("pnacl"));
354 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
355 #if defined(WIDEVINE_CDM_IS_COMPONENT)
356 case chrome::DIR_COMPONENT_WIDEVINE_CDM
:
357 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
359 cur
= cur
.Append(kWidevineCdmBaseDirectory
);
361 #endif // defined(WIDEVINE_CDM_IS_COMPONENT)
362 // TODO(xhwang): FILE_WIDEVINE_CDM_ADAPTER has different meanings.
363 // In the component case, this is the source adapter. Otherwise, it is the
364 // actual Pepper module that gets loaded.
365 case chrome::FILE_WIDEVINE_CDM_ADAPTER
:
366 if (!GetInternalPluginsDirectory(&cur
))
368 cur
= cur
.AppendASCII(kWidevineCdmAdapterFileName
);
370 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
371 case chrome::FILE_RESOURCES_PACK
:
372 #if defined(OS_MACOSX) && !defined(OS_IOS)
373 if (base::mac::AmIBundled()) {
374 cur
= base::mac::FrameworkBundlePath();
375 cur
= cur
.Append(FILE_PATH_LITERAL("Resources"))
376 .Append(FILE_PATH_LITERAL("resources.pak"));
379 #elif defined(OS_ANDROID)
380 if (!PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID
, &cur
))
383 // If we're not bundled on mac or Android, resources.pak should be next
384 // to the binary (e.g., for unit tests).
385 if (!PathService::Get(base::DIR_MODULE
, &cur
))
388 cur
= cur
.Append(FILE_PATH_LITERAL("resources.pak"));
390 case chrome::DIR_RESOURCES_EXTENSION
:
391 if (!PathService::Get(base::DIR_MODULE
, &cur
))
393 cur
= cur
.Append(FILE_PATH_LITERAL("resources"))
394 .Append(FILE_PATH_LITERAL("extension"));
396 #if defined(OS_CHROMEOS)
397 case chrome::DIR_CHROMEOS_WALLPAPERS
:
398 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
400 cur
= cur
.Append(FILE_PATH_LITERAL("wallpapers"));
402 case chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS
:
403 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
405 cur
= cur
.Append(FILE_PATH_LITERAL("wallpaper_thumbnails"));
407 case chrome::DIR_CHROMEOS_CUSTOM_WALLPAPERS
:
408 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
410 cur
= cur
.Append(FILE_PATH_LITERAL("custom_wallpapers"));
413 #if defined(OS_LINUX) && defined(ENABLE_SUPERVISED_USERS)
414 case chrome::DIR_SUPERVISED_USERS_DEFAULT_APPS
:
415 if (!PathService::Get(chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
, &cur
))
417 cur
= cur
.Append(FILE_PATH_LITERAL("managed_users"));
420 // The following are only valid in the development environment, and
421 // will fail if executed from an installed executable (because the
422 // generated path won't exist).
423 case chrome::DIR_GEN_TEST_DATA
:
424 #if defined(OS_ANDROID)
425 // On Android, our tests don't have permission to write to DIR_MODULE.
426 // gtest/test_runner.py pushes data to external storage.
427 if (!PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE
, &cur
))
430 if (!PathService::Get(base::DIR_MODULE
, &cur
))
433 cur
= cur
.Append(FILE_PATH_LITERAL("test_data"));
434 if (!base::PathExists(cur
)) // We don't want to create this.
437 case chrome::DIR_TEST_DATA
:
438 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &cur
))
440 cur
= cur
.Append(FILE_PATH_LITERAL("chrome"));
441 cur
= cur
.Append(FILE_PATH_LITERAL("test"));
442 cur
= cur
.Append(FILE_PATH_LITERAL("data"));
443 if (!base::PathExists(cur
)) // We don't want to create this.
446 case chrome::DIR_TEST_TOOLS
:
447 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &cur
))
449 cur
= cur
.Append(FILE_PATH_LITERAL("chrome"));
450 cur
= cur
.Append(FILE_PATH_LITERAL("tools"));
451 cur
= cur
.Append(FILE_PATH_LITERAL("test"));
452 if (!base::PathExists(cur
)) // We don't want to create this
455 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD)
456 case chrome::DIR_POLICY_FILES
: {
457 #if defined(GOOGLE_CHROME_BUILD)
458 cur
= base::FilePath(FILE_PATH_LITERAL("/etc/opt/chrome/policies"));
460 cur
= base::FilePath(FILE_PATH_LITERAL("/etc/chromium/policies"));
465 #if defined(OS_MACOSX) && !defined(OS_IOS)
466 case chrome::DIR_USER_LIBRARY
: {
467 if (!GetUserLibraryDirectory(&cur
))
469 if (!base::PathExists(cur
)) // We don't want to create this.
473 case chrome::DIR_USER_APPLICATIONS
: {
474 if (!GetUserApplicationsDirectory(&cur
))
476 if (!base::PathExists(cur
)) // We don't want to create this.
481 #if defined(OS_CHROMEOS) || (defined(OS_MACOSX) && !defined(OS_IOS))
482 case chrome::DIR_USER_EXTERNAL_EXTENSIONS
: {
483 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
485 cur
= cur
.Append(FILE_PATH_LITERAL("External Extensions"));
489 #if defined(OS_LINUX)
490 case chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
: {
491 cur
= base::FilePath(kFilepathSinglePrefExtensions
);
495 case chrome::DIR_EXTERNAL_EXTENSIONS
:
496 #if defined(OS_MACOSX) && !defined(OS_IOS)
497 if (!chrome::GetGlobalApplicationSupportDirectory(&cur
))
500 cur
= cur
.Append(FILE_PATH_LITERAL("Google"))
501 .Append(FILE_PATH_LITERAL("Chrome"))
502 .Append(FILE_PATH_LITERAL("External Extensions"));
505 if (!PathService::Get(base::DIR_MODULE
, &cur
))
508 cur
= cur
.Append(FILE_PATH_LITERAL("extensions"));
513 case chrome::DIR_DEFAULT_APPS
:
514 #if defined(OS_MACOSX)
515 cur
= base::mac::FrameworkBundlePath();
516 cur
= cur
.Append(FILE_PATH_LITERAL("Default Apps"));
518 if (!PathService::Get(chrome::DIR_APP
, &cur
))
520 cur
= cur
.Append(FILE_PATH_LITERAL("default_apps"));
524 #if defined(OS_LINUX) || (defined(OS_MACOSX) && !defined(OS_IOS))
525 case chrome::DIR_NATIVE_MESSAGING
:
526 #if defined(OS_MACOSX)
527 #if defined(GOOGLE_CHROME_BUILD)
528 cur
= base::FilePath(FILE_PATH_LITERAL(
529 "/Library/Google/Chrome/NativeMessagingHosts"));
531 cur
= base::FilePath(FILE_PATH_LITERAL(
532 "/Library/Application Support/Chromium/NativeMessagingHosts"));
534 #else // defined(OS_MACOSX)
535 #if defined(GOOGLE_CHROME_BUILD)
536 cur
= base::FilePath(FILE_PATH_LITERAL(
537 "/etc/opt/chrome/native-messaging-hosts"));
539 cur
= base::FilePath(FILE_PATH_LITERAL(
540 "/etc/chromium/native-messaging-hosts"));
542 #endif // !defined(OS_MACOSX)
545 case chrome::DIR_USER_NATIVE_MESSAGING
:
546 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
548 cur
= cur
.Append(FILE_PATH_LITERAL("NativeMessagingHosts"));
550 #endif // defined(OS_LINUX) || (defined(OS_MACOSX) && !defined(OS_IOS))
551 #if !defined(OS_ANDROID)
552 case chrome::DIR_GLOBAL_GCM_STORE
:
553 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
555 cur
= cur
.Append(kGCMStoreDirname
);
557 #endif // !defined(OS_ANDROID)
563 // TODO(bauerb): http://crbug.com/259796
564 base::ThreadRestrictions::ScopedAllowIO allow_io
;
565 if (create_dir
&& !base::PathExists(cur
) &&
566 !base::CreateDirectory(cur
))
573 // This cannot be done as a static initializer sadly since Visual Studio will
574 // eliminate this object file if there is no direct entry point into it.
575 void RegisterPathProvider() {
576 PathService::RegisterProvider(PathProvider
, PATH_START
, PATH_END
);
579 void SetInvalidSpecifiedUserDataDir(const base::FilePath
& user_data_dir
) {
580 g_invalid_specified_user_data_dir
.Get() = user_data_dir
;
583 const base::FilePath
& GetInvalidSpecifiedUserDataDir() {
584 return g_invalid_specified_user_data_dir
.Get();
587 } // namespace chrome