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

Side by Side Diff: content/browser/android/content_view_core_impl.cc

Issue 9192008: Hook up ContentViewCore.add/removeJavascriptInterface() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Modify ContentViewCoreImpl to use WebContentsImpl Created 8 years, 4 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 "content/browser/android/content_view_core_impl.h" 5 #include "content/browser/android/content_view_core_impl.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h" 8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h" 9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h" 10 #include "base/android/scoped_java_ref.h"
11 #include "content/browser/android/content_view_client.h" 11 #include "content/browser/android/content_view_client.h"
12 #include "content/browser/renderer_host/java/java_bound_object.h"
13 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager .h"
12 #include "content/browser/renderer_host/render_view_host_impl.h" 14 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/web_contents/navigation_controller_impl.h" 15 #include "content/browser/web_contents/navigation_controller_impl.h"
14 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/web_contents.h" 17 #include "content/public/browser/web_contents.h"
16 #include "jni/ContentViewCore_jni.h" 18 #include "jni/ContentViewCore_jni.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
17 #include "webkit/glue/webmenuitem.h" 20 #include "webkit/glue/webmenuitem.h"
18 21
19 using base::android::AttachCurrentThread; 22 using base::android::AttachCurrentThread;
20 using base::android::ConvertUTF16ToJavaString; 23 using base::android::ConvertUTF16ToJavaString;
21 using base::android::ConvertUTF8ToJavaString; 24 using base::android::ConvertUTF8ToJavaString;
22 using base::android::GetClass; 25 using base::android::GetClass;
26 using base::android::ConvertJavaStringToUTF16;
Ted C 2012/07/27 18:09:04 move under AttachCurrentThread
Steve Block 2012/07/30 11:45:14 Done.
23 using base::android::HasField; 27 using base::android::HasField;
24 using base::android::ScopedJavaGlobalRef; 28 using base::android::ScopedJavaGlobalRef;
25 using base::android::ScopedJavaLocalRef; 29 using base::android::ScopedJavaLocalRef;
26 30
27 // Describes the type and enabled state of a select popup item. 31 // Describes the type and enabled state of a select popup item.
28 // Keep in sync with the value defined in SelectPopupDialog.java 32 // Keep in sync with the value defined in SelectPopupDialog.java
29 enum PopupItemType { 33 enum PopupItemType {
30 POPUP_ITEM_TYPE_GROUP = 0, 34 POPUP_ITEM_TYPE_GROUP = 0,
31 POPUP_ITEM_TYPE_DISABLED, 35 POPUP_ITEM_TYPE_DISABLED,
32 POPUP_ITEM_TYPE_ENABLED 36 POPUP_ITEM_TYPE_ENABLED
(...skipping 24 matching lines...) Expand all
57 ContentViewCore* ContentViewCore::GetNativeContentViewCore(JNIEnv* env, 61 ContentViewCore* ContentViewCore::GetNativeContentViewCore(JNIEnv* env,
58 jobject obj) { 62 jobject obj) {
59 return reinterpret_cast<ContentViewCore*>( 63 return reinterpret_cast<ContentViewCore*>(
60 env->GetIntField(obj, g_native_content_view)); 64 env->GetIntField(obj, g_native_content_view));
61 } 65 }
62 66
63 // ---------------------------------------------------------------------------- 67 // ----------------------------------------------------------------------------
64 68
65 ContentViewCoreImpl::ContentViewCoreImpl(JNIEnv* env, jobject obj, 69 ContentViewCoreImpl::ContentViewCoreImpl(JNIEnv* env, jobject obj,
66 WebContents* web_contents) 70 WebContents* web_contents)
67 : web_contents_(web_contents), 71 : web_contents_impl_(static_cast<WebContentsImpl*>(web_contents)),
jam 2012/07/27 23:29:57 nit: just keep it web_contents_, even if it is a W
Steve Block 2012/07/30 11:45:14 Done.
68 tab_crashed_(false) { 72 tab_crashed_(false) {
69 DCHECK(web_contents) << 73 DCHECK(web_contents) <<
70 "A ContentViewCoreImpl should be created with a valid WebContents."; 74 "A ContentViewCoreImpl should be created with a valid WebContents.";
71 75
72 InitJNI(env, obj); 76 InitJNI(env, obj);
73 } 77 }
74 78
75 ContentViewCoreImpl::~ContentViewCoreImpl() { 79 ContentViewCoreImpl::~ContentViewCoreImpl() {
76 if (java_object_) { 80 if (java_object_) {
77 JNIEnv* env = AttachCurrentThread(); 81 JNIEnv* env = AttachCurrentThread();
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 if (web_contents()->GetURL().is_empty() || !content_view_client_.get()) 143 if (web_contents()->GetURL().is_empty() || !content_view_client_.get())
140 return static_cast<jdouble>(1.0); 144 return static_cast<jdouble>(1.0);
141 return static_cast<jdouble>(content_view_client_->GetLoadProgress()); 145 return static_cast<jdouble>(content_view_client_->GetLoadProgress());
142 } 146 }
143 147
144 jboolean ContentViewCoreImpl::IsIncognito(JNIEnv* env, jobject obj) { 148 jboolean ContentViewCoreImpl::IsIncognito(JNIEnv* env, jobject obj) {
145 return web_contents()->GetBrowserContext()->IsOffTheRecord(); 149 return web_contents()->GetBrowserContext()->IsOffTheRecord();
146 } 150 }
147 151
148 jboolean ContentViewCoreImpl::CanGoBack(JNIEnv* env, jobject obj) { 152 jboolean ContentViewCoreImpl::CanGoBack(JNIEnv* env, jobject obj) {
149 return web_contents_->GetController().CanGoBack(); 153 return web_contents_impl_->GetController().CanGoBack();
150 } 154 }
151 155
152 jboolean ContentViewCoreImpl::CanGoForward(JNIEnv* env, jobject obj) { 156 jboolean ContentViewCoreImpl::CanGoForward(JNIEnv* env, jobject obj) {
153 return web_contents_->GetController().CanGoForward(); 157 return web_contents_impl_->GetController().CanGoForward();
154 } 158 }
155 159
156 jboolean ContentViewCoreImpl::CanGoToOffset(JNIEnv* env, jobject obj, 160 jboolean ContentViewCoreImpl::CanGoToOffset(JNIEnv* env, jobject obj,
157 jint offset) { 161 jint offset) {
158 return web_contents_->GetController().CanGoToOffset(offset); 162 return web_contents_impl_->GetController().CanGoToOffset(offset);
159 } 163 }
160 164
161 void ContentViewCoreImpl::GoBack(JNIEnv* env, jobject obj) { 165 void ContentViewCoreImpl::GoBack(JNIEnv* env, jobject obj) {
162 web_contents_->GetController().GoBack(); 166 web_contents_impl_->GetController().GoBack();
163 tab_crashed_ = false; 167 tab_crashed_ = false;
164 } 168 }
165 169
166 void ContentViewCoreImpl::GoForward(JNIEnv* env, jobject obj) { 170 void ContentViewCoreImpl::GoForward(JNIEnv* env, jobject obj) {
167 web_contents_->GetController().GoForward(); 171 web_contents_impl_->GetController().GoForward();
168 tab_crashed_ = false; 172 tab_crashed_ = false;
169 } 173 }
170 174
171 void ContentViewCoreImpl::GoToOffset(JNIEnv* env, jobject obj, jint offset) { 175 void ContentViewCoreImpl::GoToOffset(JNIEnv* env, jobject obj, jint offset) {
172 web_contents_->GetController().GoToOffset(offset); 176 web_contents_impl_->GetController().GoToOffset(offset);
173 } 177 }
174 178
175 void ContentViewCoreImpl::StopLoading(JNIEnv* env, jobject obj) { 179 void ContentViewCoreImpl::StopLoading(JNIEnv* env, jobject obj) {
176 web_contents_->Stop(); 180 web_contents_impl_->Stop();
177 } 181 }
178 182
179 void ContentViewCoreImpl::Reload(JNIEnv* env, jobject obj) { 183 void ContentViewCoreImpl::Reload(JNIEnv* env, jobject obj) {
180 // Set check_for_repost parameter to false as we have no repost confirmation 184 // Set check_for_repost parameter to false as we have no repost confirmation
181 // dialog ("confirm form resubmission" screen will still appear, however). 185 // dialog ("confirm form resubmission" screen will still appear, however).
182 web_contents_->GetController().Reload(false); 186 web_contents_impl_->GetController().Reload(false);
183 tab_crashed_ = false; 187 tab_crashed_ = false;
184 } 188 }
185 189
186 void ContentViewCoreImpl::ClearHistory(JNIEnv* env, jobject obj) { 190 void ContentViewCoreImpl::ClearHistory(JNIEnv* env, jobject obj) {
187 web_contents_->GetController().PruneAllButActive(); 191 web_contents_impl_->GetController().PruneAllButActive();
188 } 192 }
189 193
190 jboolean ContentViewCoreImpl::NeedsReload(JNIEnv* env, jobject obj) { 194 jboolean ContentViewCoreImpl::NeedsReload(JNIEnv* env, jobject obj) {
191 return web_contents_->GetController().NeedsReload(); 195 return web_contents_impl_->GetController().NeedsReload();
192 } 196 }
193 197
194 void ContentViewCoreImpl::SetClient(JNIEnv* env, jobject obj, jobject jclient) { 198 void ContentViewCoreImpl::SetClient(JNIEnv* env, jobject obj, jobject jclient) {
195 scoped_ptr<ContentViewClient> client( 199 scoped_ptr<ContentViewClient> client(
196 ContentViewClient::CreateNativeContentViewClient(env, jclient)); 200 ContentViewClient::CreateNativeContentViewClient(env, jclient));
197 201
198 web_contents_->SetDelegate(client.get()); 202 web_contents_impl_->SetDelegate(client.get());
199 203
200 content_view_client_.swap(client); 204 content_view_client_.swap(client);
201 } 205 }
202 206
203 // -------------------------------------------------------------------------- 207 // --------------------------------------------------------------------------
204 // Methods called from native code 208 // Methods called from native code
205 // -------------------------------------------------------------------------- 209 // --------------------------------------------------------------------------
206 210
207 void ContentViewCoreImpl::LoadUrl(const GURL& url, int page_transition) { 211 void ContentViewCoreImpl::LoadUrl(const GURL& url, int page_transition) {
208 content::Referrer referer; 212 content::Referrer referer;
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 jcontent_url.obj()); 340 jcontent_url.obj());
337 } 341 }
338 342
339 // -------------------------------------------------------------------------- 343 // --------------------------------------------------------------------------
340 // Methods called from Java via JNI 344 // Methods called from Java via JNI
341 // -------------------------------------------------------------------------- 345 // --------------------------------------------------------------------------
342 346
343 void ContentViewCoreImpl::SelectPopupMenuItems(JNIEnv* env, jobject obj, 347 void ContentViewCoreImpl::SelectPopupMenuItems(JNIEnv* env, jobject obj,
344 jintArray indices) { 348 jintArray indices) {
345 RenderViewHostImpl* rvhi = static_cast<RenderViewHostImpl*>( 349 RenderViewHostImpl* rvhi = static_cast<RenderViewHostImpl*>(
346 web_contents_->GetRenderViewHost()); 350 web_contents_impl_->GetRenderViewHost());
347 DCHECK(rvhi); 351 DCHECK(rvhi);
348 if (indices == NULL) { 352 if (indices == NULL) {
349 rvhi->DidCancelPopupMenu(); 353 rvhi->DidCancelPopupMenu();
350 return; 354 return;
351 } 355 }
352 356
353 int selected_count = env->GetArrayLength(indices); 357 int selected_count = env->GetArrayLength(indices);
354 std::vector<int> selected_indices; 358 std::vector<int> selected_indices;
355 jint* indices_ptr = env->GetIntArrayElements(indices, NULL); 359 jint* indices_ptr = env->GetIntArrayElements(indices, NULL);
356 for (int i = 0; i < selected_count; ++i) 360 for (int i = 0; i < selected_count; ++i)
357 selected_indices.push_back(indices_ptr[i]); 361 selected_indices.push_back(indices_ptr[i]);
358 env->ReleaseIntArrayElements(indices, indices_ptr, JNI_ABORT); 362 env->ReleaseIntArrayElements(indices, indices_ptr, JNI_ABORT);
359 rvhi->DidSelectPopupMenuItems(selected_indices); 363 rvhi->DidSelectPopupMenuItems(selected_indices);
360 } 364 }
361 365
366 void ContentViewCoreImpl::AddJavascriptInterface(
367 JNIEnv* env,
368 jobject /* obj */,
369 jobject object,
370 jstring name,
371 jboolean allow_inherited_methods) {
372 ScopedJavaLocalRef<jobject> scoped_object(env, object);
373 // JavaBoundObject creates the NPObject with a ref count of 1, and
374 // JavaBridgeDispatcherHostManager takes its own ref.
375 NPObject* bound_object = JavaBoundObject::Create(scoped_object,
376 allow_inherited_methods);
377 web_contents_impl_->java_bridge_dispatcher_host_manager()->AddNamedObject(
378 ConvertJavaStringToUTF16(env, name), bound_object);
379 WebKit::WebBindings::releaseObject(bound_object);
380 }
381
382 void ContentViewCoreImpl::RemoveJavascriptInterface(JNIEnv* env,
383 jobject /* obj */,
384 jstring name) {
385 web_contents_impl_->java_bridge_dispatcher_host_manager()->RemoveNamedObject(
386 ConvertJavaStringToUTF16(env, name));
387 }
388
362 // -------------------------------------------------------------------------- 389 // --------------------------------------------------------------------------
363 // Methods called from native code 390 // Methods called from native code
364 // -------------------------------------------------------------------------- 391 // --------------------------------------------------------------------------
365 392
366 gfx::Rect ContentViewCoreImpl::GetBounds() const { 393 gfx::Rect ContentViewCoreImpl::GetBounds() const {
367 NOTIMPLEMENTED() << "not upstreamed yet"; 394 NOTIMPLEMENTED() << "not upstreamed yet";
368 return gfx::Rect(); 395 return gfx::Rect();
369 } 396 }
370 397
371 // ---------------------------------------------------------------------------- 398 // ----------------------------------------------------------------------------
372 399
373 bool RegisterContentViewCore(JNIEnv* env) { 400 bool RegisterContentViewCore(JNIEnv* env) {
374 if (!base::android::HasClass(env, kContentViewCoreClassPath)) { 401 if (!base::android::HasClass(env, kContentViewCoreClassPath)) {
375 DLOG(ERROR) << "Unable to find class ContentViewCore!"; 402 DLOG(ERROR) << "Unable to find class ContentViewCore!";
376 return false; 403 return false;
377 } 404 }
378 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath); 405 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewCoreClassPath);
379 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { 406 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) {
380 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; 407 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!";
381 return false; 408 return false;
382 } 409 }
383 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); 410 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I");
384 411
385 return RegisterNativesImpl(env) >= 0; 412 return RegisterNativesImpl(env) >= 0;
386 } 413 }
387 414
388 } // namespace content 415 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698