OLD | NEW |
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 |
| 39 base::LazyInstance<AwJavaScriptDialogManager>::Leaky |
23 g_javascript_dialog_manager = LAZY_INSTANCE_INITIALIZER; | 40 g_javascript_dialog_manager = LAZY_INSTANCE_INITIALIZER; |
| 41 } |
24 | 42 |
25 AwWebContentsDelegate::AwWebContentsDelegate( | 43 AwWebContentsDelegate::AwWebContentsDelegate( |
26 JNIEnv* env, | 44 JNIEnv* env, |
27 jobject obj) | 45 jobject obj) |
28 : WebContentsDelegateAndroid(env, obj) { | 46 : WebContentsDelegateAndroid(env, obj) { |
29 } | 47 } |
30 | 48 |
31 AwWebContentsDelegate::~AwWebContentsDelegate() { | 49 AwWebContentsDelegate::~AwWebContentsDelegate() { |
32 } | 50 } |
33 | 51 |
(...skipping 22 matching lines...) Expand all Loading... |
56 content::RenderViewHost* source, | 74 content::RenderViewHost* source, |
57 int request_id, | 75 int request_id, |
58 const std::string& request_method, | 76 const std::string& request_method, |
59 const base::Callback<void(bool)>& callback) { | 77 const base::Callback<void(bool)>& callback) { |
60 // Android webview intercepts download in its resource dispatcher host | 78 // Android webview intercepts download in its resource dispatcher host |
61 // delegate, so should not reach here. | 79 // delegate, so should not reach here. |
62 NOTREACHED(); | 80 NOTREACHED(); |
63 callback.Run(false); | 81 callback.Run(false); |
64 } | 82 } |
65 | 83 |
66 void AwWebContentsDelegate::AddNewContents(content::WebContents* source, | 84 void AwWebContentsDelegate::RunFileChooser(WebContents* web_contents, |
67 content::WebContents* new_contents, | 85 const FileChooserParams& params) { |
| 86 JNIEnv* env = AttachCurrentThread(); |
| 87 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); |
| 88 if (!java_delegate.obj()) |
| 89 return; |
| 90 |
| 91 int mode_flags = 0; |
| 92 if (params.mode == FileChooserParams::OpenMultiple) { |
| 93 mode_flags |= kFileChooserModeOpenMultiple; |
| 94 } else if (params.mode == FileChooserParams::UploadFolder) { |
| 95 // Folder implies multiple in Chrome. |
| 96 mode_flags |= kFileChooserModeOpenMultiple | kFileChooserModeOpenFolder; |
| 97 } else if (params.mode == FileChooserParams::Save) { |
| 98 // Save not supported, so cancel it. |
| 99 web_contents->GetRenderViewHost()->FilesSelectedInChooser( |
| 100 std::vector<ui::SelectedFileInfo>(), |
| 101 params.mode); |
| 102 return; |
| 103 } else { |
| 104 DCHECK_EQ(FileChooserParams::Open, params.mode); |
| 105 } |
| 106 Java_AwWebContentsDelegate_runFileChooser(env, |
| 107 java_delegate.obj(), |
| 108 web_contents->GetRenderProcessHost()->GetID(), |
| 109 web_contents->GetRenderViewHost()->GetRoutingID(), |
| 110 mode_flags, |
| 111 ConvertUTF16ToJavaString(env, |
| 112 JoinString(params.accept_types, ',')).obj(), |
| 113 params.title.empty() ? NULL : |
| 114 ConvertUTF16ToJavaString(env, params.title).obj(), |
| 115 params.default_file_name.empty() ? NULL : |
| 116 ConvertUTF8ToJavaString(env, params.default_file_name.value()).obj(), |
| 117 params.capture); |
| 118 } |
| 119 |
| 120 void AwWebContentsDelegate::AddNewContents(WebContents* source, |
| 121 WebContents* new_contents, |
68 WindowOpenDisposition disposition, | 122 WindowOpenDisposition disposition, |
69 const gfx::Rect& initial_pos, | 123 const gfx::Rect& initial_pos, |
70 bool user_gesture, | 124 bool user_gesture, |
71 bool* was_blocked) { | 125 bool* was_blocked) { |
72 JNIEnv* env = AttachCurrentThread(); | 126 JNIEnv* env = AttachCurrentThread(); |
73 | 127 |
74 bool is_dialog = disposition == NEW_POPUP; | 128 bool is_dialog = disposition == NEW_POPUP; |
75 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); | 129 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); |
76 bool create_popup = false; | 130 bool create_popup = false; |
77 | 131 |
(...skipping 19 matching lines...) Expand all Loading... |
97 // DeleteSoon as WebContentsImpl may call methods on |new_contents| | 151 // DeleteSoon as WebContentsImpl may call methods on |new_contents| |
98 // after this method returns. | 152 // after this method returns. |
99 base::MessageLoop::current()->DeleteSoon(FROM_HERE, new_contents); | 153 base::MessageLoop::current()->DeleteSoon(FROM_HERE, new_contents); |
100 } | 154 } |
101 | 155 |
102 if (was_blocked) { | 156 if (was_blocked) { |
103 *was_blocked = !create_popup; | 157 *was_blocked = !create_popup; |
104 } | 158 } |
105 } | 159 } |
106 | 160 |
107 void AwWebContentsDelegate::CloseContents(content::WebContents* source) { | 161 void AwWebContentsDelegate::CloseContents(WebContents* source) { |
108 JNIEnv* env = AttachCurrentThread(); | 162 JNIEnv* env = AttachCurrentThread(); |
109 | 163 |
110 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); | 164 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); |
111 if (java_delegate.obj()) { | 165 if (java_delegate.obj()) { |
112 Java_AwWebContentsDelegate_closeContents(env, java_delegate.obj()); | 166 Java_AwWebContentsDelegate_closeContents(env, java_delegate.obj()); |
113 } | 167 } |
114 } | 168 } |
115 | 169 |
116 void AwWebContentsDelegate::ActivateContents(content::WebContents* contents) { | 170 void AwWebContentsDelegate::ActivateContents(WebContents* contents) { |
117 JNIEnv* env = AttachCurrentThread(); | 171 JNIEnv* env = AttachCurrentThread(); |
118 | 172 |
119 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); | 173 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); |
120 if (java_delegate.obj()) { | 174 if (java_delegate.obj()) { |
121 Java_AwWebContentsDelegate_activateContents(env, java_delegate.obj()); | 175 Java_AwWebContentsDelegate_activateContents(env, java_delegate.obj()); |
122 } | 176 } |
123 } | 177 } |
124 | 178 |
125 void AwWebContentsDelegate::UpdatePreferredSize( | 179 void AwWebContentsDelegate::UpdatePreferredSize( |
126 WebContents* web_contents, | 180 WebContents* web_contents, |
127 const gfx::Size& pref_size) { | 181 const gfx::Size& pref_size) { |
128 JNIEnv* env = AttachCurrentThread(); | 182 JNIEnv* env = AttachCurrentThread(); |
129 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env); | 183 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env); |
130 if (obj.is_null()) | 184 if (obj.is_null()) |
131 return; | 185 return; |
132 return Java_AwWebContentsDelegate_updatePreferredSize( | 186 return Java_AwWebContentsDelegate_updatePreferredSize( |
133 env, obj.obj(), pref_size.width(), pref_size.height()); | 187 env, obj.obj(), pref_size.width(), pref_size.height()); |
134 } | 188 } |
135 | 189 |
| 190 static void FilesSelectedInChooser( |
| 191 JNIEnv* env, jclass clazz, |
| 192 jint process_id, jint render_id, jint mode_flags, |
| 193 jobjectArray file_paths) { |
| 194 content::RenderViewHost* rvh = content::RenderViewHost::FromID(process_id, |
| 195 render_id); |
| 196 if (!rvh) |
| 197 return; |
| 198 |
| 199 std::vector<std::string> file_path_str; |
| 200 // Note file_paths maybe NULL, but this will just yield a zero-length vector. |
| 201 base::android::AppendJavaStringArrayToStringVector(env, file_paths, |
| 202 &file_path_str); |
| 203 std::vector<ui::SelectedFileInfo> files; |
| 204 files.reserve(file_path_str.size()); |
| 205 for (size_t i = 0; i < file_path_str.size(); ++i) { |
| 206 files.push_back(ui::SelectedFileInfo(base::FilePath(file_path_str[i]), |
| 207 base::FilePath())); |
| 208 } |
| 209 FileChooserParams::Mode mode; |
| 210 if (mode_flags & kFileChooserModeOpenFolder) { |
| 211 mode = FileChooserParams::UploadFolder; |
| 212 } else if (mode_flags & kFileChooserModeOpenMultiple) { |
| 213 mode = FileChooserParams::OpenMultiple; |
| 214 } else { |
| 215 mode = FileChooserParams::Open; |
| 216 } |
| 217 LOG(INFO) << "File Chooser result: mode = " << mode |
| 218 << ", file paths = " << JoinString(file_path_str, ":"); |
| 219 rvh->FilesSelectedInChooser(files, mode); |
| 220 } |
| 221 |
136 bool RegisterAwWebContentsDelegate(JNIEnv* env) { | 222 bool RegisterAwWebContentsDelegate(JNIEnv* env) { |
137 return RegisterNativesImpl(env); | 223 return RegisterNativesImpl(env); |
138 } | 224 } |
139 | 225 |
140 } // namespace android_webview | 226 } // namespace android_webview |
OLD | NEW |