Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: android_webview/native/aw_web_contents_delegate.cc

Issue 20666003: [Android] Expose showFileChooser in AwContentsClient interface (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: mkosiba + rebase Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « android_webview/native/aw_web_contents_delegate.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "android_webview/native/aw_web_contents_delegate.h" 5 #include "android_webview/native/aw_web_contents_delegate.h"
6 6
7 #include "android_webview/browser/aw_javascript_dialog_manager.h" 7 #include "android_webview/browser/aw_javascript_dialog_manager.h"
8 #include "android_webview/browser/find_helper.h" 8 #include "android_webview/browser/find_helper.h"
9 #include "android_webview/native/aw_contents.h" 9 #include "android_webview/native/aw_contents.h"
10 #include "base/android/jni_array.h"
11 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h" 12 #include "base/android/scoped_java_ref.h"
11 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
12 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/strings/string_util.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/file_chooser_params.h"
14 #include "jni/AwWebContentsDelegate_jni.h" 20 #include "jni/AwWebContentsDelegate_jni.h"
21 #include "ui/shell_dialogs/selected_file_info.h"
15 22
16 using base::android::AttachCurrentThread; 23 using base::android::AttachCurrentThread;
24 using base::android::ConvertUTF16ToJavaString;
25 using base::android::ConvertUTF8ToJavaString;
17 using base::android::ScopedJavaLocalRef; 26 using base::android::ScopedJavaLocalRef;
27 using content::FileChooserParams;
18 using content::WebContents; 28 using content::WebContents;
19 29
20 namespace android_webview { 30 namespace android_webview {
21 31
22 static base::LazyInstance<AwJavaScriptDialogManager>::Leaky 32 namespace {
33
34 // WARNING: these constants are exposed in the public interface Java side, so
35 // must remain in sync with what clients are expecting.
36 const int kFileChooserModeOpenMultiple = 1 << 0;
37 const int kFileChooserModeOpenFolder = 1 << 1;
38 const int kFileChooserModeSave = 1 << 2;
39
40 base::LazyInstance<AwJavaScriptDialogManager>::Leaky
23 g_javascript_dialog_manager = LAZY_INSTANCE_INITIALIZER; 41 g_javascript_dialog_manager = LAZY_INSTANCE_INITIALIZER;
42 }
24 43
25 AwWebContentsDelegate::AwWebContentsDelegate( 44 AwWebContentsDelegate::AwWebContentsDelegate(
26 JNIEnv* env, 45 JNIEnv* env,
27 jobject obj) 46 jobject obj)
28 : WebContentsDelegateAndroid(env, obj) { 47 : WebContentsDelegateAndroid(env, obj) {
29 } 48 }
30 49
31 AwWebContentsDelegate::~AwWebContentsDelegate() { 50 AwWebContentsDelegate::~AwWebContentsDelegate() {
32 } 51 }
33 52
(...skipping 22 matching lines...) Expand all
56 content::RenderViewHost* source, 75 content::RenderViewHost* source,
57 int request_id, 76 int request_id,
58 const std::string& request_method, 77 const std::string& request_method,
59 const base::Callback<void(bool)>& callback) { 78 const base::Callback<void(bool)>& callback) {
60 // Android webview intercepts download in its resource dispatcher host 79 // Android webview intercepts download in its resource dispatcher host
61 // delegate, so should not reach here. 80 // delegate, so should not reach here.
62 NOTREACHED(); 81 NOTREACHED();
63 callback.Run(false); 82 callback.Run(false);
64 } 83 }
65 84
66 void AwWebContentsDelegate::AddNewContents(content::WebContents* source, 85 void AwWebContentsDelegate::RunFileChooser(WebContents* web_contents,
67 content::WebContents* new_contents, 86 const FileChooserParams& params) {
87 JNIEnv* env = AttachCurrentThread();
88 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
89 if (!java_delegate.obj())
90 return;
91
92 int mode_flags = 0;
93 if (params.mode == FileChooserParams::OpenMultiple) {
94 mode_flags |= kFileChooserModeOpenMultiple;
95 } else if (params.mode == FileChooserParams::UploadFolder) {
96 // Folder implies multiple in Chrome.
97 mode_flags |= kFileChooserModeOpenMultiple | kFileChooserModeOpenFolder;
98 } else if (params.mode == FileChooserParams::Save) {
99 mode_flags |= kFileChooserModeSave;
benm (inactive) 2013/08/07 09:50:59 We're exposing more things than the old file picke
joth 2013/08/08 01:13:36 Done (removed Save)
100 } else {
101 DCHECK_EQ(FileChooserParams::Open, params.mode);
102 }
103 Java_AwWebContentsDelegate_runFileChooser(env,
104 java_delegate.obj(),
105 web_contents->GetRenderProcessHost()->GetID(),
106 web_contents->GetRenderViewHost()->GetRoutingID(),
107 mode_flags,
108 ConvertUTF16ToJavaString(env,
109 JoinString(params.accept_types, ',')).obj(),
110 params.title.empty() ? NULL :
111 ConvertUTF16ToJavaString(env, params.title).obj(),
112 params.default_file_name.empty() ? NULL :
113 ConvertUTF8ToJavaString(env, params.default_file_name.value()).obj(),
114 params.capture);
115 }
116
117 void AwWebContentsDelegate::AddNewContents(WebContents* source,
118 WebContents* new_contents,
68 WindowOpenDisposition disposition, 119 WindowOpenDisposition disposition,
69 const gfx::Rect& initial_pos, 120 const gfx::Rect& initial_pos,
70 bool user_gesture, 121 bool user_gesture,
71 bool* was_blocked) { 122 bool* was_blocked) {
72 JNIEnv* env = AttachCurrentThread(); 123 JNIEnv* env = AttachCurrentThread();
73 124
74 bool is_dialog = disposition == NEW_POPUP; 125 bool is_dialog = disposition == NEW_POPUP;
75 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); 126 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
76 bool create_popup = false; 127 bool create_popup = false;
77 128
(...skipping 19 matching lines...) Expand all
97 // DeleteSoon as WebContentsImpl may call methods on |new_contents| 148 // DeleteSoon as WebContentsImpl may call methods on |new_contents|
98 // after this method returns. 149 // after this method returns.
99 base::MessageLoop::current()->DeleteSoon(FROM_HERE, new_contents); 150 base::MessageLoop::current()->DeleteSoon(FROM_HERE, new_contents);
100 } 151 }
101 152
102 if (was_blocked) { 153 if (was_blocked) {
103 *was_blocked = !create_popup; 154 *was_blocked = !create_popup;
104 } 155 }
105 } 156 }
106 157
107 void AwWebContentsDelegate::CloseContents(content::WebContents* source) { 158 void AwWebContentsDelegate::CloseContents(WebContents* source) {
108 JNIEnv* env = AttachCurrentThread(); 159 JNIEnv* env = AttachCurrentThread();
109 160
110 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); 161 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
111 if (java_delegate.obj()) { 162 if (java_delegate.obj()) {
112 Java_AwWebContentsDelegate_closeContents(env, java_delegate.obj()); 163 Java_AwWebContentsDelegate_closeContents(env, java_delegate.obj());
113 } 164 }
114 } 165 }
115 166
116 void AwWebContentsDelegate::ActivateContents(content::WebContents* contents) { 167 void AwWebContentsDelegate::ActivateContents(WebContents* contents) {
117 JNIEnv* env = AttachCurrentThread(); 168 JNIEnv* env = AttachCurrentThread();
118 169
119 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); 170 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
120 if (java_delegate.obj()) { 171 if (java_delegate.obj()) {
121 Java_AwWebContentsDelegate_activateContents(env, java_delegate.obj()); 172 Java_AwWebContentsDelegate_activateContents(env, java_delegate.obj());
122 } 173 }
123 } 174 }
124 175
125 void AwWebContentsDelegate::UpdatePreferredSize( 176 void AwWebContentsDelegate::UpdatePreferredSize(
126 WebContents* web_contents, 177 WebContents* web_contents,
127 const gfx::Size& pref_size) { 178 const gfx::Size& pref_size) {
128 JNIEnv* env = AttachCurrentThread(); 179 JNIEnv* env = AttachCurrentThread();
129 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env); 180 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
130 if (obj.is_null()) 181 if (obj.is_null())
131 return; 182 return;
132 return Java_AwWebContentsDelegate_updatePreferredSize( 183 return Java_AwWebContentsDelegate_updatePreferredSize(
133 env, obj.obj(), pref_size.width(), pref_size.height()); 184 env, obj.obj(), pref_size.width(), pref_size.height());
134 } 185 }
135 186
187 static void FilesSelectedInChooser(
188 JNIEnv* env, jclass clazz,
189 jint process_id, jint render_id, jint mode_flags,
190 jobjectArray file_paths) {
191 content::RenderViewHost* rvh = content::RenderViewHost::FromID(process_id,
192 render_id);
193 if (!rvh)
194 return;
195
196 std::vector<std::string> file_path_str;
197 // Note file_paths maybe NULL, but this will just yield a zero-length vector.
198 base::android::AppendJavaStringArrayToStringVector(env, file_paths,
199 &file_path_str);
200 std::vector<ui::SelectedFileInfo> files;
201 files.reserve(file_path_str.size());
202 for (size_t i = 0; i < file_path_str.size(); ++i) {
203 files.push_back(ui::SelectedFileInfo(base::FilePath(file_path_str[i]),
204 base::FilePath()));
205 }
206 FileChooserParams::Mode mode;
207 if (mode_flags & kFileChooserModeOpenFolder) {
208 mode = FileChooserParams::UploadFolder;
209 } else if (mode_flags & kFileChooserModeOpenMultiple) {
210 mode = FileChooserParams::OpenMultiple;
211 } else if (mode_flags & kFileChooserModeSave) {
212 mode = FileChooserParams::Save;
213 } else {
214 mode = FileChooserParams::Open;
215 }
216 LOG(INFO) << "File Chooser result: mode = " << mode
217 << ", file paths = " << JoinString(file_path_str, ":");
218 rvh->FilesSelectedInChooser(files, mode);
219 }
220
136 bool RegisterAwWebContentsDelegate(JNIEnv* env) { 221 bool RegisterAwWebContentsDelegate(JNIEnv* env) {
137 return RegisterNativesImpl(env); 222 return RegisterNativesImpl(env);
138 } 223 }
139 224
140 } // namespace android_webview 225 } // namespace android_webview
OLDNEW
« no previous file with comments | « android_webview/native/aw_web_contents_delegate.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698