Home | History | Annotate | Download | only in search
      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/search/search_delegate.h"
      6 
      7 #include "chrome/browser/ui/search/search_model.h"
      8 #include "chrome/browser/ui/search/search_tab_helper.h"
      9 
     10 SearchDelegate::SearchDelegate(SearchModel* browser_search_model,
     11                                ToolbarModel* toolbar_model)
     12     : browser_model_(browser_search_model),
     13       tab_model_() {
     14 }
     15 
     16 SearchDelegate::~SearchDelegate() {
     17   DCHECK(!tab_model_) << "All tabs should have been deactivated or closed.";
     18 }
     19 
     20 void SearchDelegate::ModelChanged(const SearchModel::State& old_state,
     21                                   const SearchModel::State& new_state) {
     22   browser_model_->SetState(new_state);
     23 }
     24 
     25 void SearchDelegate::OnTabActivated(content::WebContents* web_contents) {
     26   if (tab_model_)
     27     tab_model_->RemoveObserver(this);
     28   tab_model_ = SearchTabHelper::FromWebContents(web_contents)->model();
     29   browser_model_->SetState(tab_model_->state());
     30   tab_model_->AddObserver(this);
     31 }
     32 
     33 void SearchDelegate::OnTabDeactivated(content::WebContents* web_contents) {
     34   StopObservingTab(web_contents);
     35 }
     36 
     37 void SearchDelegate::OnTabDetached(content::WebContents* web_contents) {
     38   StopObservingTab(web_contents);
     39 }
     40 
     41 void SearchDelegate::StopObservingTab(content::WebContents* web_contents) {
     42   SearchTabHelper* search_tab_helper =
     43       SearchTabHelper::FromWebContents(web_contents);
     44   if (search_tab_helper->model() == tab_model_) {
     45     tab_model_->RemoveObserver(this);
     46     tab_model_ = NULL;
     47   }
     48 }
     49