| 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 "android_webview/native/aw_contents_io_thread_client.h" | 11 #include "android_webview/native/aw_contents_io_thread_client.h" |
| 12 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
| 13 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/supports_user_data.h" | 16 #include "base/supports_user_data.h" |
| 17 #include "content/public/browser/android/content_view_core.h" | 17 #include "content/public/browser/android/content_view_core.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 20 #include "jni/AwContents_jni.h" | 20 #include "jni/AwContents_jni.h" |
| 21 | 21 |
| 22 using base::android::AttachCurrentThread; | 22 using base::android::AttachCurrentThread; |
| 23 using base::android::ConvertJavaStringToUTF16; |
| 23 using base::android::ConvertUTF8ToJavaString; | 24 using base::android::ConvertUTF8ToJavaString; |
| 24 using base::android::ConvertUTF16ToJavaString; | 25 using base::android::ConvertUTF16ToJavaString; |
| 25 using base::android::ScopedJavaGlobalRef; | 26 using base::android::ScopedJavaGlobalRef; |
| 26 using base::android::ScopedJavaLocalRef; | 27 using base::android::ScopedJavaLocalRef; |
| 27 using content::BrowserThread; | 28 using content::BrowserThread; |
| 28 using content::ContentViewCore; | 29 using content::ContentViewCore; |
| 29 using content::WebContents; | 30 using content::WebContents; |
| 30 | 31 |
| 31 namespace android_webview { | 32 namespace android_webview { |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 const void* kAwContentsUserDataKey = &kAwContentsUserDataKey; | 36 const void* kAwContentsUserDataKey = &kAwContentsUserDataKey; |
| 36 | 37 |
| 37 class AwContentsUserData : public base::SupportsUserData::Data { | 38 class AwContentsUserData : public base::SupportsUserData::Data { |
| 38 public: | 39 public: |
| 39 AwContentsUserData(AwContents* ptr) : contents_(ptr) {} | 40 AwContentsUserData(AwContents* ptr) : contents_(ptr) {} |
| 40 AwContents* get() { return contents_; } | 41 |
| 42 static AwContents* GetContents(WebContents* web_contents) { |
| 43 if (!web_contents) |
| 44 return NULL; |
| 45 AwContentsUserData* data = reinterpret_cast<AwContentsUserData*>( |
| 46 web_contents->GetUserData(kAwContentsUserDataKey)); |
| 47 return data ? data->contents_ : NULL; |
| 48 } |
| 41 | 49 |
| 42 private: | 50 private: |
| 43 AwContents* contents_; | 51 AwContents* contents_; |
| 44 }; | 52 }; |
| 45 | 53 |
| 46 } // namespace | 54 } // namespace |
| 47 | 55 |
| 48 // static | 56 // static |
| 49 AwContents* AwContents::FromWebContents(content::WebContents* web_contents) { | 57 AwContents* AwContents::FromWebContents(WebContents* web_contents) { |
| 50 if (web_contents) { | 58 return AwContentsUserData::GetContents(web_contents); |
| 51 AwContentsUserData* data = reinterpret_cast<AwContentsUserData*>( | |
| 52 web_contents->GetUserData(kAwContentsUserDataKey)); | |
| 53 if (data) return data->get(); | |
| 54 } | |
| 55 return NULL; | |
| 56 } | 59 } |
| 57 | 60 |
| 58 AwContents::AwContents(JNIEnv* env, | 61 AwContents::AwContents(JNIEnv* env, |
| 59 jobject obj, | 62 jobject obj, |
| 60 jobject web_contents_delegate, | 63 jobject web_contents_delegate, |
| 61 bool private_browsing) | 64 bool private_browsing) |
| 62 : java_ref_(env, obj), | 65 : java_ref_(env, obj), |
| 63 web_contents_delegate_( | 66 web_contents_delegate_( |
| 64 new AwWebContentsDelegate(env, web_contents_delegate)) { | 67 new AwWebContentsDelegate(env, web_contents_delegate)) { |
| 65 android_webview::AwBrowserDependencyFactory* dependency_factory = | 68 android_webview::AwBrowserDependencyFactory* dependency_factory = |
| 66 android_webview::AwBrowserDependencyFactory::GetInstance(); | 69 android_webview::AwBrowserDependencyFactory::GetInstance(); |
| 67 content::WebContents* web_contents = | 70 |
| 71 WebContents* web_contents = |
| 68 dependency_factory->CreateWebContents(private_browsing); | 72 dependency_factory->CreateWebContents(private_browsing); |
| 73 |
| 74 DCHECK(!AwContents::FromWebContents(web_contents)); |
| 75 web_contents->SetUserData(kAwContentsUserDataKey, |
| 76 new AwContentsUserData(this)); |
| 77 |
| 69 contents_container_.reset(dependency_factory->CreateContentsContainer( | 78 contents_container_.reset(dependency_factory->CreateContentsContainer( |
| 70 web_contents)); | 79 web_contents)); |
| 71 web_contents->SetDelegate(web_contents_delegate_.get()); | 80 web_contents->SetDelegate(web_contents_delegate_.get()); |
| 72 web_contents_delegate_->SetJavaScriptDialogCreator( | 81 web_contents_delegate_->SetJavaScriptDialogCreator( |
| 73 dependency_factory->GetJavaScriptDialogCreator()); | 82 dependency_factory->GetJavaScriptDialogCreator()); |
| 74 web_contents->SetUserData(kAwContentsUserDataKey, | |
| 75 new AwContentsUserData(this)); | |
| 76 render_view_host_ext_.reset(new AwRenderViewHostExt(web_contents)); | 83 render_view_host_ext_.reset(new AwRenderViewHostExt(web_contents)); |
| 77 } | 84 } |
| 78 | 85 |
| 79 AwContents::~AwContents() { | 86 AwContents::~AwContents() { |
| 80 content::WebContents* web_contents = contents_container_->GetWebContents(); | 87 WebContents* web_contents = contents_container_->GetWebContents(); |
| 81 DCHECK(AwContents::FromWebContents(web_contents) == this); | 88 DCHECK(AwContents::FromWebContents(web_contents) == this); |
| 82 web_contents->RemoveUserData(kAwContentsUserDataKey); | 89 web_contents->RemoveUserData(kAwContentsUserDataKey); |
| 90 if (find_helper_.get()) |
| 91 find_helper_->SetListener(NULL); |
| 83 } | 92 } |
| 84 | 93 |
| 85 jint AwContents::GetWebContents(JNIEnv* env, jobject obj) { | 94 jint AwContents::GetWebContents(JNIEnv* env, jobject obj) { |
| 86 return reinterpret_cast<jint>(contents_container_->GetWebContents()); | 95 return reinterpret_cast<jint>(contents_container_->GetWebContents()); |
| 87 } | 96 } |
| 88 | 97 |
| 89 void AwContents::Destroy(JNIEnv* env, jobject obj) { | 98 void AwContents::Destroy(JNIEnv* env, jobject obj) { |
| 90 delete this; | 99 delete this; |
| 91 } | 100 } |
| 92 | 101 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 jboolean private_browsing) { | 163 jboolean private_browsing) { |
| 155 AwContents* tab = new AwContents(env, obj, web_contents_delegate, | 164 AwContents* tab = new AwContents(env, obj, web_contents_delegate, |
| 156 private_browsing); | 165 private_browsing); |
| 157 return reinterpret_cast<jint>(tab); | 166 return reinterpret_cast<jint>(tab); |
| 158 } | 167 } |
| 159 | 168 |
| 160 bool RegisterAwContents(JNIEnv* env) { | 169 bool RegisterAwContents(JNIEnv* env) { |
| 161 return RegisterNativesImpl(env) >= 0; | 170 return RegisterNativesImpl(env) >= 0; |
| 162 } | 171 } |
| 163 | 172 |
| 173 jint AwContents::FindAllSync(JNIEnv* env, jobject obj, jstring search_string) { |
| 174 return GetFindHelper()->FindAllSync( |
| 175 ConvertJavaStringToUTF16(env, search_string)); |
| 176 } |
| 177 |
| 178 void AwContents::FindAllAsync(JNIEnv* env, jobject obj, jstring search_string) { |
| 179 GetFindHelper()->FindAllAsync(ConvertJavaStringToUTF16(env, search_string)); |
| 180 } |
| 181 |
| 182 void AwContents::FindNext(JNIEnv* env, jobject obj, jboolean forward) { |
| 183 GetFindHelper()->FindNext(forward); |
| 184 } |
| 185 |
| 186 void AwContents::ClearMatches(JNIEnv* env, jobject obj) { |
| 187 GetFindHelper()->ClearMatches(); |
| 188 } |
| 189 |
| 190 FindHelper* AwContents::GetFindHelper() { |
| 191 if (!find_helper_.get()) { |
| 192 WebContents* web_contents = contents_container_->GetWebContents(); |
| 193 find_helper_.reset(new FindHelper(web_contents)); |
| 194 find_helper_->SetListener(this); |
| 195 } |
| 196 return find_helper_.get(); |
| 197 } |
| 198 |
| 199 void AwContents::OnFindResultReceived(int active_ordinal, |
| 200 int match_count, |
| 201 bool finished) { |
| 202 JNIEnv* env = AttachCurrentThread(); |
| 203 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 204 if (obj.is_null()) |
| 205 return; |
| 206 |
| 207 Java_AwContents_onFindResultReceived( |
| 208 env, obj.obj(), active_ordinal, match_count, finished); |
| 209 } |
| 164 | 210 |
| 165 } // namespace android_webview | 211 } // namespace android_webview |
| OLD | NEW |