| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "android_webview/native/aw_web_contents_delegate.h" | |
| 6 | |
| 7 #include "android_webview/browser/aw_javascript_dialog_manager.h" | |
| 8 #include "android_webview/browser/find_helper.h" | |
| 9 #include "android_webview/native/aw_contents.h" | |
| 10 #include "android_webview/native/aw_contents_io_thread_client_impl.h" | |
| 11 #include "android_webview/native/permission/media_access_permission_request.h" | |
| 12 #include "android_webview/native/permission/permission_request_handler.h" | |
| 13 #include "base/android/jni_array.h" | |
| 14 #include "base/android/jni_string.h" | |
| 15 #include "base/android/scoped_java_ref.h" | |
| 16 #include "base/lazy_instance.h" | |
| 17 #include "base/location.h" | |
| 18 #include "base/memory/ptr_util.h" | |
| 19 #include "base/single_thread_task_runner.h" | |
| 20 #include "base/strings/string_util.h" | |
| 21 #include "base/strings/utf_string_conversions.h" | |
| 22 #include "base/threading/thread_task_runner_handle.h" | |
| 23 #include "content/public/browser/render_frame_host.h" | |
| 24 #include "content/public/browser/render_process_host.h" | |
| 25 #include "content/public/browser/render_view_host.h" | |
| 26 #include "content/public/browser/render_widget_host.h" | |
| 27 #include "content/public/browser/web_contents.h" | |
| 28 #include "content/public/common/file_chooser_file_info.h" | |
| 29 #include "content/public/common/file_chooser_params.h" | |
| 30 #include "content/public/common/media_stream_request.h" | |
| 31 #include "jni/AwWebContentsDelegate_jni.h" | |
| 32 #include "net/base/escape.h" | |
| 33 | |
| 34 using base::android::AttachCurrentThread; | |
| 35 using base::android::ConvertUTF16ToJavaString; | |
| 36 using base::android::ConvertUTF8ToJavaString; | |
| 37 using base::android::JavaParamRef; | |
| 38 using base::android::ScopedJavaLocalRef; | |
| 39 using content::FileChooserParams; | |
| 40 using content::WebContents; | |
| 41 | |
| 42 namespace android_webview { | |
| 43 | |
| 44 namespace { | |
| 45 | |
| 46 // WARNING: these constants are exposed in the public interface Java side, so | |
| 47 // must remain in sync with what clients are expecting. | |
| 48 const int kFileChooserModeOpenMultiple = 1 << 0; | |
| 49 const int kFileChooserModeOpenFolder = 1 << 1; | |
| 50 | |
| 51 base::LazyInstance<AwJavaScriptDialogManager>::Leaky | |
| 52 g_javascript_dialog_manager = LAZY_INSTANCE_INITIALIZER; | |
| 53 } | |
| 54 | |
| 55 AwWebContentsDelegate::AwWebContentsDelegate( | |
| 56 JNIEnv* env, | |
| 57 jobject obj) | |
| 58 : WebContentsDelegateAndroid(env, obj), | |
| 59 is_fullscreen_(false) { | |
| 60 } | |
| 61 | |
| 62 AwWebContentsDelegate::~AwWebContentsDelegate() { | |
| 63 } | |
| 64 | |
| 65 content::JavaScriptDialogManager* | |
| 66 AwWebContentsDelegate::GetJavaScriptDialogManager(WebContents* source) { | |
| 67 return g_javascript_dialog_manager.Pointer(); | |
| 68 } | |
| 69 | |
| 70 void AwWebContentsDelegate::FindReply(WebContents* web_contents, | |
| 71 int request_id, | |
| 72 int number_of_matches, | |
| 73 const gfx::Rect& selection_rect, | |
| 74 int active_match_ordinal, | |
| 75 bool final_update) { | |
| 76 AwContents* aw_contents = AwContents::FromWebContents(web_contents); | |
| 77 if (!aw_contents) | |
| 78 return; | |
| 79 | |
| 80 aw_contents->GetFindHelper()->HandleFindReply(request_id, | |
| 81 number_of_matches, | |
| 82 active_match_ordinal, | |
| 83 final_update); | |
| 84 } | |
| 85 | |
| 86 void AwWebContentsDelegate::CanDownload( | |
| 87 const GURL& url, | |
| 88 const std::string& request_method, | |
| 89 const base::Callback<void(bool)>& callback) { | |
| 90 // Android webview intercepts download in its resource dispatcher host | |
| 91 // delegate, so should not reach here. | |
| 92 NOTREACHED(); | |
| 93 callback.Run(false); | |
| 94 } | |
| 95 | |
| 96 void AwWebContentsDelegate::RunFileChooser( | |
| 97 content::RenderFrameHost* render_frame_host, | |
| 98 const FileChooserParams& params) { | |
| 99 JNIEnv* env = AttachCurrentThread(); | |
| 100 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); | |
| 101 if (!java_delegate.obj()) | |
| 102 return; | |
| 103 | |
| 104 int mode_flags = 0; | |
| 105 if (params.mode == FileChooserParams::OpenMultiple) { | |
| 106 mode_flags |= kFileChooserModeOpenMultiple; | |
| 107 } else if (params.mode == FileChooserParams::UploadFolder) { | |
| 108 // Folder implies multiple in Chrome. | |
| 109 mode_flags |= kFileChooserModeOpenMultiple | kFileChooserModeOpenFolder; | |
| 110 } else if (params.mode == FileChooserParams::Save) { | |
| 111 // Save not supported, so cancel it. | |
| 112 render_frame_host->FilesSelectedInChooser( | |
| 113 std::vector<content::FileChooserFileInfo>(), params.mode); | |
| 114 return; | |
| 115 } else { | |
| 116 DCHECK_EQ(FileChooserParams::Open, params.mode); | |
| 117 } | |
| 118 Java_AwWebContentsDelegate_runFileChooser( | |
| 119 env, java_delegate, render_frame_host->GetProcess()->GetID(), | |
| 120 render_frame_host->GetRoutingID(), mode_flags, | |
| 121 ConvertUTF16ToJavaString( | |
| 122 env, base::JoinString(params.accept_types, base::ASCIIToUTF16(","))), | |
| 123 params.title.empty() ? nullptr | |
| 124 : ConvertUTF16ToJavaString(env, params.title), | |
| 125 params.default_file_name.empty() | |
| 126 ? nullptr | |
| 127 : ConvertUTF8ToJavaString(env, params.default_file_name.value()), | |
| 128 params.capture); | |
| 129 } | |
| 130 | |
| 131 void AwWebContentsDelegate::AddNewContents(WebContents* source, | |
| 132 WebContents* new_contents, | |
| 133 WindowOpenDisposition disposition, | |
| 134 const gfx::Rect& initial_rect, | |
| 135 bool user_gesture, | |
| 136 bool* was_blocked) { | |
| 137 JNIEnv* env = AttachCurrentThread(); | |
| 138 | |
| 139 bool is_dialog = disposition == WindowOpenDisposition::NEW_POPUP; | |
| 140 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); | |
| 141 bool create_popup = false; | |
| 142 | |
| 143 if (java_delegate.obj()) { | |
| 144 create_popup = Java_AwWebContentsDelegate_addNewContents( | |
| 145 env, java_delegate, is_dialog, user_gesture); | |
| 146 } | |
| 147 | |
| 148 if (create_popup) { | |
| 149 // The embedder would like to display the popup and we will receive | |
| 150 // a callback from them later with an AwContents to use to display | |
| 151 // it. The source AwContents takes ownership of the new WebContents | |
| 152 // until then, and when the callback is made we will swap the WebContents | |
| 153 // out into the new AwContents. | |
| 154 AwContents::FromWebContents(source)->SetPendingWebContentsForPopup( | |
| 155 base::WrapUnique(new_contents)); | |
| 156 // Hide the WebContents for the pop up now, we will show it again | |
| 157 // when the user calls us back with an AwContents to use to show it. | |
| 158 new_contents->WasHidden(); | |
| 159 } else { | |
| 160 // The embedder has forgone their chance to display this popup | |
| 161 // window, so we're done with the WebContents now. We use | |
| 162 // DeleteSoon as WebContentsImpl may call methods on |new_contents| | |
| 163 // after this method returns. | |
| 164 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, new_contents); | |
| 165 } | |
| 166 | |
| 167 if (was_blocked) { | |
| 168 *was_blocked = !create_popup; | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 void AwWebContentsDelegate::NavigationStateChanged( | |
| 173 content::WebContents* source, | |
| 174 content::InvalidateTypes changed_flags) { | |
| 175 JNIEnv* env = AttachCurrentThread(); | |
| 176 | |
| 177 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); | |
| 178 if (java_delegate.obj()) { | |
| 179 Java_AwWebContentsDelegate_navigationStateChanged(env, java_delegate, | |
| 180 changed_flags); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 // Notifies the delegate about the creation of a new WebContents. This | |
| 185 // typically happens when popups are created. | |
| 186 void AwWebContentsDelegate::WebContentsCreated( | |
| 187 WebContents* source_contents, | |
| 188 int opener_render_process_id, | |
| 189 int opener_render_frame_id, | |
| 190 const std::string& frame_name, | |
| 191 const GURL& target_url, | |
| 192 content::WebContents* new_contents) { | |
| 193 AwContentsIoThreadClientImpl::RegisterPendingContents(new_contents); | |
| 194 } | |
| 195 | |
| 196 void AwWebContentsDelegate::CloseContents(WebContents* source) { | |
| 197 JNIEnv* env = AttachCurrentThread(); | |
| 198 | |
| 199 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); | |
| 200 if (java_delegate.obj()) { | |
| 201 Java_AwWebContentsDelegate_closeContents(env, java_delegate); | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 void AwWebContentsDelegate::ActivateContents(WebContents* contents) { | |
| 206 JNIEnv* env = AttachCurrentThread(); | |
| 207 | |
| 208 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); | |
| 209 if (java_delegate.obj()) { | |
| 210 Java_AwWebContentsDelegate_activateContents(env, java_delegate); | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 void AwWebContentsDelegate::LoadingStateChanged(WebContents* source, | |
| 215 bool to_different_document) { | |
| 216 // Page title may have changed, need to inform the embedder. | |
| 217 // |source| may be null if loading has started. | |
| 218 JNIEnv* env = AttachCurrentThread(); | |
| 219 | |
| 220 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); | |
| 221 if (java_delegate.obj()) { | |
| 222 Java_AwWebContentsDelegate_loadingStateChanged(env, java_delegate); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 bool AwWebContentsDelegate::ShouldResumeRequestsForCreatedWindow() { | |
| 227 // Always return false here since we need to defer loading the created window | |
| 228 // until after we have attached a new delegate to the new webcontents (which | |
| 229 // happens asynchronously). | |
| 230 return false; | |
| 231 } | |
| 232 | |
| 233 void AwWebContentsDelegate::RequestMediaAccessPermission( | |
| 234 WebContents* web_contents, | |
| 235 const content::MediaStreamRequest& request, | |
| 236 const content::MediaResponseCallback& callback) { | |
| 237 AwContents* aw_contents = AwContents::FromWebContents(web_contents); | |
| 238 if (!aw_contents) { | |
| 239 callback.Run(content::MediaStreamDevices(), | |
| 240 content::MEDIA_DEVICE_FAILED_DUE_TO_SHUTDOWN, | |
| 241 std::unique_ptr<content::MediaStreamUI>()); | |
| 242 return; | |
| 243 } | |
| 244 aw_contents->GetPermissionRequestHandler()->SendRequest( | |
| 245 std::unique_ptr<AwPermissionRequestDelegate>( | |
| 246 new MediaAccessPermissionRequest(request, callback))); | |
| 247 } | |
| 248 | |
| 249 void AwWebContentsDelegate::EnterFullscreenModeForTab( | |
| 250 content::WebContents* web_contents, const GURL& origin) { | |
| 251 WebContentsDelegateAndroid::EnterFullscreenModeForTab(web_contents, origin); | |
| 252 is_fullscreen_ = true; | |
| 253 web_contents->GetRenderViewHost()->GetWidget()->WasResized(); | |
| 254 } | |
| 255 | |
| 256 void AwWebContentsDelegate::ExitFullscreenModeForTab( | |
| 257 content::WebContents* web_contents) { | |
| 258 WebContentsDelegateAndroid::ExitFullscreenModeForTab(web_contents); | |
| 259 is_fullscreen_ = false; | |
| 260 web_contents->GetRenderViewHost()->GetWidget()->WasResized(); | |
| 261 } | |
| 262 | |
| 263 bool AwWebContentsDelegate::IsFullscreenForTabOrPending( | |
| 264 const content::WebContents* web_contents) const { | |
| 265 return is_fullscreen_; | |
| 266 } | |
| 267 | |
| 268 static void FilesSelectedInChooser( | |
| 269 JNIEnv* env, | |
| 270 const JavaParamRef<jclass>& clazz, | |
| 271 jint process_id, | |
| 272 jint render_id, | |
| 273 jint mode_flags, | |
| 274 const JavaParamRef<jobjectArray>& file_paths, | |
| 275 const JavaParamRef<jobjectArray>& display_names) { | |
| 276 content::RenderFrameHost* rfh = | |
| 277 content::RenderFrameHost::FromID(process_id, render_id); | |
| 278 if (!rfh) | |
| 279 return; | |
| 280 | |
| 281 std::vector<std::string> file_path_str; | |
| 282 std::vector<std::string> display_name_str; | |
| 283 // Note file_paths maybe NULL, but this will just yield a zero-length vector. | |
| 284 base::android::AppendJavaStringArrayToStringVector(env, file_paths, | |
| 285 &file_path_str); | |
| 286 base::android::AppendJavaStringArrayToStringVector(env, display_names, | |
| 287 &display_name_str); | |
| 288 std::vector<content::FileChooserFileInfo> files; | |
| 289 files.reserve(file_path_str.size()); | |
| 290 for (size_t i = 0; i < file_path_str.size(); ++i) { | |
| 291 GURL url(file_path_str[i]); | |
| 292 if (!url.is_valid()) | |
| 293 continue; | |
| 294 base::FilePath path(url.SchemeIsFile() ? | |
| 295 net::UnescapeURLComponent(url.path(), | |
| 296 net::UnescapeRule::SPACES | | |
| 297 net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS) : | |
| 298 file_path_str[i]); | |
| 299 content::FileChooserFileInfo file_info; | |
| 300 file_info.file_path = path; | |
| 301 if (!display_name_str[i].empty()) | |
| 302 file_info.display_name = display_name_str[i]; | |
| 303 files.push_back(file_info); | |
| 304 } | |
| 305 FileChooserParams::Mode mode; | |
| 306 if (mode_flags & kFileChooserModeOpenFolder) { | |
| 307 mode = FileChooserParams::UploadFolder; | |
| 308 } else if (mode_flags & kFileChooserModeOpenMultiple) { | |
| 309 mode = FileChooserParams::OpenMultiple; | |
| 310 } else { | |
| 311 mode = FileChooserParams::Open; | |
| 312 } | |
| 313 DVLOG(0) << "File Chooser result: mode = " << mode | |
| 314 << ", file paths = " << base::JoinString(file_path_str, ":"); | |
| 315 rfh->FilesSelectedInChooser(files, mode); | |
| 316 } | |
| 317 | |
| 318 bool RegisterAwWebContentsDelegate(JNIEnv* env) { | |
| 319 return RegisterNativesImpl(env); | |
| 320 } | |
| 321 | |
| 322 } // namespace android_webview | |
| OLD | NEW |