GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / android_webview / native / aw_web_contents_delegate.cc
blobd2a71e6e56c416454e1e971eed67362419a6cc46
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::RequestMediaAccessPermission(
208 WebContents* web_contents,
209 const content::MediaStreamRequest& request,
210 const content::MediaResponseCallback& callback) {
211 AwContents* aw_contents = AwContents::FromWebContents(web_contents);
212 if (!aw_contents) {
213 callback.Run(content::MediaStreamDevices(),
214 content::MEDIA_DEVICE_FAILED_DUE_TO_SHUTDOWN,
215 scoped_ptr<content::MediaStreamUI>().Pass());
216 return;
218 aw_contents->GetPermissionRequestHandler()->SendRequest(
219 scoped_ptr<AwPermissionRequestDelegate>(
220 new MediaAccessPermissionRequest(request, callback)));
223 void AwWebContentsDelegate::EnterFullscreenModeForTab(
224 content::WebContents* web_contents, const GURL& origin) {
225 WebContentsDelegateAndroid::EnterFullscreenModeForTab(web_contents, origin);
226 is_fullscreen_ = true;
227 web_contents->GetRenderViewHost()->WasResized();
230 void AwWebContentsDelegate::ExitFullscreenModeForTab(
231 content::WebContents* web_contents) {
232 WebContentsDelegateAndroid::ExitFullscreenModeForTab(web_contents);
233 is_fullscreen_ = false;
234 web_contents->GetRenderViewHost()->WasResized();
237 bool AwWebContentsDelegate::IsFullscreenForTabOrPending(
238 const content::WebContents* web_contents) const {
239 return is_fullscreen_;
243 static void FilesSelectedInChooser(
244 JNIEnv* env, jclass clazz,
245 jint process_id, jint render_id, jint mode_flags,
246 jobjectArray file_paths, jobjectArray display_names) {
247 content::RenderViewHost* rvh = content::RenderViewHost::FromID(process_id,
248 render_id);
249 if (!rvh)
250 return;
252 std::vector<std::string> file_path_str;
253 std::vector<std::string> display_name_str;
254 // Note file_paths maybe NULL, but this will just yield a zero-length vector.
255 base::android::AppendJavaStringArrayToStringVector(env, file_paths,
256 &file_path_str);
257 base::android::AppendJavaStringArrayToStringVector(env, display_names,
258 &display_name_str);
259 std::vector<content::FileChooserFileInfo> files;
260 files.reserve(file_path_str.size());
261 for (size_t i = 0; i < file_path_str.size(); ++i) {
262 GURL url(file_path_str[i]);
263 if (!url.is_valid())
264 continue;
265 base::FilePath path(url.SchemeIsFile() ? url.path() : file_path_str[i]);
266 content::FileChooserFileInfo file_info;
267 file_info.file_path = path;
268 if (!display_name_str[i].empty())
269 file_info.display_name = display_name_str[i];
270 files.push_back(file_info);
272 FileChooserParams::Mode mode;
273 if (mode_flags & kFileChooserModeOpenFolder) {
274 mode = FileChooserParams::UploadFolder;
275 } else if (mode_flags & kFileChooserModeOpenMultiple) {
276 mode = FileChooserParams::OpenMultiple;
277 } else {
278 mode = FileChooserParams::Open;
280 DVLOG(0) << "File Chooser result: mode = " << mode
281 << ", file paths = " << JoinString(file_path_str, ":");
282 rvh->FilesSelectedInChooser(files, mode);
285 bool RegisterAwWebContentsDelegate(JNIEnv* env) {
286 return RegisterNativesImpl(env);
289 } // namespace android_webview