| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/android/resources/resource_manager.h" | 5 #include "ui/android/resources/resource_manager.h" |
| 6 | 6 |
| 7 #include "content/public/browser/android/ui_resource_provider.h" | 7 #include "base/android/jni_string.h" |
| 8 #include "jni/ResourceManager_jni.h" | 8 #include "jni/ResourceManager_jni.h" |
| 9 #include "ui/android/resources/ui_resource_android.h" | 9 #include "ui/android/resources/ui_resource_android.h" |
| 10 #include "ui/android/resources/ui_resource_provider.h" |
| 10 #include "ui/gfx/android/java_bitmap.h" | 11 #include "ui/gfx/android/java_bitmap.h" |
| 11 #include "ui/gfx/geometry/insets_f.h" | 12 #include "ui/gfx/geometry/insets_f.h" |
| 12 | 13 |
| 13 namespace ui { | 14 namespace ui { |
| 14 | 15 |
| 15 ResourceManager::Resource::Resource() { | 16 ResourceManager::Resource::Resource() { |
| 16 } | 17 } |
| 17 | 18 |
| 18 ResourceManager::Resource::~Resource() { | 19 ResourceManager::Resource::~Resource() { |
| 19 } | 20 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 38 (size.width() - aperture.width()) * right_scale, | 39 (size.width() - aperture.width()) * right_scale, |
| 39 (size.height() - aperture.height()) * bottom_scale); | 40 (size.height() - aperture.height()) * bottom_scale); |
| 40 } | 41 } |
| 41 | 42 |
| 42 // static | 43 // static |
| 43 ResourceManager* ResourceManager::FromJavaObject(jobject jobj) { | 44 ResourceManager* ResourceManager::FromJavaObject(jobject jobj) { |
| 44 return reinterpret_cast<ResourceManager*>(Java_ResourceManager_getNativePtr( | 45 return reinterpret_cast<ResourceManager*>(Java_ResourceManager_getNativePtr( |
| 45 base::android::AttachCurrentThread(), jobj)); | 46 base::android::AttachCurrentThread(), jobj)); |
| 46 } | 47 } |
| 47 | 48 |
| 48 ResourceManager::ResourceManager( | 49 ResourceManager::ResourceManager(ui::UIResourceProvider* ui_resource_provider) |
| 49 content::UIResourceProvider* ui_resource_provider) | |
| 50 : ui_resource_provider_(ui_resource_provider) { | 50 : ui_resource_provider_(ui_resource_provider) { |
| 51 JNIEnv* env = base::android::AttachCurrentThread(); | 51 JNIEnv* env = base::android::AttachCurrentThread(); |
| 52 java_obj_.Reset(env, Java_ResourceManager_create( | 52 java_obj_.Reset(env, Java_ResourceManager_create( |
| 53 env, base::android::GetApplicationContext(), | 53 env, base::android::GetApplicationContext(), |
| 54 reinterpret_cast<intptr_t>(this)).obj()); | 54 reinterpret_cast<intptr_t>(this)).obj()); |
| 55 DCHECK(!java_obj_.is_null()); | 55 DCHECK(!java_obj_.is_null()); |
| 56 } | 56 } |
| 57 | 57 |
| 58 ResourceManager::~ResourceManager() { | 58 ResourceManager::~ResourceManager() { |
| 59 Java_ResourceManager_destroy(base::android::AttachCurrentThread(), | 59 Java_ResourceManager_destroy(base::android::AttachCurrentThread(), |
| 60 java_obj_.obj()); | 60 java_obj_.obj()); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void ResourceManager::PreloadResource(ui::SystemUIResourceType type) { |
| 64 DCHECK_GE(type, ui::SYSTEM_UI_RESOURCE_TYPE_FIRST); |
| 65 DCHECK_LE(type, ui::SYSTEM_UI_RESOURCE_TYPE_LAST); |
| 66 |
| 67 PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM, type); |
| 68 } |
| 69 |
| 70 cc::UIResourceId ResourceManager::GetUIResourceId( |
| 71 ui::SystemUIResourceType type) { |
| 72 DCHECK_GE(type, ui::SYSTEM_UI_RESOURCE_TYPE_FIRST); |
| 73 DCHECK_LE(type, ui::SYSTEM_UI_RESOURCE_TYPE_LAST); |
| 74 |
| 75 ui::ResourceManager::Resource* resource = |
| 76 GetResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM, type); |
| 77 if (!resource || !resource->ui_resource) |
| 78 return 0; |
| 79 return resource->ui_resource->id(); |
| 80 } |
| 81 |
| 63 base::android::ScopedJavaLocalRef<jobject> ResourceManager::GetJavaObject( | 82 base::android::ScopedJavaLocalRef<jobject> ResourceManager::GetJavaObject( |
| 64 JNIEnv* env) { | 83 JNIEnv* env) { |
| 65 return base::android::ScopedJavaLocalRef<jobject>(java_obj_); | 84 return base::android::ScopedJavaLocalRef<jobject>(java_obj_); |
| 66 } | 85 } |
| 67 | 86 |
| 68 ResourceManager::Resource* ResourceManager::GetResource( | 87 ResourceManager::Resource* ResourceManager::GetResource( |
| 69 AndroidResourceType res_type, | 88 AndroidResourceType res_type, |
| 70 int res_id) { | 89 int res_id) { |
| 71 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 90 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 72 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 91 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 73 | 92 |
| 74 Resource* resource = resources_[res_type].Lookup(res_id); | 93 Resource* resource = resources_[res_type].Lookup(res_id); |
| 75 | 94 |
| 76 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || | 95 if (!resource || res_type == ANDROID_RESOURCE_TYPE_DYNAMIC || |
| 77 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { | 96 res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) { |
| 78 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(), | 97 RequestResourceFromJava(res_type, res_id); |
| 79 java_obj_.obj(), res_type, res_id); | |
| 80 resource = resources_[res_type].Lookup(res_id); | 98 resource = resources_[res_type].Lookup(res_id); |
| 81 } | 99 } |
| 82 | 100 |
| 83 return resource; | 101 return resource; |
| 84 } | 102 } |
| 85 | 103 |
| 104 ResourceManager::Resource* ResourceManager::GetResource( |
| 105 AndroidResourceType res_type, |
| 106 std::string res_name) { |
| 107 return GetResource(res_type, GetAndroidResourceIdFromString(res_name)); |
| 108 } |
| 109 |
| 86 void ResourceManager::PreloadResource(AndroidResourceType res_type, | 110 void ResourceManager::PreloadResource(AndroidResourceType res_type, |
| 87 int res_id) { | 111 int res_id) { |
| 88 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 112 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 89 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 113 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 90 | 114 |
| 91 // Don't send out a query if the resource is already loaded. | 115 // Don't send out a query if the resource is already loaded. |
| 92 if (resources_[res_type].Lookup(res_id)) | 116 if (resources_[res_type].Lookup(res_id)) |
| 93 return; | 117 return; |
| 94 | 118 |
| 95 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), | 119 PreloadResourceFromJava(res_type, res_id); |
| 96 java_obj_.obj(), res_type, res_id); | 120 } |
| 121 |
| 122 void ResourceManager::PreloadResource(AndroidResourceType res_type, |
| 123 std::string res_name) { |
| 124 PreloadResource(res_type, GetAndroidResourceIdFromString(res_name)); |
| 97 } | 125 } |
| 98 | 126 |
| 99 void ResourceManager::OnResourceReady(JNIEnv* env, | 127 void ResourceManager::OnResourceReady(JNIEnv* env, |
| 100 jobject jobj, | 128 jobject jobj, |
| 101 jint res_type, | 129 jint res_type, |
| 102 jint res_id, | 130 jint res_id, |
| 103 jobject bitmap, | 131 jobject bitmap, |
| 104 jint padding_left, | 132 jint padding_left, |
| 105 jint padding_top, | 133 jint padding_top, |
| 106 jint padding_right, | 134 jint padding_right, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 128 aperture_bottom - aperture_top); | 156 aperture_bottom - aperture_top); |
| 129 resource->ui_resource = | 157 resource->ui_resource = |
| 130 UIResourceAndroid::CreateFromJavaBitmap(ui_resource_provider_, jbitmap); | 158 UIResourceAndroid::CreateFromJavaBitmap(ui_resource_provider_, jbitmap); |
| 131 } | 159 } |
| 132 | 160 |
| 133 // static | 161 // static |
| 134 bool ResourceManager::RegisterResourceManager(JNIEnv* env) { | 162 bool ResourceManager::RegisterResourceManager(JNIEnv* env) { |
| 135 return RegisterNativesImpl(env); | 163 return RegisterNativesImpl(env); |
| 136 } | 164 } |
| 137 | 165 |
| 166 int ResourceManager::GetAndroidResourceIdFromString(std::string res_name) { |
| 167 if (android_res_name_to_res_id_map_.find(res_name) == |
| 168 android_res_name_to_res_id_map_.end()) { |
| 169 JNIEnv* env = base::android::AttachCurrentThread(); |
| 170 ScopedJavaLocalRef<jstring> jstring_url = |
| 171 base::android::ConvertUTF8ToJavaString(env, res_name); |
| 172 android_res_name_to_res_id_map_[res_name] = |
| 173 Java_ResourceManager_getAndroidResourceIdFromString( |
| 174 env, java_obj_.obj(), jstring_url.obj()); |
| 175 } |
| 176 return android_res_name_to_res_id_map_[res_name]; |
| 177 } |
| 178 |
| 179 void ResourceManager::PreloadResourceFromJava(AndroidResourceType res_type, |
| 180 int res_id) { |
| 181 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), |
| 182 java_obj_.obj(), res_type, res_id); |
| 183 } |
| 184 |
| 185 void ResourceManager::RequestResourceFromJava(AndroidResourceType res_type, |
| 186 int res_id) { |
| 187 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(), |
| 188 java_obj_.obj(), res_type, res_id); |
| 189 } |
| 190 |
| 138 } // namespace ui | 191 } // namespace ui |
| OLD | NEW |