File: layer_title_cache.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (208 lines) | stat: -rw-r--r-- 8,460 bytes parent folder | download | duplicates (5)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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 "base/android/token_android.h"
#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,
    const base::android::JavaParamRef<jobject>& jgroup_token,
    jint title_resource_id,
    jint avatar_resource_id,
    jint avatar_padding,
    bool is_incognito,
    bool is_rtl) {
  const tab_groups::TabGroupId& group_token =
      tab_groups::TabGroupId::FromRawToken(
          base::android::TokenAndroid::FromJavaToken(env, jgroup_token));
  auto it = group_layer_cache_.find(group_token);
  if (it != group_layer_cache_.end()) {
    DecorationIconTitle* title_layer = it->second.get();
    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_.erase(it);
    }
  } else {
    group_layer_cache_.emplace(
        group_token,
        std::make_unique<DecorationIconTitle>(
            resource_manager_, title_resource_id, avatar_resource_id,
            fade_width_, kEmptyWidth, avatar_padding, is_incognito, is_rtl));
  }
}

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(
    const tab_groups::TabGroupId& group_token,
    bool incognito) {
  auto it = group_layer_cache_.find(group_token);
  if (it != group_layer_cache_.end()) {
    return it->second.get();
  }

  JNIEnv* env = base::android::AttachCurrentThread();
  Java_LayerTitleCache_buildUpdatedGroupTitle(
      env, weak_java_title_cache_.get(env),
      base::android::TokenAndroid::Create(env, group_token.token()), incognito);

  // Retry the find.
  it = group_layer_cache_.find(group_token);
  return it == group_layer_cache_.end() ? nullptr : it->second.get();
}

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