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/native/aw_browser_dependency_factory.h" | 7 #include "android_webview/native/aw_browser_dependency_factory.h" |
8 #include "android_webview/native/aw_contents_container.h" | 8 #include "android_webview/native/aw_contents_container.h" |
9 #include "android_webview/native/aw_web_contents_delegate.h" | 9 #include "android_webview/native/aw_web_contents_delegate.h" |
10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/supports_user_data.h" |
12 #include "content/public/browser/android/content_view_core.h" | 13 #include "content/public/browser/android/content_view_core.h" |
13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
14 #include "jni/AwContents_jni.h" | 15 #include "jni/AwContents_jni.h" |
15 | 16 |
16 using base::android::AttachCurrentThread; | 17 using base::android::AttachCurrentThread; |
17 using base::android::ConvertUTF16ToJavaString; | 18 using base::android::ConvertUTF16ToJavaString; |
18 using content::ContentViewCore; | 19 using content::ContentViewCore; |
19 using content::WebContents; | 20 using content::WebContents; |
20 | 21 |
21 namespace android_webview { | 22 namespace android_webview { |
22 | 23 |
| 24 namespace { |
| 25 |
| 26 const void* kAwContentsUserDataKey = &kAwContentsUserDataKey; |
| 27 |
| 28 class AwContentsUserData : public base::SupportsUserData::Data { |
| 29 public: |
| 30 AwContentsUserData(AwContents* ptr) : contents_(ptr) {} |
| 31 AwContents* get() { return contents_; } |
| 32 |
| 33 private: |
| 34 AwContents* contents_; |
| 35 }; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 // static |
| 40 AwContents* AwContents::FromWebContents(content::WebContents* web_contents) { |
| 41 if (web_contents) { |
| 42 AwContentsUserData* data = reinterpret_cast<AwContentsUserData*>( |
| 43 web_contents->GetUserData(kAwContentsUserDataKey)); |
| 44 if (data) return data->get(); |
| 45 } |
| 46 return NULL; |
| 47 } |
| 48 |
23 AwContents::AwContents(JNIEnv* env, | 49 AwContents::AwContents(JNIEnv* env, |
24 jobject obj, | 50 jobject obj, |
25 jobject web_contents_delegate, | 51 jobject web_contents_delegate, |
26 bool private_browsing) | 52 bool private_browsing) |
27 : java_ref_(env, obj), | 53 : java_ref_(env, obj), |
28 web_contents_delegate_( | 54 web_contents_delegate_( |
29 new AwWebContentsDelegate(env, web_contents_delegate)) { | 55 new AwWebContentsDelegate(env, web_contents_delegate)) { |
30 android_webview::AwBrowserDependencyFactory* dependency_factory = | 56 android_webview::AwBrowserDependencyFactory* dependency_factory = |
31 android_webview::AwBrowserDependencyFactory::GetInstance(); | 57 android_webview::AwBrowserDependencyFactory::GetInstance(); |
32 content::WebContents* web_contents = | 58 content::WebContents* web_contents = |
33 dependency_factory->CreateWebContents(private_browsing); | 59 dependency_factory->CreateWebContents(private_browsing); |
34 contents_container_.reset(dependency_factory->CreateContentsContainer( | 60 contents_container_.reset(dependency_factory->CreateContentsContainer( |
35 web_contents)); | 61 web_contents)); |
36 web_contents->SetDelegate(web_contents_delegate_.get()); | 62 web_contents->SetDelegate(web_contents_delegate_.get()); |
37 web_contents_delegate_->SetJavaScriptDialogCreator( | 63 web_contents_delegate_->SetJavaScriptDialogCreator( |
38 dependency_factory->GetJavaScriptDialogCreator()); | 64 dependency_factory->GetJavaScriptDialogCreator()); |
| 65 web_contents->SetUserData(kAwContentsUserDataKey, |
| 66 new AwContentsUserData(this)); |
39 } | 67 } |
40 | 68 |
41 AwContents::~AwContents() { | 69 AwContents::~AwContents() { |
| 70 content::WebContents* web_contents = contents_container_->GetWebContents(); |
| 71 DCHECK(AwContents::FromWebContents(web_contents) == this); |
| 72 web_contents->RemoveUserData(kAwContentsUserDataKey); |
42 } | 73 } |
43 | 74 |
44 jint AwContents::GetWebContents(JNIEnv* env, jobject obj) { | 75 jint AwContents::GetWebContents(JNIEnv* env, jobject obj) { |
45 return reinterpret_cast<jint>(contents_container_->GetWebContents()); | 76 return reinterpret_cast<jint>(contents_container_->GetWebContents()); |
46 } | 77 } |
47 | 78 |
48 void AwContents::Destroy(JNIEnv* env, jobject obj) { | 79 void AwContents::Destroy(JNIEnv* env, jobject obj) { |
49 delete this; | 80 delete this; |
50 } | 81 } |
51 | 82 |
52 static jint Init(JNIEnv* env, | 83 static jint Init(JNIEnv* env, |
53 jobject obj, | 84 jobject obj, |
54 jobject web_contents_delegate, | 85 jobject web_contents_delegate, |
55 jboolean private_browsing) { | 86 jboolean private_browsing) { |
56 AwContents* tab = new AwContents(env, obj, web_contents_delegate, | 87 AwContents* tab = new AwContents(env, obj, web_contents_delegate, |
57 private_browsing); | 88 private_browsing); |
58 return reinterpret_cast<jint>(tab); | 89 return reinterpret_cast<jint>(tab); |
59 } | 90 } |
60 | 91 |
61 bool RegisterAwContents(JNIEnv* env) { | 92 bool RegisterAwContents(JNIEnv* env) { |
62 return RegisterNativesImpl(env) >= 0; | 93 return RegisterNativesImpl(env) >= 0; |
63 } | 94 } |
64 | 95 |
65 | 96 |
66 } // namespace android_webview | 97 } // namespace android_webview |
OLD | NEW |