| 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 "chrome/browser/android/chrome_web_contents_delegate_android.h" | 5 #include "chrome/browser/android/chrome_web_contents_delegate_android.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "chrome/browser/file_select_helper.h" | 8 #include "chrome/browser/file_select_helper.h" |
| 9 #include "chrome/browser/ui/find_bar/find_match_rects_details.h" |
| 10 #include "chrome/browser/ui/find_bar/find_notification_details.h" |
| 11 #include "chrome/browser/ui/find_bar/find_tab_helper.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 13 #include "chrome/common/chrome_notification_types.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" |
| 9 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/common/file_chooser_params.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" |
| 11 | 22 |
| 12 using base::android::AttachCurrentThread; | 23 using base::android::AttachCurrentThread; |
| 13 using base::android::GetClass; | 24 using base::android::GetClass; |
| 14 using base::android::ScopedJavaLocalRef; | 25 using base::android::ScopedJavaLocalRef; |
| 15 using content::FileChooserParams; | 26 using content::FileChooserParams; |
| 16 using content::WebContents; | 27 using content::WebContents; |
| 17 | 28 |
| 29 namespace { |
| 30 |
| 31 // Convenience method to create Android rects. |
| 32 // RectType should be either gfx::Rect or gfx::RectF. |
| 33 template <typename RectType> |
| 34 ScopedJavaLocalRef<jobject> CreateAndroidRect( |
| 35 JNIEnv* env, |
| 36 const ScopedJavaLocalRef<jclass>& clazz, |
| 37 const jmethodID& constructor, |
| 38 const RectType& rect) { |
| 39 |
| 40 ScopedJavaLocalRef<jobject> rect_object( |
| 41 env, |
| 42 env->NewObject(clazz.obj(), |
| 43 constructor, |
| 44 rect.x(), |
| 45 rect.y(), |
| 46 rect.right(), |
| 47 rect.bottom())); |
| 48 |
| 49 DCHECK(!rect_object.is_null()); |
| 50 return rect_object; |
| 51 } |
| 52 |
| 53 } // anonymous namespace |
| 54 |
| 18 namespace chrome { | 55 namespace chrome { |
| 19 namespace android { | 56 namespace android { |
| 20 | 57 |
| 21 ChromeWebContentsDelegateAndroid::ChromeWebContentsDelegateAndroid(JNIEnv* env, | 58 ChromeWebContentsDelegateAndroid::ChromeWebContentsDelegateAndroid(JNIEnv* env, |
| 22 jobject obj) | 59 jobject obj) |
| 23 : WebContentsDelegateAndroid(env, obj) { | 60 : WebContentsDelegateAndroid(env, obj) { |
| 24 } | 61 } |
| 25 | 62 |
| 26 ChromeWebContentsDelegateAndroid::~ChromeWebContentsDelegateAndroid() { | 63 ChromeWebContentsDelegateAndroid::~ChromeWebContentsDelegateAndroid() { |
| 64 notification_registrar_.RemoveAll(); |
| 65 } |
| 66 |
| 67 // Register native methods. |
| 68 bool RegisterChromeWebContentsDelegateAndroid(JNIEnv* env) { |
| 69 return RegisterNativesImpl(env); |
| 27 } | 70 } |
| 28 | 71 |
| 29 void ChromeWebContentsDelegateAndroid::RunFileChooser( | 72 void ChromeWebContentsDelegateAndroid::RunFileChooser( |
| 30 WebContents* web_contents, | 73 WebContents* web_contents, |
| 31 const FileChooserParams& params) { | 74 const FileChooserParams& params) { |
| 32 FileSelectHelper::RunFileChooser(web_contents, params); | 75 FileSelectHelper::RunFileChooser(web_contents, params); |
| 33 } | 76 } |
| 34 | 77 |
| 78 void ChromeWebContentsDelegateAndroid::CloseContents( |
| 79 WebContents* web_contents) { |
| 80 // Prevent dangling registrations assigned to closed web contents. |
| 81 if (notification_registrar_.IsRegistered(this, |
| 82 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, |
| 83 content::Source<WebContents>(web_contents))) { |
| 84 notification_registrar_.Remove(this, |
| 85 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, |
| 86 content::Source<WebContents>(web_contents)); |
| 87 } |
| 88 |
| 89 WebContentsDelegateAndroid::CloseContents(web_contents); |
| 90 } |
| 91 |
| 92 void ChromeWebContentsDelegateAndroid::Observe( |
| 93 int type, |
| 94 const content::NotificationSource& source, |
| 95 const content::NotificationDetails& details) { |
| 96 switch (type) { |
| 97 case chrome::NOTIFICATION_FIND_RESULT_AVAILABLE: |
| 98 OnFindResultAvailable( |
| 99 content::Source<WebContents>(source).ptr(), |
| 100 content::Details<FindNotificationDetails>(details).ptr()); |
| 101 break; |
| 102 default: |
| 103 NOTREACHED() << "Unexpected notification: " << type; |
| 104 break; |
| 105 } |
| 106 } |
| 107 |
| 108 void ChromeWebContentsDelegateAndroid::FindReply( |
| 109 WebContents* web_contents, |
| 110 int request_id, |
| 111 int number_of_matches, |
| 112 const gfx::Rect& selection_rect, |
| 113 int active_match_ordinal, |
| 114 bool final_update) { |
| 115 if (!notification_registrar_.IsRegistered(this, |
| 116 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, |
| 117 content::Source<WebContents>(web_contents))) { |
| 118 notification_registrar_.Add(this, |
| 119 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, |
| 120 content::Source<WebContents>(web_contents)); |
| 121 } |
| 122 |
| 123 TabContents* tab_contents = TabContents::FromWebContents(web_contents); |
| 124 tab_contents->find_tab_helper()->HandleFindReply(request_id, |
| 125 number_of_matches, |
| 126 selection_rect, |
| 127 active_match_ordinal, |
| 128 final_update); |
| 129 } |
| 130 |
| 131 void ChromeWebContentsDelegateAndroid::OnFindResultAvailable( |
| 132 WebContents* web_contents, |
| 133 const FindNotificationDetails* find_result) { |
| 134 JNIEnv* env = AttachCurrentThread(); |
| 135 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env); |
| 136 if (obj.is_null()) |
| 137 return; |
| 138 |
| 139 // Create the selection rect. |
| 140 ScopedJavaLocalRef<jclass> rect_clazz = |
| 141 GetClass(env, "android/graphics/Rect"); |
| 142 |
| 143 jmethodID rect_constructor = |
| 144 GetMethodID(env, rect_clazz, "<init>", "(IIII)V"); |
| 145 |
| 146 ScopedJavaLocalRef<jobject> selection_rect = CreateAndroidRect( |
| 147 env, rect_clazz, rect_constructor, find_result->selection_rect()); |
| 148 |
| 149 // Create the details object. |
| 150 ScopedJavaLocalRef<jclass> details_clazz = |
| 151 GetClass(env, "org/chromium/chrome/browser/FindNotificationDetails"); |
| 152 |
| 153 jmethodID details_constructor = GetMethodID(env, details_clazz, "<init>", |
| 154 "(ILandroid/graphics/Rect;IZ)V"); |
| 155 |
| 156 ScopedJavaLocalRef<jobject> details_object( |
| 157 env, |
| 158 env->NewObject(details_clazz.obj(), |
| 159 details_constructor, |
| 160 find_result->number_of_matches(), |
| 161 selection_rect.obj(), |
| 162 find_result->active_match_ordinal(), |
| 163 find_result->final_update())); |
| 164 DCHECK(!details_object.is_null()); |
| 165 |
| 166 Java_ChromeWebContentsDelegateAndroid_onFindResultAvailable( |
| 167 env, |
| 168 obj.obj(), |
| 169 details_object.obj()); |
| 170 } |
| 171 |
| 172 void ChromeWebContentsDelegateAndroid::FindMatchRectsReply( |
| 173 WebContents* web_contents, |
| 174 int version, |
| 175 const std::vector<gfx::RectF>& rects, |
| 176 const gfx::RectF& active_rect) { |
| 177 FindMatchRectsDetails match_rects(version, rects, active_rect); |
| 178 content::NotificationService::current()->Notify( |
| 179 chrome::NOTIFICATION_FIND_MATCH_RECTS_AVAILABLE, |
| 180 content::Source<WebContents>(web_contents), |
| 181 content::Details<FindMatchRectsDetails>(&match_rects)); |
| 182 |
| 183 JNIEnv* env = AttachCurrentThread(); |
| 184 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env); |
| 185 if (obj.is_null()) |
| 186 return; |
| 187 |
| 188 // Create the rects. |
| 189 ScopedJavaLocalRef<jclass> rect_clazz = |
| 190 GetClass(env, "android/graphics/RectF"); |
| 191 |
| 192 jmethodID rect_constructor = |
| 193 GetMethodID(env, rect_clazz, "<init>", "(FFFF)V"); |
| 194 |
| 195 ScopedJavaLocalRef<jobjectArray> jrects(env, env->NewObjectArray( |
| 196 match_rects.rects().size(), rect_clazz.obj(), NULL)); |
| 197 |
| 198 for (size_t i = 0; i < match_rects.rects().size(); ++i) { |
| 199 env->SetObjectArrayElement( |
| 200 jrects.obj(), i, |
| 201 CreateAndroidRect(env, |
| 202 rect_clazz, |
| 203 rect_constructor, |
| 204 match_rects.rects()[i]).obj()); |
| 205 } |
| 206 |
| 207 ScopedJavaLocalRef<jobject> jactive_rect = CreateAndroidRect( |
| 208 env, rect_clazz, rect_constructor, match_rects.active_rect()); |
| 209 |
| 210 // Create the details object. |
| 211 ScopedJavaLocalRef<jclass> details_clazz = |
| 212 GetClass(env, "org/chromium/chrome/browser/FindMatchRectsDetails"); |
| 213 |
| 214 jmethodID details_constructor = GetMethodID(env, details_clazz, "<init>", |
| 215 "(I[Landroid/graphics/RectF;Landroid/graphics/RectF;)V"); |
| 216 |
| 217 ScopedJavaLocalRef<jobject> details_object( |
| 218 env, |
| 219 env->NewObject(details_clazz.obj(), |
| 220 details_constructor, |
| 221 match_rects.version(), |
| 222 jrects.obj(), |
| 223 jactive_rect.obj())); |
| 224 DCHECK(!details_object.is_null()); |
| 225 |
| 226 Java_ChromeWebContentsDelegateAndroid_onFindMatchRectsAvailable( |
| 227 env, |
| 228 obj.obj(), |
| 229 details_object.obj()); |
| 230 } |
| 231 |
| 35 } // namespace android | 232 } // namespace android |
| 36 } // namespace chrome | 233 } // namespace chrome |
| OLD | NEW |