1 // Copyright (c) 2012 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.h"
7 #include "base/logging.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sync/glue/synced_window_delegate_android.h"
12 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
13 #include "content/public/browser/notification_service.h"
15 using content::NotificationService
;
17 // Keep this in sync with
18 // chrome/android/java/src/org/chromium/chrome/browser/tabmodel/TabList.java
19 static int INVALID_TAB_INDEX
= -1;
21 TabModel::TabModel(Profile
* profile
)
23 synced_window_delegate_(
24 new browser_sync::SyncedWindowDelegateAndroid(this)) {
27 // A normal Profile creates an OTR profile if it does not exist when
28 // GetOffTheRecordProfile() is called, so we guard it with
29 // HasOffTheRecordProfile(). An OTR profile returns itself when you call
30 // GetOffTheRecordProfile().
31 is_off_the_record_
= (profile
->HasOffTheRecordProfile() &&
32 profile
== profile
->GetOffTheRecordProfile());
34 // A profile can be destroyed, for example in the case of closing all
35 // incognito tabs. We therefore must listen for when this happens, and
36 // remove our pointer to the profile accordingly.
37 registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED
,
38 content::Source
<Profile
>(profile_
));
39 registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_CREATED
,
40 content::NotificationService::AllSources());
42 is_off_the_record_
= false;
46 TabModel::~TabModel() {
49 Profile
* TabModel::GetProfile() const {
53 bool TabModel::IsOffTheRecord() const {
54 return is_off_the_record_
;
57 browser_sync::SyncedWindowDelegate
* TabModel::GetSyncedWindowDelegate() const {
58 return synced_window_delegate_
.get();
61 SessionID::id_type
TabModel::GetSessionId() const {
62 return session_id_
.id();
65 content::WebContents
* TabModel::GetActiveWebContents() const {
66 int active_index
= GetActiveIndex();
67 if (active_index
== INVALID_TAB_INDEX
)
69 return GetWebContentsAt(active_index
);
72 void TabModel::BroadcastSessionRestoreComplete() {
74 NotificationService::current()->Notify(
75 chrome::NOTIFICATION_SESSION_RESTORE_COMPLETE
,
76 content::Source
<Profile
>(profile_
),
77 NotificationService::NoDetails());
79 // TODO(nyquist): Uncomment this once downstream Android uses new
80 // constructor that takes a Profile* argument. See crbug.com/159704.
85 void TabModel::Observe(
87 const content::NotificationSource
& source
,
88 const content::NotificationDetails
& details
) {
90 case chrome::NOTIFICATION_PROFILE_DESTROYED
:
91 // Our profile just got destroyed, so we delete our pointer to it.
94 case chrome::NOTIFICATION_PROFILE_CREATED
:
95 // Our incognito tab model out lives the profile, so we need to recapture
96 // the pointer if ours was previously deleted.
97 // NOTIFICATION_PROFILE_DESTROYED is not sent for every destruction, so
98 // we overwrite the pointer regardless of whether it's NULL.
99 if (is_off_the_record_
) {
100 Profile
* profile
= content::Source
<Profile
>(source
).ptr();
101 if (profile
&& profile
->IsOffTheRecord())