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");
69 const base::FilePath::CharType kEffectsPluginFileName
[] =
71 FILE_PATH_LITERAL("pepper/libppeffects.dll");
72 #elif defined(OS_MACOSX)
73 FILE_PATH_LITERAL("pepper/libppeffects.plugin");
74 #else // Linux and Chrome OS
75 FILE_PATH_LITERAL("pepper/libppeffects.so");
79 // The path to the external extension <id>.json files.
80 // /usr/share seems like a good choice, see: http://www.pathname.com/fhs/
81 const base::FilePath::CharType kFilepathSinglePrefExtensions
[] =
82 #if defined(GOOGLE_CHROME_BUILD)
83 FILE_PATH_LITERAL("/usr/share/google-chrome/extensions");
85 FILE_PATH_LITERAL("/usr/share/chromium/extensions");
86 #endif // defined(GOOGLE_CHROME_BUILD)
87 #endif // defined(OS_LINUX)
89 static base::LazyInstance
<base::FilePath
>
90 g_invalid_specified_user_data_dir
= LAZY_INSTANCE_INITIALIZER
;
92 // Gets the path for internal plugins.
93 bool GetInternalPluginsDirectory(base::FilePath
* result
) {
94 #if defined(OS_MACOSX) && !defined(OS_IOS)
95 // If called from Chrome, get internal plugins from a subdirectory of the
97 if (base::mac::AmIBundled()) {
98 *result
= chrome::GetFrameworkBundlePath();
99 DCHECK(!result
->empty());
100 *result
= result
->Append("Internet Plug-Ins");
103 // In tests, just look in the module directory (below).
106 // The rest of the world expects plugins in the module directory.
107 return PathService::Get(base::DIR_MODULE
, result
);
111 // Gets the Flash path if installed on the system.
112 bool GetSystemFlashDirectory(base::FilePath
* out_path
) {
113 base::win::RegKey
path_key(HKEY_LOCAL_MACHINE
, kFlashRegistryRoot
, KEY_READ
);
114 base::string16 path_str
;
115 if (FAILED(path_key
.ReadValue(kFlashPlayerPathValueName
, &path_str
)))
117 base::FilePath plugin_path
= base::FilePath(path_str
).DirName();
119 *out_path
= plugin_path
;
128 bool PathProvider(int key
, base::FilePath
* result
) {
129 // Some keys are just aliases...
131 case chrome::DIR_APP
:
132 return PathService::Get(base::DIR_MODULE
, result
);
133 case chrome::DIR_LOGS
:
135 // Release builds write to the data dir
136 return PathService::Get(chrome::DIR_USER_DATA
, result
);
138 // Debug builds write next to the binary (in the build tree)
139 #if defined(OS_MACOSX)
140 if (!PathService::Get(base::DIR_EXE
, result
))
142 if (base::mac::AmIBundled()) {
143 // If we're called from chrome, dump it beside the app (outside the
144 // app bundle), if we're called from a unittest, we'll already
145 // outside the bundle so use the exe dir.
146 // exe_dir gave us .../Chromium.app/Contents/MacOS/Chromium.
147 *result
= result
->DirName();
148 *result
= result
->DirName();
149 *result
= result
->DirName();
153 return PathService::Get(base::DIR_EXE
, result
);
154 #endif // defined(OS_MACOSX)
156 case chrome::FILE_RESOURCE_MODULE
:
157 return PathService::Get(base::FILE_MODULE
, result
);
160 // Assume that we will not need to create the directory if it does not exist.
161 // This flag can be set to true for the cases where we want to create it.
162 bool create_dir
= false;
166 case chrome::DIR_USER_DATA
:
167 if (!GetDefaultUserDataDirectory(&cur
)) {
173 case chrome::DIR_USER_DOCUMENTS
:
174 if (!GetUserDocumentsDirectory(&cur
))
178 case chrome::DIR_USER_MUSIC
:
179 if (!GetUserMusicDirectory(&cur
))
182 case chrome::DIR_USER_PICTURES
:
183 if (!GetUserPicturesDirectory(&cur
))
186 case chrome::DIR_USER_VIDEOS
:
187 if (!GetUserVideosDirectory(&cur
))
190 case chrome::DIR_DEFAULT_DOWNLOADS_SAFE
:
191 #if defined(OS_WIN) || defined(OS_LINUX)
192 if (!GetUserDownloadsDirectorySafe(&cur
))
196 // Fall through for all other platforms.
198 case chrome::DIR_DEFAULT_DOWNLOADS
:
199 #if defined(OS_ANDROID)
200 if (!base::android::GetDownloadsDirectory(&cur
))
203 if (!GetUserDownloadsDirectory(&cur
))
205 // Do not create the download directory here, we have done it twice now
206 // and annoyed a lot of users.
209 case chrome::DIR_CRASH_DUMPS
:
210 #if defined(OS_CHROMEOS)
211 // ChromeOS uses a separate directory. See http://crosbug.com/25089
212 cur
= base::FilePath("/var/log/chrome");
213 #elif defined(OS_ANDROID)
214 if (!base::android::GetCacheDirectory(&cur
))
217 // The crash reports are always stored relative to the default user data
218 // directory. This avoids the problem of having to re-initialize the
219 // exception handler after parsing command line options, which may
220 // override the location of the app's profile directory.
221 if (!GetDefaultUserDataDirectory(&cur
))
224 #if defined(OS_MACOSX)
225 cur
= cur
.Append(FILE_PATH_LITERAL("Crashpad"));
227 cur
= cur
.Append(FILE_PATH_LITERAL("Crash Reports"));
232 case chrome::DIR_WATCHER_DATA
:
233 // The watcher data is always stored relative to the default user data
234 // directory. This allows the watcher to be initialized before
235 // command-line options have been parsed.
236 if (!GetDefaultUserDataDirectory(&cur
))
238 cur
= cur
.Append(FILE_PATH_LITERAL("Diagnostics"));
241 case chrome::DIR_RESOURCES
:
242 #if defined(OS_MACOSX)
243 cur
= base::mac::FrameworkBundlePath();
244 cur
= cur
.Append(FILE_PATH_LITERAL("Resources"));
246 if (!PathService::Get(chrome::DIR_APP
, &cur
))
248 cur
= cur
.Append(FILE_PATH_LITERAL("resources"));
251 case chrome::DIR_INSPECTOR
:
252 if (!PathService::Get(chrome::DIR_RESOURCES
, &cur
))
254 cur
= cur
.Append(FILE_PATH_LITERAL("inspector"));
256 case chrome::DIR_APP_DICTIONARIES
:
257 #if defined(OS_POSIX)
258 // We can't write into the EXE dir on Linux, so keep dictionaries
259 // alongside the safe browsing database in the user data dir.
260 // And we don't want to write into the bundle on the Mac, so push
261 // it to the user data dir there also.
262 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
265 if (!PathService::Get(base::DIR_EXE
, &cur
))
268 cur
= cur
.Append(FILE_PATH_LITERAL("Dictionaries"));
271 case chrome::DIR_INTERNAL_PLUGINS
:
272 if (!GetInternalPluginsDirectory(&cur
))
275 case chrome::DIR_PEPPER_FLASH_PLUGIN
:
276 if (!GetInternalPluginsDirectory(&cur
))
278 cur
= cur
.Append(kPepperFlashBaseDirectory
);
280 case chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
:
281 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
283 cur
= cur
.Append(kPepperFlashBaseDirectory
);
285 case chrome::DIR_PEPPER_FLASH_SYSTEM_PLUGIN
:
287 if (!GetSystemFlashDirectory(&cur
))
289 #elif defined(OS_MACOSX) && !defined(OS_IOS)
290 if (!GetLocalLibraryDirectory(&cur
))
292 cur
= cur
.Append(kPepperFlashSystemBaseDirectory
);
294 // Chrome on iOS does not supports PPAPI binaries, return false.
295 // TODO(wfh): If Adobe release PPAPI binaries for Linux, add support here.
299 case chrome::FILE_LOCAL_STATE
:
300 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
302 cur
= cur
.Append(chrome::kLocalStateFilename
);
304 case chrome::FILE_RECORDED_SCRIPT
:
305 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
307 cur
= cur
.Append(FILE_PATH_LITERAL("script.log"));
309 case chrome::FILE_FLASH_PLUGIN
:
310 if (!GetInternalPluginsDirectory(&cur
))
312 cur
= cur
.Append(kInternalFlashPluginFileName
);
314 case chrome::FILE_PEPPER_FLASH_PLUGIN
:
315 if (!PathService::Get(chrome::DIR_PEPPER_FLASH_PLUGIN
, &cur
))
317 cur
= cur
.Append(chrome::kPepperFlashPluginFilename
);
319 case chrome::FILE_EFFECTS_PLUGIN
:
320 if (!GetInternalPluginsDirectory(&cur
))
322 cur
= cur
.Append(kEffectsPluginFileName
);
324 // TODO(teravest): Remove this case once the internal NaCl plugin is gone.
325 // We currently need a path here to look up whether the plugin is disabled
326 // and what its permissions are.
327 case chrome::FILE_NACL_PLUGIN
:
328 if (!GetInternalPluginsDirectory(&cur
))
330 cur
= cur
.Append(kInternalNaClPluginFileName
);
332 // PNaCl is currenly installable via the component updater or by being
333 // simply built-in. DIR_PNACL_BASE is used as the base directory for
334 // installation via component updater. DIR_PNACL_COMPONENT will be
335 // the final location of pnacl, which is a subdir of DIR_PNACL_BASE.
336 case chrome::DIR_PNACL_BASE
:
337 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
339 cur
= cur
.Append(FILE_PATH_LITERAL("pnacl"));
341 // Where PNaCl files are ultimately located. The default finds the files
342 // inside the InternalPluginsDirectory / build directory, as if it
343 // was shipped along with chrome. The value can be overridden
344 // if it is installed via component updater.
345 case chrome::DIR_PNACL_COMPONENT
:
346 #if defined(OS_MACOSX)
347 // PNaCl really belongs in the InternalPluginsDirectory but actually
348 // copying it there would result in the files also being shipped, which
349 // we don't want yet. So for now, just find them in the directory where
351 if (!PathService::Get(base::DIR_EXE
, &cur
))
353 if (base::mac::AmIBundled()) {
354 // If we're called from chrome, it's beside the app (outside the
355 // app bundle), if we're called from a unittest, we'll already be
356 // outside the bundle so use the exe dir.
357 // exe_dir gave us .../Chromium.app/Contents/MacOS/Chromium.
363 if (!GetInternalPluginsDirectory(&cur
))
366 cur
= cur
.Append(FILE_PATH_LITERAL("pnacl"));
368 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
369 #if defined(WIDEVINE_CDM_IS_COMPONENT)
370 case chrome::DIR_COMPONENT_WIDEVINE_CDM
:
371 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
373 cur
= cur
.Append(kWidevineCdmBaseDirectory
);
375 #endif // defined(WIDEVINE_CDM_IS_COMPONENT)
376 // TODO(xhwang): FILE_WIDEVINE_CDM_ADAPTER has different meanings.
377 // In the component case, this is the source adapter. Otherwise, it is the
378 // actual Pepper module that gets loaded.
379 case chrome::FILE_WIDEVINE_CDM_ADAPTER
:
380 if (!GetInternalPluginsDirectory(&cur
))
382 cur
= cur
.AppendASCII(kWidevineCdmAdapterFileName
);
384 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
385 case chrome::FILE_RESOURCES_PACK
:
386 #if defined(OS_MACOSX) && !defined(OS_IOS)
387 if (base::mac::AmIBundled()) {
388 cur
= base::mac::FrameworkBundlePath();
389 cur
= cur
.Append(FILE_PATH_LITERAL("Resources"))
390 .Append(FILE_PATH_LITERAL("resources.pak"));
393 #elif defined(OS_ANDROID)
394 if (!PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID
, &cur
))
397 // If we're not bundled on mac or Android, resources.pak should be next
398 // to the binary (e.g., for unit tests).
399 if (!PathService::Get(base::DIR_MODULE
, &cur
))
402 cur
= cur
.Append(FILE_PATH_LITERAL("resources.pak"));
404 case chrome::DIR_RESOURCES_EXTENSION
:
405 if (!PathService::Get(base::DIR_MODULE
, &cur
))
407 cur
= cur
.Append(FILE_PATH_LITERAL("resources"))
408 .Append(FILE_PATH_LITERAL("extension"));
410 #if defined(OS_CHROMEOS)
411 case chrome::DIR_CHROMEOS_WALLPAPERS
:
412 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
414 cur
= cur
.Append(FILE_PATH_LITERAL("wallpapers"));
416 case chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS
:
417 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
419 cur
= cur
.Append(FILE_PATH_LITERAL("wallpaper_thumbnails"));
421 case chrome::DIR_CHROMEOS_CUSTOM_WALLPAPERS
:
422 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
424 cur
= cur
.Append(FILE_PATH_LITERAL("custom_wallpapers"));
427 #if defined(OS_LINUX) && defined(ENABLE_SUPERVISED_USERS)
428 case chrome::DIR_SUPERVISED_USERS_DEFAULT_APPS
:
429 if (!PathService::Get(chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
, &cur
))
431 cur
= cur
.Append(FILE_PATH_LITERAL("managed_users"));
434 // The following are only valid in the development environment, and
435 // will fail if executed from an installed executable (because the
436 // generated path won't exist).
437 case chrome::DIR_GEN_TEST_DATA
:
438 #if defined(OS_ANDROID)
439 // On Android, our tests don't have permission to write to DIR_MODULE.
440 // gtest/test_runner.py pushes data to external storage.
441 if (!PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE
, &cur
))
444 if (!PathService::Get(base::DIR_MODULE
, &cur
))
447 cur
= cur
.Append(FILE_PATH_LITERAL("test_data"));
448 if (!base::PathExists(cur
)) // We don't want to create this.
451 case chrome::DIR_TEST_DATA
:
452 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &cur
))
454 cur
= cur
.Append(FILE_PATH_LITERAL("chrome"));
455 cur
= cur
.Append(FILE_PATH_LITERAL("test"));
456 cur
= cur
.Append(FILE_PATH_LITERAL("data"));
457 if (!base::PathExists(cur
)) // We don't want to create this.
460 case chrome::DIR_TEST_TOOLS
:
461 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &cur
))
463 cur
= cur
.Append(FILE_PATH_LITERAL("chrome"));
464 cur
= cur
.Append(FILE_PATH_LITERAL("tools"));
465 cur
= cur
.Append(FILE_PATH_LITERAL("test"));
466 if (!base::PathExists(cur
)) // We don't want to create this
469 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD)
470 case chrome::DIR_POLICY_FILES
: {
471 #if defined(GOOGLE_CHROME_BUILD)
472 cur
= base::FilePath(FILE_PATH_LITERAL("/etc/opt/chrome/policies"));
474 cur
= base::FilePath(FILE_PATH_LITERAL("/etc/chromium/policies"));
479 #if defined(OS_MACOSX) && !defined(OS_IOS)
480 case chrome::DIR_USER_LIBRARY
: {
481 if (!GetUserLibraryDirectory(&cur
))
483 if (!base::PathExists(cur
)) // We don't want to create this.
487 case chrome::DIR_USER_APPLICATIONS
: {
488 if (!GetUserApplicationsDirectory(&cur
))
490 if (!base::PathExists(cur
)) // We don't want to create this.
495 #if defined(OS_CHROMEOS) || (defined(OS_MACOSX) && !defined(OS_IOS))
496 case chrome::DIR_USER_EXTERNAL_EXTENSIONS
: {
497 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
499 cur
= cur
.Append(FILE_PATH_LITERAL("External Extensions"));
503 #if defined(OS_LINUX)
504 case chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
: {
505 cur
= base::FilePath(kFilepathSinglePrefExtensions
);
509 case chrome::DIR_EXTERNAL_EXTENSIONS
:
510 #if defined(OS_MACOSX) && !defined(OS_IOS)
511 if (!chrome::GetGlobalApplicationSupportDirectory(&cur
))
514 cur
= cur
.Append(FILE_PATH_LITERAL("Google"))
515 .Append(FILE_PATH_LITERAL("Chrome"))
516 .Append(FILE_PATH_LITERAL("External Extensions"));
519 if (!PathService::Get(base::DIR_MODULE
, &cur
))
522 cur
= cur
.Append(FILE_PATH_LITERAL("extensions"));
527 case chrome::DIR_DEFAULT_APPS
:
528 #if defined(OS_MACOSX)
529 cur
= base::mac::FrameworkBundlePath();
530 cur
= cur
.Append(FILE_PATH_LITERAL("Default Apps"));
532 if (!PathService::Get(chrome::DIR_APP
, &cur
))
534 cur
= cur
.Append(FILE_PATH_LITERAL("default_apps"));
538 #if defined(OS_LINUX) || (defined(OS_MACOSX) && !defined(OS_IOS))
539 case chrome::DIR_NATIVE_MESSAGING
:
540 #if defined(OS_MACOSX)
541 #if defined(GOOGLE_CHROME_BUILD)
542 cur
= base::FilePath(FILE_PATH_LITERAL(
543 "/Library/Google/Chrome/NativeMessagingHosts"));
545 cur
= base::FilePath(FILE_PATH_LITERAL(
546 "/Library/Application Support/Chromium/NativeMessagingHosts"));
548 #else // defined(OS_MACOSX)
549 #if defined(GOOGLE_CHROME_BUILD)
550 cur
= base::FilePath(FILE_PATH_LITERAL(
551 "/etc/opt/chrome/native-messaging-hosts"));
553 cur
= base::FilePath(FILE_PATH_LITERAL(
554 "/etc/chromium/native-messaging-hosts"));
556 #endif // !defined(OS_MACOSX)
559 case chrome::DIR_USER_NATIVE_MESSAGING
:
560 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
562 cur
= cur
.Append(FILE_PATH_LITERAL("NativeMessagingHosts"));
564 #endif // defined(OS_LINUX) || (defined(OS_MACOSX) && !defined(OS_IOS))
565 #if !defined(OS_ANDROID)
566 case chrome::DIR_GLOBAL_GCM_STORE
:
567 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
569 cur
= cur
.Append(kGCMStoreDirname
);
571 #endif // !defined(OS_ANDROID)
577 // TODO(bauerb): http://crbug.com/259796
578 base::ThreadRestrictions::ScopedAllowIO allow_io
;
579 if (create_dir
&& !base::PathExists(cur
) &&
580 !base::CreateDirectory(cur
))
587 // This cannot be done as a static initializer sadly since Visual Studio will
588 // eliminate this object file if there is no direct entry point into it.
589 void RegisterPathProvider() {
590 PathService::RegisterProvider(PathProvider
, PATH_START
, PATH_END
);
593 void SetInvalidSpecifiedUserDataDir(const base::FilePath
& user_data_dir
) {
594 g_invalid_specified_user_data_dir
.Get() = user_data_dir
;
597 const base::FilePath
& GetInvalidSpecifiedUserDataDir() {
598 return g_invalid_specified_user_data_dir
.Get();
601 } // namespace chrome