OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/instant/instant_service.h" | 5 #include "chrome/browser/instant/instant_service.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/history/top_sites.h" | |
9 #include "chrome/browser/instant/instant_loader.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/themes/theme_properties.h" | |
12 #include "chrome/browser/themes/theme_service.h" | |
13 #include "chrome/browser/themes/theme_service_factory.h" | |
14 #include "chrome/browser/ui/search/search.h" | |
15 #include "chrome/common/chrome_notification_types.h" | |
16 #include "chrome/common/pref_names.h" | |
7 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
8 #include "content/public/browser/notification_types.h" | |
9 #include "content/public/browser/render_process_host.h" | 18 #include "content/public/browser/render_process_host.h" |
19 #include "grit/theme_resources.h" | |
20 #include "ui/base/resource/resource_bundle.h" | |
21 #include "ui/gfx/image/image_skia.h" | |
22 #include "ui/gfx/sys_color_change_listener.h" | |
10 | 23 |
11 InstantService::InstantService() { | 24 namespace { |
12 registrar_.Add(this, | 25 |
13 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 26 const size_t kMaxDebugEvents = 2000; |
14 content::NotificationService::AllSources()); | 27 |
28 } // namespace | |
29 | |
30 InstantService::InstantService(Profile* profile) | |
31 : profile_(profile), | |
32 omnibox_font_size_(0), | |
33 omnibox_start_margin_(0), | |
34 instant_pref_enabled_(false), | |
35 instant_enabled_(false) { | |
36 notification_registrar_.Add(this, | |
37 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
38 content::NotificationService::AllSources()); | |
39 | |
40 chrome::search::SetInstantExtendedPrefDefault(profile); | |
41 pref_change_registrar_.Init(profile->GetPrefs()); | |
42 pref_change_registrar_.Add(prefs::kInstantEnabled, | |
43 base::Bind(&InstantService::PrefChanged, base::Unretained(this))); | |
44 pref_change_registrar_.Add(prefs::kInstantExtendedEnabled, | |
45 base::Bind(&InstantService::PrefChanged, base::Unretained(this))); | |
46 pref_change_registrar_.Add(prefs::kSearchSuggestEnabled, | |
47 base::Bind(&InstantService::PrefChanged, base::Unretained(this))); | |
48 PrefChanged(""); | |
49 | |
50 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile); | |
51 notification_registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | |
52 content::Source<ThemeService>(theme_service)); | |
53 ParseTheme(theme_service); | |
54 | |
55 history::TopSites* top_sites = profile->GetTopSites(); | |
56 notification_registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | |
57 content::Source<history::TopSites>(top_sites)); | |
58 RequestMostVisitedItems(top_sites); | |
59 | |
60 // TODO(sail): Remove this once the Mac omnibox font size is updated. | |
61 #if defined(OS_MACOSX) | |
62 ui::ResourceBundle::FontStyle font_style = ui::ResourceBundle::BaseFont; | |
63 #else | |
64 ui::ResourceBundle::FontStyle font_style = ui::ResourceBundle::MediumFont; | |
65 #endif // defined(OS_MACOSX) | |
66 const gfx::Font& omnibox_font = | |
67 ui::ResourceBundle::GetSharedInstance().GetFont(font_style); | |
68 omnibox_font_name_ = UTF8ToUTF16(omnibox_font.GetFontName()); | |
69 omnibox_font_size_ = omnibox_font.GetFontSize(); | |
15 } | 70 } |
16 | 71 |
17 InstantService::~InstantService() { | 72 InstantService::~InstantService() { |
18 } | 73 } |
19 | 74 |
75 void InstantService::LogDebugEvent(const std::string& message) { | |
76 DVLOG(1) << message; | |
77 debug_events_.push_front(std::make_pair(base::Time::Now().ToInternalValue(), | |
78 message)); | |
79 if (debug_events_.size() > kMaxDebugEvents) | |
80 debug_events_.pop_back(); | |
81 } | |
82 | |
83 void InstantService::AddObserver(InstantServiceObserver* observer) { | |
84 observers_.AddObserver(observer); | |
85 if (observers_.size() == 1) | |
samarth
2013/03/01 17:59:53
Huh, can you explain?
sreeram
2013/03/07 18:18:46
Done.
| |
86 loader_.reset(new InstantLoader(this)); | |
87 } | |
88 | |
89 void InstantService::RemoveObserver(InstantServiceObserver* observer) { | |
90 observers_.RemoveObserver(observer); | |
91 if (observers_.size() == 1) | |
92 loader_.reset(); | |
93 } | |
94 | |
20 void InstantService::AddInstantProcess(int process_id) { | 95 void InstantService::AddInstantProcess(int process_id) { |
21 process_ids_.insert(process_id); | 96 process_ids_.insert(process_id); |
22 } | 97 } |
23 | 98 |
24 bool InstantService::IsInstantProcess(int process_id) const { | 99 bool InstantService::IsInstantProcess(int process_id) const { |
25 return process_ids_.find(process_id) != process_ids_.end(); | 100 return process_ids_.find(process_id) != process_ids_.end(); |
26 } | 101 } |
27 | 102 |
28 void InstantService::Shutdown() { | 103 void InstantService::InstantSupportDecided() { |
29 process_ids_.clear(); | 104 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, |
105 InstantSupportDecided()); | |
30 } | 106 } |
31 | 107 |
32 void InstantService::Observe(int type, | 108 void InstantService::Observe(int type, |
33 const content::NotificationSource& source, | 109 const content::NotificationSource& source, |
34 const content::NotificationDetails& details) { | 110 const content::NotificationDetails& details) { |
35 int process_id = content::Source<content::RenderProcessHost>(source)->GetID(); | 111 switch (type) { |
36 process_ids_.erase(process_id); | 112 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
113 process_ids_.erase( | |
114 content::Source<content::RenderProcessHost>(source)->GetID()); | |
115 break; | |
116 | |
117 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: | |
118 ParseTheme(content::Source<ThemeService>(source).ptr()); | |
119 break; | |
120 | |
121 case chrome::NOTIFICATION_TOP_SITES_CHANGED: | |
122 RequestMostVisitedItems(content::Source<history::TopSites>(source).ptr()); | |
123 break; | |
124 | |
125 default: | |
126 NOTREACHED(); | |
127 break; | |
128 } | |
37 } | 129 } |
130 | |
131 void InstantService::PrefChanged(const std::string& pref_name) { | |
132 if (pref_name == prefs::kInstantEnabled) | |
133 chrome::search::SetInstantExtendedPrefDefault(profile_); | |
134 | |
135 bool instant_pref_enabled = chrome::search::IsInstantPrefEnabled(profile_); | |
136 bool instant_enabled = chrome::search::IsInstantEnabled(profile_); | |
137 if (instant_pref_enabled_ == instant_pref_enabled && | |
138 instant_enabled_ == instant_enabled) | |
139 return; | |
140 | |
141 instant_pref_enabled_ = instant_pref_enabled; | |
142 instant_enabled_ = instant_enabled; | |
143 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, InstantStatusChanged()); | |
144 } | |
145 | |
146 void InstantService::ParseTheme(ThemeService* theme_service) { | |
147 theme_info_ = ThemeBackgroundInfo(); | |
148 | |
149 // Set theme background color. | |
150 SkColor background_color = | |
151 theme_service->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND); | |
152 if (gfx::IsInvertedColorScheme()) | |
153 background_color = color_utils::InvertColor(background_color); | |
154 theme_info_.color_r = SkColorGetR(background_color); | |
155 theme_info_.color_g = SkColorGetG(background_color); | |
156 theme_info_.color_b = SkColorGetB(background_color); | |
157 theme_info_.color_a = SkColorGetA(background_color); | |
158 | |
159 if (!theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) | |
160 return; | |
161 | |
162 // Set theme id for theme background image url. | |
163 theme_info_.theme_id = UTF8ToUTF16(theme_service->GetThemeID()); | |
164 | |
165 // Set theme background image horizontal alignment. | |
166 int alignment = 0; | |
167 theme_service->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_ALIGNMENT, | |
168 &alignment); | |
169 if (alignment & ThemeProperties::ALIGN_LEFT) { | |
170 theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT; | |
171 } else if (alignment & ThemeProperties::ALIGN_RIGHT) { | |
172 theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_RIGHT; | |
173 } else { // ALIGN_CENTER | |
174 theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER; | |
175 } | |
176 | |
177 // Set theme background image vertical alignment. | |
178 if (alignment & ThemeProperties::ALIGN_TOP) { | |
179 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP; | |
180 } else if (alignment & ThemeProperties::ALIGN_BOTTOM) { | |
181 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM; | |
182 } else { // ALIGN_CENTER | |
183 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER; | |
184 } | |
185 | |
186 // Set theme background image tiling. | |
187 int tiling = 0; | |
188 theme_service->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_TILING, | |
189 &tiling); | |
190 switch (tiling) { | |
191 case ThemeProperties::NO_REPEAT: | |
192 theme_info_.image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT; | |
193 break; | |
194 case ThemeProperties::REPEAT_X: | |
195 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_X; | |
196 break; | |
197 case ThemeProperties::REPEAT_Y: | |
198 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y; | |
199 break; | |
200 case ThemeProperties::REPEAT: | |
201 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT; | |
202 break; | |
203 } | |
204 | |
205 // Set theme background image height. | |
206 gfx::ImageSkia* image = theme_service->GetImageSkiaNamed( | |
207 IDR_THEME_NTP_BACKGROUND); | |
208 DCHECK(image); | |
209 theme_info_.image_height = image->height(); | |
210 | |
211 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, ThemeInfoChanged()); | |
212 } | |
213 | |
214 void InstantService::RequestMostVisitedItems(history::TopSites* top_sites) { | |
215 top_sites->GetMostVisitedURLs( | |
216 base::Bind(&InstantService::MostVisitedItemsReceived, | |
217 base::Unretained(this))); | |
218 } | |
219 | |
220 void InstantService::MostVisitedItemsReceived( | |
221 const history::MostVisitedURLList& data) { | |
222 most_visited_items_.clear(); | |
223 for (size_t i = 0; i < data.size(); ++i) | |
224 most_visited_items_.push_back(MostVisitedItem(data[i].url, data[i].title)); | |
225 | |
226 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, | |
227 MostVisitedItemsChanged()); | |
228 } | |
OLD | NEW |