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.
4 
5 // Defines base::PathProviderAndroid which replaces base::PathProviderPosix for
6 // Android in base/path_service.cc.
7 
8 #include <limits.h>
9 #include <unistd.h>
10 
11 #include "base/android/jni_android.h"
12 #include "base/android/path_utils.h"
13 #include "base/base_paths.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/notreached.h"
17 #include "base/process/process_metrics.h"
18 
19 namespace base {
20 
PathProviderAndroid(int key,FilePath * result)21 bool PathProviderAndroid(int key, FilePath* result) {
22   switch (key) {
23     case base::FILE_EXE: {
24       FilePath bin_dir;
25       if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
26         NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
27         return false;
28       }
29       *result = bin_dir;
30       return true;
31     }
32     case base::FILE_MODULE:
33       // dladdr didn't work in Android as only the file name was returned.
34       NOTIMPLEMENTED();
35       return false;
36     case base::DIR_MODULE:
37       return base::android::GetNativeLibraryDirectory(result);
38     case base::DIR_SOURCE_ROOT:
39       // Used only by tests.
40       // In that context, hooked up via base/test/test_support_android.cc.
41       NOTIMPLEMENTED();
42       return false;
43     case base::DIR_USER_DESKTOP:
44       // Android doesn't support GetUserDesktop.
45       NOTIMPLEMENTED();
46       return false;
47     case base::DIR_CACHE:
48       return base::android::GetCacheDirectory(result);
49     case base::DIR_ASSETS:
50       // On Android assets are normally loaded from the APK using
51       // base::android::OpenApkAsset(). In tests, since the assets are no
52       // packaged, DIR_ASSETS is overridden to point to the build directory.
53       return false;
54     case base::DIR_ANDROID_APP_DATA:
55       return base::android::GetDataDirectory(result);
56     case base::DIR_ANDROID_EXTERNAL_STORAGE:
57       return base::android::GetExternalStorageDirectory(result);
58     default:
59       // Note: the path system expects this function to override the default
60       // behavior. So no need to log an error if we don't support a given
61       // path. The system will just use the default.
62       return false;
63   }
64 }
65 
66 }  // namespace base
67