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)),
22 toolbar_model_(new ToolbarModelImpl(this)) {
25 // A normal Profile creates an OTR profile if it does not exist when
26 // GetOffTheRecordProfile() is called, so we guard it with
27 // HasOffTheRecordProfile(). An OTR profile returns itself when you call
28 // GetOffTheRecordProfile().
29 is_off_the_record_ = (profile->HasOffTheRecordProfile() &&
30 profile == profile->GetOffTheRecordProfile());
32 // A profile can be destroyed, for example in the case of closing all
33 // incognito tabs. We therefore must listen for when this happens, and
34 // remove our pointer to the profile accordingly.
35 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
36 content::Source<Profile>(profile_));
37 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
38 content::NotificationService::AllSources());
40 is_off_the_record_ = false;
44 TabModel::~TabModel() {
47 content::WebContents* TabModel::GetActiveWebContents() const {
48 if (GetTabCount() == 0 || GetActiveIndex() < 0 ||
49 GetActiveIndex() > GetTabCount())
51 return GetWebContentsAt(GetActiveIndex());
54 Profile* TabModel::GetProfile() const {
58 bool TabModel::IsOffTheRecord() const {
59 return is_off_the_record_;
62 browser_sync::SyncedWindowDelegate* TabModel::GetSyncedWindowDelegate() const {
63 return synced_window_delegate_.get();
66 SessionID::id_type TabModel::GetSessionId() const {
67 return session_id_.id();
70 void TabModel::BroadcastSessionRestoreComplete() {
72 NotificationService::current()->Notify(
73 chrome::NOTIFICATION_SESSION_RESTORE_COMPLETE,
74 content::Source<Profile>(profile_),
75 NotificationService::NoDetails());
77 // TODO(nyquist): Uncomment this once downstream Android uses new
78 // constructor that takes a Profile* argument. See crbug.com/159704.
83 ToolbarModel* TabModel::GetToolbarModel() {
84 return toolbar_model_.get();
87 string16 TabModel::GetSearchTermsForCurrentTab() {
88 return toolbar_model_->GetText(true);
91 std::string TabModel::GetQueryExtractionParam() {
94 UIThreadSearchTermsData search_terms_data(profile_);
95 return search_terms_data.InstantExtendedEnabledParam();
98 string16 TabModel::GetCorpusNameForCurrentTab() {
99 return toolbar_model_->GetCorpusNameForMobile();
102 void TabModel::Observe(
104 const content::NotificationSource& source,
105 const content::NotificationDetails& details) {
107 case chrome::NOTIFICATION_PROFILE_DESTROYED:
108 // Our profile just got destroyed, so we delete our pointer to it.
111 case chrome::NOTIFICATION_PROFILE_CREATED:
112 // Our incognito tab model out lives the profile, so we need to recapture
113 // the pointer if ours was previously deleted.
114 // NOTIFICATION_PROFILE_DESTROYED is not sent for every destruction, so
115 // we overwrite the pointer regardless of whether it's NULL.
116 if (is_off_the_record_) {
117 Profile* profile = content::Source<Profile>(source).ptr();
118 if (profile && profile->IsOffTheRecord())