| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/android/content_view_client.h" | 5 #include "content/browser/android/content_view_client.h" |
| 6 | 6 |
| 7 #include <android/keycodes.h> | 7 #include <android/keycodes.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "content/browser/android/content_util.h" | |
| 12 #include "content/browser/android/content_view_core_impl.h" | 11 #include "content/browser/android/content_view_core_impl.h" |
| 13 #include "content/browser/android/download_controller.h" | 12 #include "content/browser/android/download_controller.h" |
| 14 #include "content/browser/android/ime_utils.h" | |
| 15 #include "content/browser/renderer_host/render_view_host_impl.h" | 13 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 16 #include "content/common/find_match_rect_android.h" | |
| 17 #include "content/public/browser/render_widget_host_view.h" | 14 #include "content/public/browser/render_widget_host_view.h" |
| 18 #include "content/public/browser/download_item.h" | 15 #include "content/public/browser/download_item.h" |
| 19 #include "content/public/browser/invalidate_type.h" | 16 #include "content/public/browser/invalidate_type.h" |
| 20 #include "content/public/browser/page_navigator.h" | 17 #include "content/public/browser/page_navigator.h" |
| 21 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
| 22 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
| 23 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/common/page_transition_types.h" | 21 #include "content/public/common/page_transition_types.h" |
| 25 #include "content/public/common/referrer.h" | 22 #include "content/public/common/referrer.h" |
| 26 #include "jni/ContentViewClient_jni.h" | 23 #include "jni/ContentViewClient_jni.h" |
| 27 #include "net/http/http_request_headers.h" | 24 #include "net/http/http_request_headers.h" |
| 28 #include "ui/gfx/rect.h" | |
| 29 #include "webkit/glue/window_open_disposition.h" | |
| 30 | 25 |
| 31 using base::android::AttachCurrentThread; | 26 using base::android::AttachCurrentThread; |
| 32 using base::android::CheckException; | 27 using base::android::CheckException; |
| 33 using base::android::ConvertUTF8ToJavaString; | 28 using base::android::ConvertUTF8ToJavaString; |
| 34 using base::android::ConvertUTF16ToJavaString; | 29 using base::android::ConvertUTF16ToJavaString; |
| 35 using base::android::GetClass; | 30 using base::android::GetClass; |
| 36 using base::android::GetMethodID; | 31 using base::android::GetMethodID; |
| 37 using base::android::HasClass; | 32 using base::android::HasClass; |
| 38 using base::android::ScopedJavaLocalRef; | 33 using base::android::ScopedJavaLocalRef; |
| 39 | 34 |
| 40 namespace content { | 35 namespace content { |
| 41 | 36 |
| 42 ContentViewClient::ContentViewClient(JNIEnv* env, jobject obj) | 37 ContentViewClient::ContentViewClient(JNIEnv* env, jobject obj) |
| 43 : weak_java_client_(env, obj), | 38 : weak_java_client_(env, obj) { |
| 44 find_helper_(NULL), | |
| 45 javascript_dialog_creator_(NULL), | |
| 46 load_progress_(0) { | |
| 47 } | 39 } |
| 48 | 40 |
| 49 ContentViewClient::~ContentViewClient() { | 41 ContentViewClient::~ContentViewClient() { |
| 50 } | 42 } |
| 51 | 43 |
| 52 // static | 44 // static |
| 53 ContentViewClient* ContentViewClient::CreateNativeContentViewClient( | 45 ContentViewClient* ContentViewClient::CreateNativeContentViewClient( |
| 54 JNIEnv* env, jobject obj) { | 46 JNIEnv* env, jobject obj) { |
| 55 DCHECK(obj); | 47 DCHECK(obj); |
| 56 return new ContentViewClient(env, obj); | 48 return new ContentViewClient(env, obj); |
| 57 } | 49 } |
| 58 | 50 |
| 59 void ContentViewClient::OnInternalPageLoadRequest( | 51 void ContentViewClient::OnInternalPageLoadRequest( |
| 60 WebContents* source, const GURL& url) { | 52 WebContents* source, const GURL& url) { |
| 61 last_requested_navigation_url_ = url; | |
| 62 } | 53 } |
| 63 | 54 |
| 64 | 55 |
| 65 void ContentViewClient::OnPageStarted(const GURL& url) { | 56 void ContentViewClient::OnPageStarted(const GURL& url) { |
| 66 JNIEnv* env = AttachCurrentThread(); | 57 JNIEnv* env = AttachCurrentThread(); |
| 58 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); |
| 59 if (obj.is_null()) |
| 60 return; |
| 67 ScopedJavaLocalRef<jstring> jstring_url = | 61 ScopedJavaLocalRef<jstring> jstring_url = |
| 68 ConvertUTF8ToJavaString(env, url.spec()); | 62 ConvertUTF8ToJavaString(env, url.spec()); |
| 69 Java_ContentViewClient_onPageStarted(env, weak_java_client_.get(env).obj(), | 63 Java_ContentViewClient_onPageStarted(env, obj.obj(), jstring_url.obj()); |
| 70 jstring_url.obj()); | |
| 71 } | 64 } |
| 72 | 65 |
| 73 void ContentViewClient::OnPageFinished(const GURL& url) { | 66 void ContentViewClient::OnPageFinished(const GURL& url) { |
| 74 if (url == last_requested_navigation_url_) | |
| 75 last_requested_navigation_url_ = GURL::EmptyGURL(); | |
| 76 | |
| 77 JNIEnv* env = AttachCurrentThread(); | 67 JNIEnv* env = AttachCurrentThread(); |
| 68 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); |
| 69 if (obj.is_null()) |
| 70 return; |
| 78 ScopedJavaLocalRef<jstring> jstring_url = | 71 ScopedJavaLocalRef<jstring> jstring_url = |
| 79 ConvertUTF8ToJavaString(env, url.spec()); | 72 ConvertUTF8ToJavaString(env, url.spec()); |
| 80 | 73 |
| 81 Java_ContentViewClient_onPageFinished(env, weak_java_client_.get(env).obj(), | 74 Java_ContentViewClient_onPageFinished(env, obj.obj(), jstring_url.obj()); |
| 82 jstring_url.obj()); | |
| 83 CheckException(env); | 75 CheckException(env); |
| 84 } | 76 } |
| 85 | 77 |
| 86 void ContentViewClient::OnReceivedError(int error_code, | 78 void ContentViewClient::OnReceivedError(int error_code, |
| 87 const string16& description, | 79 const string16& description, |
| 88 const GURL& url) { | 80 const GURL& url) { |
| 89 JNIEnv* env = AttachCurrentThread(); | 81 JNIEnv* env = AttachCurrentThread(); |
| 82 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); |
| 83 if (obj.is_null()) |
| 84 return; |
| 90 ScopedJavaLocalRef<jstring> jstring_error_description = | 85 ScopedJavaLocalRef<jstring> jstring_error_description = |
| 91 ConvertUTF8ToJavaString(env, url.spec()); | 86 ConvertUTF8ToJavaString(env, url.spec()); |
| 92 ScopedJavaLocalRef<jstring> jstring_url = | 87 ScopedJavaLocalRef<jstring> jstring_url = |
| 93 ConvertUTF8ToJavaString(env, url.spec()); | 88 ConvertUTF8ToJavaString(env, url.spec()); |
| 94 | 89 |
| 95 Java_ContentViewClient_onReceivedError( | 90 Java_ContentViewClient_onReceivedError( |
| 96 env, weak_java_client_.get(env).obj(), | 91 env, obj.obj(), |
| 97 ToContentViewClientError(error_code), | 92 ToContentViewClientError(error_code), |
| 98 jstring_error_description.obj(), jstring_url.obj()); | 93 jstring_error_description.obj(), jstring_url.obj()); |
| 99 } | 94 } |
| 100 | 95 |
| 101 void ContentViewClient::OnDidCommitMainFrame(const GURL& url, | 96 void ContentViewClient::OnDidCommitMainFrame(const GURL& url, |
| 102 const GURL& base_url) { | 97 const GURL& base_url) { |
| 103 JNIEnv* env = AttachCurrentThread(); | 98 JNIEnv* env = AttachCurrentThread(); |
| 99 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); |
| 100 if (obj.is_null()) |
| 101 return; |
| 104 ScopedJavaLocalRef<jstring> jstring_url = | 102 ScopedJavaLocalRef<jstring> jstring_url = |
| 105 ConvertUTF8ToJavaString(env, url.spec()); | 103 ConvertUTF8ToJavaString(env, url.spec()); |
| 106 ScopedJavaLocalRef<jstring> jstring_base_url = | 104 ScopedJavaLocalRef<jstring> jstring_base_url = |
| 107 ConvertUTF8ToJavaString(env, base_url.spec()); | 105 ConvertUTF8ToJavaString(env, base_url.spec()); |
| 108 | 106 |
| 109 Java_ContentViewClient_onMainFrameCommitted( | 107 Java_ContentViewClient_onMainFrameCommitted( |
| 110 env, weak_java_client_.get(env).obj(), | 108 env, obj.obj(), |
| 111 jstring_url.obj(), jstring_base_url.obj()); | 109 jstring_url.obj(), jstring_base_url.obj()); |
| 112 } | 110 } |
| 113 | 111 |
| 114 void ContentViewClient::OnInterstitialShown() { | 112 void ContentViewClient::OnInterstitialShown() { |
| 115 JNIEnv* env = AttachCurrentThread(); | 113 JNIEnv* env = AttachCurrentThread(); |
| 116 Java_ContentViewClient_onInterstitialShown( | 114 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); |
| 117 env, weak_java_client_.get(env).obj()); | 115 if (obj.is_null()) |
| 116 return; |
| 117 Java_ContentViewClient_onInterstitialShown(env, obj.obj()); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void ContentViewClient::OnInterstitialHidden() { | 120 void ContentViewClient::OnInterstitialHidden() { |
| 121 JNIEnv* env = AttachCurrentThread(); | 121 JNIEnv* env = AttachCurrentThread(); |
| 122 Java_ContentViewClient_onInterstitialHidden( | 122 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); |
| 123 env, weak_java_client_.get(env).obj()); | 123 if (obj.is_null()) |
| 124 } | 124 return; |
| 125 | 125 Java_ContentViewClient_onInterstitialHidden(env, obj.obj()); |
| 126 void ContentViewClient::SetFindHelper(FindHelper* find_helper) { | |
| 127 find_helper_ = find_helper; | |
| 128 } | |
| 129 | |
| 130 void ContentViewClient::SetJavaScriptDialogCreator( | |
| 131 JavaScriptDialogCreator* javascript_dialog_creator) { | |
| 132 javascript_dialog_creator_ = javascript_dialog_creator; | |
| 133 } | 126 } |
| 134 | 127 |
| 135 bool ContentViewClient::OnJSModalDialog(JavaScriptMessageType type, | 128 bool ContentViewClient::OnJSModalDialog(JavaScriptMessageType type, |
| 136 bool is_before_unload_dialog, | 129 bool is_before_unload_dialog, |
| 137 const GURL& url, | 130 const GURL& url, |
| 138 const string16& message, | 131 const string16& message, |
| 139 const string16& default_value) { | 132 const string16& default_value) { |
| 140 JNIEnv* env = AttachCurrentThread(); | 133 JNIEnv* env = AttachCurrentThread(); |
| 134 ScopedJavaLocalRef<jobject> obj = weak_java_client_.get(env); |
| 135 if (obj.is_null()) |
| 136 return false; |
| 141 ScopedJavaLocalRef<jstring> jurl(ConvertUTF8ToJavaString(env, url.spec())); | 137 ScopedJavaLocalRef<jstring> jurl(ConvertUTF8ToJavaString(env, url.spec())); |
| 142 ScopedJavaLocalRef<jstring> jmessage(ConvertUTF16ToJavaString(env, message)); | 138 ScopedJavaLocalRef<jstring> jmessage(ConvertUTF16ToJavaString(env, message)); |
| 143 | 139 |
| 144 // Special case for beforeunload dialogs, as that isn't encoded in the | 140 // Special case for beforeunload dialogs, as that isn't encoded in the |
| 145 // |type| of the dialog. | 141 // |type| of the dialog. |
| 146 if (is_before_unload_dialog) { | 142 if (is_before_unload_dialog) { |
| 147 return Java_ContentViewClient_onJsBeforeUnload(env, | 143 return Java_ContentViewClient_onJsBeforeUnload( |
| 148 weak_java_client_.get(env).obj(), | 144 env, obj.obj(), |
| 149 jurl.obj(), | 145 jurl.obj(), |
| 150 jmessage.obj()); | 146 jmessage.obj()); |
| 151 } | 147 } |
| 152 | 148 |
| 153 switch (type) { | 149 switch (type) { |
| 154 case JAVASCRIPT_MESSAGE_TYPE_ALERT: | 150 case JAVASCRIPT_MESSAGE_TYPE_ALERT: |
| 155 return Java_ContentViewClient_onJsAlert(env, | 151 return Java_ContentViewClient_onJsAlert(env, obj.obj(), |
| 156 weak_java_client_.get(env).obj(), | |
| 157 jurl.obj(), | 152 jurl.obj(), |
| 158 jmessage.obj()); | 153 jmessage.obj()); |
| 159 | 154 |
| 160 case JAVASCRIPT_MESSAGE_TYPE_CONFIRM: | 155 case JAVASCRIPT_MESSAGE_TYPE_CONFIRM: |
| 161 return Java_ContentViewClient_onJsConfirm(env, | 156 return Java_ContentViewClient_onJsConfirm(env, obj.obj(), |
| 162 weak_java_client_.get(env).obj(), | 157 jurl.obj(), |
| 163 jurl.obj(), | 158 jmessage.obj()); |
| 164 jmessage.obj()); | |
| 165 | 159 |
| 166 case JAVASCRIPT_MESSAGE_TYPE_PROMPT: { | 160 case JAVASCRIPT_MESSAGE_TYPE_PROMPT: { |
| 167 ScopedJavaLocalRef<jstring> jdefault_value( | 161 ScopedJavaLocalRef<jstring> jdefault_value( |
| 168 ConvertUTF16ToJavaString(env, default_value)); | 162 ConvertUTF16ToJavaString(env, default_value)); |
| 169 return Java_ContentViewClient_onJsPrompt(env, | 163 return Java_ContentViewClient_onJsPrompt(env, obj.obj(), |
| 170 weak_java_client_.get(env).obj(), | |
| 171 jurl.obj(), | 164 jurl.obj(), |
| 172 jmessage.obj(), | 165 jmessage.obj(), |
| 173 jdefault_value.obj()); | 166 jdefault_value.obj()); |
| 174 } | 167 } |
| 175 | 168 |
| 176 default: | 169 default: |
| 177 NOTREACHED(); | 170 NOTREACHED(); |
| 178 return false; | 171 return false; |
| 179 } | 172 } |
| 180 } | 173 } |
| 181 | 174 |
| 182 // ---------------------------------------------------------------------------- | |
| 183 // WebContentsDelegate methods | |
| 184 // ---------------------------------------------------------------------------- | |
| 185 | |
| 186 // OpenURLFromTab() will be called when we're performing a browser-intiated | |
| 187 // navigation. The most common scenario for this is opening new tabs (see | |
| 188 // RenderViewImpl::decidePolicyForNavigation for more details). | |
| 189 WebContents* ContentViewClient::OpenURLFromTab( | |
| 190 WebContents* source, | |
| 191 const OpenURLParams& params) { | |
| 192 const GURL& url = params.url; | |
| 193 WindowOpenDisposition disposition = params.disposition; | |
| 194 PageTransition transition( | |
| 195 PageTransitionFromInt(params.transition)); | |
| 196 | |
| 197 if (!source || (disposition != CURRENT_TAB && | |
| 198 disposition != NEW_FOREGROUND_TAB && | |
| 199 disposition != NEW_BACKGROUND_TAB && | |
| 200 disposition != OFF_THE_RECORD)) { | |
| 201 NOTIMPLEMENTED(); | |
| 202 return NULL; | |
| 203 } | |
| 204 | |
| 205 if (disposition == NEW_FOREGROUND_TAB || | |
| 206 disposition == NEW_BACKGROUND_TAB || | |
| 207 disposition == OFF_THE_RECORD) { | |
| 208 JNIEnv* env = AttachCurrentThread(); | |
| 209 ScopedJavaLocalRef<jstring> java_url = | |
| 210 ConvertUTF8ToJavaString(env, url.spec()); | |
| 211 Java_ContentViewClient_openNewTab(env, | |
| 212 weak_java_client_.get(env).obj(), | |
| 213 java_url.obj(), | |
| 214 disposition == OFF_THE_RECORD); | |
| 215 return NULL; | |
| 216 } | |
| 217 | |
| 218 // TODO(mkosiba): This should be in platform_utils OpenExternal, b/6174564. | |
| 219 if (transition == PAGE_TRANSITION_LINK && ShouldOverrideLoading(url)) | |
| 220 return NULL; | |
| 221 | |
| 222 source->GetController().LoadURL(url, params.referrer, transition, | |
| 223 std::string()); | |
| 224 return source; | |
| 225 } | |
| 226 | |
| 227 // ShouldIgnoreNavigation will be called for every non-local top level | |
| 228 // navigation made by the renderer. If true is returned the renderer will | |
| 229 // not perform the navigation. This is done by using synchronous IPC so we | |
| 230 // should avoid blocking calls from this method. | |
| 231 bool ContentViewClient::ShouldIgnoreNavigation( | |
| 232 WebContents* source, | |
| 233 const GURL& url, | |
| 234 const Referrer& referrer, | |
| 235 WindowOpenDisposition disposition, | |
| 236 PageTransition transition_type) { | |
| 237 | |
| 238 // Don't override new tabs. | |
| 239 if (disposition == NEW_FOREGROUND_TAB || | |
| 240 disposition == NEW_BACKGROUND_TAB || | |
| 241 disposition == OFF_THE_RECORD) | |
| 242 return false; | |
| 243 | |
| 244 // Don't override the navigation that has just been requested via the | |
| 245 // ContentView.loadUrl method. | |
| 246 if (url == last_requested_navigation_url_) { | |
| 247 last_requested_navigation_url_ = GURL::EmptyGURL(); | |
| 248 return false; | |
| 249 } | |
| 250 | |
| 251 return ShouldOverrideLoading(url); | |
| 252 } | |
| 253 | |
| 254 void ContentViewClient::NavigationStateChanged( | |
| 255 const WebContents* source, unsigned changed_flags) { | |
| 256 if (changed_flags & ( | |
| 257 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_TITLE)) { | |
| 258 JNIEnv* env = AttachCurrentThread(); | |
| 259 Java_ContentViewClient_onTabHeaderStateChanged( | |
| 260 env, weak_java_client_.get(env).obj()); | |
| 261 } | |
| 262 } | |
| 263 | |
| 264 void ContentViewClient::AddNewContents(WebContents* source, | |
| 265 WebContents* new_contents, | |
| 266 WindowOpenDisposition disposition, | |
| 267 const gfx::Rect& initial_pos, | |
| 268 bool user_gesture) { | |
| 269 JNIEnv* env = AttachCurrentThread(); | |
| 270 bool handled = Java_ContentViewClient_addNewContents( | |
| 271 env, | |
| 272 weak_java_client_.get(env).obj(), | |
| 273 reinterpret_cast<jint>(source), | |
| 274 reinterpret_cast<jint>(new_contents), | |
| 275 static_cast<jint>(disposition), | |
| 276 NULL, | |
| 277 user_gesture); | |
| 278 if (!handled) | |
| 279 delete new_contents; | |
| 280 } | |
| 281 | |
| 282 void ContentViewClient::ActivateContents(WebContents* contents) { | |
| 283 // TODO(dtrainor) When doing the merge I came across this. Should we be | |
| 284 // activating this tab here? | |
| 285 } | |
| 286 | |
| 287 void ContentViewClient::DeactivateContents(WebContents* contents) { | |
| 288 // Do nothing. | |
| 289 } | |
| 290 | |
| 291 void ContentViewClient::LoadingStateChanged(WebContents* source) { | |
| 292 JNIEnv* env = AttachCurrentThread(); | |
| 293 bool has_stopped = source == NULL || !source->IsLoading(); | |
| 294 | |
| 295 if (has_stopped) | |
| 296 Java_ContentViewClient_onLoadStopped( | |
| 297 env, weak_java_client_.get(env).obj()); | |
| 298 else | |
| 299 Java_ContentViewClient_onLoadStarted( | |
| 300 env, weak_java_client_.get(env).obj()); | |
| 301 } | |
| 302 | |
| 303 void ContentViewClient::LoadProgressChanged(double progress) { | |
| 304 load_progress_ = progress; | |
| 305 JNIEnv* env = AttachCurrentThread(); | |
| 306 Java_ContentViewClient_onLoadProgressChanged( | |
| 307 env, | |
| 308 weak_java_client_.get(env).obj(), | |
| 309 progress); | |
| 310 } | |
| 311 | |
| 312 double ContentViewClient::GetLoadProgress() const { | |
| 313 return load_progress_; | |
| 314 } | |
| 315 | |
| 316 void ContentViewClient::CloseContents(WebContents* source) { | |
| 317 JNIEnv* env = AttachCurrentThread(); | |
| 318 Java_ContentViewClient_closeContents(env, weak_java_client_.get(env).obj()); | |
| 319 } | |
| 320 | |
| 321 void ContentViewClient::MoveContents(WebContents* source, | |
| 322 const gfx::Rect& pos) { | |
| 323 // Do nothing. | |
| 324 } | |
| 325 | |
| 326 // TODO(merge): WARNING! method no longer available on the base class. | |
| 327 // See http://b/issue?id=5862108 | |
| 328 void ContentViewClient::URLStarredChanged(WebContents* source, bool starred) { | |
| 329 JNIEnv* env = AttachCurrentThread(); | |
| 330 Java_ContentViewClient_onUrlStarredChanged(env, | |
| 331 weak_java_client_.get(env).obj(), | |
| 332 starred); | |
| 333 } | |
| 334 | |
| 335 // This is either called from TabContents::DidNavigateMainFramePostCommit() with | |
| 336 // an empty GURL or responding to RenderViewHost::OnMsgUpateTargetURL(). In | |
| 337 // Chrome, the latter is not always called, especially not during history | |
| 338 // navigation. So we only handle the first case and pass the source TabContents' | |
| 339 // url to Java to update the UI. | |
| 340 void ContentViewClient::UpdateTargetURL(WebContents* source, | |
| 341 int32 page_id, | |
| 342 const GURL& url) { | |
| 343 if (url.is_empty()) { | |
| 344 JNIEnv* env = AttachCurrentThread(); | |
| 345 ScopedJavaLocalRef<jstring> java_url = | |
| 346 ConvertUTF8ToJavaString(env, source->GetURL().spec()); | |
| 347 Java_ContentViewClient_onUpdateUrl(env, | |
| 348 weak_java_client_.get(env).obj(), | |
| 349 java_url.obj()); | |
| 350 } | |
| 351 } | |
| 352 | |
| 353 bool ContentViewClient::CanDownload(RenderViewHost* source, | |
| 354 int request_id, | |
| 355 const std::string& request_method) { | |
| 356 if (request_method == net::HttpRequestHeaders::kGetMethod) { | |
| 357 DownloadController::GetInstance()->CreateGETDownload( | |
| 358 source, request_id); | |
| 359 return false; | |
| 360 } | |
| 361 return true; | |
| 362 } | |
| 363 | |
| 364 void ContentViewClient::OnStartDownload(WebContents* source, | |
| 365 DownloadItem* download) { | |
| 366 DownloadController::GetInstance()->OnPostDownloadStarted( | |
| 367 source, download); | |
| 368 } | |
| 369 | |
| 370 void ContentViewClient::FindReply(WebContents* web_contents, | |
| 371 int request_id, | |
| 372 int number_of_matches, | |
| 373 const gfx::Rect& selection_rect, | |
| 374 int active_match_ordinal, | |
| 375 bool final_update) { | |
| 376 /* TODO(jrg): upstream this; requires | |
| 377 content/browser/android/find_helper.h to be upstreamed */ | |
| 378 } | |
| 379 | |
| 380 void ContentViewClient::OnReceiveFindMatchRects( | |
| 381 int version, const std::vector<FindMatchRect>& rects, | |
| 382 const FindMatchRect& active_rect) { | |
| 383 JNIEnv* env = AttachCurrentThread(); | |
| 384 | |
| 385 // Constructs an float[] of (left, top, right, bottom) tuples and passes it on | |
| 386 // to the Java onReceiveFindMatchRects handler which will use it to create | |
| 387 // RectF objects equivalent to the std::vector<FindMatchRect>. | |
| 388 ScopedJavaLocalRef<jfloatArray> rect_data(env, | |
| 389 env->NewFloatArray(rects.size() * 4)); | |
| 390 jfloat* rect_data_floats = env->GetFloatArrayElements(rect_data.obj(), NULL); | |
| 391 for (size_t i = 0; i < rects.size(); ++i) { | |
| 392 rect_data_floats[4 * i] = rects[i].left; | |
| 393 rect_data_floats[4 * i + 1] = rects[i].top; | |
| 394 rect_data_floats[4 * i + 2] = rects[i].right; | |
| 395 rect_data_floats[4 * i + 3] = rects[i].bottom; | |
| 396 } | |
| 397 env->ReleaseFloatArrayElements(rect_data.obj(), rect_data_floats, 0); | |
| 398 | |
| 399 ScopedJavaLocalRef<jobject> active_rect_object; | |
| 400 if (active_rect.left < active_rect.right && | |
| 401 active_rect.top < active_rect.bottom) { | |
| 402 ScopedJavaLocalRef<jclass> rect_clazz = | |
| 403 GetClass(env, "android/graphics/RectF"); | |
| 404 jmethodID rect_constructor = | |
| 405 GetMethodID(env, rect_clazz, "<init>", "(FFFF)V"); | |
| 406 active_rect_object.Reset(env, env->NewObject(rect_clazz.obj(), | |
| 407 rect_constructor, | |
| 408 active_rect.left, | |
| 409 active_rect.top, | |
| 410 active_rect.right, | |
| 411 active_rect.bottom)); | |
| 412 DCHECK(!active_rect_object.is_null()); | |
| 413 } | |
| 414 | |
| 415 | |
| 416 Java_ContentViewClient_onReceiveFindMatchRects( | |
| 417 env, | |
| 418 weak_java_client_.get(env).obj(), | |
| 419 version, rect_data.obj(), | |
| 420 active_rect_object.obj()); | |
| 421 } | |
| 422 | |
| 423 bool ContentViewClient::ShouldOverrideLoading(const GURL& url) { | |
| 424 if (!url.is_valid()) | |
| 425 return false; | |
| 426 | |
| 427 JNIEnv* env = AttachCurrentThread(); | |
| 428 ScopedJavaLocalRef<jstring> jstring_url = | |
| 429 ConvertUTF8ToJavaString(env, url.spec()); | |
| 430 bool ret = Java_ContentViewClient_shouldOverrideUrlLoading( | |
| 431 env, weak_java_client_.get(env).obj(), jstring_url.obj()); | |
| 432 return ret; | |
| 433 } | |
| 434 | |
| 435 void ContentViewClient::HandleKeyboardEvent( | |
| 436 const NativeWebKeyboardEvent& event) { | |
| 437 jobject key_event = KeyEventFromNative(event); | |
| 438 if (key_event) { | |
| 439 JNIEnv* env = AttachCurrentThread(); | |
| 440 Java_ContentViewClient_handleKeyboardEvent( | |
| 441 env, | |
| 442 weak_java_client_.get(env).obj(), | |
| 443 key_event); | |
| 444 } | |
| 445 } | |
| 446 | |
| 447 bool ContentViewClient::TakeFocus(bool reverse) { | |
| 448 JNIEnv* env = AttachCurrentThread(); | |
| 449 return Java_ContentViewClient_takeFocus(env, | |
| 450 weak_java_client_.get(env).obj(), | |
| 451 reverse); | |
| 452 } | |
| 453 | |
| 454 ContentViewClientError ContentViewClient::ToContentViewClientError( | 175 ContentViewClientError ContentViewClient::ToContentViewClientError( |
| 455 int net_error) { | 176 int net_error) { |
| 456 // Note: many net::Error constants don't have an obvious mapping. | 177 // Note: many net::Error constants don't have an obvious mapping. |
| 457 // These will be handled by the default case, ERROR_UNKNOWN. | 178 // These will be handled by the default case, ERROR_UNKNOWN. |
| 458 switch(net_error) { | 179 switch(net_error) { |
| 459 case net::ERR_UNSUPPORTED_AUTH_SCHEME: | 180 case net::ERR_UNSUPPORTED_AUTH_SCHEME: |
| 460 return CONTENT_VIEW_CLIENT_ERROR_UNSUPPORTED_AUTH_SCHEME; | 181 return CONTENT_VIEW_CLIENT_ERROR_UNSUPPORTED_AUTH_SCHEME; |
| 461 | 182 |
| 462 case net::ERR_INVALID_AUTH_CREDENTIALS: | 183 case net::ERR_INVALID_AUTH_CREDENTIALS: |
| 463 case net::ERR_MISSING_AUTH_CREDENTIALS: | 184 case net::ERR_MISSING_AUTH_CREDENTIALS: |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 return CONTENT_VIEW_CLIENT_ERROR_OK; | 268 return CONTENT_VIEW_CLIENT_ERROR_OK; |
| 548 | 269 |
| 549 default: | 270 default: |
| 550 VLOG(1) << "ContentViewClient::ToContentViewClientError: Unknown " | 271 VLOG(1) << "ContentViewClient::ToContentViewClientError: Unknown " |
| 551 << "chromium error: " | 272 << "chromium error: " |
| 552 << net_error; | 273 << net_error; |
| 553 return CONTENT_VIEW_CLIENT_ERROR_UNKNOWN; | 274 return CONTENT_VIEW_CLIENT_ERROR_UNKNOWN; |
| 554 } | 275 } |
| 555 } | 276 } |
| 556 | 277 |
| 557 JavaScriptDialogCreator* ContentViewClient::GetJavaScriptDialogCreator() { | |
| 558 return javascript_dialog_creator_; | |
| 559 } | |
| 560 | |
| 561 void ContentViewClient::RunFileChooser( | |
| 562 WebContents* tab, const FileChooserParams& params) { | |
| 563 JNIEnv* env = AttachCurrentThread(); | |
| 564 ScopedJavaLocalRef<jobject> jparams = ToJavaFileChooserParams(env, params); | |
| 565 Java_ContentViewClient_runFileChooser(env, weak_java_client_.get(env).obj(), | |
| 566 jparams.obj()); | |
| 567 } | |
| 568 | |
| 569 // ---------------------------------------------------------------------------- | 278 // ---------------------------------------------------------------------------- |
| 570 // Native JNI methods | 279 // Native JNI methods |
| 571 // ---------------------------------------------------------------------------- | 280 // ---------------------------------------------------------------------------- |
| 572 | 281 |
| 573 // Register native methods | 282 // Register native methods |
| 574 | 283 |
| 575 bool RegisterContentViewClient(JNIEnv* env) { | 284 bool RegisterContentViewClient(JNIEnv* env) { |
| 576 if (!HasClass(env, kContentViewClientClassPath)) { | 285 if (!HasClass(env, kContentViewClientClassPath)) { |
| 577 DLOG(ERROR) << "Unable to find class ContentViewClient!"; | 286 DLOG(ERROR) << "Unable to find class ContentViewClient!"; |
| 578 return false; | 287 return false; |
| 579 } | 288 } |
| 580 return RegisterNativesImpl(env); | 289 return RegisterNativesImpl(env); |
| 581 } | 290 } |
| 582 | 291 |
| 583 } // namespace content | 292 } // namespace content |
| OLD | NEW |