Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: chrome/browser/android/chrome_web_contents_delegate_android.cc

Issue 10905058: Upstream the Android port find-in-page feature. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_source.h"
9 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
10 #include "content/public/common/file_chooser_params.h" 17 #include "content/public/common/file_chooser_params.h"
18 #include "jni/ChromeWebContentsDelegateAndroid_jni.h"
19 #include "ui/gfx/rect.h"
20 #include "ui/gfx/rect_f.h"
11 21
12 using base::android::AttachCurrentThread; 22 using base::android::AttachCurrentThread;
13 using base::android::GetClass; 23 using base::android::GetClass;
14 using base::android::ScopedJavaLocalRef; 24 using base::android::ScopedJavaLocalRef;
15 using content::FileChooserParams; 25 using content::FileChooserParams;
16 using content::WebContents; 26 using content::WebContents;
17 27
28 namespace {
29
30 // Convenience method to create Android rects.
31 // RectType should be either gfx::Rect or gfx::RectF.
32 template <typename RectType>
33 ScopedJavaLocalRef<jobject> CreateAndroidRect(
34 JNIEnv* env,
35 const ScopedJavaLocalRef<jclass>& clazz,
36 const jmethodID& constructor,
37 const RectType& rect) {
38
39 ScopedJavaLocalRef<jobject> rect_object(
40 env,
41 env->NewObject(clazz.obj(),
42 constructor,
43 rect.x(),
44 rect.y(),
45 rect.right(),
46 rect.bottom()));
47
48 DCHECK(!rect_object.is_null());
49 return rect_object;
50 }
51
52 } // anonymous namespace
53
18 namespace chrome { 54 namespace chrome {
19 namespace android { 55 namespace android {
20 56
21 ChromeWebContentsDelegateAndroid::ChromeWebContentsDelegateAndroid(JNIEnv* env, 57 ChromeWebContentsDelegateAndroid::ChromeWebContentsDelegateAndroid(JNIEnv* env,
22 jobject obj) 58 jobject obj)
23 : WebContentsDelegateAndroid(env, obj) { 59 : WebContentsDelegateAndroid(env, obj) {
24 } 60 }
25 61
26 ChromeWebContentsDelegateAndroid::~ChromeWebContentsDelegateAndroid() { 62 ChromeWebContentsDelegateAndroid::~ChromeWebContentsDelegateAndroid() {
63 notification_registrar_.RemoveAll();
64 }
65
66 // Register native methods.
67 bool RegisterChromeWebContentsDelegateAndroid(JNIEnv* env) {
68 return RegisterNativesImpl(env);
27 } 69 }
28 70
29 void ChromeWebContentsDelegateAndroid::RunFileChooser( 71 void ChromeWebContentsDelegateAndroid::RunFileChooser(
30 WebContents* web_contents, 72 WebContents* web_contents,
31 const FileChooserParams& params) { 73 const FileChooserParams& params) {
32 FileSelectHelper::RunFileChooser(web_contents, params); 74 FileSelectHelper::RunFileChooser(web_contents, params);
33 } 75 }
34 76
77 void ChromeWebContentsDelegateAndroid::CloseContents(
78 WebContents* web_contents) {
79 // Prevent dangling registrations assigned to closed web contents.
80 if (notification_registrar_.IsRegistered(this,
81 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
82 content::Source<WebContents>(web_contents))) {
83 notification_registrar_.Remove(this,
84 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
85 content::Source<WebContents>(web_contents));
86 }
87
88 if (notification_registrar_.IsRegistered(this,
89 chrome::NOTIFICATION_FIND_MATCH_RECTS_AVAILABLE,
90 content::Source<WebContents>(web_contents))) {
91 notification_registrar_.Remove(this,
92 chrome::NOTIFICATION_FIND_MATCH_RECTS_AVAILABLE,
93 content::Source<WebContents>(web_contents));
94 }
95
96 WebContentsDelegateAndroid::CloseContents(web_contents);
97 }
98
99 void ChromeWebContentsDelegateAndroid::Observe(
100 int type,
101 const content::NotificationSource& source,
102 const content::NotificationDetails& details) {
103 switch (type) {
104 case chrome::NOTIFICATION_FIND_RESULT_AVAILABLE:
105 OnFindResultAvailable(content::Source<WebContents>(source).ptr(),
jam 2012/09/04 16:39:09 nit: this indentation isn't according to style gui
Leandro Graciá Gil 2012/09/04 18:25:50 Done.
106 content::Details<FindNotificationDetails>(details).ptr());
107 break;
108 case chrome::NOTIFICATION_FIND_MATCH_RECTS_AVAILABLE:
109 OnFindMatchRectsAvailable(content::Source<WebContents>(source).ptr(),
110 content::Details<FindMatchRectsDetails>(details).ptr());
111 break;
112 default:
113 NOTREACHED() << "Unexpected notification: " << type;
114 break;
115 }
116 }
117
118 void ChromeWebContentsDelegateAndroid::FindReply(
119 WebContents* web_contents,
120 int request_id,
121 int number_of_matches,
122 const gfx::Rect& selection_rect,
123 int active_match_ordinal,
124 bool final_update) {
125 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
126 if (!tab_contents || !tab_contents->find_tab_helper())
jam 2012/09/04 16:39:09 will this condition really happen?
Leandro Graciá Gil 2012/09/04 18:25:50 Good catch. We actually removed some of these chec
127 return;
128
129 if (!notification_registrar_.IsRegistered(this,
130 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
131 content::Source<WebContents>(web_contents))) {
132 notification_registrar_.Add(this,
133 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
134 content::Source<WebContents>(web_contents));
135 }
136
137 tab_contents->find_tab_helper()->HandleFindReply(request_id,
138 number_of_matches,
139 selection_rect,
140 active_match_ordinal,
141 final_update);
142 }
143
144 void ChromeWebContentsDelegateAndroid::OnFindResultAvailable(
145 WebContents* web_contents,
146 const FindNotificationDetails* find_result) {
147 DCHECK(web_contents);
jam 2012/09/04 16:39:09 please get rid of these dchecks. if they're null,
Leandro Graciá Gil 2012/09/04 18:25:50 Done.
148 DCHECK(find_result);
149
150 JNIEnv* env = AttachCurrentThread();
151 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
152 if (obj.is_null())
153 return;
154
155 // Create the selection rect.
156 ScopedJavaLocalRef<jclass> rect_clazz =
157 GetClass(env, "android/graphics/Rect");
158
159 jmethodID rect_constructor =
160 GetMethodID(env, rect_clazz, "<init>", "(IIII)V");
161
162 ScopedJavaLocalRef<jobject> selection_rect = CreateAndroidRect(
163 env, rect_clazz, rect_constructor, find_result->selection_rect());
164
165 // Create the details object.
166 ScopedJavaLocalRef<jclass> details_clazz =
167 GetClass(env, "org/chromium/chrome/browser/FindNotificationDetails");
168
169 jmethodID details_constructor = GetMethodID(env, details_clazz, "<init>",
170 "(ILandroid/graphics/Rect;IZ)V");
171
172 ScopedJavaLocalRef<jobject> details_object(
173 env,
174 env->NewObject(details_clazz.obj(),
175 details_constructor,
176 find_result->number_of_matches(),
177 selection_rect.obj(),
178 find_result->active_match_ordinal(),
179 find_result->final_update()));
180 DCHECK(!details_object.is_null());
181
182 Java_ChromeWebContentsDelegateAndroid_onFindResultAvailable(
183 env,
184 obj.obj(),
185 details_object.obj());
186 }
187
188 void ChromeWebContentsDelegateAndroid::GetFindMatchRectsReply(
189 WebContents* web_contents,
190 int version,
191 const std::vector<gfx::RectF>& rects,
192 const gfx::RectF& active_rect) {
193 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
194 if (!tab_contents || !tab_contents->find_tab_helper())
195 return;
196
197 if (!notification_registrar_.IsRegistered(this,
198 chrome::NOTIFICATION_FIND_MATCH_RECTS_AVAILABLE,
199 content::Source<WebContents>(web_contents))) {
200 notification_registrar_.Add(this,
201 chrome::NOTIFICATION_FIND_MATCH_RECTS_AVAILABLE,
202 content::Source<WebContents>(web_contents));
203 }
204
205 tab_contents->find_tab_helper()->HandleGetFindMatchRectsReply(
206 version, rects, active_rect);
207 }
208
209 void ChromeWebContentsDelegateAndroid::OnFindMatchRectsAvailable(
210 WebContents* web_contents,
211 const FindMatchRectsDetails* match_rects) {
212 DCHECK(web_contents);
213 DCHECK(match_rects);
214
215 JNIEnv* env = AttachCurrentThread();
216 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
217 if (obj.is_null())
218 return;
219
220 // Create the rects.
221 ScopedJavaLocalRef<jclass> rect_clazz =
222 GetClass(env, "android/graphics/RectF");
223
224 jmethodID rect_constructor =
225 GetMethodID(env, rect_clazz, "<init>", "(FFFF)V");
226
227 ScopedJavaLocalRef<jobjectArray> rects(env, env->NewObjectArray(
228 match_rects->rects().size(), rect_clazz.obj(), NULL));
229
230 for (size_t i = 0; i < match_rects->rects().size(); ++i) {
231 env->SetObjectArrayElement(
232 rects.obj(), i,
233 CreateAndroidRect(env,
234 rect_clazz,
235 rect_constructor,
236 match_rects->rects()[i]).obj());
237 }
238
239 ScopedJavaLocalRef<jobject> active_rect = CreateAndroidRect(
240 env, rect_clazz, rect_constructor, match_rects->active_rect());
241
242 // Create the details object.
243 ScopedJavaLocalRef<jclass> details_clazz =
244 GetClass(env, "org/chromium/chrome/browser/FindMatchRectsDetails");
245
246 jmethodID details_constructor = GetMethodID(env, details_clazz, "<init>",
247 "(I[Landroid/graphics/RectF;Landroid/graphics/RectF;)V");
248
249 ScopedJavaLocalRef<jobject> details_object(
250 env,
251 env->NewObject(details_clazz.obj(),
252 details_constructor,
253 match_rects->version(),
254 rects.obj(),
255 active_rect.obj()));
256 DCHECK(!details_object.is_null());
257
258 Java_ChromeWebContentsDelegateAndroid_onFindMatchRectsAvailable(
259 env,
260 obj.obj(),
261 details_object.obj());
262 }
263
35 } // namespace android 264 } // namespace android
36 } // namespace chrome 265 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698