Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base; | 5 package org.chromium.base; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.pm.ApplicationInfo; | 8 import android.content.pm.ApplicationInfo; |
| 9 import android.os.AsyncTask; | 9 import android.os.AsyncTask; |
| 10 import android.os.Environment; | 10 import android.os.Environment; |
| 11 import android.os.StrictMode; | 11 import android.os.StrictMode; |
| 12 import android.os.SystemClock; | 12 import android.os.SystemClock; |
| 13 import android.support.annotation.WorkerThread; | |
| 13 | 14 |
| 14 import org.chromium.base.annotations.CalledByNative; | 15 import org.chromium.base.annotations.CalledByNative; |
| 15 import org.chromium.base.annotations.MainDex; | 16 import org.chromium.base.annotations.MainDex; |
| 16 import org.chromium.base.metrics.RecordHistogram; | 17 import org.chromium.base.metrics.RecordHistogram; |
| 17 | 18 |
| 18 import java.io.File; | 19 import java.io.File; |
| 19 import java.util.concurrent.ExecutionException; | 20 import java.util.concurrent.ExecutionException; |
| 20 import java.util.concurrent.TimeUnit; | 21 import java.util.concurrent.TimeUnit; |
| 21 | 22 |
| 22 /** | 23 /** |
| 23 * This class provides the path related methods for the native library. | 24 * This class provides the path related methods for the native library. |
| 24 */ | 25 */ |
| 25 @MainDex | 26 @MainDex |
| 26 public abstract class PathUtils { | 27 public abstract class PathUtils { |
| 27 private static final String THUMBNAIL_DIRECTORY = "textures"; | 28 private static final String THUMBNAIL_DIRECTORY = "textures"; |
| 28 | 29 |
| 29 private static final int DATA_DIRECTORY = 0; | 30 private static final int DATA_DIRECTORY = 0; |
| 30 private static final int DATABASE_DIRECTORY = 1; | 31 private static final int DATABASE_DIRECTORY = 1; |
| 31 private static final int CACHE_DIRECTORY = 2; | 32 private static final int CACHE_DIRECTORY = 2; |
| 32 private static final int NUM_DIRECTORIES = 3; | 33 private static final int NUM_DIRECTORIES = 3; |
| 33 private static AsyncTask<String, Void, String[]> sDirPathFetchTask; | 34 private static AsyncTask<String, Void, String[]> sDirPathFetchTask; |
| 34 | 35 |
| 35 private static File sThumbnailDirectory; | 36 private static File sThumbnailDirectory; |
| 37 private static Object sLazyInitLock = new Object(); | |
|
gone
2016/02/18 17:45:35
private static final? Can't imagine ever recreati
agrieve
2016/02/23 21:28:38
Deleted in favour of using sDirPathFetchTask
| |
| 36 | 38 |
| 37 // Prevent instantiation. | 39 // Prevent instantiation. |
| 38 private PathUtils() {} | 40 private PathUtils() {} |
| 39 | 41 |
| 40 /** | 42 /** |
| 41 * Starts an asynchronous task to fetch the path of the directory where priv ate data is to be | 43 * Starts an asynchronous task to fetch the path of the directory where priv ate data is to be |
| 42 * stored by the application. | 44 * stored by the application. |
| 43 * | 45 * |
| 44 * @param suffix The private data directory suffix. | 46 * @param suffix The private data directory suffix. |
| 45 * @see Context#getDir(String, int) | 47 * @see Context#getDir(String, int) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 /** | 98 /** |
| 97 * @return the cache directory. | 99 * @return the cache directory. |
| 98 */ | 100 */ |
| 99 @SuppressWarnings("unused") | 101 @SuppressWarnings("unused") |
| 100 @CalledByNative | 102 @CalledByNative |
| 101 public static String getCacheDirectory(Context appContext) { | 103 public static String getCacheDirectory(Context appContext) { |
| 102 assert sDirPathFetchTask != null : "setDataDirectorySuffix must be calle d first."; | 104 assert sDirPathFetchTask != null : "setDataDirectorySuffix must be calle d first."; |
| 103 return getDirectoryPath(CACHE_DIRECTORY); | 105 return getDirectoryPath(CACHE_DIRECTORY); |
| 104 } | 106 } |
| 105 | 107 |
| 108 @WorkerThread | |
| 106 public static File getThumbnailCacheDirectory(Context appContext) { | 109 public static File getThumbnailCacheDirectory(Context appContext) { |
| 110 ThreadUtils.assertOnFileThread(); | |
| 107 if (sThumbnailDirectory == null) { | 111 if (sThumbnailDirectory == null) { |
| 108 // Temporarily allowing disk access while fixing. TODO: http://crbug .com/473356 | 112 synchronized (sLazyInitLock) { |
| 109 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads( ); | 113 if (sThumbnailDirectory == null) { |
| 110 StrictMode.allowThreadDiskWrites(); | 114 long time = SystemClock.elapsedRealtime(); |
| 111 try { | 115 sThumbnailDirectory = |
| 112 long time = SystemClock.elapsedRealtime(); | 116 appContext.getDir(THUMBNAIL_DIRECTORY, Context.MODE_ PRIVATE); |
| 113 sThumbnailDirectory = appContext.getDir(THUMBNAIL_DIRECTORY, Con text.MODE_PRIVATE); | 117 RecordHistogram.recordTimesHistogram("Android.StrictMode.Thu mbnailCacheDir", |
| 114 RecordHistogram.recordTimesHistogram("Android.StrictMode.Thumbna ilCacheDir", | 118 SystemClock.elapsedRealtime() - time, TimeUnit.MILLI SECONDS); |
| 115 SystemClock.elapsedRealtime() - time, TimeUnit.MILLISECO NDS); | 119 } |
| 116 } finally { | |
| 117 StrictMode.setThreadPolicy(oldPolicy); | |
| 118 } | 120 } |
| 119 } | 121 } |
| 120 return sThumbnailDirectory; | 122 return sThumbnailDirectory; |
| 121 } | 123 } |
| 122 | 124 |
| 123 @CalledByNative | 125 @CalledByNative |
| 124 public static String getThumbnailCacheDirectoryPath(Context appContext) { | 126 public static String getThumbnailCacheDirectoryPath(Context appContext) { |
| 125 return getThumbnailCacheDirectory(appContext).getAbsolutePath(); | 127 return getThumbnailCacheDirectory(appContext).getAbsolutePath(); |
| 126 } | 128 } |
| 127 | 129 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 | 165 |
| 164 /** | 166 /** |
| 165 * @return the external storage directory. | 167 * @return the external storage directory. |
| 166 */ | 168 */ |
| 167 @SuppressWarnings("unused") | 169 @SuppressWarnings("unused") |
| 168 @CalledByNative | 170 @CalledByNative |
| 169 public static String getExternalStorageDirectory() { | 171 public static String getExternalStorageDirectory() { |
| 170 return Environment.getExternalStorageDirectory().getAbsolutePath(); | 172 return Environment.getExternalStorageDirectory().getAbsolutePath(); |
| 171 } | 173 } |
| 172 } | 174 } |
| OLD | NEW |