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.SharedPreferences; | 8 import android.content.SharedPreferences; |
9 import android.content.pm.PackageInfo; | 9 import android.content.pm.PackageInfo; |
10 import android.content.pm.PackageManager; | 10 import android.content.pm.PackageManager; |
(...skipping 18 matching lines...) Expand all Loading... |
29 * the file system accessible from the native code. | 29 * the file system accessible from the native code. |
30 */ | 30 */ |
31 public class ResourceExtractor { | 31 public class ResourceExtractor { |
32 | 32 |
33 private static final String TAG = "cr.base"; | 33 private static final String TAG = "cr.base"; |
34 private static final String ICU_DATA_FILENAME = "icudtl.dat"; | 34 private static final String ICU_DATA_FILENAME = "icudtl.dat"; |
35 private static final String V8_NATIVES_DATA_FILENAME = "natives_blob.bin"; | 35 private static final String V8_NATIVES_DATA_FILENAME = "natives_blob.bin"; |
36 private static final String V8_SNAPSHOT_DATA_FILENAME = "snapshot_blob.bin"; | 36 private static final String V8_SNAPSHOT_DATA_FILENAME = "snapshot_blob.bin"; |
37 private static final String APP_VERSION_PREF = "org.chromium.base.ResourceEx
tractor.Version"; | 37 private static final String APP_VERSION_PREF = "org.chromium.base.ResourceEx
tractor.Version"; |
38 | 38 |
39 private static ResourceEntry[] sResourcesToExtract = new ResourceEntry[0]; | 39 private static ArrayList<String> sResourcesToExtract = new ArrayList<String>
(); |
40 | |
41 /** | |
42 * Holds information about a res/raw file (e.g. locale .pak files). | |
43 */ | |
44 public static final class ResourceEntry { | |
45 public final int resourceId; | |
46 public final String pathWithinApk; | |
47 public final String extractedFileName; | |
48 | |
49 public ResourceEntry(int resourceId, String pathWithinApk, String extrac
tedFileName) { | |
50 this.resourceId = resourceId; | |
51 this.pathWithinApk = pathWithinApk; | |
52 this.extractedFileName = extractedFileName; | |
53 } | |
54 } | |
55 | 40 |
56 private class ExtractTask extends AsyncTask<Void, Void, Void> { | 41 private class ExtractTask extends AsyncTask<Void, Void, Void> { |
57 private static final int BUFFER_SIZE = 16 * 1024; | 42 private static final int BUFFER_SIZE = 16 * 1024; |
58 | 43 |
59 private final List<Runnable> mCompletionCallbacks = new ArrayList<Runnab
le>(); | 44 private final List<Runnable> mCompletionCallbacks = new ArrayList<Runnab
le>(); |
60 | 45 |
61 private void extractResourceHelper(InputStream is, File outFile, byte[]
buffer) | 46 private void extractResourceHelper(InputStream is, File outFile, byte[]
buffer) |
62 throws IOException { | 47 throws IOException { |
63 OutputStream os = null; | 48 OutputStream os = null; |
64 try { | 49 try { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 deleteFiles(); | 85 deleteFiles(); |
101 // Use the version only to see if files should be deleted, not t
o skip extraction. | 86 // Use the version only to see if files should be deleted, not t
o skip extraction. |
102 // We've seen files be corrupted, so always attempt extraction. | 87 // We've seen files be corrupted, so always attempt extraction. |
103 // http://crbug.com/606413 | 88 // http://crbug.com/606413 |
104 sharedPrefs.edit().putLong(APP_VERSION_PREF, curAppVersion).appl
y(); | 89 sharedPrefs.edit().putLong(APP_VERSION_PREF, curAppVersion).appl
y(); |
105 } | 90 } |
106 | 91 |
107 TraceEvent.begin("WalkAssets"); | 92 TraceEvent.begin("WalkAssets"); |
108 byte[] buffer = new byte[BUFFER_SIZE]; | 93 byte[] buffer = new byte[BUFFER_SIZE]; |
109 try { | 94 try { |
110 for (ResourceEntry entry : sResourcesToExtract) { | 95 for (String resource : sResourcesToExtract) { |
111 File output = new File(outputDir, entry.extractedFileName); | 96 File output = new File(outputDir, resource); |
112 // TODO(agrieve): It would be better to check that .length =
= expectedLength. | 97 // TODO(agrieve): It would be better to check that .length =
= expectedLength. |
113 // http://crbug.com/606413 | 98 // http://crbug.com/606413 |
114 if (output.length() != 0) { | 99 if (output.length() != 0) { |
115 continue; | 100 continue; |
116 } | 101 } |
117 TraceEvent.begin("ExtractResource"); | 102 TraceEvent.begin("ExtractResource"); |
118 InputStream inputStream = mContext.getResources().openRawRes
ource( | 103 InputStream inputStream = mContext.getAssets().open(resource
); |
119 entry.resourceId); | |
120 try { | 104 try { |
121 extractResourceHelper(inputStream, output, buffer); | 105 extractResourceHelper(inputStream, output, buffer); |
122 } finally { | 106 } finally { |
123 TraceEvent.end("ExtractResource"); | 107 TraceEvent.end("ExtractResource"); |
124 } | 108 } |
125 } | 109 } |
126 } catch (IOException e) { | 110 } catch (IOException e) { |
127 // TODO(benm): See crbug/152413. | 111 // TODO(benm): See crbug/152413. |
128 // Try to recover here, can we try again after deleting files in
stead of | 112 // Try to recover here, can we try again after deleting files in
stead of |
129 // returning null? It might be useful to gather UMA here too to
track if | 113 // returning null? It might be useful to gather UMA here too to
track if |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 sInstance = new ResourceExtractor(context); | 171 sInstance = new ResourceExtractor(context); |
188 } | 172 } |
189 return sInstance; | 173 return sInstance; |
190 } | 174 } |
191 | 175 |
192 /** | 176 /** |
193 * Specifies the files that should be extracted from the APK. | 177 * Specifies the files that should be extracted from the APK. |
194 * and moved to {@link #getOutputDir()}. | 178 * and moved to {@link #getOutputDir()}. |
195 */ | 179 */ |
196 @SuppressFBWarnings("EI_EXPOSE_STATIC_REP2") | 180 @SuppressFBWarnings("EI_EXPOSE_STATIC_REP2") |
197 public static void setResourcesToExtract(ResourceEntry[] entries) { | 181 public static void setResourcesToExtract(ArrayList<String> resources) { |
198 assert (sInstance == null || sInstance.mExtractTask == null) | 182 assert (sInstance == null || sInstance.mExtractTask == null) |
199 : "Must be called before startExtractingResources is called"; | 183 : "Must be called before startExtractingResources is called"; |
200 sResourcesToExtract = entries; | 184 sResourcesToExtract = resources; |
201 } | 185 } |
202 | 186 |
203 private ResourceExtractor(Context context) { | 187 private ResourceExtractor(Context context) { |
204 mContext = context.getApplicationContext(); | 188 mContext = context.getApplicationContext(); |
205 } | 189 } |
206 | 190 |
207 /** | 191 /** |
208 * Synchronously wait for the resource extraction to be completed. | 192 * Synchronously wait for the resource extraction to be completed. |
209 * <p> | 193 * <p> |
210 * This method is bad and you should feel bad for using it. | 194 * This method is bad and you should feel bad for using it. |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 } | 306 } |
323 } | 307 } |
324 } | 308 } |
325 } | 309 } |
326 } | 310 } |
327 | 311 |
328 /** | 312 /** |
329 * Pak extraction not necessarily required by the embedder. | 313 * Pak extraction not necessarily required by the embedder. |
330 */ | 314 */ |
331 private static boolean shouldSkipPakExtraction() { | 315 private static boolean shouldSkipPakExtraction() { |
332 return sResourcesToExtract.length == 0; | 316 return sResourcesToExtract.size() == 0; |
333 } | 317 } |
334 } | 318 } |
OLD | NEW |