OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "ui/base/resource/resource_bundle_android.h" |
| 6 |
| 7 #include "base/android/apk_assets.h" |
| 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_string.h" |
| 10 //#include "base/files/file_util.h" |
| 11 #include "base/logging.h" |
| 12 #include "jni/ResourceBundle_jni.h" |
5 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
6 | 14 |
7 #include <string> | 15 namespace { |
8 | 16 // It is okay to cache and share these file descriptors since the |
9 #include "base/android/jni_android.h" | 17 // ResourceBundle singleton never closes the handles. |
10 #include "base/android/jni_string.h" | 18 int g_chrome_100_percent_fd = -1; |
11 #include "base/files/file_path.h" | 19 int g_resources_pack_fd = -1; |
12 #include "base/files/file_util.h" | 20 base::MemoryMappedFile::Region g_chrome_100_percent_region; |
13 #include "base/logging.h" | 21 base::MemoryMappedFile::Region g_resources_pack_region; |
14 #include "base/path_service.h" | 22 } |
15 #include "base/strings/stringprintf.h" | |
16 #include "jni/ResourceBundle_jni.h" | |
17 #include "ui/base/resource/resource_handle.h" | |
18 #include "ui/base/ui_base_paths.h" | |
19 | 23 |
20 namespace ui { | 24 namespace ui { |
21 | 25 |
22 void ResourceBundle::LoadCommonResources() { | 26 void ResourceBundle::LoadCommonResources() { |
23 base::FilePath path; | 27 DCHECK_EQ(g_chrome_100_percent_fd, -1); |
24 PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &path); | 28 g_chrome_100_percent_fd = base::android::OpenApkAsset( |
25 AddDataPackFromPath(path.AppendASCII("chrome_100_percent.pak"), | 29 "assets/chrome_100_percent.pak", &g_chrome_100_percent_region); |
26 SCALE_FACTOR_100P); | 30 DCHECK_GE(g_chrome_100_percent_fd, 0); |
| 31 AddDataPackFromFileRegion(base::File(g_chrome_100_percent_fd), |
| 32 g_chrome_100_percent_region, SCALE_FACTOR_100P); |
27 } | 33 } |
28 | 34 |
29 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { | 35 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { |
30 // Flipped image is not used on Android. | 36 // Flipped image is not used on Android. |
31 DCHECK_EQ(rtl, RTL_DISABLED); | 37 DCHECK_EQ(rtl, RTL_DISABLED); |
32 return GetImageNamed(resource_id); | 38 return GetImageNamed(resource_id); |
33 } | 39 } |
34 | 40 |
| 41 void LoadMainAndroidPackFile() { |
| 42 DCHECK_EQ(g_resources_pack_fd, -1); |
| 43 g_resources_pack_fd = base::android::OpenApkAsset("assets/resources.pak", |
| 44 &g_resources_pack_region); |
| 45 DCHECK_GE(g_resources_pack_fd, 0); |
| 46 ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion( |
| 47 base::File(g_resources_pack_fd), g_resources_pack_region, |
| 48 SCALE_FACTOR_NONE); |
| 49 } |
| 50 |
| 51 int GetMainAndroidPackFd(base::MemoryMappedFile::Region* out_region) { |
| 52 DCHECK_GE(g_resources_pack_fd, 0); |
| 53 *out_region = g_resources_pack_region; |
| 54 return g_resources_pack_fd; |
| 55 } |
| 56 |
| 57 int GetCommonResourcesPackFd(base::MemoryMappedFile::Region* out_region) { |
| 58 DCHECK_GE(g_chrome_100_percent_fd, 0); |
| 59 *out_region = g_chrome_100_percent_region; |
| 60 return g_chrome_100_percent_fd; |
| 61 } |
| 62 |
35 bool AssetContainedInApk(const std::string& filename) { | 63 bool AssetContainedInApk(const std::string& filename) { |
36 JNIEnv* env = base::android::AttachCurrentThread(); | 64 JNIEnv* env = base::android::AttachCurrentThread(); |
37 return Java_ResourceBundle_assetContainedInApk( | 65 return Java_ResourceBundle_assetContainedInApk( |
38 env, | 66 env, |
39 base::android::GetApplicationContext(), | 67 base::android::GetApplicationContext(), |
40 base::android::ConvertUTF8ToJavaString(env, filename).obj()); | 68 base::android::ConvertUTF8ToJavaString(env, filename).obj()); |
41 } | 69 } |
42 | 70 |
43 bool RegisterResourceBundleAndroid(JNIEnv* env) { | 71 bool RegisterResourceBundleAndroid(JNIEnv* env) { |
44 return RegisterNativesImpl(env); | 72 return RegisterNativesImpl(env); |
45 } | 73 } |
46 | 74 |
47 } | 75 } |
OLD | NEW |