[Android WebView] Send WebChromeClient.onReceivedTitle when navigating back
[chromium-blink-merge.git] / android_webview / native / aw_web_contents_delegate.cc
blobb1ac9d4f40a9afd2d975d3ea55554a034e03cab0
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"
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),
51 is_fullscreen_(false) {
54 AwWebContentsDelegate::~AwWebContentsDelegate() {
57 content::JavaScriptDialogManager*
58 AwWebContentsDelegate::GetJavaScriptDialogManager(WebContents* source) {
59 return g_javascript_dialog_manager.Pointer();
62 void AwWebContentsDelegate::FindReply(WebContents* web_contents,
63 int request_id,
64 int number_of_matches,
65 const gfx::Rect& selection_rect,
66 int active_match_ordinal,
67 bool final_update) {
68 AwContents* aw_contents = AwContents::FromWebContents(web_contents);
69 if (!aw_contents)
70 return;
72 aw_contents->GetFindHelper()->HandleFindReply(request_id,
73 number_of_matches,
74 active_match_ordinal,
75 final_update);
78 void AwWebContentsDelegate::CanDownload(
79 content::RenderViewHost* source,
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 base::string16& 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() ? url.path() : file_path_str[i]);
278 content::FileChooserFileInfo file_info;
279 file_info.file_path = path;
280 if (!display_name_str[i].empty())
281 file_info.display_name = display_name_str[i];
282 files.push_back(file_info);
284 FileChooserParams::Mode mode;
285 if (mode_flags & kFileChooserModeOpenFolder) {
286 mode = FileChooserParams::UploadFolder;
287 } else if (mode_flags & kFileChooserModeOpenMultiple) {
288 mode = FileChooserParams::OpenMultiple;
289 } else {
290 mode = FileChooserParams::Open;
292 DVLOG(0) << "File Chooser result: mode = " << mode
293 << ", file paths = " << JoinString(file_path_str, ":");
294 rvh->FilesSelectedInChooser(files, mode);
297 bool RegisterAwWebContentsDelegate(JNIEnv* env) {
298 return RegisterNativesImpl(env);
301 } // namespace android_webview