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

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

Issue 11067002: Move ContentViewCore native ownership to WebContents (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: jcivelli feedback Created 8 years, 2 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
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"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 using WebKit::WebInputEventFactory; 58 using WebKit::WebInputEventFactory;
59 59
60 // Describes the type and enabled state of a select popup item. 60 // Describes the type and enabled state of a select popup item.
61 // Keep in sync with the value defined in SelectPopupDialog.java 61 // Keep in sync with the value defined in SelectPopupDialog.java
62 enum PopupItemType { 62 enum PopupItemType {
63 POPUP_ITEM_TYPE_GROUP = 0, 63 POPUP_ITEM_TYPE_GROUP = 0,
64 POPUP_ITEM_TYPE_DISABLED, 64 POPUP_ITEM_TYPE_DISABLED,
65 POPUP_ITEM_TYPE_ENABLED 65 POPUP_ITEM_TYPE_ENABLED
66 }; 66 };
67 67
68 namespace content {
69
68 namespace { 70 namespace {
69 jfieldID g_native_content_view; 71 jfieldID g_native_content_view;
72
73 const void* kContentViewUserDataKey = &kContentViewUserDataKey;
70 } // namespace 74 } // namespace
71 75
72 namespace content { 76 // Enables a callback when the underlying WebContents is destroyed, to enable
77 // nulling the back-pointer.
78 class ContentViewCoreImpl::ContentViewUserData
79 : public base::SupportsUserData::Data {
80 public:
81 explicit ContentViewUserData(ContentViewCoreImpl* content_view_core)
82 : content_view_core_(content_view_core) {
83 }
84
85 virtual ~ContentViewUserData() {
86 // TODO(joth): When chrome has finished removing the TabContents class (see
87 // crbug.com/107201) consider inverting relationship, so ContentViewCore
88 // would own WebContents. That effectively implies making the WebContents
89 // destructor private on Android.
90 delete content_view_core_;
91 }
92
93 ContentViewCoreImpl* get() const { return content_view_core_; }
94
95 private:
96 // Not using scoped_ptr as ContentViewCoreImpl destructor is private.
97 ContentViewCoreImpl* content_view_core_;
98
99 DISALLOW_IMPLICIT_CONSTRUCTORS(ContentViewUserData);
100 };
73 101
74 struct ContentViewCoreImpl::JavaObject { 102 struct ContentViewCoreImpl::JavaObject {
75 ScopedJavaGlobalRef<jclass> rect_clazz; 103 ScopedJavaGlobalRef<jclass> rect_clazz;
76 jmethodID rect_constructor; 104 jmethodID rect_constructor;
77 }; 105 };
78 106
107 // static
108 ContentViewCoreImpl* ContentViewCoreImpl::FromWebContents(
109 content::WebContents* web_contents) {
110 ContentViewCoreImpl::ContentViewUserData* data =
111 reinterpret_cast<ContentViewCoreImpl::ContentViewUserData*>(
112 web_contents->GetUserData(kContentViewUserDataKey));
113 return data ? data->get() : NULL;
114 }
115
116 // static
117 ContentViewCore* ContentViewCore::FromWebContents(
118 content::WebContents* web_contents) {
119 return ContentViewCoreImpl::FromWebContents(web_contents);
120 }
121
79 ContentViewCore* ContentViewCore::GetNativeContentViewCore(JNIEnv* env, 122 ContentViewCore* ContentViewCore::GetNativeContentViewCore(JNIEnv* env,
80 jobject obj) { 123 jobject obj) {
81 return reinterpret_cast<ContentViewCore*>( 124 return reinterpret_cast<ContentViewCore*>(
82 env->GetIntField(obj, g_native_content_view)); 125 env->GetIntField(obj, g_native_content_view));
83 } 126 }
84 127
85
86 ContentViewCoreImpl::ContentViewCoreImpl(JNIEnv* env, jobject obj, 128 ContentViewCoreImpl::ContentViewCoreImpl(JNIEnv* env, jobject obj,
87 bool hardware_accelerated, 129 bool hardware_accelerated,
88 WebContents* web_contents, 130 WebContents* web_contents,
89 ui::WindowAndroid* window_android) 131 ui::WindowAndroid* window_android)
90 : java_ref_(env, obj), 132 : java_ref_(env, obj),
91 web_contents_(static_cast<WebContentsImpl*>(web_contents)), 133 web_contents_(static_cast<WebContentsImpl*>(web_contents)),
92 tab_crashed_(false), 134 tab_crashed_(false),
93 window_android_(window_android) { 135 window_android_(window_android) {
94 DCHECK(web_contents) << 136 DCHECK(web_contents) <<
95 "A ContentViewCoreImpl should be created with a valid WebContents."; 137 "A ContentViewCoreImpl should be created with a valid WebContents.";
(...skipping 16 matching lines...) Expand all
112 // Currently, the only use case we have for overriding a user agent involves 154 // Currently, the only use case we have for overriding a user agent involves
113 // spoofing a desktop Linux user agent for "Request desktop site". 155 // spoofing a desktop Linux user agent for "Request desktop site".
114 // Automatically set it for all WebContents so that it is available when a 156 // Automatically set it for all WebContents so that it is available when a
115 // NavigationEntry requires the user agent to be overridden. 157 // NavigationEntry requires the user agent to be overridden.
116 const char kLinuxInfoStr[] = "X11; Linux x86_64"; 158 const char kLinuxInfoStr[] = "X11; Linux x86_64";
117 std::string product = content::GetContentClient()->GetProduct(); 159 std::string product = content::GetContentClient()->GetProduct();
118 std::string spoofed_ua = 160 std::string spoofed_ua =
119 webkit_glue::BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product); 161 webkit_glue::BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product);
120 web_contents->SetUserAgentOverride(spoofed_ua); 162 web_contents->SetUserAgentOverride(spoofed_ua);
121 163
122 InitWebContents(web_contents); 164 InitWebContents();
123 } 165 }
124 166
125 ContentViewCoreImpl::~ContentViewCoreImpl() { 167 ContentViewCoreImpl::~ContentViewCoreImpl() {
168 JNIEnv* env = base::android::AttachCurrentThread();
169 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
170 java_ref_.reset();
171 if (!j_obj.is_null()) {
172 Java_ContentViewCore_onNativeContentViewCoreDestroyed(
173 env, j_obj.obj(), reinterpret_cast<jint>(this));
174 }
126 // Make sure nobody calls back into this object while we are tearing things 175 // Make sure nobody calls back into this object while we are tearing things
127 // down. 176 // down.
128 notification_registrar_.RemoveAll(); 177 notification_registrar_.RemoveAll();
129 178
130 delete java_object_; 179 delete java_object_;
131 java_object_ = NULL; 180 java_object_ = NULL;
181 }
182
183 void ContentViewCoreImpl::OnJavaContentViewCoreDestroyed(JNIEnv* env,
184 jobject obj) {
185 DCHECK(env->IsSameObject(java_ref_.get(env).obj(), obj));
132 java_ref_.reset(); 186 java_ref_.reset();
133 } 187 }
134 188
135 void ContentViewCoreImpl::Destroy(JNIEnv* env, jobject obj) { 189 void ContentViewCoreImpl::InitWebContents() {
136 delete this; 190 DCHECK(web_contents_);
137 }
138
139 void ContentViewCoreImpl::InitWebContents(WebContents* web_contents) {
140 web_contents_ = static_cast<WebContentsImpl*>(web_contents);
141 notification_registrar_.Add(this, 191 notification_registrar_.Add(this,
142 NOTIFICATION_RENDER_VIEW_HOST_CHANGED, 192 NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
143 Source<NavigationController>(&web_contents_->GetController())); 193 Source<NavigationController>(&web_contents_->GetController()));
144 194
145 static_cast<WebContentsViewAndroid*>(web_contents_->GetView())-> 195 static_cast<WebContentsViewAndroid*>(web_contents_->GetView())->
146 SetContentViewCore(this); 196 SetContentViewCore(this);
197 DCHECK(!web_contents_->GetUserData(kContentViewUserDataKey));
198 web_contents_->SetUserData(kContentViewUserDataKey,
199 new ContentViewUserData(this));
147 } 200 }
148 201
149 void ContentViewCoreImpl::Observe(int type, 202 void ContentViewCoreImpl::Observe(int type,
150 const NotificationSource& source, 203 const NotificationSource& source,
151 const NotificationDetails& details) { 204 const NotificationDetails& details) {
152 switch (type) { 205 switch (type) {
153 case NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT: { 206 case NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT: {
154 if (!web_contents_ || Source<RenderViewHost>(source).ptr() != 207 if (!web_contents_ || Source<RenderViewHost>(source).ptr() !=
155 web_contents_->GetRenderViewHost()) { 208 web_contents_->GetRenderViewHost()) {
156 return; 209 return;
(...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after
966 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) { 1019 if (!HasField(env, clazz, "mNativeContentViewCore", "I")) {
967 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!"; 1020 DLOG(ERROR) << "Unable to find ContentView.mNativeContentViewCore!";
968 return false; 1021 return false;
969 } 1022 }
970 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I"); 1023 g_native_content_view = GetFieldID(env, clazz, "mNativeContentViewCore", "I");
971 1024
972 return RegisterNativesImpl(env) >= 0; 1025 return RegisterNativesImpl(env) >= 0;
973 } 1026 }
974 1027
975 } // namespace content 1028 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698