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.
38 // The Pepper Flash plugins are in a directory with this name.
39 const base::FilePath::CharType kPepperFlashBaseDirectory
[] =
40 FILE_PATH_LITERAL("PepperFlash");
42 #if defined(OS_MACOSX) && !defined(OS_IOS)
43 const base::FilePath::CharType kPepperFlashSystemBaseDirectory
[] =
44 FILE_PATH_LITERAL("Internet Plug-Ins/PepperFlashPlayer");
45 const base::FilePath::CharType kFlashSystemBaseDirectory
[] =
46 FILE_PATH_LITERAL("Internet Plug-Ins");
47 const base::FilePath::CharType kFlashSystemPluginName
[] =
48 FILE_PATH_LITERAL("Flash Player.plugin");
51 const base::FilePath::CharType kInternalNaClPluginFileName
[] =
52 FILE_PATH_LITERAL("internal-nacl-plugin");
55 // The path to the external extension <id>.json files.
56 // /usr/share seems like a good choice, see: http://www.pathname.com/fhs/
57 const base::FilePath::CharType kFilepathSinglePrefExtensions
[] =
58 #if defined(GOOGLE_CHROME_BUILD)
59 FILE_PATH_LITERAL("/usr/share/google-chrome/extensions");
61 FILE_PATH_LITERAL("/usr/share/chromium/extensions");
62 #endif // defined(GOOGLE_CHROME_BUILD)
63 #endif // defined(OS_LINUX)
65 static base::LazyInstance
<base::FilePath
>
66 g_invalid_specified_user_data_dir
= LAZY_INSTANCE_INITIALIZER
;
68 // Gets the path for internal plugins.
69 bool GetInternalPluginsDirectory(base::FilePath
* result
) {
70 #if defined(OS_MACOSX) && !defined(OS_IOS)
71 // If called from Chrome, get internal plugins from a subdirectory of the
73 if (base::mac::AmIBundled()) {
74 *result
= chrome::GetFrameworkBundlePath();
75 DCHECK(!result
->empty());
76 *result
= result
->Append("Internet Plug-Ins");
79 // In tests, just look in the module directory (below).
82 // The rest of the world expects plugins in the module directory.
83 return PathService::Get(base::DIR_MODULE
, result
);
87 // Gets the Flash path if installed on the system. |is_npapi| determines whether
88 // to return the NPAPI of the PPAPI version of the system plugin.
89 bool GetSystemFlashFilename(base::FilePath
* out_path
, bool is_npapi
) {
90 const wchar_t kNpapiFlashRegistryRoot
[] =
91 L
"SOFTWARE\\Macromedia\\FlashPlayerPlugin";
92 const wchar_t kPepperFlashRegistryRoot
[] =
93 L
"SOFTWARE\\Macromedia\\FlashPlayerPepper";
94 const wchar_t kFlashPlayerPathValueName
[] = L
"PlayerPath";
96 base::win::RegKey
path_key(
98 is_npapi
? kNpapiFlashRegistryRoot
: kPepperFlashRegistryRoot
, KEY_READ
);
99 base::string16 path_str
;
100 if (FAILED(path_key
.ReadValue(kFlashPlayerPathValueName
, &path_str
)))
103 *out_path
= base::FilePath(path_str
);
112 bool PathProvider(int key
, base::FilePath
* result
) {
113 // Some keys are just aliases...
115 case chrome::DIR_APP
:
116 return PathService::Get(base::DIR_MODULE
, result
);
117 case chrome::DIR_LOGS
:
119 // Release builds write to the data dir
120 return PathService::Get(chrome::DIR_USER_DATA
, result
);
122 // Debug builds write next to the binary (in the build tree)
123 #if defined(OS_MACOSX)
124 if (!PathService::Get(base::DIR_EXE
, result
))
126 if (base::mac::AmIBundled()) {
127 // If we're called from chrome, dump it beside the app (outside the
128 // app bundle), if we're called from a unittest, we'll already
129 // outside the bundle so use the exe dir.
130 // exe_dir gave us .../Chromium.app/Contents/MacOS/Chromium.
131 *result
= result
->DirName();
132 *result
= result
->DirName();
133 *result
= result
->DirName();
137 return PathService::Get(base::DIR_EXE
, result
);
138 #endif // defined(OS_MACOSX)
140 case chrome::FILE_RESOURCE_MODULE
:
141 return PathService::Get(base::FILE_MODULE
, result
);
144 // Assume that we will not need to create the directory if it does not exist.
145 // This flag can be set to true for the cases where we want to create it.
146 bool create_dir
= false;
150 case chrome::DIR_USER_DATA
:
151 if (!GetDefaultUserDataDirectory(&cur
)) {
157 case chrome::DIR_USER_DOCUMENTS
:
158 if (!GetUserDocumentsDirectory(&cur
))
162 case chrome::DIR_USER_MUSIC
:
163 if (!GetUserMusicDirectory(&cur
))
166 case chrome::DIR_USER_PICTURES
:
167 if (!GetUserPicturesDirectory(&cur
))
170 case chrome::DIR_USER_VIDEOS
:
171 if (!GetUserVideosDirectory(&cur
))
174 case chrome::DIR_DEFAULT_DOWNLOADS_SAFE
:
175 #if defined(OS_WIN) || defined(OS_LINUX)
176 if (!GetUserDownloadsDirectorySafe(&cur
))
180 // Fall through for all other platforms.
182 case chrome::DIR_DEFAULT_DOWNLOADS
:
183 #if defined(OS_ANDROID)
184 if (!base::android::GetDownloadsDirectory(&cur
))
187 if (!GetUserDownloadsDirectory(&cur
))
189 // Do not create the download directory here, we have done it twice now
190 // and annoyed a lot of users.
193 case chrome::DIR_CRASH_DUMPS
:
194 #if defined(OS_CHROMEOS)
195 // ChromeOS uses a separate directory. See http://crosbug.com/25089
196 cur
= base::FilePath("/var/log/chrome");
197 #elif defined(OS_ANDROID)
198 if (!base::android::GetCacheDirectory(&cur
))
201 // The crash reports are always stored relative to the default user data
202 // directory. This avoids the problem of having to re-initialize the
203 // exception handler after parsing command line options, which may
204 // override the location of the app's profile directory.
205 if (!GetDefaultUserDataDirectory(&cur
))
208 #if defined(OS_MACOSX)
209 cur
= cur
.Append(FILE_PATH_LITERAL("Crashpad"));
211 cur
= cur
.Append(FILE_PATH_LITERAL("Crash Reports"));
216 case chrome::DIR_WATCHER_DATA
:
217 // The watcher data is always stored relative to the default user data
218 // directory. This allows the watcher to be initialized before
219 // command-line options have been parsed.
220 if (!GetDefaultUserDataDirectory(&cur
))
222 cur
= cur
.Append(FILE_PATH_LITERAL("Diagnostics"));
225 case chrome::DIR_RESOURCES
:
226 #if defined(OS_MACOSX)
227 cur
= base::mac::FrameworkBundlePath();
228 cur
= cur
.Append(FILE_PATH_LITERAL("Resources"));
230 if (!PathService::Get(chrome::DIR_APP
, &cur
))
232 cur
= cur
.Append(FILE_PATH_LITERAL("resources"));
235 case chrome::DIR_INSPECTOR
:
236 if (!PathService::Get(chrome::DIR_RESOURCES
, &cur
))
238 cur
= cur
.Append(FILE_PATH_LITERAL("inspector"));
240 case chrome::DIR_APP_DICTIONARIES
:
241 #if defined(OS_POSIX)
242 // We can't write into the EXE dir on Linux, so keep dictionaries
243 // alongside the safe browsing database in the user data dir.
244 // And we don't want to write into the bundle on the Mac, so push
245 // it to the user data dir there also.
246 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
249 if (!PathService::Get(base::DIR_EXE
, &cur
))
252 cur
= cur
.Append(FILE_PATH_LITERAL("Dictionaries"));
255 case chrome::DIR_INTERNAL_PLUGINS
:
256 if (!GetInternalPluginsDirectory(&cur
))
259 case chrome::DIR_PEPPER_FLASH_PLUGIN
:
260 if (!GetInternalPluginsDirectory(&cur
))
262 cur
= cur
.Append(kPepperFlashBaseDirectory
);
264 case chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
:
265 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
267 cur
= cur
.Append(kPepperFlashBaseDirectory
);
269 case chrome::FILE_PEPPER_FLASH_SYSTEM_PLUGIN
:
271 if (!GetSystemFlashFilename(&cur
, false))
273 #elif defined(OS_MACOSX) && !defined(OS_IOS)
274 if (!GetLocalLibraryDirectory(&cur
))
276 cur
= cur
.Append(kPepperFlashSystemBaseDirectory
);
277 cur
= cur
.Append(chrome::kPepperFlashPluginFilename
);
279 // Chrome on iOS does not supports PPAPI binaries, return false.
280 // TODO(wfh): If Adobe release PPAPI binaries for Linux, add support here.
284 case chrome::FILE_FLASH_SYSTEM_PLUGIN
:
286 if (!GetSystemFlashFilename(&cur
, true))
288 #elif defined(OS_MACOSX) && !defined(OS_IOS)
289 if (!GetLocalLibraryDirectory(&cur
))
291 cur
= cur
.Append(kFlashSystemBaseDirectory
);
292 cur
= cur
.Append(kFlashSystemPluginName
);
294 // Chrome on other platforms does not supports system NPAPI binaries.
298 case chrome::FILE_LOCAL_STATE
:
299 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
301 cur
= cur
.Append(chrome::kLocalStateFilename
);
303 case chrome::FILE_RECORDED_SCRIPT
:
304 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
306 cur
= cur
.Append(FILE_PATH_LITERAL("script.log"));
308 case chrome::FILE_PEPPER_FLASH_PLUGIN
:
309 if (!PathService::Get(chrome::DIR_PEPPER_FLASH_PLUGIN
, &cur
))
311 cur
= cur
.Append(chrome::kPepperFlashPluginFilename
);
313 // TODO(teravest): Remove this case once the internal NaCl plugin is gone.
314 // We currently need a path here to look up whether the plugin is disabled
315 // and what its permissions are.
316 case chrome::FILE_NACL_PLUGIN
:
317 if (!GetInternalPluginsDirectory(&cur
))
319 cur
= cur
.Append(kInternalNaClPluginFileName
);
321 // PNaCl is currenly installable via the component updater or by being
322 // simply built-in. DIR_PNACL_BASE is used as the base directory for
323 // installation via component updater. DIR_PNACL_COMPONENT will be
324 // the final location of pnacl, which is a subdir of DIR_PNACL_BASE.
325 case chrome::DIR_PNACL_BASE
:
326 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
328 cur
= cur
.Append(FILE_PATH_LITERAL("pnacl"));
330 // Where PNaCl files are ultimately located. The default finds the files
331 // inside the InternalPluginsDirectory / build directory, as if it
332 // was shipped along with chrome. The value can be overridden
333 // if it is installed via component updater.
334 case chrome::DIR_PNACL_COMPONENT
:
335 #if defined(OS_MACOSX)
336 // PNaCl really belongs in the InternalPluginsDirectory but actually
337 // copying it there would result in the files also being shipped, which
338 // we don't want yet. So for now, just find them in the directory where
340 if (!PathService::Get(base::DIR_EXE
, &cur
))
342 if (base::mac::AmIBundled()) {
343 // If we're called from chrome, it's beside the app (outside the
344 // app bundle), if we're called from a unittest, we'll already be
345 // outside the bundle so use the exe dir.
346 // exe_dir gave us .../Chromium.app/Contents/MacOS/Chromium.
352 if (!GetInternalPluginsDirectory(&cur
))
355 cur
= cur
.Append(FILE_PATH_LITERAL("pnacl"));
357 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
358 #if defined(WIDEVINE_CDM_IS_COMPONENT)
359 case chrome::DIR_COMPONENT_WIDEVINE_CDM
:
360 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
362 cur
= cur
.Append(kWidevineCdmBaseDirectory
);
364 #endif // defined(WIDEVINE_CDM_IS_COMPONENT)
365 // TODO(xhwang): FILE_WIDEVINE_CDM_ADAPTER has different meanings.
366 // In the component case, this is the source adapter. Otherwise, it is the
367 // actual Pepper module that gets loaded.
368 case chrome::FILE_WIDEVINE_CDM_ADAPTER
:
369 if (!GetInternalPluginsDirectory(&cur
))
371 cur
= cur
.AppendASCII(kWidevineCdmAdapterFileName
);
373 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
374 case chrome::FILE_RESOURCES_PACK
:
375 #if defined(OS_MACOSX) && !defined(OS_IOS)
376 if (base::mac::AmIBundled()) {
377 cur
= base::mac::FrameworkBundlePath();
378 cur
= cur
.Append(FILE_PATH_LITERAL("Resources"))
379 .Append(FILE_PATH_LITERAL("resources.pak"));
382 #elif defined(OS_ANDROID)
383 if (!PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID
, &cur
))
386 // If we're not bundled on mac or Android, resources.pak should be next
387 // to the binary (e.g., for unit tests).
388 if (!PathService::Get(base::DIR_MODULE
, &cur
))
391 cur
= cur
.Append(FILE_PATH_LITERAL("resources.pak"));
393 case chrome::DIR_RESOURCES_EXTENSION
:
394 if (!PathService::Get(base::DIR_MODULE
, &cur
))
396 cur
= cur
.Append(FILE_PATH_LITERAL("resources"))
397 .Append(FILE_PATH_LITERAL("extension"));
399 #if defined(OS_CHROMEOS)
400 case chrome::DIR_CHROMEOS_WALLPAPERS
:
401 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
403 cur
= cur
.Append(FILE_PATH_LITERAL("wallpapers"));
405 case chrome::DIR_CHROMEOS_WALLPAPER_THUMBNAILS
:
406 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
408 cur
= cur
.Append(FILE_PATH_LITERAL("wallpaper_thumbnails"));
410 case chrome::DIR_CHROMEOS_CUSTOM_WALLPAPERS
:
411 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
413 cur
= cur
.Append(FILE_PATH_LITERAL("custom_wallpapers"));
416 #if defined(OS_LINUX) && defined(ENABLE_SUPERVISED_USERS)
417 case chrome::DIR_SUPERVISED_USERS_DEFAULT_APPS
:
418 if (!PathService::Get(chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
, &cur
))
420 cur
= cur
.Append(FILE_PATH_LITERAL("managed_users"));
423 // The following are only valid in the development environment, and
424 // will fail if executed from an installed executable (because the
425 // generated path won't exist).
426 case chrome::DIR_GEN_TEST_DATA
:
427 #if defined(OS_ANDROID)
428 // On Android, our tests don't have permission to write to DIR_MODULE.
429 // gtest/test_runner.py pushes data to external storage.
430 if (!PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE
, &cur
))
433 if (!PathService::Get(base::DIR_MODULE
, &cur
))
436 cur
= cur
.Append(FILE_PATH_LITERAL("test_data"));
437 if (!base::PathExists(cur
)) // We don't want to create this.
440 case chrome::DIR_TEST_DATA
:
441 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &cur
))
443 cur
= cur
.Append(FILE_PATH_LITERAL("chrome"));
444 cur
= cur
.Append(FILE_PATH_LITERAL("test"));
445 cur
= cur
.Append(FILE_PATH_LITERAL("data"));
446 if (!base::PathExists(cur
)) // We don't want to create this.
449 case chrome::DIR_TEST_TOOLS
:
450 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &cur
))
452 cur
= cur
.Append(FILE_PATH_LITERAL("chrome"));
453 cur
= cur
.Append(FILE_PATH_LITERAL("tools"));
454 cur
= cur
.Append(FILE_PATH_LITERAL("test"));
455 if (!base::PathExists(cur
)) // We don't want to create this
458 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD)
459 case chrome::DIR_POLICY_FILES
: {
460 #if defined(GOOGLE_CHROME_BUILD)
461 cur
= base::FilePath(FILE_PATH_LITERAL("/etc/opt/chrome/policies"));
463 cur
= base::FilePath(FILE_PATH_LITERAL("/etc/chromium/policies"));
468 #if defined(OS_MACOSX) && !defined(OS_IOS)
469 case chrome::DIR_USER_LIBRARY
: {
470 if (!GetUserLibraryDirectory(&cur
))
472 if (!base::PathExists(cur
)) // We don't want to create this.
476 case chrome::DIR_USER_APPLICATIONS
: {
477 if (!GetUserApplicationsDirectory(&cur
))
479 if (!base::PathExists(cur
)) // We don't want to create this.
484 #if defined(OS_CHROMEOS) || (defined(OS_LINUX) && defined(CHROMIUM_BUILD)) || \
485 (defined(OS_MACOSX) && !defined(OS_IOS))
486 case chrome::DIR_USER_EXTERNAL_EXTENSIONS
: {
487 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
489 cur
= cur
.Append(FILE_PATH_LITERAL("External Extensions"));
493 #if defined(OS_LINUX)
494 case chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
: {
495 cur
= base::FilePath(kFilepathSinglePrefExtensions
);
499 case chrome::DIR_EXTERNAL_EXTENSIONS
:
500 #if defined(OS_MACOSX) && !defined(OS_IOS)
501 if (!chrome::GetGlobalApplicationSupportDirectory(&cur
))
504 cur
= cur
.Append(FILE_PATH_LITERAL("Google"))
505 .Append(FILE_PATH_LITERAL("Chrome"))
506 .Append(FILE_PATH_LITERAL("External Extensions"));
509 if (!PathService::Get(base::DIR_MODULE
, &cur
))
512 cur
= cur
.Append(FILE_PATH_LITERAL("extensions"));
517 case chrome::DIR_DEFAULT_APPS
:
518 #if defined(OS_MACOSX)
519 cur
= base::mac::FrameworkBundlePath();
520 cur
= cur
.Append(FILE_PATH_LITERAL("Default Apps"));
522 if (!PathService::Get(chrome::DIR_APP
, &cur
))
524 cur
= cur
.Append(FILE_PATH_LITERAL("default_apps"));
528 #if defined(OS_LINUX) || (defined(OS_MACOSX) && !defined(OS_IOS))
529 case chrome::DIR_NATIVE_MESSAGING
:
530 #if defined(OS_MACOSX)
531 #if defined(GOOGLE_CHROME_BUILD)
532 cur
= base::FilePath(FILE_PATH_LITERAL(
533 "/Library/Google/Chrome/NativeMessagingHosts"));
535 cur
= base::FilePath(FILE_PATH_LITERAL(
536 "/Library/Application Support/Chromium/NativeMessagingHosts"));
538 #else // defined(OS_MACOSX)
539 #if defined(GOOGLE_CHROME_BUILD)
540 cur
= base::FilePath(FILE_PATH_LITERAL(
541 "/etc/opt/chrome/native-messaging-hosts"));
543 cur
= base::FilePath(FILE_PATH_LITERAL(
544 "/etc/chromium/native-messaging-hosts"));
546 #endif // !defined(OS_MACOSX)
549 case chrome::DIR_USER_NATIVE_MESSAGING
:
550 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
552 cur
= cur
.Append(FILE_PATH_LITERAL("NativeMessagingHosts"));
554 #endif // defined(OS_LINUX) || (defined(OS_MACOSX) && !defined(OS_IOS))
555 #if !defined(OS_ANDROID)
556 case chrome::DIR_GLOBAL_GCM_STORE
:
557 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
559 cur
= cur
.Append(kGCMStoreDirname
);
561 #endif // !defined(OS_ANDROID)
562 #if defined(OS_ANDROID)
563 case chrome::DIR_OFFLINE_PAGE_METADATA
:
564 if (!PathService::Get(chrome::DIR_USER_DATA
, &cur
))
566 cur
= cur
.Append(kOfflinePageMetadataDirname
);
568 #endif // defined(OS_ANDROID)
574 // TODO(bauerb): http://crbug.com/259796
575 base::ThreadRestrictions::ScopedAllowIO allow_io
;
576 if (create_dir
&& !base::PathExists(cur
) &&
577 !base::CreateDirectory(cur
))
584 // This cannot be done as a static initializer sadly since Visual Studio will
585 // eliminate this object file if there is no direct entry point into it.
586 void RegisterPathProvider() {
587 PathService::RegisterProvider(PathProvider
, PATH_START
, PATH_END
);
590 void SetInvalidSpecifiedUserDataDir(const base::FilePath
& user_data_dir
) {
591 g_invalid_specified_user_data_dir
.Get() = user_data_dir
;
594 const base::FilePath
& GetInvalidSpecifiedUserDataDir() {
595 return g_invalid_specified_user_data_dir
.Get();
598 } // namespace chrome