OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/tab/background_content_view_helper.h" |
| 6 |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "chrome/browser/android/chrome_web_contents_delegate_android.h" |
| 9 #include "chrome/browser/android/tab_android.h" |
| 10 #include "chrome/browser/ui/android/window_android_helper.h" |
| 11 #include "chrome/browser/ui/tab_helpers.h" |
| 12 #include "chrome/common/variations/variations_util.h" |
| 13 #include "components/variations/variations_associated_data.h" |
| 14 #include "content/public/browser/android/content_view_core.h" |
| 15 #include "content/public/browser/web_contents.h" |
| 16 #include "jni/BackgroundContentViewHelper_jni.h" |
| 17 |
| 18 BackgroundContentViewHelper* BackgroundContentViewHelper::FromJavaObject( |
| 19 JNIEnv* env, |
| 20 jobject jobj) { |
| 21 return reinterpret_cast<BackgroundContentViewHelper*>( |
| 22 Java_BackgroundContentViewHelper_getNativePtr(env, jobj)); |
| 23 } |
| 24 |
| 25 BackgroundContentViewHelper::BackgroundContentViewHelper() { |
| 26 } |
| 27 |
| 28 BackgroundContentViewHelper::~BackgroundContentViewHelper() { |
| 29 } |
| 30 |
| 31 void BackgroundContentViewHelper::Destroy(JNIEnv* env, jobject jobj) { |
| 32 delete this; |
| 33 } |
| 34 |
| 35 void BackgroundContentViewHelper::SetWebContents( |
| 36 JNIEnv* env, |
| 37 jobject jobj, |
| 38 jobject jcontent_view_core, |
| 39 jobject jweb_contents_delegate) { |
| 40 content::ContentViewCore* content_view_core = |
| 41 content::ContentViewCore::GetNativeContentViewCore(env, |
| 42 jcontent_view_core); |
| 43 DCHECK(content_view_core); |
| 44 DCHECK(content_view_core->GetWebContents()); |
| 45 |
| 46 web_contents_.reset(content_view_core->GetWebContents()); |
| 47 // TODO(ksimbili): Do we actually need tab helpers? |
| 48 TabAndroid::AttachTabHelpers(web_contents_.get()); |
| 49 WindowAndroidHelper::FromWebContents(web_contents_.get())-> |
| 50 SetWindowAndroid(content_view_core->GetWindowAndroid()); |
| 51 web_contents_delegate_.reset( |
| 52 new chrome::android::ChromeWebContentsDelegateAndroid( |
| 53 env, jweb_contents_delegate)); |
| 54 web_contents_delegate_->LoadProgressChanged(web_contents_.get(), 0); |
| 55 web_contents_->SetDelegate(web_contents_delegate_.get()); |
| 56 } |
| 57 |
| 58 void BackgroundContentViewHelper::ReleaseWebContents(JNIEnv* env, |
| 59 jobject jobj) { |
| 60 DCHECK(web_contents_.get()); |
| 61 ignore_result(web_contents_.release()); |
| 62 } |
| 63 |
| 64 void BackgroundContentViewHelper::DestroyWebContents(JNIEnv* env, |
| 65 jobject jobj) { |
| 66 DCHECK(web_contents_.get()); |
| 67 web_contents_->SetDelegate(NULL); |
| 68 web_contents_.reset(); |
| 69 DCHECK(web_contents_delegate_.get()); |
| 70 web_contents_delegate_.reset(); |
| 71 } |
| 72 |
| 73 void BackgroundContentViewHelper::MergeHistoryFrom(JNIEnv* env, |
| 74 jobject obj, |
| 75 jobject jweb_contents) { |
| 76 if (!web_contents_.get()) |
| 77 return; |
| 78 content::WebContents* from_web_contents = |
| 79 content::WebContents::FromJavaWebContents(jweb_contents); |
| 80 DCHECK(from_web_contents); |
| 81 |
| 82 if (web_contents_->GetController().CanPruneAllButLastCommitted()) { |
| 83 web_contents_->GetController().CopyStateFromAndPrune( |
| 84 &from_web_contents->GetController(), true); |
| 85 } else { |
| 86 web_contents_->GetController().CopyStateFrom( |
| 87 from_web_contents->GetController()); |
| 88 } |
| 89 } |
| 90 |
| 91 namespace { |
| 92 |
| 93 class OnCloseWebContentsDeleter |
| 94 : public content::WebContentsDelegate, |
| 95 public base::SupportsWeakPtr<OnCloseWebContentsDeleter> { |
| 96 public: |
| 97 explicit OnCloseWebContentsDeleter(content::WebContents* web_contents) |
| 98 : web_contents_(web_contents) { |
| 99 web_contents_->SetDelegate(this); |
| 100 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 101 base::Bind(&OnCloseWebContentsDeleter::Destroy, |
| 102 AsWeakPtr()), |
| 103 base::TimeDelta::FromSeconds(kDeleteWithExtremePrejudiceSeconds)); |
| 104 } |
| 105 |
| 106 void CloseContents(content::WebContents* source) override { |
| 107 DCHECK_EQ(web_contents_, source); |
| 108 Destroy(); |
| 109 } |
| 110 |
| 111 void SwappedOut(content::WebContents* source) override { |
| 112 DCHECK_EQ(web_contents_, source); |
| 113 Destroy(); |
| 114 } |
| 115 |
| 116 virtual void Destroy() { |
| 117 delete this; |
| 118 } |
| 119 |
| 120 private: |
| 121 static const int kDeleteWithExtremePrejudiceSeconds = 3; |
| 122 |
| 123 scoped_ptr<content::WebContents> web_contents_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(OnCloseWebContentsDeleter); |
| 126 }; |
| 127 |
| 128 } // namespace |
| 129 |
| 130 void BackgroundContentViewHelper::UnloadAndDeleteWebContents( |
| 131 JNIEnv* env, |
| 132 jobject jobj, |
| 133 jobject jweb_contents) { |
| 134 content::WebContents* web_contents = |
| 135 content::WebContents::FromJavaWebContents(jweb_contents); |
| 136 if (!web_contents) |
| 137 return; |
| 138 web_contents->DispatchBeforeUnload(false); |
| 139 ignore_result(new OnCloseWebContentsDeleter(web_contents)); |
| 140 } |
| 141 |
| 142 int BackgroundContentViewHelper::GetSwapTimeoutMs(JNIEnv* env, jobject jobj) { |
| 143 std::string value = variations::GetVariationParamValue( |
| 144 "InstantSearchClicks", "swap_timeout"); |
| 145 int swap_timeout = 0; |
| 146 return (base::StringToInt(value.c_str(), &swap_timeout) ? swap_timeout : 0); |
| 147 } |
| 148 |
| 149 // Returns the minimum number of renderer frames needed before swap is |
| 150 // triggered. The default value returned is 2 and is configurable by finch |
| 151 // params. The reason for default being 2 is, the background image requests are |
| 152 // always issued only after the first non-empty layout(first recalc style) and |
| 153 // hence most of the page loads doesn't have background images rendered in their |
| 154 // first frame. |
| 155 int BackgroundContentViewHelper::GetNumFramesNeededForSwap(JNIEnv* env, |
| 156 jobject jobj) { |
| 157 std::string value = variations::GetVariationParamValue( |
| 158 "InstantSearchClicks", "num_frames_needed_for_swap"); |
| 159 int num_frames_needed = 0; |
| 160 return (base::StringToInt(value.c_str(), |
| 161 &num_frames_needed) ? num_frames_needed : 2); |
| 162 } |
| 163 |
| 164 static jlong Init(JNIEnv* env, jobject obj) { |
| 165 return reinterpret_cast<long>(new BackgroundContentViewHelper()); |
| 166 } |
| 167 |
| 168 bool BackgroundContentViewHelper::Register(JNIEnv* env) { |
| 169 return RegisterNativesImpl(env); |
| 170 } |
OLD | NEW |