[Android] Add partial output to TimeoutError raised in cmd_helper.
[chromium-blink-merge.git] / android_webview / native / aw_web_contents_delegate.cc
blob1d22633c80a58b4502084ca41ece50e77d5cc780
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.
5 #include "android_webview/native/aw_web_contents_delegate.h"
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/message_loop/message_loop.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/file_chooser_file_info.h"
24 #include "content/public/common/file_chooser_params.h"
25 #include "content/public/common/media_stream_request.h"
26 #include "jni/AwWebContentsDelegate_jni.h"
27 #include "net/base/escape.h"
29 using base::android::AttachCurrentThread;
30 using base::android::ConvertUTF16ToJavaString;
31 using base::android::ConvertUTF8ToJavaString;
32 using base::android::ScopedJavaLocalRef;
33 using content::FileChooserParams;
34 using content::WebContents;
36 namespace android_webview {
38 namespace {
40 // WARNING: these constants are exposed in the public interface Java side, so
41 // must remain in sync with what clients are expecting.
42 const int kFileChooserModeOpenMultiple = 1 << 0;
43 const int kFileChooserModeOpenFolder = 1 << 1;
45 base::LazyInstance<AwJavaScriptDialogManager>::Leaky
46 g_javascript_dialog_manager = LAZY_INSTANCE_INITIALIZER;
49 AwWebContentsDelegate::AwWebContentsDelegate(
50 JNIEnv* env,
51 jobject obj)
52 : WebContentsDelegateAndroid(env, obj),
53 is_fullscreen_(false) {
56 AwWebContentsDelegate::~AwWebContentsDelegate() {
59 content::JavaScriptDialogManager*
60 AwWebContentsDelegate::GetJavaScriptDialogManager(WebContents* source) {
61 return g_javascript_dialog_manager.Pointer();
64 void AwWebContentsDelegate::FindReply(WebContents* web_contents,
65 int request_id,
66 int number_of_matches,
67 const gfx::Rect& selection_rect,
68 int active_match_ordinal,
69 bool final_update) {
70 AwContents* aw_contents = AwContents::FromWebContents(web_contents);
71 if (!aw_contents)
72 return;
74 aw_contents->GetFindHelper()->HandleFindReply(request_id,
75 number_of_matches,
76 active_match_ordinal,
77 final_update);
80 void AwWebContentsDelegate::CanDownload(
81 const GURL& url,
82 const std::string& request_method,
83 const base::Callback<void(bool)>& callback) {
84 // Android webview intercepts download in its resource dispatcher host
85 // delegate, so should not reach here.
86 NOTREACHED();
87 callback.Run(false);
90 void AwWebContentsDelegate::RunFileChooser(WebContents* web_contents,
91 const FileChooserParams& params) {
92 JNIEnv* env = AttachCurrentThread();
93 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
94 if (!java_delegate.obj())
95 return;
97 int mode_flags = 0;
98 if (params.mode == FileChooserParams::OpenMultiple) {
99 mode_flags |= kFileChooserModeOpenMultiple;
100 } else if (params.mode == FileChooserParams::UploadFolder) {
101 // Folder implies multiple in Chrome.
102 mode_flags |= kFileChooserModeOpenMultiple | kFileChooserModeOpenFolder;
103 } else if (params.mode == FileChooserParams::Save) {
104 // Save not supported, so cancel it.
105 web_contents->GetRenderViewHost()->FilesSelectedInChooser(
106 std::vector<content::FileChooserFileInfo>(),
107 params.mode);
108 return;
109 } else {
110 DCHECK_EQ(FileChooserParams::Open, params.mode);
112 Java_AwWebContentsDelegate_runFileChooser(env,
113 java_delegate.obj(),
114 web_contents->GetRenderProcessHost()->GetID(),
115 web_contents->GetRenderViewHost()->GetRoutingID(),
116 mode_flags,
117 ConvertUTF16ToJavaString(env,
118 base::JoinString(params.accept_types, base::ASCIIToUTF16(","))).obj(),
119 params.title.empty() ? NULL :
120 ConvertUTF16ToJavaString(env, params.title).obj(),
121 params.default_file_name.empty() ? NULL :
122 ConvertUTF8ToJavaString(env, params.default_file_name.value()).obj(),
123 params.capture);
126 void AwWebContentsDelegate::AddNewContents(WebContents* source,
127 WebContents* new_contents,
128 WindowOpenDisposition disposition,
129 const gfx::Rect& initial_rect,
130 bool user_gesture,
131 bool* was_blocked) {
132 JNIEnv* env = AttachCurrentThread();
134 bool is_dialog = disposition == NEW_POPUP;
135 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
136 bool create_popup = false;
138 if (java_delegate.obj()) {
139 create_popup = Java_AwWebContentsDelegate_addNewContents(env,
140 java_delegate.obj(), is_dialog, user_gesture);
143 if (create_popup) {
144 // The embedder would like to display the popup and we will receive
145 // a callback from them later with an AwContents to use to display
146 // it. The source AwContents takes ownership of the new WebContents
147 // until then, and when the callback is made we will swap the WebContents
148 // out into the new AwContents.
149 AwContents::FromWebContents(source)->SetPendingWebContentsForPopup(
150 make_scoped_ptr(new_contents));
151 // Hide the WebContents for the pop up now, we will show it again
152 // when the user calls us back with an AwContents to use to show it.
153 new_contents->WasHidden();
154 } else {
155 // The embedder has forgone their chance to display this popup
156 // window, so we're done with the WebContents now. We use
157 // DeleteSoon as WebContentsImpl may call methods on |new_contents|
158 // after this method returns.
159 base::MessageLoop::current()->DeleteSoon(FROM_HERE, new_contents);
162 if (was_blocked) {
163 *was_blocked = !create_popup;
167 void AwWebContentsDelegate::NavigationStateChanged(
168 content::WebContents* source,
169 content::InvalidateTypes changed_flags) {
170 JNIEnv* env = AttachCurrentThread();
172 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
173 if (java_delegate.obj()) {
174 Java_AwWebContentsDelegate_navigationStateChanged(env, java_delegate.obj(),
175 changed_flags);
179 // Notifies the delegate about the creation of a new WebContents. This
180 // typically happens when popups are created.
181 void AwWebContentsDelegate::WebContentsCreated(
182 WebContents* source_contents,
183 int opener_render_frame_id,
184 const std::string& frame_name,
185 const GURL& target_url,
186 content::WebContents* new_contents) {
187 AwContentsIoThreadClientImpl::RegisterPendingContents(new_contents);
190 void AwWebContentsDelegate::CloseContents(WebContents* source) {
191 JNIEnv* env = AttachCurrentThread();
193 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
194 if (java_delegate.obj()) {
195 Java_AwWebContentsDelegate_closeContents(env, java_delegate.obj());
199 void AwWebContentsDelegate::ActivateContents(WebContents* contents) {
200 JNIEnv* env = AttachCurrentThread();
202 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
203 if (java_delegate.obj()) {
204 Java_AwWebContentsDelegate_activateContents(env, java_delegate.obj());
208 void AwWebContentsDelegate::LoadingStateChanged(WebContents* source,
209 bool to_different_document) {
210 // Page title may have changed, need to inform the embedder.
211 // |source| may be null if loading has started.
212 JNIEnv* env = AttachCurrentThread();
214 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
215 if (java_delegate.obj()) {
216 Java_AwWebContentsDelegate_loadingStateChanged(env, java_delegate.obj());
220 void AwWebContentsDelegate::RequestMediaAccessPermission(
221 WebContents* web_contents,
222 const content::MediaStreamRequest& request,
223 const content::MediaResponseCallback& callback) {
224 AwContents* aw_contents = AwContents::FromWebContents(web_contents);
225 if (!aw_contents) {
226 callback.Run(content::MediaStreamDevices(),
227 content::MEDIA_DEVICE_FAILED_DUE_TO_SHUTDOWN,
228 scoped_ptr<content::MediaStreamUI>().Pass());
229 return;
231 aw_contents->GetPermissionRequestHandler()->SendRequest(
232 scoped_ptr<AwPermissionRequestDelegate>(
233 new MediaAccessPermissionRequest(request, callback)));
236 void AwWebContentsDelegate::EnterFullscreenModeForTab(
237 content::WebContents* web_contents, const GURL& origin) {
238 WebContentsDelegateAndroid::EnterFullscreenModeForTab(web_contents, origin);
239 is_fullscreen_ = true;
240 web_contents->GetRenderViewHost()->WasResized();
243 void AwWebContentsDelegate::ExitFullscreenModeForTab(
244 content::WebContents* web_contents) {
245 WebContentsDelegateAndroid::ExitFullscreenModeForTab(web_contents);
246 is_fullscreen_ = false;
247 web_contents->GetRenderViewHost()->WasResized();
250 bool AwWebContentsDelegate::IsFullscreenForTabOrPending(
251 const content::WebContents* web_contents) const {
252 return is_fullscreen_;
256 static void FilesSelectedInChooser(
257 JNIEnv* env, jclass clazz,
258 jint process_id, jint render_id, jint mode_flags,
259 jobjectArray file_paths, jobjectArray display_names) {
260 content::RenderViewHost* rvh = content::RenderViewHost::FromID(process_id,
261 render_id);
262 if (!rvh)
263 return;
265 std::vector<std::string> file_path_str;
266 std::vector<std::string> display_name_str;
267 // Note file_paths maybe NULL, but this will just yield a zero-length vector.
268 base::android::AppendJavaStringArrayToStringVector(env, file_paths,
269 &file_path_str);
270 base::android::AppendJavaStringArrayToStringVector(env, display_names,
271 &display_name_str);
272 std::vector<content::FileChooserFileInfo> files;
273 files.reserve(file_path_str.size());
274 for (size_t i = 0; i < file_path_str.size(); ++i) {
275 GURL url(file_path_str[i]);
276 if (!url.is_valid())
277 continue;
278 base::FilePath path(url.SchemeIsFile() ?
279 net::UnescapeURLComponent(url.path(),
280 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS) :
281 file_path_str[i]);
282 content::FileChooserFileInfo file_info;
283 file_info.file_path = path;
284 if (!display_name_str[i].empty())
285 file_info.display_name = display_name_str[i];
286 files.push_back(file_info);
288 FileChooserParams::Mode mode;
289 if (mode_flags & kFileChooserModeOpenFolder) {
290 mode = FileChooserParams::UploadFolder;
291 } else if (mode_flags & kFileChooserModeOpenMultiple) {
292 mode = FileChooserParams::OpenMultiple;
293 } else {
294 mode = FileChooserParams::Open;
296 DVLOG(0) << "File Chooser result: mode = " << mode
297 << ", file paths = " << base::JoinString(file_path_str, ":");
298 rvh->FilesSelectedInChooser(files, mode);
301 bool RegisterAwWebContentsDelegate(JNIEnv* env) {
302 return RegisterNativesImpl(env);
305 } // namespace android_webview