Chromium Code Reviews| Index: chrome/browser/ui/android/tab_model/tab_model_jni_bridge.cc |
| diff --git a/chrome/browser/ui/android/tab_model/tab_model_jni_bridge.cc b/chrome/browser/ui/android/tab_model/tab_model_jni_bridge.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0459a15c9e15cee016a7211e9799445f4d1b5391 |
| --- /dev/null |
| +++ b/chrome/browser/ui/android/tab_model/tab_model_jni_bridge.cc |
| @@ -0,0 +1,118 @@ |
| +// Copyright 2014 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 "chrome/browser/ui/android/tab_model/tab_model_jni_bridge.h" |
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_weak_ref.h" |
| +#include "chrome/browser/android/tab_android.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_android.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| +#include "jni/TabModelJniBridge_jni.h" |
| + |
| +using base::android::AttachCurrentThread; |
| + |
| +namespace { |
| + |
| +static Profile* FindProfile(jboolean is_incognito) { |
| + if (g_browser_process == NULL || |
| + g_browser_process->profile_manager() == NULL) { |
| + LOG(ERROR) << "Browser process or profile manager not initialized"; |
| + return NULL; |
| + } |
| + Profile* profile = ProfileManager::GetActiveUserProfile(); |
| + if (is_incognito) |
| + return profile->GetOffTheRecordProfile(); |
| + return profile; |
| +} |
| + |
| +} // namespace |
| + |
| +TabModelJniBridge::TabModelJniBridge(JNIEnv* env, |
| + jobject jobj, |
| + bool is_incognito) |
| + : TabModel(FindProfile(is_incognito)), |
| + java_object_(env, env->NewWeakGlobalRef(jobj)) { |
| + TabModelList::AddTabModel(this); |
| + Java_TabModelJniBridge_setNativePtr(env, |
| + jobj, |
| + reinterpret_cast<intptr_t>(this)); |
| +} |
| + |
| +void TabModelJniBridge::Destroy(JNIEnv* env, jobject obj) { |
| + TabModelList::RemoveTabModel(this); |
|
David Trainor- moved to gerrit
2014/10/09 20:43:42
Should this go in the destructor? Might make more
gone
2014/10/10 22:39:19
Done.
|
| + delete this; |
| +} |
| + |
| +ScopedJavaLocalRef<jobject> TabModelJniBridge::GetProfileAndroid(JNIEnv* env, |
| + jobject obj) { |
| + ProfileAndroid* profile_android = ProfileAndroid::FromProfile(GetProfile()); |
| + if (!profile_android) |
| + return ScopedJavaLocalRef<jobject>(); |
| + return profile_android->GetJavaObject(); |
| +} |
| + |
| +void TabModelJniBridge::TabAddedToModel(JNIEnv* env, |
| + jobject obj, |
| + jobject jtab) { |
| + TabAndroid* tab = TabAndroid::GetNativeTab(env, jtab); |
| + |
| + // Tab#initialize() should have been called by now otherwise we can't push |
| + // the window id. |
| + DCHECK(tab); |
| + |
| + tab->SetWindowSessionID(GetSessionId()); |
| +} |
| + |
| +int TabModelJniBridge::GetTabCount() const { |
| + JNIEnv* env = AttachCurrentThread(); |
| + return Java_TabModelJniBridge_getCount(env, java_object_.get(env).obj()); |
| +} |
| + |
| +int TabModelJniBridge::GetActiveIndex() const { |
| + JNIEnv* env = AttachCurrentThread(); |
| + return Java_TabModelJniBridge_index(env, java_object_.get(env).obj()); |
| +} |
| + |
| +void TabModelJniBridge::CreateTab(content::WebContents* web_contents, |
| + int parent_tab_id) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + Java_TabModelJniBridge_createTabWithNativeContents( |
| + env, java_object_.get(env).obj(), |
| + web_contents->GetBrowserContext()->IsOffTheRecord(), |
| + reinterpret_cast<intptr_t>(web_contents), parent_tab_id); |
| +} |
| + |
| +content::WebContents* TabModelJniBridge::GetWebContentsAt(int index) const { |
| + TabAndroid* tab = GetTabAt(index); |
| + return tab == NULL ? NULL : tab->web_contents(); |
| +} |
| + |
| +void TabModelJniBridge::SetActiveIndex(int index) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + Java_TabModelJniBridge_setIndex(env, java_object_.get(env).obj(), index); |
| +} |
| + |
| +bool TabModelJniBridge::IsSessionRestoreInProgress() const { |
| + JNIEnv* env = AttachCurrentThread(); |
| + return Java_TabModelJniBridge_isSessionRestoreInProgress( |
| + env, java_object_.get(env).obj()); |
| +} |
| + |
| +void TabModelJniBridge::BroadcastSessionRestoreComplete(JNIEnv* env, |
| + jobject obj) { |
| + TabModel::BroadcastSessionRestoreComplete(); |
| +} |
| + |
| +TabModelJniBridge::~TabModelJniBridge() { |
| + JNIEnv* env = AttachCurrentThread(); |
| + Java_TabModelJniBridge_clearNativePtr(env, java_object_.get(env).obj()); |
| +} |
| + |
| +bool TabModelJniBridge::Register(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |