OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/android/content_view_impl.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "base/android/scoped_java_ref.h" | |
10 #include "content/browser/android/content_view_client.h" | |
11 #include "content/browser/web_contents/navigation_controller_impl.h" | |
12 #include "content/public/browser/browser_context.h" | |
13 #include "content/public/browser/web_contents.h" | |
14 #include "jni/content_view_jni.h" | |
15 | |
16 using base::android::AttachCurrentThread; | |
17 using base::android::ConvertUTF16ToJavaString; | |
18 using base::android::ConvertUTF8ToJavaString; | |
19 using base::android::GetClass; | |
20 using base::android::HasField; | |
21 using base::android::ScopedJavaLocalRef; | |
22 | |
23 namespace { | |
24 jfieldID g_native_content_view; | |
25 } // namespace | |
26 | |
27 namespace content { | |
28 | |
29 struct ContentViewImpl::JavaObject { | |
30 jweak obj; | |
31 | |
32 ScopedJavaLocalRef<jobject> View(JNIEnv* env) { | |
33 return GetRealObject(env, obj); | |
34 } | |
35 }; | |
36 | |
37 // ---------------------------------------------------------------------------- | |
38 // Implementation of static ContentView public interfaces | |
39 | |
40 ContentView* ContentView::Create(JNIEnv* env, jobject obj, | |
41 WebContents* web_contents) { | |
42 return new ContentViewImpl(env, obj, web_contents); | |
43 } | |
44 | |
45 ContentView* ContentView::GetNativeContentView(JNIEnv* env, jobject obj) { | |
46 return reinterpret_cast<ContentView*>( | |
47 env->GetIntField(obj, g_native_content_view)); | |
48 } | |
49 | |
50 // ---------------------------------------------------------------------------- | |
51 | |
52 ContentViewImpl::ContentViewImpl(JNIEnv* env, jobject obj, | |
53 WebContents* web_contents) | |
54 : web_contents_(web_contents), | |
55 tab_crashed_(false) { | |
56 DCHECK(web_contents) << | |
57 "A ContentViewImpl should be created with a valid WebContents."; | |
58 | |
59 InitJNI(env, obj); | |
60 } | |
61 | |
62 ContentViewImpl::~ContentViewImpl() { | |
63 if (java_object_) { | |
64 JNIEnv* env = AttachCurrentThread(); | |
65 env->DeleteWeakGlobalRef(java_object_->obj); | |
66 delete java_object_; | |
67 java_object_ = 0; | |
68 } | |
69 } | |
70 | |
71 void ContentViewImpl::Destroy(JNIEnv* env, jobject obj) { | |
72 delete this; | |
73 } | |
74 | |
75 void ContentViewImpl::Observe(int type, | |
76 const NotificationSource& source, | |
77 const NotificationDetails& details) { | |
78 // TODO(jrg) | |
79 } | |
80 | |
81 void ContentViewImpl::InitJNI(JNIEnv* env, jobject obj) { | |
82 java_object_ = new JavaObject; | |
83 java_object_->obj = env->NewWeakGlobalRef(obj); | |
84 } | |
85 | |
86 // ---------------------------------------------------------------------------- | |
87 // Methods called from Java via JNI | |
88 // ---------------------------------------------------------------------------- | |
89 | |
90 void ContentViewImpl::LoadUrlWithoutUrlSanitization(JNIEnv* env, | |
91 jobject, | |
92 jstring jurl, | |
93 int page_transition) { | |
94 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl)); | |
95 | |
96 LoadUrl(url, page_transition); | |
97 } | |
98 | |
99 void ContentViewImpl::LoadUrlWithoutUrlSanitizationWithUserAgentOverride( | |
100 JNIEnv* env, | |
101 jobject, | |
102 jstring jurl, | |
103 int page_transition, | |
104 jstring user_agent_override) { | |
105 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl)); | |
106 | |
107 LoadUrlWithUserAgentOverride( | |
108 url, | |
109 page_transition, | |
110 base::android::ConvertJavaStringToUTF8(env, user_agent_override)); | |
111 } | |
112 | |
113 ScopedJavaLocalRef<jstring> ContentViewImpl::GetURL( | |
114 JNIEnv* env, jobject) const { | |
115 return ConvertUTF8ToJavaString(env, web_contents()->GetURL().spec()); | |
116 } | |
117 | |
118 ScopedJavaLocalRef<jstring> ContentViewImpl::GetTitle( | |
119 JNIEnv* env, jobject obj) const { | |
120 return ConvertUTF16ToJavaString(env, web_contents()->GetTitle()); | |
121 } | |
122 | |
123 jdouble ContentViewImpl::GetLoadProgress(JNIEnv* env, jobject obj) const { | |
124 // An empty page never loads anything and always has a progress of 0. | |
125 // We report 1 in that case so the UI does not assume the page is loading. | |
126 if (web_contents()->GetURL().is_empty() || !content_view_client_.get()) | |
127 return static_cast<jdouble>(1.0); | |
128 return static_cast<jdouble>(content_view_client_->GetLoadProgress()); | |
129 } | |
130 | |
131 jboolean ContentViewImpl::IsIncognito(JNIEnv* env, jobject obj) { | |
132 return web_contents()->GetBrowserContext()->IsOffTheRecord(); | |
133 } | |
134 | |
135 jboolean ContentViewImpl::CanGoBack(JNIEnv* env, jobject obj) { | |
136 return web_contents_->GetController().CanGoBack(); | |
137 } | |
138 | |
139 jboolean ContentViewImpl::CanGoForward(JNIEnv* env, jobject obj) { | |
140 return web_contents_->GetController().CanGoForward(); | |
141 } | |
142 | |
143 jboolean ContentViewImpl::CanGoToOffset( | |
144 JNIEnv* env, jobject obj, jint offset) { | |
145 return web_contents_->GetController().CanGoToOffset(offset); | |
146 } | |
147 | |
148 void ContentViewImpl::GoBack(JNIEnv* env, jobject obj) { | |
149 web_contents_->GetController().GoBack(); | |
150 tab_crashed_ = false; | |
151 } | |
152 | |
153 void ContentViewImpl::GoForward(JNIEnv* env, jobject obj) { | |
154 web_contents_->GetController().GoForward(); | |
155 tab_crashed_ = false; | |
156 } | |
157 | |
158 void ContentViewImpl::GoToOffset(JNIEnv* env, jobject obj, jint offset) { | |
159 web_contents_->GetController().GoToOffset(offset); | |
160 } | |
161 | |
162 void ContentViewImpl::StopLoading(JNIEnv* env, jobject obj) { | |
163 web_contents_->Stop(); | |
164 } | |
165 | |
166 void ContentViewImpl::Reload(JNIEnv* env, jobject obj) { | |
167 // Set check_for_repost parameter to false as we have no repost confirmation | |
168 // dialog ("confirm form resubmission" screen will still appear, however). | |
169 web_contents_->GetController().Reload(false); | |
170 tab_crashed_ = false; | |
171 } | |
172 | |
173 void ContentViewImpl::ClearHistory(JNIEnv* env, jobject obj) { | |
174 web_contents_->GetController().PruneAllButActive(); | |
175 } | |
176 | |
177 jboolean ContentViewImpl::NeedsReload(JNIEnv* env, jobject obj) { | |
178 return web_contents_->GetController().NeedsReload(); | |
179 } | |
180 | |
181 void ContentViewImpl::SetClient(JNIEnv* env, jobject obj, jobject jclient) { | |
182 scoped_ptr<ContentViewClient> client( | |
183 ContentViewClient::CreateNativeContentViewClient(env, jclient)); | |
184 | |
185 web_contents_->SetDelegate(client.get()); | |
186 | |
187 content_view_client_.swap(client); | |
188 } | |
189 | |
190 // -------------------------------------------------------------------------- | |
191 // Methods called from native code | |
192 // -------------------------------------------------------------------------- | |
193 | |
194 void ContentViewImpl::LoadUrl(const GURL& url, int page_transition) { | |
195 content::Referrer referer; | |
196 | |
197 web_contents()->GetController().LoadURL( | |
198 url, referer, content::PageTransitionFromInt(page_transition), | |
199 std::string()); | |
200 PostLoadUrl(url); | |
201 } | |
202 | |
203 void ContentViewImpl::LoadUrlWithUserAgentOverride( | |
204 const GURL& url, | |
205 int page_transition, | |
206 const std::string& user_agent_override) { | |
207 web_contents()->SetUserAgentOverride(user_agent_override); | |
208 bool is_overriding_user_agent(!user_agent_override.empty()); | |
209 content::Referrer referer; | |
210 web_contents()->GetController().LoadURLWithUserAgentOverride( | |
211 url, referer, content::PageTransitionFromInt(page_transition), | |
212 false, std::string(), is_overriding_user_agent); | |
213 PostLoadUrl(url); | |
214 } | |
215 | |
216 void ContentViewImpl::PostLoadUrl(const GURL& url) { | |
217 tab_crashed_ = false; | |
218 // TODO(tedchoc): Update the content view client of the page load request. | |
219 } | |
220 | |
221 // ---------------------------------------------------------------------------- | |
222 // Native JNI methods | |
223 // ---------------------------------------------------------------------------- | |
224 | |
225 // This is called for each ContentView. | |
226 static jint Init(JNIEnv* env, jobject obj, jint native_web_contents) { | |
227 ContentView* view = ContentView::Create( | |
228 env, obj, reinterpret_cast<WebContents*>(native_web_contents)); | |
229 return reinterpret_cast<jint>(view); | |
230 } | |
231 | |
232 // -------------------------------------------------------------------------- | |
233 // Public methods that call to Java via JNI | |
234 // -------------------------------------------------------------------------- | |
235 | |
236 void ContentViewImpl::OnTabCrashed(const base::ProcessHandle handle) { | |
237 NOTIMPLEMENTED() << "not upstreamed yet"; | |
238 } | |
239 | |
240 void ContentViewImpl::SetTitle(const string16& title) { | |
241 NOTIMPLEMENTED() << "not upstreamed yet"; | |
242 } | |
243 | |
244 bool ContentViewImpl::HasFocus() { | |
245 NOTIMPLEMENTED() << "not upstreamed yet"; | |
246 return false; | |
247 } | |
248 | |
249 void ContentViewImpl::OnSelectionChanged(const std::string& text) { | |
250 NOTIMPLEMENTED() << "not upstreamed yet"; | |
251 } | |
252 | |
253 void ContentViewImpl::OnSelectionBoundsChanged( | |
254 int startx, | |
255 int starty, | |
256 base::i18n::TextDirection start_dir, | |
257 int endx, | |
258 int endy, | |
259 base::i18n::TextDirection end_dir) { | |
260 NOTIMPLEMENTED() << "not upstreamed yet"; | |
261 } | |
262 | |
263 void ContentViewImpl::OnAcceleratedCompositingStateChange( | |
264 RenderWidgetHostViewAndroid* rwhva, bool activated, bool force) { | |
265 NOTIMPLEMENTED() << "not upstreamed yet"; | |
266 } | |
267 | |
268 void ContentViewImpl::StartContentIntent(const GURL& content_url) { | |
269 JNIEnv* env = AttachCurrentThread(); | |
270 ScopedJavaLocalRef<jstring> jcontent_url = | |
271 ConvertUTF8ToJavaString(env, content_url.spec()); | |
272 Java_ContentView_startContentIntent(env, | |
273 java_object_->View(env).obj(), | |
274 jcontent_url.obj()); | |
275 } | |
276 | |
277 // -------------------------------------------------------------------------- | |
278 // Methods called from Java via JNI | |
279 // -------------------------------------------------------------------------- | |
280 | |
281 // -------------------------------------------------------------------------- | |
282 // Methods called from native code | |
283 // -------------------------------------------------------------------------- | |
284 | |
285 gfx::Rect ContentViewImpl::GetBounds() const { | |
286 NOTIMPLEMENTED() << "not upstreamed yet"; | |
287 return gfx::Rect(); | |
288 } | |
289 | |
290 // ---------------------------------------------------------------------------- | |
291 | |
292 bool RegisterContentView(JNIEnv* env) { | |
293 if (!base::android::HasClass(env, kContentViewClassPath)) { | |
294 DLOG(ERROR) << "Unable to find class ContentView!"; | |
295 return false; | |
296 } | |
297 ScopedJavaLocalRef<jclass> clazz = GetClass(env, kContentViewClassPath); | |
298 if (!HasField(env, clazz, "mNativeContentView", "I")) { | |
299 DLOG(ERROR) << "Unable to find ContentView.mNativeContentView!"; | |
300 return false; | |
301 } | |
302 g_native_content_view = GetFieldID(env, clazz, "mNativeContentView", "I"); | |
303 | |
304 return RegisterNativesImpl(env) >= 0; | |
305 } | |
306 | |
307 } // namespace content | |
OLD | NEW |