ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / android_webview / native / aw_web_contents_delegate.cc
blobacece1bf63e245fab3344e72cb01fed5e16c447e
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 // Notifies the delegate about the creation of a new WebContents. This
167 // typically happens when popups are created.
168 void AwWebContentsDelegate::WebContentsCreated(
169 WebContents* source_contents,
170 int opener_render_frame_id,
171 const base::string16& frame_name,
172 const GURL& target_url,
173 content::WebContents* new_contents) {
174 AwContentsIoThreadClientImpl::RegisterPendingContents(new_contents);
177 void AwWebContentsDelegate::CloseContents(WebContents* source) {
178 JNIEnv* env = AttachCurrentThread();
180 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
181 if (java_delegate.obj()) {
182 Java_AwWebContentsDelegate_closeContents(env, java_delegate.obj());
186 void AwWebContentsDelegate::ActivateContents(WebContents* contents) {
187 JNIEnv* env = AttachCurrentThread();
189 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
190 if (java_delegate.obj()) {
191 Java_AwWebContentsDelegate_activateContents(env, java_delegate.obj());
195 void AwWebContentsDelegate::RequestMediaAccessPermission(
196 WebContents* web_contents,
197 const content::MediaStreamRequest& request,
198 const content::MediaResponseCallback& callback) {
199 AwContents* aw_contents = AwContents::FromWebContents(web_contents);
200 if (!aw_contents) {
201 callback.Run(content::MediaStreamDevices(),
202 content::MEDIA_DEVICE_FAILED_DUE_TO_SHUTDOWN,
203 scoped_ptr<content::MediaStreamUI>().Pass());
204 return;
206 aw_contents->GetPermissionRequestHandler()->SendRequest(
207 scoped_ptr<AwPermissionRequestDelegate>(
208 new MediaAccessPermissionRequest(request, callback)));
211 void AwWebContentsDelegate::EnterFullscreenModeForTab(
212 content::WebContents* web_contents, const GURL& origin) {
213 WebContentsDelegateAndroid::EnterFullscreenModeForTab(web_contents, origin);
214 is_fullscreen_ = true;
215 web_contents->GetRenderViewHost()->WasResized();
218 void AwWebContentsDelegate::ExitFullscreenModeForTab(
219 content::WebContents* web_contents) {
220 WebContentsDelegateAndroid::ExitFullscreenModeForTab(web_contents);
221 is_fullscreen_ = false;
222 web_contents->GetRenderViewHost()->WasResized();
225 bool AwWebContentsDelegate::IsFullscreenForTabOrPending(
226 const content::WebContents* web_contents) const {
227 return is_fullscreen_;
231 static void FilesSelectedInChooser(
232 JNIEnv* env, jclass clazz,
233 jint process_id, jint render_id, jint mode_flags,
234 jobjectArray file_paths, jobjectArray display_names) {
235 content::RenderViewHost* rvh = content::RenderViewHost::FromID(process_id,
236 render_id);
237 if (!rvh)
238 return;
240 std::vector<std::string> file_path_str;
241 std::vector<std::string> display_name_str;
242 // Note file_paths maybe NULL, but this will just yield a zero-length vector.
243 base::android::AppendJavaStringArrayToStringVector(env, file_paths,
244 &file_path_str);
245 base::android::AppendJavaStringArrayToStringVector(env, display_names,
246 &display_name_str);
247 std::vector<content::FileChooserFileInfo> files;
248 files.reserve(file_path_str.size());
249 for (size_t i = 0; i < file_path_str.size(); ++i) {
250 GURL url(file_path_str[i]);
251 if (!url.is_valid())
252 continue;
253 base::FilePath path(url.SchemeIsFile() ? url.path() : file_path_str[i]);
254 content::FileChooserFileInfo file_info;
255 file_info.file_path = path;
256 if (!display_name_str[i].empty())
257 file_info.display_name = display_name_str[i];
258 files.push_back(file_info);
260 FileChooserParams::Mode mode;
261 if (mode_flags & kFileChooserModeOpenFolder) {
262 mode = FileChooserParams::UploadFolder;
263 } else if (mode_flags & kFileChooserModeOpenMultiple) {
264 mode = FileChooserParams::OpenMultiple;
265 } else {
266 mode = FileChooserParams::Open;
268 DVLOG(0) << "File Chooser result: mode = " << mode
269 << ", file paths = " << JoinString(file_path_str, ":");
270 rvh->FilesSelectedInChooser(files, mode);
273 bool RegisterAwWebContentsDelegate(JNIEnv* env) {
274 return RegisterNativesImpl(env);
277 } // namespace android_webview