1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/android/web_contents_observer_android.h"
#include <string>
#include <jni.h>
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "content/browser/android/content_view_core_impl.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "base/android/scoped_java_ref.h"
#include "jni/WebContentsObserverAndroid_jni.h"
using base::android::AttachCurrentThread;
using base::android::ScopedJavaLocalRef;
using base::android::ConvertUTF8ToJavaString;
using base::android::ConvertUTF16ToJavaString;
using base::android::HasClass;
namespace content {
WebContentsObserverAndroid::WebContentsObserverAndroid(
JNIEnv* env,
jobject obj,
WebContents* web_contents)
: WebContentsObserver(web_contents),
weak_java_observer_(env, obj){
}
WebContentsObserverAndroid::~WebContentsObserverAndroid() {
}
jint Init(JNIEnv* env, jobject obj, jint native_content_view_core) {
ContentViewCore* content_view_core =
reinterpret_cast<ContentViewCore*>(native_content_view_core);
WebContentsObserverAndroid* native_observer = new WebContentsObserverAndroid(
env, obj, content_view_core->GetWebContents());
return reinterpret_cast<jint>(native_observer);
}
void WebContentsObserverAndroid::Destroy(JNIEnv* env, jobject obj) {
delete this;
}
void WebContentsObserverAndroid::WebContentsDestroyed(
WebContents* web_contents) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = weak_java_observer_.get(env);
if (obj.is_null()) {
delete this;
} else {
// The java side will destroy |this|
Java_WebContentsObserverAndroid_detachFromWebContents(env, obj.obj());
}
}
void WebContentsObserverAndroid::DidStartLoading(
RenderViewHost* render_view_host) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = weak_java_observer_.get(env);
if (obj.is_null())
return;
ScopedJavaLocalRef<jstring> jstring_url =
ConvertUTF8ToJavaString(env, web_contents()->GetURL().spec());
Java_WebContentsObserverAndroid_didStartLoading(
env, obj.obj(), jstring_url.obj());
}
void WebContentsObserverAndroid::DidStopLoading(
RenderViewHost* render_view_host) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = weak_java_observer_.get(env);
if (obj.is_null())
return;
ScopedJavaLocalRef<jstring> jstring_url =
ConvertUTF8ToJavaString(env, web_contents()->GetURL().spec());
Java_WebContentsObserverAndroid_didStopLoading(
env, obj.obj(), jstring_url.obj());
}
void WebContentsObserverAndroid::DidFailProvisionalLoad(
int64 frame_id,
bool is_main_frame,
const GURL& validated_url,
int error_code,
const string16& error_description,
RenderViewHost* render_view_host) {
DidFailLoadInternal(
true, is_main_frame, error_code, error_description, validated_url);
}
void WebContentsObserverAndroid::DidFailLoad(
int64 frame_id,
const GURL& validated_url,
bool is_main_frame,
int error_code,
const string16& error_description,
RenderViewHost* render_view_host) {
DidFailLoadInternal(
false, is_main_frame, error_code, error_description, validated_url);
}
void WebContentsObserverAndroid::DidNavigateMainFrame(
const LoadCommittedDetails& details,
const FrameNavigateParams& params) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = weak_java_observer_.get(env);
if (obj.is_null())
return;
ScopedJavaLocalRef<jstring> jstring_url =
ConvertUTF8ToJavaString(env, params.url.spec());
ScopedJavaLocalRef<jstring> jstring_base_url =
ConvertUTF8ToJavaString(env, params.base_url.spec());
Java_WebContentsObserverAndroid_didNavigateMainFrame(
env, obj.obj(), jstring_url.obj(), jstring_base_url.obj());
}
void WebContentsObserverAndroid::DidFailLoadInternal(
bool is_provisional_load,
bool is_main_frame,
int error_code,
const string16& description,
const GURL& url) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = weak_java_observer_.get(env);
if (obj.is_null())
return;
ScopedJavaLocalRef<jstring> jstring_error_description =
ConvertUTF16ToJavaString(env, description);
ScopedJavaLocalRef<jstring> jstring_url =
ConvertUTF8ToJavaString(env, url.spec());
Java_WebContentsObserverAndroid_didFailLoad(
env, obj.obj(),
is_provisional_load,
is_main_frame,
error_code,
jstring_error_description.obj(), jstring_url.obj());
}
bool RegisterWebContentsObserverAndroid(JNIEnv* env) {
if (!HasClass(env, kWebContentsObserverAndroidClassPath)) {
DLOG(ERROR) << "Unable to find class WebContentsObserverAndroid!";
return false;
}
return RegisterNativesImpl(env);
}
} // namespace content
|