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_model.h"
6
7 #include "chrome/browser/ui/search/search_model_observer.h"
8 #include "components/search/search.h"
9
State()10 SearchModel::State::State()
11 : instant_support(INSTANT_SUPPORT_UNKNOWN),
12 voice_search_supported(false) {
13 }
14
State(const SearchMode & mode,InstantSupportState instant_support,bool voice_search_supported)15 SearchModel::State::State(const SearchMode& mode,
16 InstantSupportState instant_support,
17 bool voice_search_supported)
18 : mode(mode),
19 instant_support(instant_support),
20 voice_search_supported(voice_search_supported) {
21 }
22
operator ==(const State & rhs) const23 bool SearchModel::State::operator==(const State& rhs) const {
24 return mode == rhs.mode && instant_support == rhs.instant_support &&
25 voice_search_supported == rhs.voice_search_supported;
26 }
27
SearchModel()28 SearchModel::SearchModel() {
29 }
30
~SearchModel()31 SearchModel::~SearchModel() {
32 }
33
SetState(const State & new_state)34 void SearchModel::SetState(const State& new_state) {
35 DCHECK(chrome::IsInstantExtendedAPIEnabled())
36 << "Please do not try to set the SearchModel mode without first "
37 << "checking if Search is enabled.";
38
39 if (state_ == new_state)
40 return;
41
42 const State old_state = state_;
43 state_ = new_state;
44
45 FOR_EACH_OBSERVER(SearchModelObserver, observers_,
46 ModelChanged(old_state, state_));
47 }
48
SetMode(const SearchMode & new_mode)49 void SearchModel::SetMode(const SearchMode& new_mode) {
50 DCHECK(chrome::IsInstantExtendedAPIEnabled())
51 << "Please do not try to set the SearchModel mode without first "
52 << "checking if Search is enabled.";
53
54 if (state_.mode == new_mode)
55 return;
56
57 const State old_state = state_;
58 state_.mode = new_mode;
59
60 FOR_EACH_OBSERVER(SearchModelObserver, observers_,
61 ModelChanged(old_state, state_));
62 }
63
SetInstantSupportState(InstantSupportState instant_support)64 void SearchModel::SetInstantSupportState(InstantSupportState instant_support) {
65 DCHECK(chrome::IsInstantExtendedAPIEnabled())
66 << "Please do not try to set the SearchModel state without first "
67 << "checking if Search is enabled.";
68
69 if (state_.instant_support == instant_support)
70 return;
71
72 const State old_state = state_;
73 state_.instant_support = instant_support;
74 FOR_EACH_OBSERVER(SearchModelObserver, observers_,
75 ModelChanged(old_state, state_));
76 }
77
SetVoiceSearchSupported(bool supported)78 void SearchModel::SetVoiceSearchSupported(bool supported) {
79 DCHECK(chrome::IsInstantExtendedAPIEnabled())
80 << "Please do not try to set the SearchModel state without first "
81 << "checking if Search is enabled.";
82
83 if (state_.voice_search_supported == supported)
84 return;
85
86 const State old_state = state_;
87 state_.voice_search_supported = supported;
88
89 FOR_EACH_OBSERVER(SearchModelObserver, observers_,
90 ModelChanged(old_state, state_));
91 }
92
AddObserver(SearchModelObserver * observer)93 void SearchModel::AddObserver(SearchModelObserver* observer) {
94 observers_.AddObserver(observer);
95 }
96
RemoveObserver(SearchModelObserver * observer)97 void SearchModel::RemoveObserver(SearchModelObserver* observer) {
98 observers_.RemoveObserver(observer);
99 }
100