Chromium Code Reviews| 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/compositor/tab_content_manager.h" | 5 #include "chrome/browser/android/compositor/tab_content_manager.h" |
| 6 | 6 |
| 7 #include <android/bitmap.h> | 7 #include <android/bitmap.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
| 13 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
| 14 #include "base/android/scoped_java_ref.h" | 14 #include "base/android/scoped_java_ref.h" |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/bind_helpers.h" | 16 #include "base/bind_helpers.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 19 #include "cc/layers/layer.h" | 19 #include "cc/layers/layer.h" |
| 20 #include "chrome/browser/android/compositor/layer/thumbnail_layer.h" | 20 #include "chrome/browser/android/compositor/layer/thumbnail_layer.h" |
| 21 #include "chrome/browser/android/tab_android.h" | 21 #include "chrome/browser/android/tab_android.h" |
| 22 #include "chrome/browser/android/thumbnail/thumbnail.h" | 22 #include "chrome/browser/android/thumbnail/thumbnail.h" |
| 23 #include "content/public/browser/android/content_view_core.h" | |
| 24 #include "content/public/browser/readback_types.h" | 23 #include "content/public/browser/readback_types.h" |
| 25 #include "content/public/browser/render_view_host.h" | 24 #include "content/public/browser/render_view_host.h" |
| 26 #include "content/public/browser/render_widget_host.h" | 25 #include "content/public/browser/render_widget_host.h" |
| 27 #include "content/public/browser/render_widget_host_view.h" | 26 #include "content/public/browser/render_widget_host_view.h" |
| 28 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 29 #include "jni/TabContentManager_jni.h" | 28 #include "jni/TabContentManager_jni.h" |
| 30 #include "ui/android/resources/ui_resource_provider.h" | 29 #include "ui/android/resources/ui_resource_provider.h" |
| 31 #include "ui/gfx/android/java_bitmap.h" | 30 #include "ui/gfx/android/java_bitmap.h" |
| 32 #include "ui/gfx/geometry/dip_util.h" | 31 #include "ui/gfx/geometry/dip_util.h" |
| 33 #include "ui/gfx/geometry/rect.h" | 32 #include "ui/gfx/geometry/rect.h" |
| 34 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 35 | 34 |
| 36 namespace { | 35 namespace { |
| 37 | 36 |
| 38 const size_t kMaxReadbacks = 1; | 37 const size_t kMaxReadbacks = 1; |
| 39 typedef base::Callback<void(float, const SkBitmap&)> TabReadbackCallback; | 38 typedef base::Callback<void(float, const SkBitmap&)> TabReadbackCallback; |
| 40 | 39 |
| 41 } // namespace | 40 } // namespace |
| 42 | 41 |
| 43 namespace chrome { | 42 namespace chrome { |
| 44 namespace android { | 43 namespace android { |
| 45 | 44 |
| 46 class TabContentManager::TabReadbackRequest { | 45 class TabContentManager::TabReadbackRequest { |
| 47 public: | 46 public: |
| 48 TabReadbackRequest(jobject content_view_core, | 47 TabReadbackRequest(content::WebContents* web_contents, |
| 49 float thumbnail_scale, | 48 float thumbnail_scale, |
| 50 const TabReadbackCallback& end_callback) | 49 const TabReadbackCallback& end_callback) |
| 51 : thumbnail_scale_(thumbnail_scale), | 50 : thumbnail_scale_(thumbnail_scale), |
| 52 end_callback_(end_callback), | 51 end_callback_(end_callback), |
| 53 drop_after_readback_(false), | 52 drop_after_readback_(false), |
| 54 weak_factory_(this) { | 53 weak_factory_(this) { |
| 55 JNIEnv* env = base::android::AttachCurrentThread(); | 54 DCHECK(web_contents); |
| 56 j_content_view_core_.Reset(env, content_view_core); | |
| 57 } | |
| 58 | |
| 59 virtual ~TabReadbackRequest() {} | |
| 60 | |
| 61 void Run() { | |
| 62 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 63 content::ReadbackRequestCallback result_callback = | 55 content::ReadbackRequestCallback result_callback = |
| 64 base::Bind(&TabReadbackRequest::OnFinishGetTabThumbnailBitmap, | 56 base::Bind(&TabReadbackRequest::OnFinishGetTabThumbnailBitmap, |
| 65 weak_factory_.GetWeakPtr()); | 57 weak_factory_.GetWeakPtr()); |
| 66 | 58 |
| 67 if (j_content_view_core_.is_null()) { | 59 content::RenderWidgetHost* rwh = |
| 68 result_callback.Run(SkBitmap(), content::READBACK_FAILED); | 60 web_contents->GetRenderViewHost()->GetWidget(); |
| 69 return; | 61 DCHECK(rwh); |
| 70 } | |
| 71 | |
| 72 content::ContentViewCore* view = | |
| 73 content::ContentViewCore::GetNativeContentViewCore( | |
| 74 env, j_content_view_core_.obj()); | |
| 75 | |
| 76 if (!view) { | |
| 77 result_callback.Run(SkBitmap(), content::READBACK_FAILED); | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 DCHECK(view->GetWebContents()); | |
| 82 content::RenderWidgetHost* rwh = view->GetWebContents() | |
| 83 ->GetRenderViewHost() | |
| 84 ->GetWidget(); | |
| 85 | 62 |
| 86 SkColorType color_type = kN32_SkColorType; | 63 SkColorType color_type = kN32_SkColorType; |
| 87 gfx::Rect src_rect = rwh->GetView()->GetViewBounds(); | 64 gfx::Rect src_rect = rwh->GetView()->GetViewBounds(); |
| 88 gfx::Size dst_size( | 65 gfx::Size dst_size( |
| 89 gfx::ScaleToCeiledSize(src_rect.size(), thumbnail_scale_)); | 66 gfx::ScaleToCeiledSize(src_rect.size(), thumbnail_scale_)); |
| 90 rwh->CopyFromBackingStore(src_rect, dst_size, result_callback, color_type); | 67 rwh->CopyFromBackingStore(src_rect, dst_size, result_callback, color_type); |
| 91 } | 68 } |
| 92 | 69 |
| 70 virtual ~TabReadbackRequest() {} | |
| 71 | |
| 93 void OnFinishGetTabThumbnailBitmap(const SkBitmap& bitmap, | 72 void OnFinishGetTabThumbnailBitmap(const SkBitmap& bitmap, |
| 94 content::ReadbackResponse response) { | 73 content::ReadbackResponse response) { |
| 95 DCHECK(!j_content_view_core_.is_null()); | |
| 96 if (response != content::READBACK_SUCCESS || drop_after_readback_) { | 74 if (response != content::READBACK_SUCCESS || drop_after_readback_) { |
| 97 end_callback_.Run(0.f, SkBitmap()); | 75 end_callback_.Run(0.f, SkBitmap()); |
| 98 return; | 76 return; |
| 99 } | 77 } |
| 100 | 78 |
| 101 SkBitmap result_bitmap = bitmap; | 79 SkBitmap result_bitmap = bitmap; |
| 102 result_bitmap.setImmutable(); | 80 result_bitmap.setImmutable(); |
| 103 end_callback_.Run(thumbnail_scale_, bitmap); | 81 end_callback_.Run(thumbnail_scale_, bitmap); |
| 104 } | 82 } |
| 105 | 83 |
| 106 void SetToDropAfterReadback() { drop_after_readback_ = true; } | 84 void SetToDropAfterReadback() { drop_after_readback_ = true; } |
| 107 | 85 |
| 108 private: | 86 private: |
| 109 base::android::ScopedJavaGlobalRef<jobject> j_content_view_core_; | |
| 110 const float thumbnail_scale_; | 87 const float thumbnail_scale_; |
| 111 TabReadbackCallback end_callback_; | 88 TabReadbackCallback end_callback_; |
| 112 bool drop_after_readback_; | 89 bool drop_after_readback_; |
| 113 | 90 |
| 114 base::WeakPtrFactory<TabReadbackRequest> weak_factory_; | 91 base::WeakPtrFactory<TabReadbackRequest> weak_factory_; |
| 115 | 92 |
| 116 DISALLOW_COPY_AND_ASSIGN(TabReadbackRequest); | 93 DISALLOW_COPY_AND_ASSIGN(TabReadbackRequest); |
| 117 }; | 94 }; |
| 118 | 95 |
| 119 // static | 96 // static |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 jboolean TabContentManager::HasFullCachedThumbnail( | 204 jboolean TabContentManager::HasFullCachedThumbnail( |
| 228 JNIEnv* env, | 205 JNIEnv* env, |
| 229 const JavaParamRef<jobject>& obj, | 206 const JavaParamRef<jobject>& obj, |
| 230 jint tab_id) { | 207 jint tab_id) { |
| 231 return thumbnail_cache_->Get(tab_id, false, false) != nullptr; | 208 return thumbnail_cache_->Get(tab_id, false, false) != nullptr; |
| 232 } | 209 } |
| 233 | 210 |
| 234 void TabContentManager::CacheTab(JNIEnv* env, | 211 void TabContentManager::CacheTab(JNIEnv* env, |
| 235 const JavaParamRef<jobject>& obj, | 212 const JavaParamRef<jobject>& obj, |
| 236 const JavaParamRef<jobject>& tab, | 213 const JavaParamRef<jobject>& tab, |
| 237 const JavaParamRef<jobject>& content_view_core, | 214 const JavaParamRef<jobject>& jweb_contents, |
| 238 jfloat thumbnail_scale) { | 215 jfloat thumbnail_scale) { |
| 239 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, tab); | 216 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, tab); |
| 240 DCHECK(tab_android); | 217 DCHECK(tab_android); |
| 241 int tab_id = tab_android->GetAndroidId(); | 218 int tab_id = tab_android->GetAndroidId(); |
| 242 GURL url = tab_android->GetURL(); | 219 GURL url = tab_android->GetURL(); |
| 243 | 220 |
| 244 content::ContentViewCore* view = | 221 content::WebContents* web_contents = |
| 245 content::ContentViewCore::GetNativeContentViewCore(env, | 222 content::WebContents::FromJavaWebContents(jweb_contents.obj()); |
|
Ted C
2016/07/25 23:46:54
not an entirely fair comment since you're just cha
no sievers
2016/07/29 20:54:25
Good point, even better. Done.
| |
| 246 content_view_core); | 223 DCHECK(web_contents); |
| 247 | 224 |
| 248 if (thumbnail_cache_->CheckAndUpdateThumbnailMetaData(tab_id, url)) { | 225 if (thumbnail_cache_->CheckAndUpdateThumbnailMetaData(tab_id, url)) { |
| 249 if (!view || | 226 if (!web_contents->GetRenderViewHost() || |
| 250 !view->GetWebContents()->GetRenderViewHost() || | 227 !web_contents->GetRenderViewHost()->GetWidget() || |
| 251 !view->GetWebContents()->GetRenderViewHost()->GetWidget() || | 228 !web_contents->GetRenderViewHost() |
| 252 !view->GetWebContents() | |
| 253 ->GetRenderViewHost() | |
| 254 ->GetWidget() | 229 ->GetWidget() |
| 255 ->CanCopyFromBackingStore() || | 230 ->CanCopyFromBackingStore() || |
| 256 pending_tab_readbacks_.find(tab_id) != pending_tab_readbacks_.end() || | 231 pending_tab_readbacks_.find(tab_id) != pending_tab_readbacks_.end() || |
| 257 pending_tab_readbacks_.size() >= kMaxReadbacks) { | 232 pending_tab_readbacks_.size() >= kMaxReadbacks) { |
| 258 thumbnail_cache_->Remove(tab_id); | 233 thumbnail_cache_->Remove(tab_id); |
| 259 return; | 234 return; |
| 260 } | 235 } |
| 261 | 236 |
| 262 TabReadbackCallback readback_done_callback = | 237 TabReadbackCallback readback_done_callback = |
| 263 base::Bind(&TabContentManager::PutThumbnailIntoCache, | 238 base::Bind(&TabContentManager::PutThumbnailIntoCache, |
| 264 weak_factory_.GetWeakPtr(), tab_id); | 239 weak_factory_.GetWeakPtr(), tab_id); |
| 265 std::unique_ptr<TabReadbackRequest> readback_request = | 240 pending_tab_readbacks_.set( |
| 266 base::WrapUnique(new TabReadbackRequest( | 241 tab_id, base::WrapUnique(new TabReadbackRequest( |
| 267 content_view_core, thumbnail_scale, readback_done_callback)); | 242 web_contents, thumbnail_scale, readback_done_callback))); |
|
Ted C
2016/07/25 23:46:53
this indenting looks off, shouldn't it be like it
no sievers
2016/07/29 20:54:24
I'm actually not sure, but clang-format did this,
| |
| 268 pending_tab_readbacks_.set(tab_id, std::move(readback_request)); | |
|
Ted C
2016/07/25 23:46:53
any idea why it was done this way in the past? do
no sievers
2016/07/29 20:54:24
Yes, true, but no idea why it did it that way. It
| |
| 269 pending_tab_readbacks_.get(tab_id)->Run(); | |
| 270 } | 243 } |
| 271 } | 244 } |
| 272 | 245 |
| 273 void TabContentManager::CacheTabWithBitmap(JNIEnv* env, | 246 void TabContentManager::CacheTabWithBitmap(JNIEnv* env, |
| 274 const JavaParamRef<jobject>& obj, | 247 const JavaParamRef<jobject>& obj, |
| 275 const JavaParamRef<jobject>& tab, | 248 const JavaParamRef<jobject>& tab, |
| 276 const JavaParamRef<jobject>& bitmap, | 249 const JavaParamRef<jobject>& bitmap, |
| 277 jfloat thumbnail_scale) { | 250 jfloat thumbnail_scale) { |
| 278 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, tab); | 251 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, tab); |
| 279 DCHECK(tab_android); | 252 DCHECK(tab_android); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 371 jboolean use_approximation_thumbnail) { | 344 jboolean use_approximation_thumbnail) { |
| 372 TabContentManager* manager = new TabContentManager( | 345 TabContentManager* manager = new TabContentManager( |
| 373 env, obj, default_cache_size, approximation_cache_size, | 346 env, obj, default_cache_size, approximation_cache_size, |
| 374 compression_queue_max_size, write_queue_max_size, | 347 compression_queue_max_size, write_queue_max_size, |
| 375 use_approximation_thumbnail); | 348 use_approximation_thumbnail); |
| 376 return reinterpret_cast<intptr_t>(manager); | 349 return reinterpret_cast<intptr_t>(manager); |
| 377 } | 350 } |
| 378 | 351 |
| 379 } // namespace android | 352 } // namespace android |
| 380 } // namespace chrome | 353 } // namespace chrome |
| OLD | NEW |