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