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 "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 "base/json/json_writer.h" | |
11 #include "content/browser/android/content_view_client.h" | 12 #include "content/browser/android/content_view_client.h" |
12 #include "content/browser/android/touch_point.h" | 13 #include "content/browser/android/touch_point.h" |
13 #include "content/browser/renderer_host/java/java_bound_object.h" | 14 #include "content/browser/renderer_host/java/java_bound_object.h" |
14 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager .h" | 15 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager .h" |
15 #include "content/browser/renderer_host/render_view_host_impl.h" | 16 #include "content/browser/renderer_host/render_view_host_impl.h" |
16 #include "content/browser/renderer_host/render_widget_host_impl.h" | 17 #include "content/browser/renderer_host/render_widget_host_impl.h" |
17 #include "content/browser/renderer_host/render_widget_host_view_android.h" | 18 #include "content/browser/renderer_host/render_widget_host_view_android.h" |
18 #include "content/browser/web_contents/navigation_controller_impl.h" | 19 #include "content/browser/web_contents/navigation_controller_impl.h" |
19 #include "content/public/browser/browser_context.h" | 20 #include "content/public/browser/browser_context.h" |
20 #include "content/public/browser/interstitial_page.h" | 21 #include "content/public/browser/interstitial_page.h" |
22 #include "content/public/browser/notification_details.h" | |
23 #include "content/public/browser/notification_service.h" | |
24 #include "content/public/browser/notification_source.h" | |
25 #include "content/public/browser/notification_types.h" | |
21 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
22 #include "jni/ContentViewCore_jni.h" | 27 #include "jni/ContentViewCore_jni.h" |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/android/WebInputEvent Factory.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/android/WebInputEvent Factory.h" |
26 #include "webkit/glue/webmenuitem.h" | 31 #include "webkit/glue/webmenuitem.h" |
27 | 32 |
28 using base::android::AttachCurrentThread; | 33 using base::android::AttachCurrentThread; |
29 using base::android::ConvertJavaStringToUTF16; | 34 using base::android::ConvertJavaStringToUTF16; |
30 using base::android::ConvertUTF16ToJavaString; | 35 using base::android::ConvertUTF16ToJavaString; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 // ---------------------------------------------------------------------------- | 80 // ---------------------------------------------------------------------------- |
76 | 81 |
77 ContentViewCoreImpl::ContentViewCoreImpl(JNIEnv* env, jobject obj, | 82 ContentViewCoreImpl::ContentViewCoreImpl(JNIEnv* env, jobject obj, |
78 WebContents* web_contents) | 83 WebContents* web_contents) |
79 : web_contents_(static_cast<WebContentsImpl*>(web_contents)), | 84 : web_contents_(static_cast<WebContentsImpl*>(web_contents)), |
80 tab_crashed_(false) { | 85 tab_crashed_(false) { |
81 DCHECK(web_contents) << | 86 DCHECK(web_contents) << |
82 "A ContentViewCoreImpl should be created with a valid WebContents."; | 87 "A ContentViewCoreImpl should be created with a valid WebContents."; |
83 | 88 |
84 InitJNI(env, obj); | 89 InitJNI(env, obj); |
90 | |
91 notification_registrar_.Add(this, | |
92 NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, | |
93 NotificationService::AllSources()); | |
85 } | 94 } |
86 | 95 |
87 ContentViewCoreImpl::~ContentViewCoreImpl() { | 96 ContentViewCoreImpl::~ContentViewCoreImpl() { |
97 // Make sure nobody calls back into this object while we are tearing things | |
98 // down. | |
99 notification_registrar_.RemoveAll(); | |
100 | |
88 if (java_object_) { | 101 if (java_object_) { |
89 JNIEnv* env = AttachCurrentThread(); | 102 JNIEnv* env = AttachCurrentThread(); |
90 env->DeleteWeakGlobalRef(java_object_->obj); | 103 env->DeleteWeakGlobalRef(java_object_->obj); |
91 delete java_object_; | 104 delete java_object_; |
92 java_object_ = 0; | 105 java_object_ = 0; |
93 } | 106 } |
94 } | 107 } |
95 | 108 |
96 void ContentViewCoreImpl::Destroy(JNIEnv* env, jobject obj) { | 109 void ContentViewCoreImpl::Destroy(JNIEnv* env, jobject obj) { |
97 delete this; | 110 delete this; |
98 } | 111 } |
99 | 112 |
100 void ContentViewCoreImpl::Observe(int type, | 113 void ContentViewCoreImpl::Observe(int type, |
101 const NotificationSource& source, | 114 const NotificationSource& source, |
102 const NotificationDetails& details) { | 115 const NotificationDetails& details) { |
116 switch (type) { | |
117 case NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT: { | |
118 if (!web_contents_ || Source<RenderViewHost>(source).ptr() != | |
119 web_contents_->GetRenderViewHost()) { | |
120 return; | |
121 } | |
122 | |
123 JNIEnv* env = base::android::AttachCurrentThread(); | |
124 std::pair<int, Value*>* result_pair = | |
125 Details<std::pair<int, Value*> >(details).ptr(); | |
126 std::string json; | |
127 base::JSONWriter::Write(result_pair->second, &json); | |
128 ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, | |
129 json); | |
130 Java_ContentViewCore_onEvaluateJavaScriptResult(env, | |
131 java_object_->View(env).obj(), | |
132 static_cast<jint>(result_pair->first), j_json.obj()); | |
133 break; | |
134 } | |
135 } | |
136 | |
103 // TODO(jrg) | 137 // TODO(jrg) |
104 } | 138 } |
105 | 139 |
106 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) { | 140 void ContentViewCoreImpl::InitJNI(JNIEnv* env, jobject obj) { |
107 java_object_ = new JavaObject; | 141 java_object_ = new JavaObject; |
108 java_object_->obj = env->NewWeakGlobalRef(obj); | 142 java_object_->obj = env->NewWeakGlobalRef(obj); |
109 } | 143 } |
110 | 144 |
111 RenderWidgetHostViewAndroid* | 145 RenderWidgetHostViewAndroid* |
112 ContentViewCoreImpl::GetRenderWidgetHostViewAndroid() { | 146 ContentViewCoreImpl::GetRenderWidgetHostViewAndroid() { |
(...skipping 19 matching lines...) Expand all Loading... | |
132 | 166 |
133 int selected_count = env->GetArrayLength(indices); | 167 int selected_count = env->GetArrayLength(indices); |
134 std::vector<int> selected_indices; | 168 std::vector<int> selected_indices; |
135 jint* indices_ptr = env->GetIntArrayElements(indices, NULL); | 169 jint* indices_ptr = env->GetIntArrayElements(indices, NULL); |
136 for (int i = 0; i < selected_count; ++i) | 170 for (int i = 0; i < selected_count; ++i) |
137 selected_indices.push_back(indices_ptr[i]); | 171 selected_indices.push_back(indices_ptr[i]); |
138 env->ReleaseIntArrayElements(indices, indices_ptr, JNI_ABORT); | 172 env->ReleaseIntArrayElements(indices, indices_ptr, JNI_ABORT); |
139 rvhi->DidSelectPopupMenuItems(selected_indices); | 173 rvhi->DidSelectPopupMenuItems(selected_indices); |
140 } | 174 } |
141 | 175 |
176 jint ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env, jobject obj, | |
177 jstring script) { | |
178 RenderViewHost* host = web_contents_->GetRenderViewHost(); | |
179 DCHECK(host); | |
jam
2012/07/31 18:20:44
nit: unnecessary, if null will crash right below
David Trainor- moved to gerrit
2012/07/31 20:38:47
Done.
| |
180 | |
181 string16 script_utf16 = ConvertJavaStringToUTF16(env, script); | |
182 return host->ExecuteJavascriptInWebFrameNotifyResult(string16(), | |
183 script_utf16); | |
184 } | |
185 | |
142 void ContentViewCoreImpl::LoadUrlWithoutUrlSanitization(JNIEnv* env, | 186 void ContentViewCoreImpl::LoadUrlWithoutUrlSanitization(JNIEnv* env, |
143 jobject, | 187 jobject, |
144 jstring jurl, | 188 jstring jurl, |
145 int page_transition) { | 189 int page_transition) { |
146 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl)); | 190 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl)); |
147 | 191 |
148 LoadUrl(url, page_transition); | 192 LoadUrl(url, page_transition); |
149 } | 193 } |
150 | 194 |
151 void ContentViewCoreImpl::LoadUrlWithoutUrlSanitizationWithUserAgentOverride( | 195 void ContentViewCoreImpl::LoadUrlWithoutUrlSanitizationWithUserAgentOverride( |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
394 // Native JNI methods | 438 // Native JNI methods |
395 // ---------------------------------------------------------------------------- | 439 // ---------------------------------------------------------------------------- |
396 | 440 |
397 // This is called for each ContentViewCore. | 441 // This is called for each ContentViewCore. |
398 jint Init(JNIEnv* env, jobject obj, jint native_web_contents) { | 442 jint Init(JNIEnv* env, jobject obj, jint native_web_contents) { |
399 ContentViewCore* view = ContentViewCore::Create( | 443 ContentViewCore* view = ContentViewCore::Create( |
400 env, obj, reinterpret_cast<WebContents*>(native_web_contents)); | 444 env, obj, reinterpret_cast<WebContents*>(native_web_contents)); |
401 return reinterpret_cast<jint>(view); | 445 return reinterpret_cast<jint>(view); |
402 } | 446 } |
403 | 447 |
448 jint EvaluateJavaScript(JNIEnv* env, jobject obj, jstring script) { | |
449 ContentViewCoreImpl* view = | |
450 (ContentViewCoreImpl*) ContentViewCore::GetNativeContentViewCore(env, | |
jam
2012/07/31 18:20:44
google style is to not use c style casts. so use s
David Trainor- moved to gerrit
2012/07/31 20:38:47
Done.
| |
451 obj); | |
452 DCHECK(view); | |
jam
2012/07/31 18:20:44
ditto
David Trainor- moved to gerrit
2012/07/31 20:38:47
Done.
| |
453 | |
454 return view->EvaluateJavaScript(env, obj, script); | |
455 } | |
456 | |
404 // -------------------------------------------------------------------------- | 457 // -------------------------------------------------------------------------- |
405 // Public methods that call to Java via JNI | 458 // Public methods that call to Java via JNI |
406 // -------------------------------------------------------------------------- | 459 // -------------------------------------------------------------------------- |
407 | 460 |
408 void ContentViewCoreImpl::OnTabCrashed(const base::ProcessHandle handle) { | 461 void ContentViewCoreImpl::OnTabCrashed(const base::ProcessHandle handle) { |
409 NOTIMPLEMENTED() << "not upstreamed yet"; | 462 NOTIMPLEMENTED() << "not upstreamed yet"; |
410 } | 463 } |
411 | 464 |
412 void ContentViewCoreImpl::SetTitle(const string16& title) { | 465 void ContentViewCoreImpl::SetTitle(const string16& title) { |
413 NOTIMPLEMENTED() << "not upstreamed yet"; | 466 NOTIMPLEMENTED() << "not upstreamed yet"; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { | 584 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { |
532 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; | 585 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; |
533 return false; | 586 return false; |
534 } | 587 } |
535 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); | 588 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); |
536 | 589 |
537 return RegisterNativesImpl(env) >= 0; | 590 return RegisterNativesImpl(env) >= 0; |
538 } | 591 } |
539 | 592 |
540 } // namespace content | 593 } // namespace content |
OLD | NEW |