Home | History | Annotate | Download | only in android
      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 "chrome/browser/android/chrome_web_contents_delegate_android.h"
      6 
      7 #include "base/android/jni_android.h"
      8 #include "chrome/browser/chrome_notification_types.h"
      9 #include "chrome/browser/file_select_helper.h"
     10 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
     11 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h"
     12 #include "chrome/browser/ui/find_bar/find_notification_details.h"
     13 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
     14 #include "content/public/browser/notification_details.h"
     15 #include "content/public/browser/notification_service.h"
     16 #include "content/public/browser/notification_source.h"
     17 #include "content/public/browser/web_contents.h"
     18 #include "content/public/common/file_chooser_params.h"
     19 #include "jni/ChromeWebContentsDelegateAndroid_jni.h"
     20 #include "ui/gfx/rect.h"
     21 #include "ui/gfx/rect_f.h"
     22 
     23 #if defined(ENABLE_PLUGINS)
     24 #include "chrome/browser/pepper_broker_infobar_delegate.h"
     25 #endif
     26 
     27 using base::android::ScopedJavaLocalRef;
     28 using content::FileChooserParams;
     29 using content::WebContents;
     30 
     31 namespace {
     32 
     33 ScopedJavaLocalRef<jobject> CreateJavaRectF(
     34     JNIEnv* env,
     35     const gfx::RectF& rect) {
     36   return ScopedJavaLocalRef<jobject>(
     37       Java_ChromeWebContentsDelegateAndroid_createRectF(env,
     38                                                         rect.x(),
     39                                                         rect.y(),
     40                                                         rect.right(),
     41                                                         rect.bottom()));
     42 }
     43 
     44 ScopedJavaLocalRef<jobject> CreateJavaRect(
     45     JNIEnv* env,
     46     const gfx::Rect& rect) {
     47   return ScopedJavaLocalRef<jobject>(
     48       Java_ChromeWebContentsDelegateAndroid_createRect(
     49           env,
     50           static_cast<int>(rect.x()),
     51           static_cast<int>(rect.y()),
     52           static_cast<int>(rect.right()),
     53           static_cast<int>(rect.bottom())));
     54 }
     55 
     56 }  // anonymous namespace
     57 
     58 namespace chrome {
     59 namespace android {
     60 
     61 ChromeWebContentsDelegateAndroid::ChromeWebContentsDelegateAndroid(JNIEnv* env,
     62                                                                    jobject obj)
     63     : WebContentsDelegateAndroid(env, obj) {
     64 }
     65 
     66 ChromeWebContentsDelegateAndroid::~ChromeWebContentsDelegateAndroid() {
     67   notification_registrar_.RemoveAll();
     68 }
     69 
     70 // Register native methods.
     71 bool RegisterChromeWebContentsDelegateAndroid(JNIEnv* env) {
     72   return RegisterNativesImpl(env);
     73 }
     74 
     75 void ChromeWebContentsDelegateAndroid::RunFileChooser(
     76     WebContents* web_contents,
     77     const FileChooserParams& params) {
     78   FileSelectHelper::RunFileChooser(web_contents, params);
     79 }
     80 
     81 void ChromeWebContentsDelegateAndroid::CloseContents(
     82     WebContents* web_contents) {
     83   // Prevent dangling registrations assigned to closed web contents.
     84   if (notification_registrar_.IsRegistered(this,
     85       chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
     86       content::Source<WebContents>(web_contents))) {
     87     notification_registrar_.Remove(this,
     88         chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
     89         content::Source<WebContents>(web_contents));
     90   }
     91 
     92   WebContentsDelegateAndroid::CloseContents(web_contents);
     93 }
     94 
     95 void ChromeWebContentsDelegateAndroid::Observe(
     96     int type,
     97     const content::NotificationSource& source,
     98     const content::NotificationDetails& details) {
     99   switch (type) {
    100     case chrome::NOTIFICATION_FIND_RESULT_AVAILABLE:
    101       OnFindResultAvailable(
    102           content::Source<WebContents>(source).ptr(),
    103           content::Details<FindNotificationDetails>(details).ptr());
    104       break;
    105     default:
    106       NOTREACHED() << "Unexpected notification: " << type;
    107       break;
    108   }
    109 }
    110 
    111 void ChromeWebContentsDelegateAndroid::FindReply(
    112     WebContents* web_contents,
    113     int request_id,
    114     int number_of_matches,
    115     const gfx::Rect& selection_rect,
    116     int active_match_ordinal,
    117     bool final_update) {
    118   if (!notification_registrar_.IsRegistered(this,
    119       chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
    120       content::Source<WebContents>(web_contents))) {
    121     notification_registrar_.Add(this,
    122         chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
    123         content::Source<WebContents>(web_contents));
    124   }
    125 
    126   FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents);
    127   find_tab_helper->HandleFindReply(request_id,
    128                                    number_of_matches,
    129                                    selection_rect,
    130                                    active_match_ordinal,
    131                                    final_update);
    132 }
    133 
    134 void ChromeWebContentsDelegateAndroid::OnFindResultAvailable(
    135     WebContents* web_contents,
    136     const FindNotificationDetails* find_result) {
    137   JNIEnv* env = base::android::AttachCurrentThread();
    138   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
    139   if (obj.is_null())
    140     return;
    141 
    142   ScopedJavaLocalRef<jobject> selection_rect = CreateJavaRect(
    143       env, find_result->selection_rect());
    144 
    145   // Create the details object.
    146   ScopedJavaLocalRef<jobject> details_object =
    147       Java_ChromeWebContentsDelegateAndroid_createFindNotificationDetails(
    148           env,
    149           find_result->number_of_matches(),
    150           selection_rect.obj(),
    151           find_result->active_match_ordinal(),
    152           find_result->final_update());
    153 
    154   Java_ChromeWebContentsDelegateAndroid_onFindResultAvailable(
    155       env,
    156       obj.obj(),
    157       details_object.obj());
    158 }
    159 
    160 void ChromeWebContentsDelegateAndroid::FindMatchRectsReply(
    161     WebContents* web_contents,
    162     int version,
    163     const std::vector<gfx::RectF>& rects,
    164     const gfx::RectF& active_rect) {
    165   JNIEnv* env = base::android::AttachCurrentThread();
    166   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
    167   if (obj.is_null())
    168     return;
    169 
    170   // Create the details object.
    171   ScopedJavaLocalRef<jobject> details_object =
    172       Java_ChromeWebContentsDelegateAndroid_createFindMatchRectsDetails(
    173           env,
    174           version,
    175           rects.size(),
    176           CreateJavaRectF(env, active_rect).obj());
    177 
    178   // Add the rects
    179   for (size_t i = 0; i < rects.size(); ++i) {
    180       Java_ChromeWebContentsDelegateAndroid_setMatchRectByIndex(
    181           env,
    182           details_object.obj(),
    183           i,
    184           CreateJavaRectF(env, rects[i]).obj());
    185   }
    186 
    187   Java_ChromeWebContentsDelegateAndroid_onFindMatchRectsAvailable(
    188       env,
    189       obj.obj(),
    190       details_object.obj());
    191 }
    192 
    193 content::JavaScriptDialogManager*
    194 ChromeWebContentsDelegateAndroid::GetJavaScriptDialogManager() {
    195   return GetJavaScriptDialogManagerInstance();
    196 }
    197 
    198 void ChromeWebContentsDelegateAndroid::RequestMediaAccessPermission(
    199     content::WebContents* web_contents,
    200     const content::MediaStreamRequest& request,
    201     const content::MediaResponseCallback& callback) {
    202   MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
    203       web_contents, request, callback, NULL);
    204 }
    205 
    206 bool ChromeWebContentsDelegateAndroid::RequestPpapiBrokerPermission(
    207     WebContents* web_contents,
    208     const GURL& url,
    209     const base::FilePath& plugin_path,
    210     const base::Callback<void(bool)>& callback) {
    211 #if defined(ENABLE_PLUGINS)
    212     PepperBrokerInfoBarDelegate::Create(
    213         web_contents, url, plugin_path, callback);
    214     return true;
    215 #else
    216     return false;
    217 #endif
    218 }
    219 
    220 
    221 }  // namespace android
    222 }  // namespace chrome
    223