cros: Fixes the omnibox focus issue when a packaged app used.
[chromium-blink-merge.git] / android_webview / native / aw_web_contents_delegate.cc
blobf9c42b12d204b816d75a241ba409730db8e70bc4
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_params.h"
23 #include "content/public/common/media_stream_request.h"
24 #include "jni/AwWebContentsDelegate_jni.h"
25 #include "ui/shell_dialogs/selected_file_info.h"
27 using base::android::AttachCurrentThread;
28 using base::android::ConvertUTF16ToJavaString;
29 using base::android::ConvertUTF8ToJavaString;
30 using base::android::ScopedJavaLocalRef;
31 using content::FileChooserParams;
32 using content::WebContents;
34 namespace android_webview {
36 namespace {
38 // WARNING: these constants are exposed in the public interface Java side, so
39 // must remain in sync with what clients are expecting.
40 const int kFileChooserModeOpenMultiple = 1 << 0;
41 const int kFileChooserModeOpenFolder = 1 << 1;
43 base::LazyInstance<AwJavaScriptDialogManager>::Leaky
44 g_javascript_dialog_manager = LAZY_INSTANCE_INITIALIZER;
47 AwWebContentsDelegate::AwWebContentsDelegate(
48 JNIEnv* env,
49 jobject obj)
50 : WebContentsDelegateAndroid(env, obj) {
53 AwWebContentsDelegate::~AwWebContentsDelegate() {
56 content::JavaScriptDialogManager*
57 AwWebContentsDelegate::GetJavaScriptDialogManager() {
58 return g_javascript_dialog_manager.Pointer();
61 void AwWebContentsDelegate::FindReply(WebContents* web_contents,
62 int request_id,
63 int number_of_matches,
64 const gfx::Rect& selection_rect,
65 int active_match_ordinal,
66 bool final_update) {
67 AwContents* aw_contents = AwContents::FromWebContents(web_contents);
68 if (!aw_contents)
69 return;
71 aw_contents->GetFindHelper()->HandleFindReply(request_id,
72 number_of_matches,
73 active_match_ordinal,
74 final_update);
77 void AwWebContentsDelegate::CanDownload(
78 content::RenderViewHost* source,
79 const GURL& url,
80 const std::string& request_method,
81 const base::Callback<void(bool)>& callback) {
82 // Android webview intercepts download in its resource dispatcher host
83 // delegate, so should not reach here.
84 NOTREACHED();
85 callback.Run(false);
88 void AwWebContentsDelegate::RunFileChooser(WebContents* web_contents,
89 const FileChooserParams& params) {
90 JNIEnv* env = AttachCurrentThread();
91 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
92 if (!java_delegate.obj())
93 return;
95 int mode_flags = 0;
96 if (params.mode == FileChooserParams::OpenMultiple) {
97 mode_flags |= kFileChooserModeOpenMultiple;
98 } else if (params.mode == FileChooserParams::UploadFolder) {
99 // Folder implies multiple in Chrome.
100 mode_flags |= kFileChooserModeOpenMultiple | kFileChooserModeOpenFolder;
101 } else if (params.mode == FileChooserParams::Save) {
102 // Save not supported, so cancel it.
103 web_contents->GetRenderViewHost()->FilesSelectedInChooser(
104 std::vector<ui::SelectedFileInfo>(),
105 params.mode);
106 return;
107 } else {
108 DCHECK_EQ(FileChooserParams::Open, params.mode);
110 Java_AwWebContentsDelegate_runFileChooser(env,
111 java_delegate.obj(),
112 web_contents->GetRenderProcessHost()->GetID(),
113 web_contents->GetRenderViewHost()->GetRoutingID(),
114 mode_flags,
115 ConvertUTF16ToJavaString(env,
116 JoinString(params.accept_types, ',')).obj(),
117 params.title.empty() ? NULL :
118 ConvertUTF16ToJavaString(env, params.title).obj(),
119 params.default_file_name.empty() ? NULL :
120 ConvertUTF8ToJavaString(env, params.default_file_name.value()).obj(),
121 params.capture);
124 void AwWebContentsDelegate::AddNewContents(WebContents* source,
125 WebContents* new_contents,
126 WindowOpenDisposition disposition,
127 const gfx::Rect& initial_pos,
128 bool user_gesture,
129 bool* was_blocked) {
130 JNIEnv* env = AttachCurrentThread();
132 bool is_dialog = disposition == NEW_POPUP;
133 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
134 bool create_popup = false;
136 if (java_delegate.obj()) {
137 create_popup = Java_AwWebContentsDelegate_addNewContents(env,
138 java_delegate.obj(), is_dialog, user_gesture);
141 if (create_popup) {
142 // The embedder would like to display the popup and we will receive
143 // a callback from them later with an AwContents to use to display
144 // it. The source AwContents takes ownership of the new WebContents
145 // until then, and when the callback is made we will swap the WebContents
146 // out into the new AwContents.
147 AwContents::FromWebContents(source)->SetPendingWebContentsForPopup(
148 make_scoped_ptr(new_contents));
149 // Hide the WebContents for the pop up now, we will show it again
150 // when the user calls us back with an AwContents to use to show it.
151 new_contents->WasHidden();
152 } else {
153 // The embedder has forgone their chance to display this popup
154 // window, so we're done with the WebContents now. We use
155 // DeleteSoon as WebContentsImpl may call methods on |new_contents|
156 // after this method returns.
157 base::MessageLoop::current()->DeleteSoon(FROM_HERE, new_contents);
160 if (was_blocked) {
161 *was_blocked = !create_popup;
165 // Notifies the delegate about the creation of a new WebContents. This
166 // typically happens when popups are created.
167 void AwWebContentsDelegate::WebContentsCreated(
168 WebContents* source_contents,
169 int opener_render_frame_id,
170 const base::string16& frame_name,
171 const GURL& target_url,
172 content::WebContents* new_contents) {
173 AwContentsIoThreadClientImpl::RegisterPendingContents(new_contents);
176 void AwWebContentsDelegate::CloseContents(WebContents* source) {
177 JNIEnv* env = AttachCurrentThread();
179 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
180 if (java_delegate.obj()) {
181 Java_AwWebContentsDelegate_closeContents(env, java_delegate.obj());
185 void AwWebContentsDelegate::ActivateContents(WebContents* contents) {
186 JNIEnv* env = AttachCurrentThread();
188 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
189 if (java_delegate.obj()) {
190 Java_AwWebContentsDelegate_activateContents(env, java_delegate.obj());
194 void AwWebContentsDelegate::RequestMediaAccessPermission(
195 WebContents* web_contents,
196 const content::MediaStreamRequest& request,
197 const content::MediaResponseCallback& callback) {
198 AwContents* aw_contents = AwContents::FromWebContents(web_contents);
199 if (!aw_contents) {
200 callback.Run(content::MediaStreamDevices(),
201 content::MEDIA_DEVICE_INVALID_STATE,
202 scoped_ptr<content::MediaStreamUI>().Pass());
203 return;
205 aw_contents->GetPermissionRequestHandler()->SendRequest(
206 scoped_ptr<AwPermissionRequestDelegate>(
207 new MediaAccessPermissionRequest(request, callback)));
210 static void FilesSelectedInChooser(
211 JNIEnv* env, jclass clazz,
212 jint process_id, jint render_id, jint mode_flags,
213 jobjectArray file_paths) {
214 content::RenderViewHost* rvh = content::RenderViewHost::FromID(process_id,
215 render_id);
216 if (!rvh)
217 return;
219 std::vector<std::string> file_path_str;
220 // Note file_paths maybe NULL, but this will just yield a zero-length vector.
221 base::android::AppendJavaStringArrayToStringVector(env, file_paths,
222 &file_path_str);
223 std::vector<ui::SelectedFileInfo> files;
224 files.reserve(file_path_str.size());
225 for (size_t i = 0; i < file_path_str.size(); ++i) {
226 GURL url(file_path_str[i]);
227 if (!url.is_valid())
228 continue;
229 base::FilePath path(url.SchemeIsFile() ? url.path() : file_path_str[i]);
230 files.push_back(ui::SelectedFileInfo(path, base::FilePath()));
232 FileChooserParams::Mode mode;
233 if (mode_flags & kFileChooserModeOpenFolder) {
234 mode = FileChooserParams::UploadFolder;
235 } else if (mode_flags & kFileChooserModeOpenMultiple) {
236 mode = FileChooserParams::OpenMultiple;
237 } else {
238 mode = FileChooserParams::Open;
240 DVLOG(0) << "File Chooser result: mode = " << mode
241 << ", file paths = " << JoinString(file_path_str, ":");
242 rvh->FilesSelectedInChooser(files, mode);
245 bool RegisterAwWebContentsDelegate(JNIEnv* env) {
246 return RegisterNativesImpl(env);
249 } // namespace android_webview