Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / android / tab_model / tab_model_jni_bridge.cc
blob2f9000979a0f3cf0a4b5d6a35be16aaf72b26772
1 // Copyright 2014 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.
5 #include "chrome/browser/ui/android/tab_model/tab_model_jni_bridge.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/jni_weak_ref.h"
10 #include "base/metrics/histogram.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/android/tab_android.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_android.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/tab_contents/tab_util.h"
18 #include "chrome/browser/ui/android/tab_model/tab_model_list.h"
19 #include "content/public/browser/web_contents.h"
20 #include "jni/TabModelJniBridge_jni.h"
22 using base::android::AttachCurrentThread;
23 using base::android::ConvertUTF8ToJavaString;
24 using content::WebContents;
26 namespace {
28 static Profile* FindProfile(jboolean is_incognito) {
29 if (g_browser_process == NULL ||
30 g_browser_process->profile_manager() == NULL) {
31 LOG(ERROR) << "Browser process or profile manager not initialized";
32 return NULL;
34 Profile* profile = ProfileManager::GetActiveUserProfile();
35 if (is_incognito)
36 return profile->GetOffTheRecordProfile();
37 return profile;
40 } // namespace
42 TabModelJniBridge::TabModelJniBridge(JNIEnv* env,
43 jobject jobj,
44 bool is_incognito)
45 : TabModel(FindProfile(is_incognito)),
46 java_object_(env, env->NewWeakGlobalRef(jobj)) {
47 TabModelList::AddTabModel(this);
50 void TabModelJniBridge::Destroy(JNIEnv* env, jobject obj) {
51 delete this;
54 ScopedJavaLocalRef<jobject> TabModelJniBridge::GetProfileAndroid(JNIEnv* env,
55 jobject obj) {
56 ProfileAndroid* profile_android = ProfileAndroid::FromProfile(GetProfile());
57 if (!profile_android)
58 return ScopedJavaLocalRef<jobject>();
59 return profile_android->GetJavaObject();
62 void TabModelJniBridge::TabAddedToModel(JNIEnv* env,
63 jobject obj,
64 jobject jtab) {
65 TabAndroid* tab = TabAndroid::GetNativeTab(env, jtab);
67 // Tab#initialize() should have been called by now otherwise we can't push
68 // the window id.
69 DCHECK(tab);
71 tab->SetWindowSessionID(GetSessionId());
74 int TabModelJniBridge::GetTabCount() const {
75 JNIEnv* env = AttachCurrentThread();
76 return Java_TabModelJniBridge_getCount(env, java_object_.get(env).obj());
79 int TabModelJniBridge::GetActiveIndex() const {
80 JNIEnv* env = AttachCurrentThread();
81 return Java_TabModelJniBridge_index(env, java_object_.get(env).obj());
84 void TabModelJniBridge::CreateTab(WebContents* web_contents,
85 int parent_tab_id) {
86 JNIEnv* env = AttachCurrentThread();
87 Java_TabModelJniBridge_createTabWithWebContents(
88 env, java_object_.get(env).obj(),
89 web_contents->GetBrowserContext()->IsOffTheRecord(),
90 web_contents->GetJavaWebContents().obj(),
91 parent_tab_id);
94 WebContents* TabModelJniBridge::GetWebContentsAt(int index) const {
95 TabAndroid* tab = GetTabAt(index);
96 return tab == NULL ? NULL : tab->web_contents();
99 TabAndroid* TabModelJniBridge::GetTabAt(int index) const {
100 JNIEnv* env = AttachCurrentThread();
101 ScopedJavaLocalRef<jobject> jtab =
102 Java_TabModelJniBridge_getTabAt(env,
103 java_object_.get(env).obj(),
104 index);
106 return jtab.is_null() ?
107 NULL : TabAndroid::GetNativeTab(env, jtab.obj());
110 void TabModelJniBridge::SetActiveIndex(int index) {
111 JNIEnv* env = AttachCurrentThread();
112 Java_TabModelJniBridge_setIndex(env, java_object_.get(env).obj(), index);
115 void TabModelJniBridge::CloseTabAt(int index) {
116 JNIEnv* env = AttachCurrentThread();
117 Java_TabModelJniBridge_closeTabAt(env,
118 java_object_.get(env).obj(),
119 index);
122 WebContents* TabModelJniBridge::CreateNewTabForDevTools(
123 const GURL& url) {
124 // TODO(dfalcantara): Change the Java side so that it creates and returns the
125 // WebContents, which we can load the URL on and return.
126 JNIEnv* env = AttachCurrentThread();
127 ScopedJavaLocalRef<jstring> jurl = ConvertUTF8ToJavaString(env, url.spec());
128 ScopedJavaLocalRef<jobject> obj =
129 Java_TabModelJniBridge_createNewTabForDevTools(
130 env,
131 java_object_.get(env).obj(),
132 jurl.obj());
133 if (obj.is_null()) {
134 VLOG(0) << "Failed to create java tab";
135 return NULL;
137 TabAndroid* tab = TabAndroid::GetNativeTab(env, obj.obj());
138 if (!tab) {
139 VLOG(0) << "Failed to create java tab";
140 return NULL;
142 return tab->web_contents();
145 bool TabModelJniBridge::IsSessionRestoreInProgress() const {
146 JNIEnv* env = AttachCurrentThread();
147 return Java_TabModelJniBridge_isSessionRestoreInProgress(
148 env, java_object_.get(env).obj());
151 void TabModelJniBridge::BroadcastSessionRestoreComplete(JNIEnv* env,
152 jobject obj) {
153 TabModel::BroadcastSessionRestoreComplete();
156 inline static base::TimeDelta GetTimeDelta(jlong ms) {
157 return base::TimeDelta::FromMilliseconds(static_cast<int64>(ms));
160 void LogFromCloseMetric(JNIEnv* env,
161 jclass jcaller,
162 jlong ms,
163 jboolean perceived) {
164 if (perceived) {
165 UMA_HISTOGRAM_TIMES("Tabs.SwitchFromCloseLatency_Perceived",
166 GetTimeDelta(ms));
167 } else {
168 UMA_HISTOGRAM_TIMES("Tabs.SwitchFromCloseLatency_Actual",
169 GetTimeDelta(ms));
173 void LogFromExitMetric(JNIEnv* env,
174 jclass jcaller,
175 jlong ms,
176 jboolean perceived) {
177 if (perceived) {
178 UMA_HISTOGRAM_TIMES("Tabs.SwitchFromExitLatency_Perceived",
179 GetTimeDelta(ms));
180 } else {
181 UMA_HISTOGRAM_TIMES("Tabs.SwitchFromExitLatency_Actual",
182 GetTimeDelta(ms));
186 void LogFromNewMetric(JNIEnv* env,
187 jclass jcaller,
188 jlong ms,
189 jboolean perceived) {
190 if (perceived) {
191 UMA_HISTOGRAM_TIMES("Tabs.SwitchFromNewLatency_Perceived",
192 GetTimeDelta(ms));
193 } else {
194 UMA_HISTOGRAM_TIMES("Tabs.SwitchFromNewLatency_Actual",
195 GetTimeDelta(ms));
199 void LogFromUserMetric(JNIEnv* env,
200 jclass jcaller,
201 jlong ms,
202 jboolean perceived) {
203 if (perceived) {
204 UMA_HISTOGRAM_TIMES("Tabs.SwitchFromUserLatency_Perceived",
205 GetTimeDelta(ms));
206 } else {
207 UMA_HISTOGRAM_TIMES("Tabs.SwitchFromUserLatency_Actual",
208 GetTimeDelta(ms));
212 TabModelJniBridge::~TabModelJniBridge() {
213 TabModelList::RemoveTabModel(this);
216 bool TabModelJniBridge::Register(JNIEnv* env) {
217 return RegisterNativesImpl(env);
220 static jlong Init(JNIEnv* env, jobject obj, jboolean is_incognito) {
221 TabModel* tab_model = new TabModelJniBridge(env, obj, is_incognito);
222 return reinterpret_cast<intptr_t>(tab_model);