File: layer_title_cache.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (195 lines) | stat: -rw-r--r-- 8,249 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/android/compositor/layer_title_cache.h"

#include <android/bitmap.h>

#include <memory>

#include "cc/layers/layer.h"
#include "cc/layers/ui_resource_layer.h"
#include "chrome/browser/android/compositor/decoration_icon_title.h"
#include "chrome/browser/android/compositor/decoration_tab_title.h"
#include "chrome/browser/android/compositor/decoration_title.h"
#include "ui/android/resources/resource_manager.h"
#include "ui/android/resources/resource_manager_impl.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/android/chrome_jni_headers/LayerTitleCache_jni.h"

using base::android::JavaParamRef;
using base::android::JavaRef;

namespace android {

// static
LayerTitleCache* LayerTitleCache::FromJavaObject(const JavaRef<jobject>& jobj) {
  if (jobj.is_null())
    return nullptr;
  return reinterpret_cast<LayerTitleCache*>(Java_LayerTitleCache_getNativePtr(
      base::android::AttachCurrentThread(), jobj));
}

LayerTitleCache::LayerTitleCache(JNIEnv* env,
                                 const jni_zero::JavaRef<jobject>& obj,
                                 jint fade_width,
                                 jint icon_start_padding,
                                 jint icon_end_padding,
                                 jint spinner_resource_id,
                                 jint spinner_incognito_resource_id,
                                 jint bubble_inner_dimension,
                                 jint bubble_outer_dimension,
                                 jint bubble_offset,
                                 jint bubble_inner_tint,
                                 jint bubble_outer_tint,
                                 ui::ResourceManager* resource_manager)
    : weak_java_title_cache_(env, obj),
      fade_width_(fade_width),
      icon_start_padding_(icon_start_padding),
      icon_end_padding_(icon_end_padding),
      spinner_resource_id_(spinner_resource_id),
      spinner_incognito_resource_id_(spinner_incognito_resource_id),
      bubble_inner_dimension_(bubble_inner_dimension),
      bubble_outer_dimension_(bubble_outer_dimension),
      bubble_offset_(bubble_offset),
      bubble_inner_tint_(bubble_inner_tint),
      bubble_outer_tint_(bubble_outer_tint),
      resource_manager_(resource_manager) {}

void LayerTitleCache::Destroy(JNIEnv* env) {
  delete this;
}

void LayerTitleCache::UpdateLayer(JNIEnv* env,
                                  const JavaParamRef<jobject>& obj,
                                  jint tab_id,
                                  jint title_resource_id,
                                  jint icon_resource_id,
                                  bool is_incognito,
                                  bool is_rtl,
                                  bool show_bubble) {
  DecorationTabTitle* title_layer = layer_cache_.Lookup(tab_id);
  if (title_layer) {
    if (title_resource_id != ui::Resource::kInvalidResourceId &&
        icon_resource_id != ui::Resource::kInvalidResourceId) {
      title_layer->Update(title_resource_id, icon_resource_id, fade_width_,
                          icon_start_padding_, icon_end_padding_, is_incognito,
                          is_rtl, show_bubble);
    } else {
      layer_cache_.Remove(tab_id);
    }
  } else {
    layer_cache_.AddWithID(
        std::make_unique<DecorationTabTitle>(
            resource_manager_, title_resource_id, icon_resource_id,
            spinner_resource_id_, spinner_incognito_resource_id_, fade_width_,
            icon_start_padding_, icon_end_padding_, is_incognito, is_rtl,
            show_bubble, bubble_inner_dimension_, bubble_outer_dimension_,
            bubble_offset_, bubble_inner_tint_, bubble_outer_tint_),
        tab_id);
  }
}

void LayerTitleCache::UpdateGroupLayer(JNIEnv* env,
                                       const JavaParamRef<jobject>& obj,
                                       jint group_root_id,
                                       jint title_resource_id,
                                       jint avatar_resource_id,
                                       jint avatar_padding,
                                       bool is_incognito,
                                       bool is_rtl) {
  DecorationIconTitle* title_layer = group_layer_cache_.Lookup(group_root_id);
  if (title_layer) {
    if (title_resource_id != ui::Resource::kInvalidResourceId) {
      title_layer->Update(title_resource_id, avatar_resource_id, fade_width_,
                          kEmptyWidth, avatar_padding, is_incognito, is_rtl);
    } else {
      group_layer_cache_.Remove(group_root_id);
    }
  } else {
    group_layer_cache_.AddWithID(
        std::make_unique<DecorationIconTitle>(
            resource_manager_, title_resource_id, avatar_resource_id,
            fade_width_, kEmptyWidth, avatar_padding, is_incognito, is_rtl),
        group_root_id);
  }
}

void LayerTitleCache::UpdateIcon(JNIEnv* env,
                                 const JavaParamRef<jobject>& obj,
                                 jint tab_id,
                                 jint icon_resource_id,
                                 bool show_bubble) {
  DecorationTabTitle* title_layer = layer_cache_.Lookup(tab_id);
  if (title_layer && icon_resource_id != ui::Resource::kInvalidResourceId) {
    title_layer->SetIconResourceId(icon_resource_id);
  }
}

void LayerTitleCache::UpdateTabBubble(JNIEnv* env,
                                      const JavaParamRef<jobject>& obj,
                                      jint tab_id,
                                      bool show_bubble) {
  DecorationTabTitle* title_layer = layer_cache_.Lookup(tab_id);
  if (title_layer) {
    title_layer->SetShowBubble(show_bubble);
  }
}

DecorationTabTitle* LayerTitleCache::GetTitleLayer(int tab_id) {
  if (!layer_cache_.Lookup(tab_id)) {
    JNIEnv* env = base::android::AttachCurrentThread();
    Java_LayerTitleCache_buildUpdatedTitle(env, weak_java_title_cache_.get(env),
        tab_id);
  }

  return layer_cache_.Lookup(tab_id);
}

DecorationIconTitle* LayerTitleCache::GetGroupTitleLayer(int group_root_id,
                                                         bool incognito) {
  if (!group_layer_cache_.Lookup(group_root_id)) {
    JNIEnv* env = base::android::AttachCurrentThread();
    Java_LayerTitleCache_buildUpdatedGroupTitle(
        env, weak_java_title_cache_.get(env), group_root_id, incognito);
  }

  return group_layer_cache_.Lookup(group_root_id);
}

LayerTitleCache::~LayerTitleCache() = default;

// ----------------------------------------------------------------------------
// Native JNI methods
// ----------------------------------------------------------------------------

jlong JNI_LayerTitleCache_Init(JNIEnv* env,
                               const JavaParamRef<jobject>& obj,
                               jint fade_width,
                               jint icon_start_padding,
                               jint icon_end_padding,
                               jint spinner_resource_id,
                               jint spinner_incognito_resource_id,
                               jint bubble_inner_dimension,
                               jint bubble_outer_dimension,
                               jint bubble_offset,
                               jint bubble_inner_tint,
                               jint bubble_outer_tint,
                               const JavaParamRef<jobject>& jresource_manager) {
  ui::ResourceManager* resource_manager =
      ui::ResourceManagerImpl::FromJavaObject(jresource_manager);
  LayerTitleCache* cache = new LayerTitleCache(
      env, obj, fade_width, icon_start_padding, icon_end_padding,
      spinner_resource_id, spinner_incognito_resource_id,
      bubble_inner_dimension, bubble_outer_dimension, bubble_offset,
      bubble_inner_tint, bubble_outer_tint, resource_manager);
  return reinterpret_cast<intptr_t>(cache);
}

}  // namespace android