Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / android_webview / native / aw_web_contents_delegate.cc
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.
4
5 #include "android_webview/native/aw_web_contents_delegate.h"
6
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"
26
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;
33
34 namespace android_webview {
35
36 namespace {
37
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;
42
43 base::LazyInstance<AwJavaScriptDialogManager>::Leaky
44     g_javascript_dialog_manager = LAZY_INSTANCE_INITIALIZER;
45 }
46
47 AwWebContentsDelegate::AwWebContentsDelegate(
48     JNIEnv* env,
49     jobject obj)
50     : WebContentsDelegateAndroid(env, obj) {
51 }
52
53 AwWebContentsDelegate::~AwWebContentsDelegate() {
54 }
55
56 content::JavaScriptDialogManager*
57 AwWebContentsDelegate::GetJavaScriptDialogManager() {
58   return g_javascript_dialog_manager.Pointer();
59 }
60
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;
70
71   aw_contents->GetFindHelper()->HandleFindReply(request_id,
72                                                 number_of_matches,
73                                                 active_match_ordinal,
74                                                 final_update);
75 }
76
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);
86 }
87
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;
94
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);
109   }
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);
122 }
123
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();
131
132   bool is_dialog = disposition == NEW_POPUP;
133   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
134   bool create_popup = false;
135
136   if (java_delegate.obj()) {
137     create_popup = Java_AwWebContentsDelegate_addNewContents(env,
138         java_delegate.obj(), is_dialog, user_gesture);
139   }
140
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);
158   }
159
160   if (was_blocked) {
161     *was_blocked = !create_popup;
162   }
163 }
164
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);
174 }
175
176 void AwWebContentsDelegate::CloseContents(WebContents* source) {
177   JNIEnv* env = AttachCurrentThread();
178
179   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
180   if (java_delegate.obj()) {
181     Java_AwWebContentsDelegate_closeContents(env, java_delegate.obj());
182   }
183 }
184
185 void AwWebContentsDelegate::ActivateContents(WebContents* contents) {
186   JNIEnv* env = AttachCurrentThread();
187
188   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
189   if (java_delegate.obj()) {
190     Java_AwWebContentsDelegate_activateContents(env, java_delegate.obj());
191   }
192 }
193
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;
204   }
205   aw_contents->GetPermissionRequestHandler()->SendRequest(
206       scoped_ptr<AwPermissionRequestDelegate>(
207           new MediaAccessPermissionRequest(request, callback)));
208 }
209
210 static void FilesSelectedInChooser(
211     JNIEnv* env, jclass clazz,
212     jint process_id, jint render_id, jint mode_flags,
213     jobjectArray file_paths, jobjectArray display_names) {
214   content::RenderViewHost* rvh = content::RenderViewHost::FromID(process_id,
215                                                                  render_id);
216   if (!rvh)
217     return;
218
219   std::vector<std::string> file_path_str;
220   std::vector<std::string> display_name_str;
221   // Note file_paths maybe NULL, but this will just yield a zero-length vector.
222   base::android::AppendJavaStringArrayToStringVector(env, file_paths,
223                                                      &file_path_str);
224   base::android::AppendJavaStringArrayToStringVector(env, display_names,
225                                                      &display_name_str);
226   std::vector<ui::SelectedFileInfo> files;
227   files.reserve(file_path_str.size());
228   for (size_t i = 0; i < file_path_str.size(); ++i) {
229     GURL url(file_path_str[i]);
230     if (!url.is_valid())
231       continue;
232     base::FilePath path(url.SchemeIsFile() ? url.path() : file_path_str[i]);
233     ui::SelectedFileInfo file_info(path, base::FilePath());
234     if (!display_name_str[i].empty())
235       file_info.display_name = display_name_str[i];
236     files.push_back(file_info);
237   }
238   FileChooserParams::Mode mode;
239   if (mode_flags & kFileChooserModeOpenFolder) {
240     mode = FileChooserParams::UploadFolder;
241   } else if (mode_flags & kFileChooserModeOpenMultiple) {
242     mode = FileChooserParams::OpenMultiple;
243   } else {
244     mode = FileChooserParams::Open;
245   }
246   DVLOG(0) << "File Chooser result: mode = " << mode
247            << ", file paths = " << JoinString(file_path_str, ":");
248   rvh->FilesSelectedInChooser(files, mode);
249 }
250
251 bool RegisterAwWebContentsDelegate(JNIEnv* env) {
252   return RegisterNativesImpl(env);
253 }
254
255 }  // namespace android_webview