| 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 "android_webview/native/aw_contents.h" | 5 #include "android_webview/native/aw_contents.h" |
| 6 | 6 |
| 7 #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h" | 7 #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h" |
| 8 #include "android_webview/native/aw_browser_dependency_factory.h" | 8 #include "android_webview/native/aw_browser_dependency_factory.h" |
| 9 #include "android_webview/native/aw_contents_container.h" | 9 #include "android_webview/native/aw_contents_container.h" |
| 10 #include "android_webview/native/aw_web_contents_delegate.h" | 10 #include "android_webview/native/aw_web_contents_delegate.h" |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/supports_user_data.h" | 15 #include "base/supports_user_data.h" |
| 16 #include "content/public/browser/android/content_view_core.h" | 16 #include "content/public/browser/android/content_view_core.h" |
| 17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 18 #include "jni/AwContents_jni.h" | 18 #include "jni/AwContents_jni.h" |
| 19 | 19 |
| 20 using base::android::AttachCurrentThread; | 20 using base::android::AttachCurrentThread; |
| 21 using base::android::ConvertJavaStringToUTF16; |
| 21 using base::android::ConvertUTF8ToJavaString; | 22 using base::android::ConvertUTF8ToJavaString; |
| 22 using base::android::ConvertUTF16ToJavaString; | 23 using base::android::ConvertUTF16ToJavaString; |
| 23 using base::android::ScopedJavaGlobalRef; | 24 using base::android::ScopedJavaGlobalRef; |
| 24 using base::android::ScopedJavaLocalRef; | 25 using base::android::ScopedJavaLocalRef; |
| 25 using content::ContentViewCore; | 26 using content::ContentViewCore; |
| 26 using content::WebContents; | 27 using content::WebContents; |
| 27 | 28 |
| 28 namespace android_webview { | 29 namespace android_webview { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 const void* kAwContentsUserDataKey = &kAwContentsUserDataKey; | 33 const void* kAwContentsUserDataKey = &kAwContentsUserDataKey; |
| 33 | 34 |
| 34 class AwContentsUserData : public base::SupportsUserData::Data { | 35 class AwContentsUserData : public base::SupportsUserData::Data { |
| 35 public: | 36 public: |
| 36 AwContentsUserData(AwContents* ptr) : contents_(ptr) {} | 37 AwContentsUserData(AwContents* ptr) : contents_(ptr) {} |
| 37 AwContents* get() { return contents_; } | 38 |
| 39 static AwContents* GetContents(WebContents* web_contents) { |
| 40 if (!web_contents) |
| 41 return NULL; |
| 42 AwContentsUserData* data = reinterpret_cast<AwContentsUserData*>( |
| 43 web_contents->GetUserData(kAwContentsUserDataKey)); |
| 44 return data ? data->contents_ : NULL; |
| 45 } |
| 38 | 46 |
| 39 private: | 47 private: |
| 40 AwContents* contents_; | 48 AwContents* contents_; |
| 41 }; | 49 }; |
| 42 | 50 |
| 43 } // namespace | 51 } // namespace |
| 44 | 52 |
| 45 // static | 53 // static |
| 46 AwContents* AwContents::FromWebContents(content::WebContents* web_contents) { | 54 AwContents* AwContents::FromWebContents(WebContents* web_contents) { |
| 47 if (web_contents) { | 55 return AwContentsUserData::GetContents(web_contents); |
| 48 AwContentsUserData* data = reinterpret_cast<AwContentsUserData*>( | |
| 49 web_contents->GetUserData(kAwContentsUserDataKey)); | |
| 50 if (data) return data->get(); | |
| 51 } | |
| 52 return NULL; | |
| 53 } | 56 } |
| 54 | 57 |
| 55 AwContents::AwContents(JNIEnv* env, | 58 AwContents::AwContents(JNIEnv* env, |
| 56 jobject obj, | 59 jobject obj, |
| 57 jobject web_contents_delegate, | 60 jobject web_contents_delegate, |
| 58 bool private_browsing) | 61 bool private_browsing) |
| 59 : java_ref_(env, obj), | 62 : java_ref_(env, obj), |
| 60 web_contents_delegate_( | 63 web_contents_delegate_( |
| 61 new AwWebContentsDelegate(env, web_contents_delegate)) { | 64 new AwWebContentsDelegate(env, web_contents_delegate)) { |
| 62 android_webview::AwBrowserDependencyFactory* dependency_factory = | 65 android_webview::AwBrowserDependencyFactory* dependency_factory = |
| 63 android_webview::AwBrowserDependencyFactory::GetInstance(); | 66 android_webview::AwBrowserDependencyFactory::GetInstance(); |
| 64 content::WebContents* web_contents = | 67 |
| 68 WebContents* web_contents = |
| 65 dependency_factory->CreateWebContents(private_browsing); | 69 dependency_factory->CreateWebContents(private_browsing); |
| 70 |
| 71 DCHECK(!AwContents::FromWebContents(web_contents)); |
| 72 web_contents->SetUserData(kAwContentsUserDataKey, |
| 73 new AwContentsUserData(this)); |
| 74 |
| 66 contents_container_.reset(dependency_factory->CreateContentsContainer( | 75 contents_container_.reset(dependency_factory->CreateContentsContainer( |
| 67 web_contents)); | 76 web_contents)); |
| 68 web_contents->SetDelegate(web_contents_delegate_.get()); | 77 web_contents->SetDelegate(web_contents_delegate_.get()); |
| 69 web_contents_delegate_->SetJavaScriptDialogCreator( | 78 web_contents_delegate_->SetJavaScriptDialogCreator( |
| 70 dependency_factory->GetJavaScriptDialogCreator()); | 79 dependency_factory->GetJavaScriptDialogCreator()); |
| 71 web_contents->SetUserData(kAwContentsUserDataKey, | |
| 72 new AwContentsUserData(this)); | |
| 73 render_view_host_ext_.reset(new AwRenderViewHostExt(web_contents)); | 80 render_view_host_ext_.reset(new AwRenderViewHostExt(web_contents)); |
| 74 } | 81 } |
| 75 | 82 |
| 76 AwContents::~AwContents() { | 83 AwContents::~AwContents() { |
| 77 content::WebContents* web_contents = contents_container_->GetWebContents(); | 84 WebContents* web_contents = contents_container_->GetWebContents(); |
| 78 DCHECK(AwContents::FromWebContents(web_contents) == this); | 85 DCHECK(AwContents::FromWebContents(web_contents) == this); |
| 79 web_contents->RemoveUserData(kAwContentsUserDataKey); | 86 web_contents->RemoveUserData(kAwContentsUserDataKey); |
| 87 if (find_helper_.get()) |
| 88 find_helper_->SetListener(NULL); |
| 80 } | 89 } |
| 81 | 90 |
| 82 jint AwContents::GetWebContents(JNIEnv* env, jobject obj) { | 91 jint AwContents::GetWebContents(JNIEnv* env, jobject obj) { |
| 83 return reinterpret_cast<jint>(contents_container_->GetWebContents()); | 92 return reinterpret_cast<jint>(contents_container_->GetWebContents()); |
| 84 } | 93 } |
| 85 | 94 |
| 86 void AwContents::Destroy(JNIEnv* env, jobject obj) { | 95 void AwContents::Destroy(JNIEnv* env, jobject obj) { |
| 87 delete this; | 96 delete this; |
| 88 } | 97 } |
| 89 | 98 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 jboolean private_browsing) { | 132 jboolean private_browsing) { |
| 124 AwContents* tab = new AwContents(env, obj, web_contents_delegate, | 133 AwContents* tab = new AwContents(env, obj, web_contents_delegate, |
| 125 private_browsing); | 134 private_browsing); |
| 126 return reinterpret_cast<jint>(tab); | 135 return reinterpret_cast<jint>(tab); |
| 127 } | 136 } |
| 128 | 137 |
| 129 bool RegisterAwContents(JNIEnv* env) { | 138 bool RegisterAwContents(JNIEnv* env) { |
| 130 return RegisterNativesImpl(env) >= 0; | 139 return RegisterNativesImpl(env) >= 0; |
| 131 } | 140 } |
| 132 | 141 |
| 142 jint AwContents::FindAllSync(JNIEnv* env, jobject obj, jstring search_string) { |
| 143 return GetFindHelper()->FindAllSync( |
| 144 ConvertJavaStringToUTF16(env, search_string)); |
| 145 } |
| 146 |
| 147 void AwContents::FindAllAsync(JNIEnv* env, jobject obj, jstring search_string) { |
| 148 GetFindHelper()->FindAllAsync(ConvertJavaStringToUTF16(env, search_string)); |
| 149 } |
| 150 |
| 151 void AwContents::FindNext(JNIEnv* env, jobject obj, jboolean forward) { |
| 152 GetFindHelper()->FindNext(forward); |
| 153 } |
| 154 |
| 155 void AwContents::ClearMatches(JNIEnv* env, jobject obj) { |
| 156 GetFindHelper()->ClearMatches(); |
| 157 } |
| 158 |
| 159 FindHelper* AwContents::GetFindHelper() { |
| 160 if (!find_helper_.get()) { |
| 161 WebContents* web_contents = contents_container_->GetWebContents(); |
| 162 find_helper_.reset(new FindHelper(web_contents)); |
| 163 find_helper_->SetListener(this); |
| 164 } |
| 165 return find_helper_.get(); |
| 166 } |
| 167 |
| 168 void AwContents::OnFindResultReceived(int active_ordinal, |
| 169 int match_count, |
| 170 bool finished) { |
| 171 JNIEnv* env = AttachCurrentThread(); |
| 172 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 173 if (obj.is_null()) |
| 174 return; |
| 175 |
| 176 Java_AwContents_onFindResultReceived( |
| 177 env, obj.obj(), active_ordinal, match_count, finished); |
| 178 } |
| 133 | 179 |
| 134 } // namespace android_webview | 180 } // namespace android_webview |
| OLD | NEW |