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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/image_fetcher/image_fetcher_bridge.h"
#include <jni.h>
#include <utility>
#include "base/android/callback_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/no_destructor.h"
#include "components/embedder_support/android/simple_factory_key/simple_factory_key_handle.h"
#include "components/image_fetcher/core/cache/image_cache.h"
#include "components/image_fetcher/core/image_fetcher.h"
#include "components/image_fetcher/core/image_fetcher_metrics_reporter.h"
#include "components/image_fetcher/core/image_fetcher_service.h"
#include "components/image_fetcher/image_fetcher_service_provider.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/image/image.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/image_fetcher/jni_headers/ImageFetcherBridge_jni.h"
using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
namespace image_fetcher {
namespace {
// Keep in sync with postfix found in image_data_store_disk.cc.
const base::FilePath::CharType kPathPostfix[] =
FILE_PATH_LITERAL("image_data_storage");
// TODO(wylieb): Allow java clients to map to a traffic_annotation here.
constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
net::DefineNetworkTrafficAnnotation("cached_image_fetcher", R"(
semantics {
sender: "Cached Image Fetcher Fetch"
description:
"Fetches and caches images for Chrome features."
trigger:
"Triggered when a feature requests an image fetch."
data: "None."
destination: WEBSITE
}
policy {
cookies_allowed: NO
setting:
"Cache can be cleared through settings."
policy_exception_justification:
"This feature allows many different Chrome features to fetch/cache "
"images and thus there is no Chrome-wide policy to disable it."
})");
data_decoder::DataDecoder* GetDataDecoder() {
static base::NoDestructor<data_decoder::DataDecoder> data_decoder;
return data_decoder.get();
}
} // namespace
ImageFetcherBridge::~ImageFetcherBridge() = default;
// static
ScopedJavaLocalRef<jstring> ImageFetcherBridge::GetFilePath(
JNIEnv* j_env,
const JavaParamRef<jobject>& j_simple_factory_key,
const JavaParamRef<jstring>& j_url) {
std::string url = base::android::ConvertJavaStringToUTF8(j_url);
SimpleFactoryKey* key =
simple_factory_key::SimpleFactoryKeyFromJavaHandle(j_simple_factory_key);
std::string file_path = image_fetcher::GetImageFetcherCachePath(
key, base::FilePath(kPathPostfix)
.Append(ImageCache::HashUrlToKey(url))
.MaybeAsASCII());
return base::android::ConvertUTF8ToJavaString(j_env, file_path);
}
// static
void ImageFetcherBridge::FetchImageData(
JNIEnv* j_env,
const JavaParamRef<jobject>& j_simple_factory_key,
const jint j_image_fetcher_config,
const JavaParamRef<jstring>& j_url,
const JavaParamRef<jstring>& j_client_name,
const jint j_expiration_interval_mins,
const JavaParamRef<jobject>& j_callback) {
ScopedJavaGlobalRef<jobject> callback(j_callback);
ImageFetcherConfig config =
static_cast<ImageFetcherConfig>(j_image_fetcher_config);
std::string url = base::android::ConvertJavaStringToUTF8(j_url);
std::string client_name =
base::android::ConvertJavaStringToUTF8(j_client_name);
image_fetcher::ImageFetcherParams params(kTrafficAnnotation, client_name);
if (j_expiration_interval_mins > 0) {
params.set_hold_for_expiration_interval(
base::Minutes(j_expiration_interval_mins));
}
params.set_data_decoder(GetDataDecoder());
// We can skip transcoding here because this method is used in java as
// ImageFetcher.fetchGif, which decodes the data in a Java-only library.
params.set_skip_transcoding(true);
SimpleFactoryKey* key =
simple_factory_key::SimpleFactoryKeyFromJavaHandle(j_simple_factory_key);
image_fetcher::GetImageFetcherService(key)
->GetImageFetcher(config)
->FetchImageData(
GURL(url),
base::BindOnce(&ImageFetcherBridge::OnImageDataFetched, callback),
std::move(params));
}
// static
void ImageFetcherBridge::FetchImage(
JNIEnv* j_env,
const JavaParamRef<jobject>& j_simple_factory_key,
const jint j_image_fetcher_config,
const JavaParamRef<jstring>& j_url,
const JavaParamRef<jstring>& j_client_name,
const jint j_frame_width,
const jint j_frame_height,
const jint j_expiration_interval_mins,
const JavaParamRef<jobject>& j_callback) {
ScopedJavaGlobalRef<jobject> callback(j_callback);
ImageFetcherConfig config =
static_cast<ImageFetcherConfig>(j_image_fetcher_config);
std::string url = base::android::ConvertJavaStringToUTF8(j_url);
std::string client_name =
base::android::ConvertJavaStringToUTF8(j_client_name);
ImageFetcherParams params(kTrafficAnnotation, client_name);
params.set_frame_size(gfx::Size(j_frame_width, j_frame_height));
if (j_expiration_interval_mins > 0) {
params.set_hold_for_expiration_interval(
base::Minutes(j_expiration_interval_mins));
}
params.set_data_decoder(GetDataDecoder());
SimpleFactoryKey* key =
simple_factory_key::SimpleFactoryKeyFromJavaHandle(j_simple_factory_key);
image_fetcher::GetImageFetcherService(key)
->GetImageFetcher(config)
->FetchImage(
GURL(url),
base::BindOnce(&ImageFetcherBridge::OnImageFetched, callback),
std::move(params));
}
// static
void ImageFetcherBridge::ReportEvent(
JNIEnv* j_env,
const base::android::JavaParamRef<jstring>& j_client_name,
const jint j_event_id) {
std::string client_name =
base::android::ConvertJavaStringToUTF8(j_client_name);
ImageFetcherEvent event = static_cast<ImageFetcherEvent>(j_event_id);
ImageFetcherMetricsReporter::ReportEvent(client_name, event);
}
// static
void ImageFetcherBridge::ReportCacheHitTime(
JNIEnv* j_env,
const base::android::JavaParamRef<jstring>& j_client_name,
const jlong start_time_millis) {
std::string client_name =
base::android::ConvertJavaStringToUTF8(j_client_name);
base::Time start_time =
base::Time::FromMillisecondsSinceUnixEpoch(start_time_millis);
ImageFetcherMetricsReporter::ReportImageLoadFromCacheTimeJava(client_name,
start_time);
}
// static
void ImageFetcherBridge::ReportTotalFetchTimeFromNative(
JNIEnv* j_env,
const base::android::JavaParamRef<jstring>& j_client_name,
const jlong start_time_millis) {
std::string client_name =
base::android::ConvertJavaStringToUTF8(j_client_name);
base::Time start_time =
base::Time::FromMillisecondsSinceUnixEpoch(start_time_millis);
ImageFetcherMetricsReporter::ReportTotalFetchFromNativeTimeJava(client_name,
start_time);
}
// ------------------ JNI functions ------------------
// static
ScopedJavaLocalRef<jstring> JNI_ImageFetcherBridge_GetFilePath(
JNIEnv* j_env,
const JavaParamRef<jobject>& j_simple_factory_key,
const JavaParamRef<jstring>& j_url) {
return ImageFetcherBridge::GetFilePath(j_env, j_simple_factory_key, j_url);
}
// static
void JNI_ImageFetcherBridge_FetchImageData(
JNIEnv* j_env,
const JavaParamRef<jobject>& j_simple_factory_key,
const jint j_image_fetcher_config,
const JavaParamRef<jstring>& j_url,
const JavaParamRef<jstring>& j_client_name,
const jint j_expiration_interval_mins,
const JavaParamRef<jobject>& j_callback) {
ImageFetcherBridge::FetchImageData(
j_env, j_simple_factory_key, j_image_fetcher_config, j_url, j_client_name,
j_expiration_interval_mins, j_callback);
}
// static
void JNI_ImageFetcherBridge_FetchImage(
JNIEnv* j_env,
const JavaParamRef<jobject>& j_simple_factory_key,
const jint j_image_fetcher_config,
const JavaParamRef<jstring>& j_url,
const JavaParamRef<jstring>& j_client_name,
const jint j_frame_width,
const jint j_frame_height,
const jint j_expiration_interval_mins,
const JavaParamRef<jobject>& j_callback) {
ImageFetcherBridge::FetchImage(
j_env, j_simple_factory_key, j_image_fetcher_config, j_url, j_client_name,
j_frame_width, j_frame_height, j_expiration_interval_mins, j_callback);
}
// static
void JNI_ImageFetcherBridge_ReportEvent(
JNIEnv* j_env,
const base::android::JavaParamRef<jstring>& j_client_name,
const jint j_event_id) {
ImageFetcherBridge::ReportEvent(j_env, j_client_name, j_event_id);
}
// static
void JNI_ImageFetcherBridge_ReportCacheHitTime(
JNIEnv* j_env,
const base::android::JavaParamRef<jstring>& j_client_name,
const jlong start_time_millis) {
ImageFetcherBridge::ReportCacheHitTime(j_env, j_client_name,
start_time_millis);
}
// static
void JNI_ImageFetcherBridge_ReportTotalFetchTimeFromNative(
JNIEnv* j_env,
const base::android::JavaParamRef<jstring>& j_client_name,
const jlong start_time_millis) {
ImageFetcherBridge::ReportTotalFetchTimeFromNative(j_env, j_client_name,
start_time_millis);
}
// ------------------ Private functions ------------------
// static
void ImageFetcherBridge::OnImageDataFetched(
base::android::ScopedJavaGlobalRef<jobject> callback,
const std::string& image_data,
const RequestMetadata& request_metadata) {
JNIEnv* env = jni_zero::AttachCurrentThread();
ScopedJavaLocalRef<jbyteArray> j_bytes =
base::android::ToJavaByteArray(env, image_data);
base::android::RunObjectCallbackAndroid(callback, j_bytes);
}
// static
void ImageFetcherBridge::OnImageFetched(
base::android::ScopedJavaGlobalRef<jobject> callback,
const gfx::Image& image,
const RequestMetadata& request_metadata) {
ScopedJavaLocalRef<jobject> j_bitmap;
if (!image.IsEmpty()) {
j_bitmap = gfx::ConvertToJavaBitmap(*image.ToSkBitmap());
}
base::android::RunObjectCallbackAndroid(callback, j_bitmap);
}
} // namespace image_fetcher
|