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
3 // found in the LICENSE file.
5 #include "chrome/browser/android/compositor/layer_title_cache.h"
7 #include <android/bitmap.h>
9 #include "base/memory/scoped_ptr.h"
10 #include "cc/layers/layer.h"
11 #include "cc/layers/ui_resource_layer.h"
12 #include "chrome/browser/android/compositor/decoration_title.h"
13 #include "jni/LayerTitleCache_jni.h"
14 #include "ui/android/resources/resource_manager.h"
15 #include "ui/gfx/android/java_bitmap.h"
16 #include "ui/gfx/geometry/point_f.h"
17 #include "ui/gfx/geometry/rect_f.h"
18 #include "ui/gfx/geometry/size.h"
24 LayerTitleCache
* LayerTitleCache::FromJavaObject(jobject jobj
) {
27 return reinterpret_cast<LayerTitleCache
*>(Java_LayerTitleCache_getNativePtr(
28 base::android::AttachCurrentThread(), jobj
));
31 LayerTitleCache::LayerTitleCache(JNIEnv
* env
,
34 jint favicon_start_padding
,
35 jint favicon_end_padding
,
36 jint spinner_resource_id
,
37 jint spinner_incognito_resource_id
)
38 : weak_java_title_cache_(env
, obj
),
39 fade_width_(fade_width
),
40 favicon_start_padding_(favicon_start_padding
),
41 favicon_end_padding_(favicon_end_padding
),
42 spinner_resource_id_(spinner_resource_id
),
43 spinner_incognito_resource_id_(spinner_incognito_resource_id
),
44 resource_manager_(nullptr) {
47 void LayerTitleCache::Destroy(JNIEnv
* env
, jobject obj
) {
51 void LayerTitleCache::UpdateLayer(JNIEnv
* env
,
54 jint title_resource_id
,
55 jint favicon_resource_id
,
58 DecorationTitle
* title_layer
= layer_cache_
.Lookup(tab_id
);
59 if (title_layer
== NULL
) {
60 layer_cache_
.AddWithID(
62 this, resource_manager_
, title_resource_id
, favicon_resource_id
,
63 spinner_resource_id_
, spinner_incognito_resource_id_
, fade_width_
,
64 favicon_start_padding_
, favicon_end_padding_
, is_incognito
, is_rtl
),
67 if (title_resource_id
!= -1 && favicon_resource_id
!= -1) {
68 title_layer
->Update(title_resource_id
, favicon_resource_id
, fade_width_
,
69 favicon_start_padding_
, favicon_end_padding_
,
70 is_incognito
, is_rtl
);
72 layer_cache_
.Remove(tab_id
);
77 void LayerTitleCache::ClearExcept(JNIEnv
* env
, jobject obj
, jint except_id
) {
78 IDMap
<DecorationTitle
, IDMapOwnPointer
>::iterator
iter(&layer_cache_
);
79 for (; !iter
.IsAtEnd(); iter
.Advance()) {
80 const int id
= iter
.GetCurrentKey();
82 layer_cache_
.Remove(id
);
86 DecorationTitle
* LayerTitleCache::GetTitleLayer(int tab_id
) {
87 return layer_cache_
.Lookup(tab_id
);
90 void LayerTitleCache::SetResourceManager(
91 ui::ResourceManager
* resource_manager
) {
92 resource_manager_
= resource_manager
;
94 IDMap
<DecorationTitle
, IDMapOwnPointer
>::iterator
iter(&layer_cache_
);
95 for (; !iter
.IsAtEnd(); iter
.Advance()) {
96 iter
.GetCurrentValue()->SetResourceManager(resource_manager_
);
100 LayerTitleCache::~LayerTitleCache() {
103 bool RegisterLayerTitleCache(JNIEnv
* env
) {
104 return RegisterNativesImpl(env
);
107 // ----------------------------------------------------------------------------
108 // Native JNI methods
109 // ----------------------------------------------------------------------------
111 jlong
Init(JNIEnv
* env
,
112 const JavaParamRef
<jobject
>& obj
,
114 jint favicon_start_padding
,
115 jint favicon_end_padding
,
116 jint spinner_resource_id
,
117 jint spinner_incognito_resource_id
) {
118 LayerTitleCache
* cache
= new LayerTitleCache(
119 env
, obj
, fade_width
, favicon_start_padding
, favicon_end_padding
,
120 spinner_resource_id
, spinner_incognito_resource_id
);
121 return reinterpret_cast<intptr_t>(cache
);
124 } // namespace android
125 } // namespace chrome