| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/android/tab_model/tab_model_base.h" | 5 #include "chrome/browser/ui/android/tab_model/tab_model_base.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/android/jni_weak_ref.h" | 9 #include "base/android/jni_weak_ref.h" |
| 10 #include "chrome/browser/android/tab_android.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" | 11 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 16 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 12 #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" | 13 #include "jni/TabModelBase_jni.h" |
| 20 | 14 |
| 21 using base::android::AttachCurrentThread; | 15 using base::android::AttachCurrentThread; |
| 22 using base::android::CheckException; | 16 using base::android::CheckException; |
| 23 using base::android::ConvertUTF8ToJavaString; | 17 using base::android::ConvertUTF8ToJavaString; |
| 24 using base::android::ConvertUTF16ToJavaString; | 18 using base::android::ConvertUTF16ToJavaString; |
| 25 using base::android::ScopedJavaLocalRef; | 19 using base::android::ScopedJavaLocalRef; |
| 26 | 20 |
| 27 TabModelBase::TabModelBase(JNIEnv* env, jobject obj, Profile* profile) | 21 TabModelBase::TabModelBase(JNIEnv* env, jobject obj, bool is_incognito) |
| 28 : TabModel(profile), | 22 : TabModelJniBridge(env, obj, is_incognito) { |
| 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 } | 23 } |
| 75 | 24 |
| 76 TabAndroid* TabModelBase::GetTabAt(int index) const { | 25 TabAndroid* TabModelBase::GetTabAt(int index) const { |
| 77 JNIEnv* env = AttachCurrentThread(); | 26 JNIEnv* env = AttachCurrentThread(); |
| 78 ScopedJavaLocalRef<jobject> jtab = | 27 ScopedJavaLocalRef<jobject> jtab = |
| 79 Java_TabModelBase_getTabAt(env, | 28 Java_TabModelBase_getTabAt(env, |
| 80 java_object_.get(env).obj(), | 29 java_object_.get(env).obj(), |
| 81 index); | 30 index); |
| 82 DCHECK(!jtab.is_null()); | 31 DCHECK(!jtab.is_null()); |
| 83 | 32 |
| 84 return TabAndroid::GetNativeTab(env, jtab.obj()); | 33 return TabAndroid::GetNativeTab(env, jtab.obj()); |
| 85 } | 34 } |
| 86 | 35 |
| 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) { | 36 void TabModelBase::CloseTabAt(int index) { |
| 96 JNIEnv* env = AttachCurrentThread(); | 37 JNIEnv* env = AttachCurrentThread(); |
| 97 ScopedJavaLocalRef<jobject> jtab = | 38 ScopedJavaLocalRef<jobject> jtab = |
| 98 Java_TabModelBase_getTabAt(env, | 39 Java_TabModelBase_getTabAt(env, |
| 99 java_object_.get(env).obj(), | 40 java_object_.get(env).obj(), |
| 100 index); | 41 index); |
| 101 if (!jtab.is_null()) { | 42 if (!jtab.is_null()) { |
| 102 Java_TabModelBase_closeTab(env, | 43 Java_TabModelBase_closeTab(env, |
| 103 java_object_.get(env).obj(), | 44 java_object_.get(env).obj(), |
| 104 jtab.obj()); | 45 jtab.obj()); |
| 105 } | 46 } |
| 106 } | 47 } |
| 107 | 48 |
| 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) { | 49 content::WebContents* TabModelBase::CreateNewTabForDevTools(const GURL& url) { |
| 118 JNIEnv* env = AttachCurrentThread(); | 50 JNIEnv* env = AttachCurrentThread(); |
| 119 ScopedJavaLocalRef<jstring> jurl = ConvertUTF8ToJavaString(env, url.spec()); | 51 ScopedJavaLocalRef<jstring> jurl = ConvertUTF8ToJavaString(env, url.spec()); |
| 120 ScopedJavaLocalRef<jobject> obj = | 52 ScopedJavaLocalRef<jobject> obj = |
| 121 Java_TabModelBase_createNewTabForDevTools( | 53 Java_TabModelBase_createNewTabForDevTools( |
| 122 env, | 54 env, |
| 123 java_object_.get(env).obj(), | 55 java_object_.get(env).obj(), |
| 124 jurl.obj()); | 56 jurl.obj()); |
| 125 if (obj.is_null()) { | 57 if (obj.is_null()) { |
| 126 VLOG(0) << "Failed to create java tab"; | 58 VLOG(0) << "Failed to create java tab"; |
| 127 return NULL; | 59 return NULL; |
| 128 } | 60 } |
| 129 TabAndroid* tab = TabAndroid::GetNativeTab(env, obj.obj()); | 61 TabAndroid* tab = TabAndroid::GetNativeTab(env, obj.obj()); |
| 130 if (!tab) { | 62 if (!tab) { |
| 131 VLOG(0) << "Failed to create java tab"; | 63 VLOG(0) << "Failed to create java tab"; |
| 132 return NULL; | 64 return NULL; |
| 133 } | 65 } |
| 134 return tab->web_contents(); | 66 return tab->web_contents(); |
| 135 } | 67 } |
| 136 | 68 |
| 137 bool TabModelBase::IsSessionRestoreInProgress() const { | |
| 138 JNIEnv* env = AttachCurrentThread(); | |
| 139 return Java_TabModelBase_isSessionRestoreInProgress( | |
| 140 env, java_object_.get(env).obj()); | |
| 141 } | |
| 142 | 69 |
| 143 void TabModelBase::BroadcastSessionRestoreComplete(JNIEnv* env, | |
| 144 jobject obj) { | |
| 145 TabModel::BroadcastSessionRestoreComplete(); | |
| 146 } | |
| 147 | |
| 148 TabModelBase::~TabModelBase() { | |
| 149 } | |
| 150 | |
| 151 namespace { | |
| 152 | |
| 153 static Profile* FindProfile(jboolean is_incognito) { | |
| 154 if (g_browser_process == NULL || | |
| 155 g_browser_process->profile_manager() == NULL) { | |
| 156 LOG(ERROR) << "Browser process or profile manager not initialized"; | |
| 157 return NULL; | |
| 158 } | |
| 159 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 160 if (is_incognito) { | |
| 161 return profile->GetOffTheRecordProfile(); | |
| 162 } | |
| 163 return profile; | |
| 164 } | |
| 165 | |
| 166 } // namespace | |
| 167 | 70 |
| 168 // ---------------------------------------------------------------------------- | 71 // ---------------------------------------------------------------------------- |
| 169 // Native JNI methods | 72 // Native JNI methods |
| 170 // ---------------------------------------------------------------------------- | 73 // ---------------------------------------------------------------------------- |
| 171 | 74 |
| 172 static jlong Init(JNIEnv* env, jobject obj, jboolean is_incognito) { | 75 static jlong Init(JNIEnv* env, jobject obj, jboolean is_incognito) { |
| 173 Profile* profile = FindProfile(is_incognito); | 76 TabModel* tab_model = new TabModelBase(env, obj, is_incognito); |
| 174 TabModel* tab_model = new TabModelBase(env, obj, profile); | |
| 175 TabModelList::AddTabModel(tab_model); | |
| 176 return reinterpret_cast<intptr_t>(tab_model); | 77 return reinterpret_cast<intptr_t>(tab_model); |
| 177 } | 78 } |
| 178 | 79 |
| 179 // Register native methods | 80 // Register native methods |
| 180 | 81 |
| 181 bool RegisterTabModelBase(JNIEnv* env) { | 82 bool RegisterTabModelBase(JNIEnv* env) { |
| 182 return RegisterNativesImpl(env); | 83 return RegisterNativesImpl(env); |
| 183 } | 84 } |
| OLD | NEW |