Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/web_contents/web_contents_android.h" | 5 #include "content/browser/web_contents/web_contents_android.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "content/browser/android/interstitial_page_delegate_android.h" | |
| 11 #include "content/browser/frame_host/interstitial_page_impl.h" | |
| 12 #include "content/browser/frame_host/navigation_controller_impl.h" | |
| 13 #include "content/browser/frame_host/navigation_entry_impl.h" | |
| 14 #include "content/browser/media/media_web_contents_observer.h" | |
| 15 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 16 #include "content/browser/ssl/ssl_host_state.h" | |
| 17 #include "content/common/frame_messages.h" | |
| 18 #include "content/common/input_messages.h" | |
| 19 #include "content/common/view_messages.h" | |
| 10 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 12 #include "jni/WebContentsImpl_jni.h" | 22 #include "jni/WebContentsImpl_jni.h" |
| 23 #include "ui/gfx/screen.h" | |
| 13 | 24 |
| 14 using base::android::AttachCurrentThread; | 25 using base::android::AttachCurrentThread; |
| 26 using base::android::ConvertJavaStringToUTF8; | |
| 27 using base::android::ConvertUTF8ToJavaString; | |
| 15 | 28 |
| 16 namespace content { | 29 namespace content { |
| 17 | 30 |
| 18 // static | 31 // static |
| 19 WebContents* WebContents::FromJavaWebContents( | 32 WebContents* WebContents::FromJavaWebContents( |
| 20 jobject jweb_contents_android) { | 33 jobject jweb_contents_android) { |
| 21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 22 if (!jweb_contents_android) | 35 if (!jweb_contents_android) |
| 23 return NULL; | 36 return NULL; |
| 24 | 37 |
| 25 WebContentsAndroid* web_contents_android = | 38 WebContentsAndroid* web_contents_android = |
| 26 reinterpret_cast<WebContentsAndroid*>( | 39 reinterpret_cast<WebContentsAndroid*>( |
| 27 Java_WebContentsImpl_getNativePointer(AttachCurrentThread(), | 40 Java_WebContentsImpl_getNativePointer(AttachCurrentThread(), |
| 28 jweb_contents_android)); | 41 jweb_contents_android)); |
| 29 if (!web_contents_android) | 42 if (!web_contents_android) |
| 30 return NULL; | 43 return NULL; |
| 31 return web_contents_android->web_contents(); | 44 return web_contents_android->web_contents(); |
| 32 } | 45 } |
| 33 | 46 |
| 34 // static | 47 // static |
| 35 bool WebContentsAndroid::Register(JNIEnv* env) { | 48 bool WebContentsAndroid::Register(JNIEnv* env) { |
| 36 return RegisterNativesImpl(env); | 49 return RegisterNativesImpl(env); |
| 37 } | 50 } |
| 38 | 51 |
| 52 float GetPrimaryDisplayDeviceScaleFactor() { | |
| 53 const gfx::Display& display = | |
| 54 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay(); | |
| 55 return display.device_scale_factor(); | |
| 56 } | |
| 57 | |
| 39 WebContentsAndroid::WebContentsAndroid(WebContents* web_contents) | 58 WebContentsAndroid::WebContentsAndroid(WebContents* web_contents) |
| 40 : web_contents_(web_contents), | 59 : web_contents_(web_contents), |
| 41 navigation_controller_(&(web_contents->GetController())) { | 60 navigation_controller_(&(web_contents->GetController())), |
| 61 dpi_scale_(GetPrimaryDisplayDeviceScaleFactor()) { | |
| 42 JNIEnv* env = AttachCurrentThread(); | 62 JNIEnv* env = AttachCurrentThread(); |
| 43 obj_.Reset(env, | 63 obj_.Reset(env, |
| 44 Java_WebContentsImpl_create( | 64 Java_WebContentsImpl_create( |
| 45 env, | 65 env, |
| 46 reinterpret_cast<intptr_t>(this), | 66 reinterpret_cast<intptr_t>(this), |
| 47 navigation_controller_.GetJavaObject().obj()).obj()); | 67 navigation_controller_.GetJavaObject().obj()).obj()); |
| 48 } | 68 } |
| 49 | 69 |
| 50 WebContentsAndroid::~WebContentsAndroid() { | 70 WebContentsAndroid::~WebContentsAndroid() { |
| 51 Java_WebContentsImpl_destroy(AttachCurrentThread(), obj_.obj()); | 71 Java_WebContentsImpl_destroy(AttachCurrentThread(), obj_.obj()); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 70 | 90 |
| 71 void WebContentsAndroid::Stop(JNIEnv* env, jobject obj) { | 91 void WebContentsAndroid::Stop(JNIEnv* env, jobject obj) { |
| 72 web_contents_->Stop(); | 92 web_contents_->Stop(); |
| 73 } | 93 } |
| 74 | 94 |
| 75 void WebContentsAndroid::InsertCSS( | 95 void WebContentsAndroid::InsertCSS( |
| 76 JNIEnv* env, jobject jobj, jstring jcss) { | 96 JNIEnv* env, jobject jobj, jstring jcss) { |
| 77 web_contents_->InsertCSS(base::android::ConvertJavaStringToUTF8(env, jcss)); | 97 web_contents_->InsertCSS(base::android::ConvertJavaStringToUTF8(env, jcss)); |
| 78 } | 98 } |
| 79 | 99 |
| 100 RenderWidgetHostViewAndroid* | |
| 101 WebContentsAndroid::GetRenderWidgetHostViewAndroid() { | |
| 102 RenderWidgetHostView* rwhv = NULL; | |
| 103 if (web_contents_) { | |
| 104 rwhv = web_contents_->GetRenderWidgetHostView(); | |
| 105 if (web_contents_->ShowingInterstitialPage()) { | |
| 106 rwhv = static_cast<InterstitialPageImpl*>( | |
| 107 web_contents_->GetInterstitialPage())-> | |
| 108 GetRenderViewHost()->GetView(); | |
| 109 } | |
| 110 } | |
| 111 return static_cast<RenderWidgetHostViewAndroid*>(rwhv); | |
| 112 } | |
| 113 | |
| 114 jint WebContentsAndroid::GetBackgroundColor(JNIEnv* env, jobject obj) { | |
| 115 RenderWidgetHostViewAndroid* rwhva = GetRenderWidgetHostViewAndroid(); | |
| 116 if (!rwhva) | |
| 117 return SK_ColorWHITE; | |
| 118 return rwhva->GetCachedBackgroundColor(); | |
| 119 } | |
| 120 | |
| 121 void WebContentsAndroid::OnHide(JNIEnv* env, jobject obj) { | |
| 122 web_contents_->WasHidden(); | |
| 123 PauseVideo(); | |
| 124 } | |
| 125 | |
| 126 void WebContentsAndroid::OnShow(JNIEnv* env, jobject obj) { | |
| 127 web_contents_->WasShown(); | |
| 128 } | |
| 129 | |
| 130 void WebContentsAndroid::PauseVideo() { | |
| 131 RenderViewHostImpl* rvhi = static_cast<RenderViewHostImpl*>( | |
| 132 web_contents_->GetRenderViewHost()); | |
| 133 if (rvhi) | |
| 134 rvhi->media_web_contents_observer()->PauseVideo(); | |
| 135 } | |
| 136 | |
| 137 void WebContentsAndroid::AddStyleSheetByURL( | |
| 138 JNIEnv* env, | |
| 139 jobject obj, | |
| 140 jstring url) { | |
| 141 if (!web_contents_) | |
| 142 return; | |
| 143 | |
| 144 web_contents_->GetMainFrame()->Send(new FrameMsg_AddStyleSheetByURL( | |
| 145 web_contents_->GetMainFrame()->GetRoutingID(), | |
| 146 ConvertJavaStringToUTF8(env, url))); | |
| 147 } | |
| 148 | |
| 149 void WebContentsAndroid::ShowInterstitialPage( | |
| 150 JNIEnv* env, | |
| 151 jobject obj, | |
| 152 jstring jurl, | |
| 153 jlong delegate_ptr) { | |
| 154 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl)); | |
| 155 InterstitialPageDelegateAndroid* delegate = | |
| 156 reinterpret_cast<InterstitialPageDelegateAndroid*>(delegate_ptr); | |
| 157 InterstitialPage* interstitial = InterstitialPage::Create( | |
| 158 web_contents_, false, url, delegate); | |
| 159 delegate->set_interstitial_page(interstitial); | |
| 160 interstitial->Show(); | |
| 161 } | |
| 162 | |
| 163 jboolean WebContentsAndroid::IsShowingInterstitialPage(JNIEnv* env, | |
| 164 jobject obj) { | |
| 165 return web_contents_->ShowingInterstitialPage(); | |
| 166 } | |
| 167 | |
| 168 jboolean WebContentsAndroid::IsRenderWidgetHostViewReady( | |
| 169 JNIEnv* env, | |
| 170 jobject obj) { | |
| 171 RenderWidgetHostViewAndroid* view = GetRenderWidgetHostViewAndroid(); | |
| 172 return view && view->HasValidFrame(); | |
| 173 } | |
| 174 | |
| 175 void WebContentsAndroid::ExitFullscreen(JNIEnv* env, jobject obj) { | |
| 176 RenderViewHost* host = web_contents_->GetRenderViewHost(); | |
| 177 if (!host) | |
| 178 return; | |
| 179 host->ExitFullscreen(); | |
| 180 } | |
| 181 | |
| 182 void WebContentsAndroid::UpdateTopControlsState( | |
| 183 JNIEnv* env, | |
| 184 jobject obj, | |
| 185 bool enable_hiding, | |
| 186 bool enable_showing, | |
| 187 bool animate) { | |
| 188 RenderViewHost* host = web_contents_->GetRenderViewHost(); | |
| 189 if (!host) | |
| 190 return; | |
| 191 host->Send(new ViewMsg_UpdateTopControlsState(host->GetRoutingID(), | |
| 192 enable_hiding, | |
| 193 enable_showing, | |
| 194 animate)); | |
| 195 } | |
| 196 | |
| 197 void WebContentsAndroid::ShowImeIfNeeded(JNIEnv* env, jobject obj) { | |
| 198 RenderViewHost* host = web_contents_->GetRenderViewHost(); | |
| 199 host->Send(new ViewMsg_ShowImeIfNeeded(host->GetRoutingID())); | |
| 200 } | |
| 201 | |
| 202 void WebContentsAndroid::ScrollFocusedEditableNodeIntoView( | |
| 203 JNIEnv* env, | |
| 204 jobject obj) { | |
| 205 RenderViewHost* host = web_contents_->GetRenderViewHost(); | |
| 206 host->Send(new InputMsg_ScrollFocusedEditableNodeIntoRect( | |
| 207 host->GetRoutingID(), gfx::Rect())); | |
| 208 } | |
| 209 | |
| 210 void WebContentsAndroid::SelectWordAroundCaret(JNIEnv* env, jobject obj) { | |
| 211 RenderViewHost* host = web_contents_->GetRenderViewHost(); | |
| 212 if (!host) | |
| 213 return; | |
| 214 host->SelectWordAroundCaret(); | |
| 215 } | |
| 216 | |
| 217 ScopedJavaLocalRef<jstring> | |
| 218 WebContentsAndroid::GetOriginalUrlForActiveNavigationEntry( | |
| 219 JNIEnv* env, | |
| 220 jobject obj) { | |
| 221 NavigationEntry* entry = web_contents_->GetController().GetVisibleEntry(); | |
|
Yaron
2014/07/15 21:23:48
Seems like this would be better in NavigationContr
AKVT
2014/07/16 09:53:47
Will take care in next patch when I handle Navigat
| |
| 222 if (entry == NULL) | |
| 223 return ScopedJavaLocalRef<jstring>(env, NULL); | |
| 224 return ConvertUTF8ToJavaString(env, entry->GetOriginalRequestURL().spec()); | |
| 225 } | |
| 226 | |
| 227 long WebContentsAndroid::GetNativeImeAdapter(JNIEnv* env, jobject obj) { | |
| 228 RenderWidgetHostViewAndroid* rwhva = GetRenderWidgetHostViewAndroid(); | |
| 229 if (!rwhva) | |
| 230 return 0; | |
| 231 return rwhva->GetNativeImeAdapter(); | |
| 232 } | |
| 233 | |
| 234 bool WebContentsAndroid::GetUseDesktopUserAgent( | |
| 235 JNIEnv* env, | |
| 236 jobject obj) { | |
| 237 NavigationEntry* entry = web_contents_->GetController().GetVisibleEntry(); | |
| 238 return entry && entry->GetIsOverridingUserAgent(); | |
| 239 } | |
| 240 | |
| 241 void WebContentsAndroid::ClearSslPreferences(JNIEnv* env, jobject obj) { | |
| 242 SSLHostState* state = SSLHostState::GetFor( | |
| 243 web_contents_->GetController().GetBrowserContext()); | |
| 244 state->Clear(); | |
| 245 } | |
| 246 | |
| 247 void WebContentsAndroid::SetUseDesktopUserAgent( | |
| 248 JNIEnv* env, | |
| 249 jobject obj, | |
| 250 jboolean enabled, | |
| 251 jboolean reload_on_state_change) { | |
| 252 if (GetUseDesktopUserAgent(env, obj) == enabled) | |
| 253 return; | |
| 254 | |
| 255 // Make sure the navigation entry actually exists. | |
| 256 NavigationEntry* entry = web_contents_->GetController().GetVisibleEntry(); | |
| 257 if (!entry) | |
| 258 return; | |
| 259 | |
| 260 // Set the flag in the NavigationEntry. | |
| 261 entry->SetIsOverridingUserAgent(enabled); | |
| 262 | |
| 263 // Send the override to the renderer. | |
| 264 if (reload_on_state_change) { | |
| 265 // Reloading the page will send the override down as part of the | |
| 266 // navigation IPC message. | |
| 267 NavigationControllerImpl& controller = | |
| 268 static_cast<NavigationControllerImpl&>(web_contents_->GetController()); | |
| 269 controller.ReloadOriginalRequestURL(false); | |
| 270 } | |
| 271 } | |
| 272 | |
| 273 void WebContentsAndroid::ExtractSmartClipData( | |
| 274 JNIEnv* env, | |
| 275 jobject obj, | |
| 276 jint x, | |
| 277 jint y, | |
| 278 jint width, | |
| 279 jint height) { | |
| 280 gfx::Rect rect( | |
| 281 static_cast<int>(x / dpi_scale()), | |
| 282 static_cast<int>(y / dpi_scale()), | |
| 283 static_cast<int>((width > 0 && width < dpi_scale()) ? | |
| 284 1 : (int)(width / dpi_scale())), | |
| 285 static_cast<int>((height > 0 && height < dpi_scale()) ? | |
| 286 1 : (int)(height / dpi_scale()))); | |
| 287 web_contents_->Send(new ViewMsg_ExtractSmartClipData( | |
| 288 web_contents_->GetRoutingID(), rect)); | |
| 289 } | |
| 290 | |
| 80 } // namespace content | 291 } // namespace content |
| OLD | NEW |