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 "chrome/browser/android/chrome_web_contents_delegate_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/command_line.h"
9 #include "chrome/browser/android/tab_android.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/file_select_helper.h"
12 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
13 #include "chrome/browser/media/protected_media_identifier_permission_context.h"
14 #include "chrome/browser/media/protected_media_identifier_permission_context_factory.h"
15 #include "chrome/browser/prerender/prerender_manager.h"
16 #include "chrome/browser/prerender/prerender_manager_factory.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h"
19 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
20 #include "chrome/browser/ui/browser_navigator.h"
21 #include "chrome/browser/ui/find_bar/find_notification_details.h"
22 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "content/public/browser/notification_details.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/notification_source.h"
27 #include "content/public/browser/render_process_host.h"
28 #include "content/public/browser/render_view_host.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/common/file_chooser_params.h"
31 #include "jni/ChromeWebContentsDelegateAndroid_jni.h"
32 #include "third_party/WebKit/public/web/WebWindowFeatures.h"
33 #include "ui/gfx/rect.h"
34 #include "ui/gfx/rect_f.h"
36 #if defined(ENABLE_PLUGINS)
37 #include "chrome/browser/pepper_broker_infobar_delegate.h"
40 using base::android::AttachCurrentThread
;
41 using base::android::ScopedJavaLocalRef
;
42 using content::FileChooserParams
;
43 using content::WebContents
;
47 ScopedJavaLocalRef
<jobject
> CreateJavaRectF(
49 const gfx::RectF
& rect
) {
50 return ScopedJavaLocalRef
<jobject
>(
51 Java_ChromeWebContentsDelegateAndroid_createRectF(env
,
58 ScopedJavaLocalRef
<jobject
> CreateJavaRect(
60 const gfx::Rect
& rect
) {
61 return ScopedJavaLocalRef
<jobject
>(
62 Java_ChromeWebContentsDelegateAndroid_createRect(
64 static_cast<int>(rect
.x()),
65 static_cast<int>(rect
.y()),
66 static_cast<int>(rect
.right()),
67 static_cast<int>(rect
.bottom())));
70 } // anonymous namespace
75 ChromeWebContentsDelegateAndroid::ChromeWebContentsDelegateAndroid(JNIEnv
* env
,
77 : WebContentsDelegateAndroid(env
, obj
) {
80 ChromeWebContentsDelegateAndroid::~ChromeWebContentsDelegateAndroid() {
81 notification_registrar_
.RemoveAll();
84 // Register native methods.
85 bool RegisterChromeWebContentsDelegateAndroid(JNIEnv
* env
) {
86 return RegisterNativesImpl(env
);
89 void ChromeWebContentsDelegateAndroid::LoadingStateChanged(
90 WebContents
* source
) {
91 bool has_stopped
= source
== NULL
|| !source
->IsLoading();
92 WebContentsDelegateAndroid::LoadingStateChanged(source
);
93 LoadProgressChanged(source
, has_stopped
? 1 : 0);
96 void ChromeWebContentsDelegateAndroid::RunFileChooser(
97 WebContents
* web_contents
,
98 const FileChooserParams
& params
) {
99 FileSelectHelper::RunFileChooser(web_contents
, params
);
102 void ChromeWebContentsDelegateAndroid::CloseContents(
103 WebContents
* web_contents
) {
104 // Prevent dangling registrations assigned to closed web contents.
105 if (notification_registrar_
.IsRegistered(this,
106 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE
,
107 content::Source
<WebContents
>(web_contents
))) {
108 notification_registrar_
.Remove(this,
109 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE
,
110 content::Source
<WebContents
>(web_contents
));
113 WebContentsDelegateAndroid::CloseContents(web_contents
);
116 void ChromeWebContentsDelegateAndroid::Observe(
118 const content::NotificationSource
& source
,
119 const content::NotificationDetails
& details
) {
121 case chrome::NOTIFICATION_FIND_RESULT_AVAILABLE
:
122 OnFindResultAvailable(
123 content::Source
<WebContents
>(source
).ptr(),
124 content::Details
<FindNotificationDetails
>(details
).ptr());
127 NOTREACHED() << "Unexpected notification: " << type
;
132 void ChromeWebContentsDelegateAndroid::FindReply(
133 WebContents
* web_contents
,
135 int number_of_matches
,
136 const gfx::Rect
& selection_rect
,
137 int active_match_ordinal
,
139 if (!notification_registrar_
.IsRegistered(this,
140 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE
,
141 content::Source
<WebContents
>(web_contents
))) {
142 notification_registrar_
.Add(this,
143 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE
,
144 content::Source
<WebContents
>(web_contents
));
147 FindTabHelper
* find_tab_helper
= FindTabHelper::FromWebContents(web_contents
);
148 find_tab_helper
->HandleFindReply(request_id
,
151 active_match_ordinal
,
155 void ChromeWebContentsDelegateAndroid::OnFindResultAvailable(
156 WebContents
* web_contents
,
157 const FindNotificationDetails
* find_result
) {
158 JNIEnv
* env
= base::android::AttachCurrentThread();
159 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
163 ScopedJavaLocalRef
<jobject
> selection_rect
= CreateJavaRect(
164 env
, find_result
->selection_rect());
166 // Create the details object.
167 ScopedJavaLocalRef
<jobject
> details_object
=
168 Java_ChromeWebContentsDelegateAndroid_createFindNotificationDetails(
170 find_result
->number_of_matches(),
171 selection_rect
.obj(),
172 find_result
->active_match_ordinal(),
173 find_result
->final_update());
175 Java_ChromeWebContentsDelegateAndroid_onFindResultAvailable(
178 details_object
.obj());
181 void ChromeWebContentsDelegateAndroid::FindMatchRectsReply(
182 WebContents
* web_contents
,
184 const std::vector
<gfx::RectF
>& rects
,
185 const gfx::RectF
& active_rect
) {
186 JNIEnv
* env
= base::android::AttachCurrentThread();
187 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
191 // Create the details object.
192 ScopedJavaLocalRef
<jobject
> details_object
=
193 Java_ChromeWebContentsDelegateAndroid_createFindMatchRectsDetails(
197 CreateJavaRectF(env
, active_rect
).obj());
200 for (size_t i
= 0; i
< rects
.size(); ++i
) {
201 Java_ChromeWebContentsDelegateAndroid_setMatchRectByIndex(
203 details_object
.obj(),
205 CreateJavaRectF(env
, rects
[i
]).obj());
208 Java_ChromeWebContentsDelegateAndroid_onFindMatchRectsAvailable(
211 details_object
.obj());
214 content::JavaScriptDialogManager
*
215 ChromeWebContentsDelegateAndroid::GetJavaScriptDialogManager() {
216 return GetJavaScriptDialogManagerInstance();
219 void ChromeWebContentsDelegateAndroid::RequestMediaAccessPermission(
220 content::WebContents
* web_contents
,
221 const content::MediaStreamRequest
& request
,
222 const content::MediaResponseCallback
& callback
) {
223 MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
224 web_contents
, request
, callback
, NULL
);
227 bool ChromeWebContentsDelegateAndroid::RequestPpapiBrokerPermission(
228 WebContents
* web_contents
,
230 const base::FilePath
& plugin_path
,
231 const base::Callback
<void(bool)>& callback
) {
232 #if defined(ENABLE_PLUGINS)
233 PepperBrokerInfoBarDelegate::Create(
234 web_contents
, url
, plugin_path
, callback
);
241 WebContents
* ChromeWebContentsDelegateAndroid::OpenURLFromTab(
243 const content::OpenURLParams
& params
) {
244 WindowOpenDisposition disposition
= params
.disposition
;
245 if (!source
|| (disposition
!= CURRENT_TAB
&&
246 disposition
!= NEW_FOREGROUND_TAB
&&
247 disposition
!= NEW_BACKGROUND_TAB
&&
248 disposition
!= OFF_THE_RECORD
&&
249 disposition
!= NEW_POPUP
&&
250 disposition
!= NEW_WINDOW
)) {
251 // We can't handle this here. Give the parent a chance.
252 return WebContentsDelegateAndroid::OpenURLFromTab(source
, params
);
255 Profile
* profile
= Profile::FromBrowserContext(source
->GetBrowserContext());
256 chrome::NavigateParams
nav_params(profile
,
259 FillNavigateParamsFromOpenURLParams(&nav_params
, params
);
260 nav_params
.source_contents
= source
;
261 nav_params
.window_action
= chrome::NavigateParams::SHOW_WINDOW
;
262 nav_params
.user_gesture
= params
.user_gesture
;
264 PopupBlockerTabHelper
* popup_blocker_helper
=
265 PopupBlockerTabHelper::FromWebContents(source
);
266 DCHECK(popup_blocker_helper
);
268 if ((params
.disposition
== NEW_POPUP
||
269 params
.disposition
== NEW_FOREGROUND_TAB
||
270 params
.disposition
== NEW_BACKGROUND_TAB
||
271 params
.disposition
== NEW_WINDOW
) &&
272 !params
.user_gesture
&&
273 !CommandLine::ForCurrentProcess()->HasSwitch(
274 switches::kDisablePopupBlocking
)) {
275 if (popup_blocker_helper
->MaybeBlockPopup(nav_params
,
276 blink::WebWindowFeatures())) {
281 if (disposition
== CURRENT_TAB
) {
282 // Only prerender for a current-tab navigation to avoid session storage
284 nav_params
.target_contents
= source
;
285 prerender::PrerenderManager
* prerender_manager
=
286 prerender::PrerenderManagerFactory::GetForProfile(profile
);
287 if (prerender_manager
&&
288 prerender_manager
->MaybeUsePrerenderedPage(params
.url
, &nav_params
)) {
289 return nav_params
.target_contents
;
293 return WebContentsDelegateAndroid::OpenURLFromTab(source
, params
);
296 void ChromeWebContentsDelegateAndroid::AddNewContents(
298 WebContents
* new_contents
,
299 WindowOpenDisposition disposition
,
300 const gfx::Rect
& initial_pos
,
303 // No code for this yet.
304 DCHECK_NE(disposition
, SAVE_TO_DISK
);
305 // Can't create a new contents for the current tab - invalid case.
306 DCHECK_NE(disposition
, CURRENT_TAB
);
308 TabAndroid::InitTabHelpers(new_contents
);
310 JNIEnv
* env
= AttachCurrentThread();
311 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
312 bool handled
= false;
313 if (!obj
.is_null()) {
314 handled
= Java_ChromeWebContentsDelegateAndroid_addNewContents(
317 reinterpret_cast<intptr_t>(source
),
318 reinterpret_cast<intptr_t>(new_contents
),
319 static_cast<jint
>(disposition
),
328 } // namespace android
329 } // namespace chrome