OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/ui/android/tab_model/tab_model_base.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/android/jni_weak_ref.h" |
| 10 #include "chrome/browser/android/tab_android.h" |
| 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/profiles/profile_android.h" |
| 14 #include "chrome/browser/profiles/profile_manager.h" |
| 15 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 16 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 17 #include "chrome/browser/ui/toolbar/toolbar_model.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 #include "jni/TabModelBase_jni.h" |
| 20 |
| 21 using base::android::AttachCurrentThread; |
| 22 using base::android::CheckException; |
| 23 using base::android::ConvertUTF8ToJavaString; |
| 24 using base::android::ConvertUTF16ToJavaString; |
| 25 using base::android::ScopedJavaLocalRef; |
| 26 |
| 27 TabModelBase::TabModelBase(JNIEnv* env, jobject obj, Profile* profile) |
| 28 : TabModel(profile), |
| 29 java_object_(env, env->NewWeakGlobalRef(obj)) { |
| 30 } |
| 31 |
| 32 void TabModelBase::Destroy(JNIEnv* env, jobject obj) { |
| 33 TabModelList::RemoveTabModel(this); |
| 34 delete this; |
| 35 } |
| 36 |
| 37 ScopedJavaLocalRef<jobject> TabModelBase::GetProfileAndroid(JNIEnv* env, |
| 38 jobject obj) { |
| 39 ProfileAndroid* profile_android = ProfileAndroid::FromProfile(GetProfile()); |
| 40 if (!profile_android) |
| 41 return ScopedJavaLocalRef<jobject>(); |
| 42 |
| 43 return profile_android->GetJavaObject(); |
| 44 } |
| 45 |
| 46 void TabModelBase::TabAddedToModel(JNIEnv* env, jobject obj, jobject jtab) { |
| 47 TabAndroid* tab = TabAndroid::GetNativeTab(env, jtab); |
| 48 |
| 49 // Tab#initialize() should have been called by now otherwise we can't push |
| 50 // the window id. |
| 51 DCHECK(tab); |
| 52 |
| 53 tab->SetWindowSessionID(GetSessionId()); |
| 54 } |
| 55 |
| 56 int TabModelBase::GetTabCount() const { |
| 57 JNIEnv* env = AttachCurrentThread(); |
| 58 jint count = Java_TabModelBase_getCount( |
| 59 env, java_object_.get(env).obj()); |
| 60 return count; |
| 61 } |
| 62 |
| 63 int TabModelBase::GetActiveIndex() const { |
| 64 JNIEnv* env = AttachCurrentThread(); |
| 65 jint index = Java_TabModelBase_index( |
| 66 env, java_object_.get(env).obj()); |
| 67 return index; |
| 68 } |
| 69 |
| 70 content::WebContents* TabModelBase::GetWebContentsAt( |
| 71 int index) const { |
| 72 TabAndroid* tab = GetTabAt(index); |
| 73 return tab == NULL ? NULL : tab->web_contents(); |
| 74 } |
| 75 |
| 76 TabAndroid* TabModelBase::GetTabAt(int index) const { |
| 77 JNIEnv* env = AttachCurrentThread(); |
| 78 ScopedJavaLocalRef<jobject> jtab = |
| 79 Java_TabModelBase_getTabAt(env, |
| 80 java_object_.get(env).obj(), |
| 81 index); |
| 82 DCHECK(!jtab.is_null()); |
| 83 |
| 84 return TabAndroid::GetNativeTab(env, jtab.obj()); |
| 85 } |
| 86 |
| 87 void TabModelBase::SetActiveIndex(int index) { |
| 88 JNIEnv* env = AttachCurrentThread(); |
| 89 Java_TabModelBase_setIndex( |
| 90 env, |
| 91 java_object_.get(env).obj(), |
| 92 index); |
| 93 } |
| 94 |
| 95 void TabModelBase::CloseTabAt(int index) { |
| 96 JNIEnv* env = AttachCurrentThread(); |
| 97 ScopedJavaLocalRef<jobject> jtab = |
| 98 Java_TabModelBase_getTabAt(env, |
| 99 java_object_.get(env).obj(), |
| 100 index); |
| 101 if (!jtab.is_null()) { |
| 102 Java_TabModelBase_closeTab(env, |
| 103 java_object_.get(env).obj(), |
| 104 jtab.obj()); |
| 105 } |
| 106 } |
| 107 |
| 108 void TabModelBase::CreateTab(content::WebContents* web_contents, |
| 109 int parent_tab_id) { |
| 110 JNIEnv* env = AttachCurrentThread(); |
| 111 Java_TabModelBase_createTabWithNativeContents( |
| 112 env, java_object_.get(env).obj(), |
| 113 web_contents->GetBrowserContext()->IsOffTheRecord(), |
| 114 reinterpret_cast<intptr_t>(web_contents), parent_tab_id); |
| 115 } |
| 116 |
| 117 content::WebContents* TabModelBase::CreateNewTabForDevTools(const GURL& url) { |
| 118 JNIEnv* env = AttachCurrentThread(); |
| 119 ScopedJavaLocalRef<jstring> jurl = ConvertUTF8ToJavaString(env, url.spec()); |
| 120 ScopedJavaLocalRef<jobject> obj = |
| 121 Java_TabModelBase_createNewTabForDevTools( |
| 122 env, |
| 123 java_object_.get(env).obj(), |
| 124 jurl.obj()); |
| 125 if (obj.is_null()) { |
| 126 VLOG(0) << "Failed to create java tab"; |
| 127 return NULL; |
| 128 } |
| 129 TabAndroid* tab = TabAndroid::GetNativeTab(env, obj.obj()); |
| 130 if (!tab) { |
| 131 VLOG(0) << "Failed to create java tab"; |
| 132 return NULL; |
| 133 } |
| 134 return tab->web_contents(); |
| 135 } |
| 136 |
| 137 bool TabModelBase::IsSessionRestoreInProgress() const { |
| 138 JNIEnv* env = AttachCurrentThread(); |
| 139 return Java_TabModelBase_isSessionRestoreInProgress( |
| 140 env, java_object_.get(env).obj()); |
| 141 } |
| 142 |
| 143 void TabModelBase::OpenClearBrowsingData() const { |
| 144 JNIEnv* env = AttachCurrentThread(); |
| 145 Java_TabModelBase_openClearBrowsingData(env, |
| 146 java_object_.get(env).obj()); |
| 147 } |
| 148 |
| 149 void TabModelBase::BroadcastSessionRestoreComplete(JNIEnv* env, |
| 150 jobject obj) { |
| 151 TabModel::BroadcastSessionRestoreComplete(); |
| 152 } |
| 153 |
| 154 TabModelBase::~TabModelBase() { |
| 155 } |
| 156 |
| 157 namespace { |
| 158 |
| 159 static Profile* FindProfile(jboolean is_incognito) { |
| 160 if (g_browser_process == NULL || |
| 161 g_browser_process->profile_manager() == NULL) { |
| 162 LOG(ERROR) << "Browser process or profile manager not initialized"; |
| 163 return NULL; |
| 164 } |
| 165 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 166 if (is_incognito) { |
| 167 return profile->GetOffTheRecordProfile(); |
| 168 } |
| 169 return profile; |
| 170 } |
| 171 |
| 172 } // namespace |
| 173 |
| 174 // ---------------------------------------------------------------------------- |
| 175 // Native JNI methods |
| 176 // ---------------------------------------------------------------------------- |
| 177 |
| 178 static jlong Init(JNIEnv* env, jobject obj, jboolean is_incognito) { |
| 179 Profile* profile = FindProfile(is_incognito); |
| 180 TabModel* tab_model = new TabModelBase(env, obj, profile); |
| 181 TabModelList::AddTabModel(tab_model); |
| 182 return reinterpret_cast<intptr_t>(tab_model); |
| 183 } |
| 184 |
| 185 // Register native methods |
| 186 |
| 187 bool RegisterTabModelBase(JNIEnv* env) { |
| 188 return RegisterNativesImpl(env); |
| 189 } |
OLD | NEW |