OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/logo_bridge.h" | 5 #include "chrome/browser/android/logo_bridge.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
11 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
13 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/feature_list.h" | 15 #include "base/feature_list.h" |
16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
17 #include "chrome/browser/android/chrome_feature_list.h" | 17 #include "chrome/browser/android/chrome_feature_list.h" |
18 #include "chrome/browser/android/logo_service.h" | 18 #include "chrome/browser/android/logo_service.h" |
19 #include "chrome/browser/doodle/doodle_service_factory.h" | 19 #include "chrome/browser/doodle/doodle_service_factory.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/profiles/profile_android.h" | 21 #include "chrome/browser/profiles/profile_android.h" |
22 #include "chrome/browser/search/suggestions/image_decoder_impl.h" | |
23 #include "components/image_fetcher/core/image_fetcher_impl.h" | |
24 #include "components/search_provider_logos/logo_tracker.h" | 22 #include "components/search_provider_logos/logo_tracker.h" |
25 #include "jni/LogoBridge_jni.h" | 23 #include "jni/LogoBridge_jni.h" |
26 #include "net/url_request/url_fetcher.h" | 24 #include "net/url_request/url_fetcher.h" |
27 #include "net/url_request/url_fetcher_delegate.h" | 25 #include "net/url_request/url_fetcher_delegate.h" |
28 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
29 #include "net/url_request/url_request_status.h" | 27 #include "net/url_request/url_request_status.h" |
30 #include "third_party/skia/include/core/SkBitmap.h" | 28 #include "third_party/skia/include/core/SkBitmap.h" |
31 #include "ui/gfx/android/java_bitmap.h" | 29 #include "ui/gfx/android/java_bitmap.h" |
32 #include "ui/gfx/image/image.h" | 30 #include "ui/gfx/image/image.h" |
33 #include "url/gurl.h" | 31 #include "url/gurl.h" |
34 | 32 |
35 using base::android::ConvertJavaStringToUTF8; | 33 using base::android::ConvertJavaStringToUTF8; |
36 using base::android::ConvertUTF8ToJavaString; | 34 using base::android::ConvertUTF8ToJavaString; |
37 using base::android::JavaParamRef; | 35 using base::android::JavaParamRef; |
38 using base::android::ScopedJavaLocalRef; | 36 using base::android::ScopedJavaLocalRef; |
39 using base::android::ToJavaByteArray; | 37 using base::android::ToJavaByteArray; |
40 | 38 |
41 namespace { | 39 namespace { |
42 | 40 |
43 const int64_t kMaxImageDownloadBytes = 1024 * 1024; | |
44 | |
45 ScopedJavaLocalRef<jobject> MakeJavaLogo(JNIEnv* env, | 41 ScopedJavaLocalRef<jobject> MakeJavaLogo(JNIEnv* env, |
46 const SkBitmap* bitmap, | 42 const SkBitmap* bitmap, |
47 const GURL& on_click_url, | 43 const GURL& on_click_url, |
48 const std::string& alt_text, | 44 const std::string& alt_text, |
49 const GURL& animated_url) { | 45 const GURL& animated_url) { |
50 ScopedJavaLocalRef<jobject> j_bitmap = gfx::ConvertToJavaBitmap(bitmap); | 46 ScopedJavaLocalRef<jobject> j_bitmap = gfx::ConvertToJavaBitmap(bitmap); |
51 | 47 |
52 ScopedJavaLocalRef<jstring> j_on_click_url; | 48 ScopedJavaLocalRef<jstring> j_on_click_url; |
53 if (on_click_url.is_valid()) | 49 if (on_click_url.is_valid()) |
54 j_on_click_url = ConvertUTF8ToJavaString(env, on_click_url.spec()); | 50 j_on_click_url = ConvertUTF8ToJavaString(env, on_click_url.spec()); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 LogoBridge::LogoBridge(jobject j_profile) | 189 LogoBridge::LogoBridge(jobject j_profile) |
194 : logo_service_(nullptr), | 190 : logo_service_(nullptr), |
195 doodle_service_(nullptr), | 191 doodle_service_(nullptr), |
196 doodle_observer_(this), | 192 doodle_observer_(this), |
197 weak_ptr_factory_(this) { | 193 weak_ptr_factory_(this) { |
198 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 194 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
199 DCHECK(profile); | 195 DCHECK(profile); |
200 | 196 |
201 if (base::FeatureList::IsEnabled(chrome::android::kUseNewDoodleApi)) { | 197 if (base::FeatureList::IsEnabled(chrome::android::kUseNewDoodleApi)) { |
202 doodle_service_ = DoodleServiceFactory::GetForProfile(profile); | 198 doodle_service_ = DoodleServiceFactory::GetForProfile(profile); |
203 image_fetcher_ = base::MakeUnique<image_fetcher::ImageFetcherImpl>( | |
204 base::MakeUnique<suggestions::ImageDecoderImpl>(), | |
205 profile->GetRequestContext()); | |
206 image_fetcher_->SetImageDownloadLimit(kMaxImageDownloadBytes); | |
207 | |
208 doodle_observer_.Add(doodle_service_); | 199 doodle_observer_.Add(doodle_service_); |
209 } else { | 200 } else { |
210 logo_service_ = LogoServiceFactory::GetForProfile(profile); | 201 logo_service_ = LogoServiceFactory::GetForProfile(profile); |
211 } | 202 } |
212 | 203 |
213 animated_logo_fetcher_ = base::MakeUnique<AnimatedLogoFetcher>( | 204 animated_logo_fetcher_ = base::MakeUnique<AnimatedLogoFetcher>( |
214 profile->GetRequestContext()); | 205 profile->GetRequestContext()); |
215 } | 206 } |
216 | 207 |
217 LogoBridge::~LogoBridge() {} | 208 LogoBridge::~LogoBridge() {} |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 void LogoBridge::NotifyNoLogoAvailable(bool from_cache) { | 268 void LogoBridge::NotifyNoLogoAvailable(bool from_cache) { |
278 JNIEnv* env = base::android::AttachCurrentThread(); | 269 JNIEnv* env = base::android::AttachCurrentThread(); |
279 Java_LogoObserver_onLogoAvailable(env, j_logo_observer_, | 270 Java_LogoObserver_onLogoAvailable(env, j_logo_observer_, |
280 ScopedJavaLocalRef<jobject>(), from_cache); | 271 ScopedJavaLocalRef<jobject>(), from_cache); |
281 } | 272 } |
282 | 273 |
283 void LogoBridge::FetchDoodleImage(const doodle::DoodleConfig& doodle_config, | 274 void LogoBridge::FetchDoodleImage(const doodle::DoodleConfig& doodle_config, |
284 bool from_cache) { | 275 bool from_cache) { |
285 DCHECK(!j_logo_observer_.is_null()); | 276 DCHECK(!j_logo_observer_.is_null()); |
286 | 277 |
287 // If there is a CTA image, that means the main image is animated. Show the | 278 // If there is a CTA image, that means the main image is animated. We show the |
288 // non-animated CTA image first, and load the animated one only when the | 279 // non-animated CTA image first, and load the animated one only when the |
289 // user requests it. | 280 // user requests it. |
290 bool has_cta = doodle_config.large_cta_image.has_value(); | 281 bool has_cta = doodle_config.large_cta_image.has_value(); |
291 const GURL& image_url = has_cta ? doodle_config.large_cta_image->url | |
292 : doodle_config.large_image.url; | |
293 const GURL& animated_image_url = | 282 const GURL& animated_image_url = |
294 has_cta ? doodle_config.large_image.url : GURL::EmptyGURL(); | 283 has_cta ? doodle_config.large_image.url : GURL::EmptyGURL(); |
295 // TODO(treib): For interactive doodles, use |fullpage_interactive_url| | |
fhorschig
2017/05/15 13:54:35
Why has that changed?
Marc Treib
2017/05/15 14:25:21
Sorry, I meant to comment on that and then forgot
| |
296 // instead of |target_url|? | |
297 const GURL& on_click_url = doodle_config.target_url; | 284 const GURL& on_click_url = doodle_config.target_url; |
298 const std::string& alt_text = doodle_config.alt_text; | 285 const std::string& alt_text = doodle_config.alt_text; |
299 image_fetcher_->StartOrQueueNetworkRequest( | 286 doodle_service_->GetImage( |
300 image_url.spec(), image_url, | |
301 base::Bind(&LogoBridge::DoodleImageFetched, base::Unretained(this), | 287 base::Bind(&LogoBridge::DoodleImageFetched, base::Unretained(this), |
302 from_cache, on_click_url, alt_text, animated_image_url)); | 288 from_cache, on_click_url, alt_text, animated_image_url)); |
303 } | 289 } |
304 | 290 |
305 void LogoBridge::DoodleImageFetched( | 291 void LogoBridge::DoodleImageFetched(bool config_from_cache, |
306 bool config_from_cache, | 292 const GURL& on_click_url, |
307 const GURL& on_click_url, | 293 const std::string& alt_text, |
308 const std::string& alt_text, | 294 const GURL& animated_image_url, |
309 const GURL& animated_image_url, | 295 const gfx::Image& image) { |
310 const std::string& image_fetch_id, | |
311 const gfx::Image& image, | |
312 const image_fetcher::RequestMetadata& metadata) { | |
313 JNIEnv* env = base::android::AttachCurrentThread(); | 296 JNIEnv* env = base::android::AttachCurrentThread(); |
314 | 297 |
315 if (image.IsEmpty()) { | 298 ScopedJavaLocalRef<jobject> j_logo; |
316 DLOG(WARNING) << "Failed to download doodle image"; | 299 if (!image.IsEmpty()) { |
317 Java_LogoObserver_onLogoAvailable(env, j_logo_observer_, | 300 j_logo = MakeJavaLogo(env, image.ToSkBitmap(), on_click_url, alt_text, |
318 ScopedJavaLocalRef<jobject>(), | 301 animated_image_url); |
319 config_from_cache); | |
320 return; | |
321 } | 302 } |
322 | 303 |
323 UMA_HISTOGRAM_BOOLEAN("NewTabPage.LogoImageDownloaded", | |
324 metadata.from_http_cache); | |
325 | |
326 ScopedJavaLocalRef<jobject> j_logo = MakeJavaLogo( | |
327 env, image.ToSkBitmap(), on_click_url, alt_text, animated_image_url); | |
328 Java_LogoObserver_onLogoAvailable(env, j_logo_observer_, j_logo, | 304 Java_LogoObserver_onLogoAvailable(env, j_logo_observer_, j_logo, |
329 config_from_cache); | 305 config_from_cache); |
330 } | 306 } |
331 | 307 |
332 // static | 308 // static |
333 bool RegisterLogoBridge(JNIEnv* env) { | 309 bool RegisterLogoBridge(JNIEnv* env) { |
334 return RegisterNativesImpl(env); | 310 return RegisterNativesImpl(env); |
335 } | 311 } |
OLD | NEW |