Home | History | Annotate | Download | only in base
      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 <unistd.h>
      9 
     10 #include "base/android/jni_android.h"
     11 #include "base/android/path_utils.h"
     12 #include "base/base_paths.h"
     13 #include "base/file_util.h"
     14 #include "base/files/file_path.h"
     15 #include "base/logging.h"
     16 #include "base/process/process_metrics.h"
     17 
     18 namespace base {
     19 
     20 bool PathProviderAndroid(int key, FilePath* result) {
     21   switch (key) {
     22     case base::FILE_EXE: {
     23       char bin_dir[PATH_MAX + 1];
     24       int bin_dir_size = readlink(kProcSelfExe, bin_dir, PATH_MAX);
     25       if (bin_dir_size < 0 || bin_dir_size > PATH_MAX) {
     26         NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
     27         return false;
     28       }
     29       bin_dir[bin_dir_size] = 0;
     30       *result = FilePath(bin_dir);
     31       return true;
     32     }
     33     case base::FILE_MODULE:
     34       // dladdr didn't work in Android as only the file name was returned.
     35       NOTIMPLEMENTED();
     36       return false;
     37     case base::DIR_MODULE:
     38       return base::android::GetNativeLibraryDirectory(result);
     39     case base::DIR_SOURCE_ROOT:
     40       // This const is only used for tests.
     41       return base::android::GetExternalStorageDirectory(result);
     42     case base::DIR_USER_DESKTOP:
     43       // Android doesn't support GetUserDesktop.
     44       NOTIMPLEMENTED();
     45       return false;
     46     case base::DIR_CACHE:
     47       return base::android::GetCacheDirectory(result);
     48     case base::DIR_ANDROID_APP_DATA:
     49       return base::android::GetDataDirectory(result);
     50     case base::DIR_HOME:
     51       *result = GetHomeDir();
     52       return true;
     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