- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / android / tab_model / tab_model.cc
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.
4
5 #include "chrome/browser/ui/android/tab_model/tab_model.h"
6
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"
15
16 using content::NotificationService;
17
18 TabModel::TabModel(Profile* profile)
19   : profile_(profile),
20     synced_window_delegate_(
21         new browser_sync::SyncedWindowDelegateAndroid(this)),
22     toolbar_model_(new ToolbarModelImpl(this)) {
23
24   if (profile) {
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());
31
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());
39   } else {
40     is_off_the_record_ = false;
41   }
42 }
43
44 TabModel::~TabModel() {
45 }
46
47 content::WebContents* TabModel::GetActiveWebContents() const {
48   if (GetTabCount() == 0 || GetActiveIndex() < 0 ||
49       GetActiveIndex() > GetTabCount())
50     return NULL;
51   return GetWebContentsAt(GetActiveIndex());
52 }
53
54 Profile* TabModel::GetProfile() const {
55   return profile_;
56 }
57
58 bool TabModel::IsOffTheRecord() const {
59   return is_off_the_record_;
60 }
61
62 browser_sync::SyncedWindowDelegate* TabModel::GetSyncedWindowDelegate() const {
63   return synced_window_delegate_.get();
64 }
65
66 SessionID::id_type TabModel::GetSessionId() const {
67   return session_id_.id();
68 }
69
70 void TabModel::BroadcastSessionRestoreComplete() {
71   if (profile_) {
72     NotificationService::current()->Notify(
73         chrome::NOTIFICATION_SESSION_RESTORE_COMPLETE,
74         content::Source<Profile>(profile_),
75         NotificationService::NoDetails());
76   } else {
77     // TODO(nyquist): Uncomment this once downstream Android uses new
78     // constructor that takes a Profile* argument. See crbug.com/159704.
79     // NOTREACHED();
80   }
81 }
82
83 ToolbarModel* TabModel::GetToolbarModel() {
84   return toolbar_model_.get();
85 }
86
87 string16 TabModel::GetSearchTermsForCurrentTab() {
88   return toolbar_model_->GetText(true);
89 }
90
91 std::string TabModel::GetQueryExtractionParam() {
92   if (!profile_)
93     return std::string();
94   UIThreadSearchTermsData search_terms_data(profile_);
95   return search_terms_data.InstantExtendedEnabledParam();
96 }
97
98 string16 TabModel::GetCorpusNameForCurrentTab() {
99   return toolbar_model_->GetCorpusNameForMobile();
100 }
101
102 void TabModel::Observe(
103     int type,
104     const content::NotificationSource& source,
105     const content::NotificationDetails& details) {
106   switch (type) {
107     case chrome::NOTIFICATION_PROFILE_DESTROYED:
108       // Our profile just got destroyed, so we delete our pointer to it.
109       profile_ = NULL;
110       break;
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())
119           profile_ = profile;
120       }
121       break;
122     default:
123       NOTREACHED();
124   }
125 }