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/logging.h"
17#include "base/process/process_metrics.h"
18
19namespace base {
20
21bool PathProviderAndroid(int key, FilePath* result) {
22  switch (key) {
23    case base::FILE_EXE: {
24      char bin_dir[PATH_MAX + 1];
25      int bin_dir_size = readlink(kProcSelfExe, bin_dir, PATH_MAX);
26      if (bin_dir_size < 0 || bin_dir_size > PATH_MAX) {
27        NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
28        return false;
29      }
30      bin_dir[bin_dir_size] = 0;
31      *result = FilePath(bin_dir);
32      return true;
33    }
34    case base::FILE_MODULE:
35      // dladdr didn't work in Android as only the file name was returned.
36      NOTIMPLEMENTED();
37      return false;
38    case base::DIR_MODULE:
39      return base::android::GetNativeLibraryDirectory(result);
40    case base::DIR_SOURCE_ROOT:
41      // Used only by tests.
42      // In that context, hooked up via base/test/test_support_android.cc.
43      NOTIMPLEMENTED();
44      return false;
45    case base::DIR_USER_DESKTOP:
46      // Android doesn't support GetUserDesktop.
47      NOTIMPLEMENTED();
48      return false;
49    case base::DIR_CACHE:
50      return base::android::GetCacheDirectory(result);
51    case base::DIR_ANDROID_APP_DATA:
52      return base::android::GetDataDirectory(result);
53    case base::DIR_ANDROID_EXTERNAL_STORAGE:
54      return base::android::GetExternalStorageDirectory(result);
55    default:
56      // Note: the path system expects this function to override the default
57      // behavior. So no need to log an error if we don't support a given
58      // path. The system will just use the default.
59      return false;
60  }
61}
62
63}  // namespace base
64