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