summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/search/search_delegate.cc
blob: 6c0ec71c1eaa6216523b38aa55be8352d3ad7d4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/search/search_delegate.h"

#include "chrome/browser/ui/search/search_model.h"
#include "chrome/browser/ui/search/search_tab_helper.h"

namespace chrome {
namespace search {

SearchDelegate::SearchDelegate(SearchModel* browser_search_model,
                               ToolbarModel* toolbar_model)
    : browser_model_(browser_search_model),
      tab_model_() {
}

SearchDelegate::~SearchDelegate() {
  DCHECK(!tab_model_) << "All tabs should have been deactivated or closed.";
}

void SearchDelegate::ModelChanged(const SearchModel::State& old_state,
                                  const SearchModel::State& new_state) {
  browser_model_->SetState(new_state);
}

void SearchDelegate::OnTabActivated(content::WebContents* web_contents) {
  if (tab_model_)
    tab_model_->RemoveObserver(this);
  tab_model_ =
      chrome::search::SearchTabHelper::FromWebContents(web_contents)->model();
  browser_model_->SetState(tab_model_->state());
  tab_model_->AddObserver(this);
}

void SearchDelegate::OnTabDeactivated(content::WebContents* web_contents) {
  StopObservingTab(web_contents);
}

void SearchDelegate::OnTabDetached(content::WebContents* web_contents) {
  StopObservingTab(web_contents);
}

void SearchDelegate::StopObservingTab(content::WebContents* web_contents) {
  chrome::search::SearchTabHelper* search_tab_helper =
      chrome::search::SearchTabHelper::FromWebContents(web_contents);
  if (search_tab_helper->model() == tab_model_) {
    tab_model_->RemoveObserver(this);
    tab_model_ = NULL;
  }
}

}  // namespace search
}  // namespace chrome