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/search_engines/search_terms_data.h"
12 #include "chrome/browser/sync/glue/synced_window_delegate_android.h"
13 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
14 #include "content/public/browser/notification_service.h"
16 using content::NotificationService
;
18 TabModel::TabModel(Profile
* profile
)
20 synced_window_delegate_(
21 new browser_sync::SyncedWindowDelegateAndroid(this)) {
24 // A normal Profile creates an OTR profile if it does not exist when
25 // GetOffTheRecordProfile() is called, so we guard it with
26 // HasOffTheRecordProfile(). An OTR profile returns itself when you call
27 // GetOffTheRecordProfile().
28 is_off_the_record_
= (profile
->HasOffTheRecordProfile() &&
29 profile
== profile
->GetOffTheRecordProfile());
31 // A profile can be destroyed, for example in the case of closing all
32 // incognito tabs. We therefore must listen for when this happens, and
33 // remove our pointer to the profile accordingly.
34 registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED
,
35 content::Source
<Profile
>(profile_
));
36 registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_CREATED
,
37 content::NotificationService::AllSources());
39 is_off_the_record_
= false;
43 TabModel::~TabModel() {
46 Profile
* TabModel::GetProfile() const {
50 bool TabModel::IsOffTheRecord() const {
51 return is_off_the_record_
;
54 browser_sync::SyncedWindowDelegate
* TabModel::GetSyncedWindowDelegate() const {
55 return synced_window_delegate_
.get();
58 SessionID::id_type
TabModel::GetSessionId() const {
59 return session_id_
.id();
62 void TabModel::BroadcastSessionRestoreComplete() {
64 NotificationService::current()->Notify(
65 chrome::NOTIFICATION_SESSION_RESTORE_COMPLETE
,
66 content::Source
<Profile
>(profile_
),
67 NotificationService::NoDetails());
69 // TODO(nyquist): Uncomment this once downstream Android uses new
70 // constructor that takes a Profile* argument. See crbug.com/159704.
75 void TabModel::Observe(
77 const content::NotificationSource
& source
,
78 const content::NotificationDetails
& details
) {
80 case chrome::NOTIFICATION_PROFILE_DESTROYED
:
81 // Our profile just got destroyed, so we delete our pointer to it.
84 case chrome::NOTIFICATION_PROFILE_CREATED
:
85 // Our incognito tab model out lives the profile, so we need to recapture
86 // the pointer if ours was previously deleted.
87 // NOTIFICATION_PROFILE_DESTROYED is not sent for every destruction, so
88 // we overwrite the pointer regardless of whether it's NULL.
89 if (is_off_the_record_
) {
90 Profile
* profile
= content::Source
<Profile
>(source
).ptr();
91 if (profile
&& profile
->IsOffTheRecord())