File: tab_favicon.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 (145 lines) | stat: -rw-r--r-- 5,204 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
// Copyright 2019 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/tab_favicon.h"

#include "components/favicon/content/content_favicon_driver.h"
#include "content/public/browser/back_forward_transition_animation_manager.h"
#include "content/public/browser/web_contents.h"
#include "skia/ext/image_operations.h"
#include "third_party/blink/public/common/features_generated.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "url/android/gurl_android.h"

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

namespace {

using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;

SkBitmap RescaleSkBitmap(const SkBitmap& original, int new_size_dip) {
  SkImageInfo scaledInfo = original.info().makeWH(new_size_dip, new_size_dip);
  SkBitmap resized;
  resized.allocPixels(scaledInfo);
  bool success = original.pixmap().scalePixels(
      resized.pixmap(), SkSamplingOptions(SkFilterMode::kLinear));
  CHECK(success);
  resized.setImmutable();
  return resized;
}

}  // namespace

TabFavicon::TabFavicon(JNIEnv* env,
                       const JavaParamRef<jobject>& obj,
                       int navigation_transition_favicon_size)
    : navigation_transition_favicon_size_(navigation_transition_favicon_size),
      jobj_(env, obj) {}

TabFavicon::~TabFavicon() = default;

void TabFavicon::SetWebContents(JNIEnv* env,
                                const JavaParamRef<jobject>& obj,
                                const JavaParamRef<jobject>& jweb_contents) {
  active_web_contents_ =
      content::WebContents::FromJavaWebContents(jweb_contents);
  favicon_driver_ =
      favicon::ContentFaviconDriver::FromWebContents(active_web_contents_);
  if (favicon_driver_)
    favicon_driver_->AddObserver(this);
}

void TabFavicon::ResetWebContents(JNIEnv* env,
                                  const JavaParamRef<jobject>& obj) {
  active_web_contents_ = nullptr;
  if (favicon_driver_) {
    favicon_driver_->RemoveObserver(this);
    favicon_driver_ = nullptr;
  }
}

void TabFavicon::OnDestroyed(JNIEnv* env, const JavaParamRef<jobject>& obj) {
  delete this;
}

ScopedJavaLocalRef<jobject> TabFavicon::GetFavicon(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj) {
  ScopedJavaLocalRef<jobject> bitmap;

  if (!favicon_driver_ || !favicon_driver_->FaviconIsValid()) {
    return bitmap;
  }

  // Always return the default favicon in Android.
  SkBitmap favicon = favicon_driver_->GetFavicon().AsBitmap();
  if (!favicon.empty()) {
    const float device_scale_factor =
        display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor();
    int target_size_dip = device_scale_factor * gfx::kFaviconSize;
    if (favicon.width() != target_size_dip ||
        favicon.height() != target_size_dip) {
      favicon = skia::ImageOperations::Resize(
          favicon, skia::ImageOperations::RESIZE_BEST, target_size_dip,
          target_size_dip);
    }

    bitmap = gfx::ConvertToJavaBitmap(favicon);
  }
  return bitmap;
}

void TabFavicon::OnFaviconUpdated(favicon::FaviconDriver* favicon_driver,
                                  NotificationIconType notification_icon_type,
                                  const GURL& icon_url,
                                  bool icon_url_changed,
                                  const gfx::Image& image) {
  if (notification_icon_type != NON_TOUCH_LARGEST &&
      notification_icon_type != TOUCH_LARGEST) {
    return;
  }

  SkBitmap favicon = image.AsImageSkia().GetRepresentation(1.0f).GetBitmap();
  if (favicon.empty())
    return;

  JNIEnv* env = base::android::AttachCurrentThread();

  auto new_width = image.Width();
  auto new_height = image.Height();
  if (static_cast<bool>(Java_TabFavicon_shouldUpdateFaviconForBrowserUi(
          env, jobj_, new_width, new_height))) {
    ScopedJavaLocalRef<jobject> j_icon_url =
        url::GURLAndroid::FromNativeGURL(env, icon_url);
    Java_TabFavicon_onFaviconAvailable(
        env, jobj_, gfx::ConvertToJavaBitmap(favicon), j_icon_url);
  }
  if (base::FeatureList::IsEnabled(blink::features::kBackForwardTransitions)) {
    CHECK(active_web_contents_);
    if (static_cast<bool>(
            Java_TabFavicon_shouldUpdateFaviconForNavigationTransitions(
                env, jobj_, new_width, new_height))) {
      SkBitmap scaled =
          RescaleSkBitmap(favicon, navigation_transition_favicon_size_);
      auto* manager =
          active_web_contents_->GetBackForwardTransitionAnimationManager();
      CHECK(manager);
      manager->SetFavicon(std::move(scaled));
    }
  }
}

static jlong JNI_TabFavicon_Init(JNIEnv* env,
                                 const JavaParamRef<jobject>& obj,
                                 int navigation_transition_favicon_size) {
  return reinterpret_cast<intptr_t>(
      new TabFavicon(env, obj, navigation_transition_favicon_size));
}