web_contents_android.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright 2013 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/web_contents/web_contents_android.h"
6
7#include "base/android/jni_android.h"
8#include "base/android/jni_string.h"
9#include "base/logging.h"
10#include "content/browser/android/interstitial_page_delegate_android.h"
11#include "content/browser/frame_host/interstitial_page_impl.h"
12#include "content/browser/media/media_web_contents_observer.h"
13#include "content/browser/renderer_host/render_view_host_impl.h"
14#include "content/common/frame_messages.h"
15#include "content/common/input_messages.h"
16#include "content/common/view_messages.h"
17#include "content/public/browser/browser_thread.h"
18#include "content/public/browser/web_contents.h"
19#include "jni/WebContentsImpl_jni.h"
20
21using base::android::AttachCurrentThread;
22using base::android::ConvertJavaStringToUTF8;
23
24namespace content {
25
26// static
27WebContents* WebContents::FromJavaWebContents(
28    jobject jweb_contents_android) {
29  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
30  if (!jweb_contents_android)
31    return NULL;
32
33  WebContentsAndroid* web_contents_android =
34      reinterpret_cast<WebContentsAndroid*>(
35          Java_WebContentsImpl_getNativePointer(AttachCurrentThread(),
36                                                jweb_contents_android));
37  if (!web_contents_android)
38    return NULL;
39  return web_contents_android->web_contents();
40}
41
42// static
43bool WebContentsAndroid::Register(JNIEnv* env) {
44  return RegisterNativesImpl(env);
45}
46
47WebContentsAndroid::WebContentsAndroid(WebContents* web_contents)
48    : web_contents_(web_contents),
49      navigation_controller_(&(web_contents->GetController())) {
50  JNIEnv* env = AttachCurrentThread();
51  obj_.Reset(env,
52             Java_WebContentsImpl_create(
53                 env,
54                 reinterpret_cast<intptr_t>(this),
55                 navigation_controller_.GetJavaObject().obj()).obj());
56}
57
58WebContentsAndroid::~WebContentsAndroid() {
59  Java_WebContentsImpl_destroy(AttachCurrentThread(), obj_.obj());
60}
61
62base::android::ScopedJavaLocalRef<jobject>
63WebContentsAndroid::GetJavaObject() {
64  return base::android::ScopedJavaLocalRef<jobject>(obj_);
65}
66
67ScopedJavaLocalRef<jstring> WebContentsAndroid::GetTitle(
68    JNIEnv* env, jobject obj) const {
69  return base::android::ConvertUTF16ToJavaString(env,
70                                                 web_contents_->GetTitle());
71}
72
73ScopedJavaLocalRef<jstring> WebContentsAndroid::GetVisibleURL(
74    JNIEnv* env, jobject obj) const {
75  return base::android::ConvertUTF8ToJavaString(
76      env, web_contents_->GetVisibleURL().spec());
77}
78
79void WebContentsAndroid::Stop(JNIEnv* env, jobject obj) {
80  web_contents_->Stop();
81}
82
83void WebContentsAndroid::InsertCSS(
84    JNIEnv* env, jobject jobj, jstring jcss) {
85  web_contents_->InsertCSS(base::android::ConvertJavaStringToUTF8(env, jcss));
86}
87
88RenderWidgetHostViewAndroid*
89    WebContentsAndroid::GetRenderWidgetHostViewAndroid() {
90  RenderWidgetHostView* rwhv = NULL;
91  rwhv = web_contents_->GetRenderWidgetHostView();
92  if (web_contents_->ShowingInterstitialPage()) {
93    rwhv = static_cast<InterstitialPageImpl*>(
94        web_contents_->GetInterstitialPage())->
95            GetRenderViewHost()->GetView();
96  }
97  return static_cast<RenderWidgetHostViewAndroid*>(rwhv);
98}
99
100jint WebContentsAndroid::GetBackgroundColor(JNIEnv* env, jobject obj) {
101  RenderWidgetHostViewAndroid* rwhva = GetRenderWidgetHostViewAndroid();
102  if (!rwhva)
103    return SK_ColorWHITE;
104  return rwhva->GetCachedBackgroundColor();
105}
106
107void WebContentsAndroid::OnHide(JNIEnv* env, jobject obj) {
108  web_contents_->WasHidden();
109  PauseVideo();
110}
111
112void WebContentsAndroid::OnShow(JNIEnv* env, jobject obj) {
113  web_contents_->WasShown();
114}
115
116void WebContentsAndroid::PauseVideo() {
117  RenderViewHostImpl* rvhi = static_cast<RenderViewHostImpl*>(
118      web_contents_->GetRenderViewHost());
119  if (rvhi)
120    rvhi->media_web_contents_observer()->PauseVideo();
121}
122
123void WebContentsAndroid::AddStyleSheetByURL(
124    JNIEnv* env,
125    jobject obj,
126    jstring url) {
127  web_contents_->GetMainFrame()->Send(new FrameMsg_AddStyleSheetByURL(
128      web_contents_->GetMainFrame()->GetRoutingID(),
129      ConvertJavaStringToUTF8(env, url)));
130}
131
132void WebContentsAndroid::ShowInterstitialPage(
133    JNIEnv* env,
134    jobject obj,
135    jstring jurl,
136    jlong delegate_ptr) {
137  GURL url(base::android::ConvertJavaStringToUTF8(env, jurl));
138  InterstitialPageDelegateAndroid* delegate =
139      reinterpret_cast<InterstitialPageDelegateAndroid*>(delegate_ptr);
140  InterstitialPage* interstitial = InterstitialPage::Create(
141      web_contents_, false, url, delegate);
142  delegate->set_interstitial_page(interstitial);
143  interstitial->Show();
144}
145
146jboolean WebContentsAndroid::IsShowingInterstitialPage(JNIEnv* env,
147                                                        jobject obj) {
148  return web_contents_->ShowingInterstitialPage();
149}
150
151jboolean WebContentsAndroid::IsRenderWidgetHostViewReady(
152    JNIEnv* env,
153    jobject obj) {
154  RenderWidgetHostViewAndroid* view = GetRenderWidgetHostViewAndroid();
155  return view && view->HasValidFrame();
156}
157
158void WebContentsAndroid::ExitFullscreen(JNIEnv* env, jobject obj) {
159  RenderViewHost* host = web_contents_->GetRenderViewHost();
160  if (!host)
161    return;
162  host->ExitFullscreen();
163}
164
165void WebContentsAndroid::UpdateTopControlsState(
166    JNIEnv* env,
167    jobject obj,
168    bool enable_hiding,
169    bool enable_showing,
170    bool animate) {
171  RenderViewHost* host = web_contents_->GetRenderViewHost();
172  if (!host)
173    return;
174  host->Send(new ViewMsg_UpdateTopControlsState(host->GetRoutingID(),
175                                                enable_hiding,
176                                                enable_showing,
177                                                animate));
178}
179
180void WebContentsAndroid::ShowImeIfNeeded(JNIEnv* env, jobject obj) {
181  RenderViewHost* host = web_contents_->GetRenderViewHost();
182  if (!host)
183    return;
184  host->Send(new ViewMsg_ShowImeIfNeeded(host->GetRoutingID()));
185}
186
187void WebContentsAndroid::ScrollFocusedEditableNodeIntoView(
188    JNIEnv* env,
189    jobject obj) {
190  RenderViewHost* host = web_contents_->GetRenderViewHost();
191  if (!host)
192    return;
193  host->Send(new InputMsg_ScrollFocusedEditableNodeIntoRect(
194      host->GetRoutingID(), gfx::Rect()));
195}
196
197void WebContentsAndroid::SelectWordAroundCaret(JNIEnv* env, jobject obj) {
198  RenderViewHost* host = web_contents_->GetRenderViewHost();
199  if (!host)
200    return;
201  host->SelectWordAroundCaret();
202}
203
204}  // namespace content
205