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
{
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(
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
,
66 int number_of_matches
,
67 const gfx::Rect
& selection_rect
,
68 int active_match_ordinal
,
70 AwContents
* aw_contents
= AwContents::FromWebContents(web_contents
);
74 aw_contents
->GetFindHelper()->HandleFindReply(request_id
,
80 void AwWebContentsDelegate::CanDownload(
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.
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())
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
>(),
110 DCHECK_EQ(FileChooserParams::Open
, params
.mode
);
112 Java_AwWebContentsDelegate_runFileChooser(env
,
114 web_contents
->GetRenderProcessHost()->GetID(),
115 web_contents
->GetRenderViewHost()->GetRoutingID(),
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(),
126 void AwWebContentsDelegate::AddNewContents(WebContents
* source
,
127 WebContents
* new_contents
,
128 WindowOpenDisposition disposition
,
129 const gfx::Rect
& initial_rect
,
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
);
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();
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
);
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(),
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
);
226 callback
.Run(content::MediaStreamDevices(),
227 content::MEDIA_DEVICE_FAILED_DUE_TO_SHUTDOWN
,
228 scoped_ptr
<content::MediaStreamUI
>().Pass());
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_
;
255 static void FilesSelectedInChooser(
257 const JavaParamRef
<jclass
>& clazz
,
261 const JavaParamRef
<jobjectArray
>& file_paths
,
262 const JavaParamRef
<jobjectArray
>& display_names
) {
263 content::RenderViewHost
* rvh
= content::RenderViewHost::FromID(process_id
,
268 std::vector
<std::string
> file_path_str
;
269 std::vector
<std::string
> display_name_str
;
270 // Note file_paths maybe NULL, but this will just yield a zero-length vector.
271 base::android::AppendJavaStringArrayToStringVector(env
, file_paths
,
273 base::android::AppendJavaStringArrayToStringVector(env
, display_names
,
275 std::vector
<content::FileChooserFileInfo
> files
;
276 files
.reserve(file_path_str
.size());
277 for (size_t i
= 0; i
< file_path_str
.size(); ++i
) {
278 GURL
url(file_path_str
[i
]);
281 base::FilePath
path(url
.SchemeIsFile() ?
282 net::UnescapeURLComponent(url
.path(),
283 net::UnescapeRule::SPACES
| net::UnescapeRule::URL_SPECIAL_CHARS
) :
285 content::FileChooserFileInfo file_info
;
286 file_info
.file_path
= path
;
287 if (!display_name_str
[i
].empty())
288 file_info
.display_name
= display_name_str
[i
];
289 files
.push_back(file_info
);
291 FileChooserParams::Mode mode
;
292 if (mode_flags
& kFileChooserModeOpenFolder
) {
293 mode
= FileChooserParams::UploadFolder
;
294 } else if (mode_flags
& kFileChooserModeOpenMultiple
) {
295 mode
= FileChooserParams::OpenMultiple
;
297 mode
= FileChooserParams::Open
;
299 DVLOG(0) << "File Chooser result: mode = " << mode
300 << ", file paths = " << base::JoinString(file_path_str
, ":");
301 rvh
->FilesSelectedInChooser(files
, mode
);
304 bool RegisterAwWebContentsDelegate(JNIEnv
* env
) {
305 return RegisterNativesImpl(env
);
308 } // namespace android_webview