| 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_jni_bridge.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/tab_contents/tab_util.h" |
| 16 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 17 #include "content/public/browser/web_contents.h" |
| 18 #include "jni/TabModelJniBridge_jni.h" |
| 19 |
| 20 using base::android::AttachCurrentThread; |
| 21 using base::android::ConvertUTF8ToJavaString; |
| 22 using content::WebContents; |
| 23 |
| 24 namespace { |
| 25 |
| 26 static Profile* FindProfile(jboolean is_incognito) { |
| 27 if (g_browser_process == NULL || |
| 28 g_browser_process->profile_manager() == NULL) { |
| 29 LOG(ERROR) << "Browser process or profile manager not initialized"; |
| 30 return NULL; |
| 31 } |
| 32 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 33 if (is_incognito) |
| 34 return profile->GetOffTheRecordProfile(); |
| 35 return profile; |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 TabModelJniBridge::TabModelJniBridge(JNIEnv* env, |
| 41 jobject jobj, |
| 42 bool is_incognito) |
| 43 : TabModel(FindProfile(is_incognito)), |
| 44 java_object_(env, env->NewWeakGlobalRef(jobj)) { |
| 45 TabModelList::AddTabModel(this); |
| 46 Java_TabModelJniBridge_setNativePtr(env, |
| 47 jobj, |
| 48 reinterpret_cast<intptr_t>(this)); |
| 49 } |
| 50 |
| 51 void TabModelJniBridge::Destroy(JNIEnv* env, jobject obj) { |
| 52 delete this; |
| 53 } |
| 54 |
| 55 ScopedJavaLocalRef<jobject> TabModelJniBridge::GetProfileAndroid(JNIEnv* env, |
| 56 jobject obj) { |
| 57 ProfileAndroid* profile_android = ProfileAndroid::FromProfile(GetProfile()); |
| 58 if (!profile_android) |
| 59 return ScopedJavaLocalRef<jobject>(); |
| 60 return profile_android->GetJavaObject(); |
| 61 } |
| 62 |
| 63 void TabModelJniBridge::TabAddedToModel(JNIEnv* env, |
| 64 jobject obj, |
| 65 jobject jtab) { |
| 66 TabAndroid* tab = TabAndroid::GetNativeTab(env, jtab); |
| 67 |
| 68 // Tab#initialize() should have been called by now otherwise we can't push |
| 69 // the window id. |
| 70 DCHECK(tab); |
| 71 |
| 72 tab->SetWindowSessionID(GetSessionId()); |
| 73 } |
| 74 |
| 75 int TabModelJniBridge::GetTabCount() const { |
| 76 JNIEnv* env = AttachCurrentThread(); |
| 77 return Java_TabModelJniBridge_getCount(env, java_object_.get(env).obj()); |
| 78 } |
| 79 |
| 80 int TabModelJniBridge::GetActiveIndex() const { |
| 81 JNIEnv* env = AttachCurrentThread(); |
| 82 return Java_TabModelJniBridge_index(env, java_object_.get(env).obj()); |
| 83 } |
| 84 |
| 85 void TabModelJniBridge::CreateTab(WebContents* web_contents, |
| 86 int parent_tab_id) { |
| 87 JNIEnv* env = AttachCurrentThread(); |
| 88 Java_TabModelJniBridge_createTabWithNativeContents( |
| 89 env, java_object_.get(env).obj(), |
| 90 web_contents->GetBrowserContext()->IsOffTheRecord(), |
| 91 reinterpret_cast<intptr_t>(web_contents), parent_tab_id); |
| 92 } |
| 93 |
| 94 WebContents* TabModelJniBridge::GetWebContentsAt(int index) const { |
| 95 TabAndroid* tab = GetTabAt(index); |
| 96 return tab == NULL ? NULL : tab->web_contents(); |
| 97 } |
| 98 |
| 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); |
| 105 |
| 106 return jtab.is_null() ? |
| 107 NULL : TabAndroid::GetNativeTab(env, jtab.obj()); |
| 108 } |
| 109 |
| 110 void TabModelJniBridge::SetActiveIndex(int index) { |
| 111 JNIEnv* env = AttachCurrentThread(); |
| 112 Java_TabModelJniBridge_setIndex(env, java_object_.get(env).obj(), index); |
| 113 } |
| 114 |
| 115 void TabModelJniBridge::CloseTabAt(int index) { |
| 116 JNIEnv* env = AttachCurrentThread(); |
| 117 Java_TabModelJniBridge_closeTabAt(env, |
| 118 java_object_.get(env).obj(), |
| 119 index); |
| 120 } |
| 121 |
| 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; |
| 136 } |
| 137 TabAndroid* tab = TabAndroid::GetNativeTab(env, obj.obj()); |
| 138 if (!tab) { |
| 139 VLOG(0) << "Failed to create java tab"; |
| 140 return NULL; |
| 141 } |
| 142 return tab->web_contents(); |
| 143 } |
| 144 |
| 145 bool TabModelJniBridge::IsSessionRestoreInProgress() const { |
| 146 JNIEnv* env = AttachCurrentThread(); |
| 147 return Java_TabModelJniBridge_isSessionRestoreInProgress( |
| 148 env, java_object_.get(env).obj()); |
| 149 } |
| 150 |
| 151 void TabModelJniBridge::BroadcastSessionRestoreComplete(JNIEnv* env, |
| 152 jobject obj) { |
| 153 TabModel::BroadcastSessionRestoreComplete(); |
| 154 } |
| 155 |
| 156 TabModelJniBridge::~TabModelJniBridge() { |
| 157 TabModelList::RemoveTabModel(this); |
| 158 JNIEnv* env = AttachCurrentThread(); |
| 159 Java_TabModelJniBridge_clearNativePtr(env, java_object_.get(env).obj()); |
| 160 } |
| 161 |
| 162 bool TabModelJniBridge::Register(JNIEnv* env) { |
| 163 return RegisterNativesImpl(env); |
| 164 } |
| OLD | NEW |