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