Chromium Code Reviews| 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) | |
|
jdduke (slow)
2014/12/08 16:23:13
The result of GetResource should never be null, I
Jaekyun Seok (inactive)
2014/12/09 11:42:35
Yes, the result of GetResource can't be null in a
jdduke (slow)
2014/12/09 16:11:07
Hmm, can we avoid that? It's a shame to have to mo
Jaekyun Seok (inactive)
2014/12/09 23:35:22
Done.
| |
| 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 Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(), |
| 79 java_obj_.obj(), res_type, res_id); | 98 java_obj_.obj(), res_type, res_id); |
| 80 resource = resources_[res_type].Lookup(res_id); | 99 resource = resources_[res_type].Lookup(res_id); |
| 81 } | 100 } |
| 82 | 101 |
| 83 return resource; | 102 return resource; |
| 84 } | 103 } |
| 85 | 104 |
| 105 ResourceManager::Resource* ResourceManager::GetResource( | |
| 106 AndroidResourceType res_type, | |
| 107 std::string res_name) { | |
| 108 return GetResource(res_type, GetAndroidResourceIdFromString(res_name)); | |
| 109 } | |
| 110 | |
| 86 void ResourceManager::PreloadResource(AndroidResourceType res_type, | 111 void ResourceManager::PreloadResource(AndroidResourceType res_type, |
| 87 int res_id) { | 112 int res_id) { |
| 88 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); | 113 DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); |
| 89 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); | 114 DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); |
| 90 | 115 |
| 91 // Don't send out a query if the resource is already loaded. | 116 // Don't send out a query if the resource is already loaded. |
| 92 if (resources_[res_type].Lookup(res_id)) | 117 if (resources_[res_type].Lookup(res_id)) |
| 93 return; | 118 return; |
| 94 | 119 |
| 95 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), | 120 Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(), |
| 96 java_obj_.obj(), res_type, res_id); | 121 java_obj_.obj(), res_type, res_id); |
| 97 } | 122 } |
| 98 | 123 |
| 124 void ResourceManager::PreloadResource(AndroidResourceType res_type, | |
| 125 std::string res_name) { | |
| 126 PreloadResource(res_type, GetAndroidResourceIdFromString(res_name)); | |
| 127 } | |
| 128 | |
| 99 void ResourceManager::OnResourceReady(JNIEnv* env, | 129 void ResourceManager::OnResourceReady(JNIEnv* env, |
| 100 jobject jobj, | 130 jobject jobj, |
| 101 jint res_type, | 131 jint res_type, |
| 102 jint res_id, | 132 jint res_id, |
| 103 jobject bitmap, | 133 jobject bitmap, |
| 104 jint padding_left, | 134 jint padding_left, |
| 105 jint padding_top, | 135 jint padding_top, |
| 106 jint padding_right, | 136 jint padding_right, |
| 107 jint padding_bottom, | 137 jint padding_bottom, |
| 108 jint aperture_left, | 138 jint aperture_left, |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 128 aperture_bottom - aperture_top); | 158 aperture_bottom - aperture_top); |
| 129 resource->ui_resource = | 159 resource->ui_resource = |
| 130 UIResourceAndroid::CreateFromJavaBitmap(ui_resource_provider_, jbitmap); | 160 UIResourceAndroid::CreateFromJavaBitmap(ui_resource_provider_, jbitmap); |
| 131 } | 161 } |
| 132 | 162 |
| 133 // static | 163 // static |
| 134 bool ResourceManager::RegisterResourceManager(JNIEnv* env) { | 164 bool ResourceManager::RegisterResourceManager(JNIEnv* env) { |
| 135 return RegisterNativesImpl(env); | 165 return RegisterNativesImpl(env); |
| 136 } | 166 } |
| 137 | 167 |
| 168 int ResourceManager::GetAndroidResourceIdFromString(std::string res_name) { | |
| 169 if (android_res_name_to_res_id_map_.find(res_name) == | |
| 170 android_res_name_to_res_id_map_.end()) { | |
| 171 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 172 ScopedJavaLocalRef<jstring> jstring_url = | |
| 173 base::android::ConvertUTF8ToJavaString(env, res_name); | |
| 174 android_res_name_to_res_id_map_[res_name] = | |
| 175 Java_ResourceManager_getAndroidResourceIdFromString( | |
| 176 env, java_obj_.obj(), jstring_url.obj()); | |
| 177 } | |
| 178 return android_res_name_to_res_id_map_[res_name]; | |
| 179 } | |
| 180 | |
| 138 } // namespace ui | 181 } // namespace ui |
| OLD | NEW |