Index: ui/base/resource/resource_bundle_android.cc |
diff --git a/ui/base/resource/resource_bundle_android.cc b/ui/base/resource/resource_bundle_android.cc |
index 9dc036eeded50c7ce4431733c5d5949e9d766463..bcf306b0ad9fa6be4172b077713fd1adda5224dd 100644 |
--- a/ui/base/resource/resource_bundle_android.cc |
+++ b/ui/base/resource/resource_bundle_android.cc |
@@ -2,28 +2,62 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "ui/base/resource/resource_bundle.h" |
- |
-#include <string> |
+#include "ui/base/resource/resource_bundle_android.h" |
+#include "base/android/apk_assets.h" |
#include "base/android/jni_android.h" |
#include "base/android/jni_string.h" |
-#include "base/files/file_path.h" |
-#include "base/files/file_util.h" |
#include "base/logging.h" |
#include "base/path_service.h" |
-#include "base/strings/stringprintf.h" |
#include "jni/ResourceBundle_jni.h" |
-#include "ui/base/resource/resource_handle.h" |
+#include "ui/base/resource/resource_bundle.h" |
#include "ui/base/ui_base_paths.h" |
namespace ui { |
+namespace { |
+ |
+// It is okay to cache and share these file descriptors since the |
+// ResourceBundle singleton never closes the handles. |
+int g_chrome_100_percent_fd = -1; |
+int g_resources_pack_fd = -1; |
+base::MemoryMappedFile::Region g_chrome_100_percent_region; |
+base::MemoryMappedFile::Region g_resources_pack_region; |
+ |
+bool LoadFromApkOrFile(const char* apk_path, |
+ const base::FilePath* disk_path, |
+ int* fd_out, |
+ base::MemoryMappedFile::Region* region_out) { |
+ DCHECK_EQ(*fd_out, -1) << "Attempt to load " << apk_path << " twice."; |
+ if (apk_path != nullptr) { |
+ *fd_out = base::android::OpenApkAsset(apk_path, region_out); |
+ } |
+ // For unit tests, the file exists on disk. |
+ if (*fd_out < 0 && disk_path != nullptr) { |
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
+ *fd_out = base::File(*disk_path, flags).TakePlatformFile(); |
+ *region_out = base::MemoryMappedFile::Region::kWholeFile; |
+ } |
+ bool success = *fd_out >= 0; |
+ if (!success) { |
+ LOG(ERROR) << "Failed to open pak file: " << apk_path; |
+ } |
+ return success; |
+} |
+ |
+} // namespace |
+ |
void ResourceBundle::LoadCommonResources() { |
- base::FilePath path; |
- PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &path); |
- AddDataPackFromPath(path.AppendASCII("chrome_100_percent.pak"), |
- SCALE_FACTOR_100P); |
+ base::FilePath disk_path; |
+ PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &disk_path); |
+ disk_path = disk_path.AppendASCII("chrome_100_percent.pak"); |
+ if (LoadFromApkOrFile("assets/chrome_100_percent.pak", |
+ &disk_path, |
+ &g_chrome_100_percent_fd, |
+ &g_chrome_100_percent_region)) { |
+ AddDataPackFromFileRegion(base::File(g_chrome_100_percent_fd), |
+ g_chrome_100_percent_region, SCALE_FACTOR_100P); |
+ } |
} |
gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { |
@@ -32,6 +66,30 @@ gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { |
return GetImageNamed(resource_id); |
} |
+void LoadMainAndroidPackFile(const char* path_within_apk, |
+ const base::FilePath& disk_file_path) { |
+ if (LoadFromApkOrFile(path_within_apk, |
+ &disk_file_path, |
+ &g_resources_pack_fd, |
+ &g_resources_pack_region)) { |
+ ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion( |
+ base::File(g_resources_pack_fd), g_resources_pack_region, |
+ SCALE_FACTOR_NONE); |
+ } |
+} |
+ |
+int GetMainAndroidPackFd(base::MemoryMappedFile::Region* out_region) { |
+ DCHECK_GE(g_resources_pack_fd, 0); |
+ *out_region = g_resources_pack_region; |
+ return g_resources_pack_fd; |
+} |
+ |
+int GetCommonResourcesPackFd(base::MemoryMappedFile::Region* out_region) { |
+ DCHECK_GE(g_chrome_100_percent_fd, 0); |
+ *out_region = g_chrome_100_percent_region; |
+ return g_chrome_100_percent_fd; |
+} |
+ |
bool AssetContainedInApk(const std::string& filename) { |
JNIEnv* env = base::android::AttachCurrentThread(); |
return Java_ResourceBundle_assetContainedInApk( |
@@ -44,4 +102,4 @@ bool RegisterResourceBundleAndroid(JNIEnv* env) { |
return RegisterNativesImpl(env); |
} |
-} |
+} // namespace ui |