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/search/instant_service.h" | 5 #include "chrome/browser/search/instant_service.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 color.g = SkColorGetG(sKColor); | 60 color.g = SkColorGetG(sKColor); |
61 color.b = SkColorGetB(sKColor); | 61 color.b = SkColorGetB(sKColor); |
62 color.a = SkColorGetA(sKColor); | 62 color.a = SkColorGetA(sKColor); |
63 return color; | 63 return color; |
64 } | 64 } |
65 | 65 |
66 } // namespace | 66 } // namespace |
67 | 67 |
68 InstantService::InstantService(Profile* profile) | 68 InstantService::InstantService(Profile* profile) |
69 : profile_(profile), | 69 : profile_(profile), |
70 ntp_prerenderer_(profile, this, profile->GetPrefs()), | |
71 browser_instant_controller_object_count_(0), | |
72 weak_ptr_factory_(this) { | 70 weak_ptr_factory_(this) { |
73 // Stub for unit tests. | 71 // Stub for unit tests. |
74 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 72 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
75 return; | 73 return; |
76 | 74 |
77 ResetInstantSearchPrerenderer(); | 75 ResetInstantSearchPrerenderer(); |
78 | 76 |
79 registrar_.Add(this, | 77 registrar_.Add(this, |
80 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 78 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
81 content::NotificationService::AllSources()); | 79 content::NotificationService::AllSources()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); | 115 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); |
118 | 116 |
119 profile_pref_registrar_.Init(profile_->GetPrefs()); | 117 profile_pref_registrar_.Init(profile_->GetPrefs()); |
120 profile_pref_registrar_.Add( | 118 profile_pref_registrar_.Add( |
121 prefs::kDefaultSearchProviderID, | 119 prefs::kDefaultSearchProviderID, |
122 base::Bind(&InstantService::OnDefaultSearchProviderChanged, | 120 base::Bind(&InstantService::OnDefaultSearchProviderChanged, |
123 base::Unretained(this))); | 121 base::Unretained(this))); |
124 | 122 |
125 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | 123 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
126 content::Source<Profile>(profile_->GetOriginalProfile())); | 124 content::Source<Profile>(profile_->GetOriginalProfile())); |
127 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | |
128 content::Source<Profile>(profile_)); | |
129 } | 125 } |
130 | 126 |
131 InstantService::~InstantService() { | 127 InstantService::~InstantService() { |
132 } | 128 } |
133 | 129 |
134 void InstantService::AddInstantProcess(int process_id) { | 130 void InstantService::AddInstantProcess(int process_id) { |
135 process_ids_.insert(process_id); | 131 process_ids_.insert(process_id); |
136 | 132 |
137 if (instant_io_context_.get()) { | 133 if (instant_io_context_.get()) { |
138 BrowserThread::PostTask(BrowserThread::IO, | 134 BrowserThread::PostTask(BrowserThread::IO, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 if (instant_io_context_.get()) { | 194 if (instant_io_context_.get()) { |
199 BrowserThread::PostTask( | 195 BrowserThread::PostTask( |
200 BrowserThread::IO, | 196 BrowserThread::IO, |
201 FROM_HERE, | 197 FROM_HERE, |
202 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 198 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
203 instant_io_context_)); | 199 instant_io_context_)); |
204 } | 200 } |
205 instant_io_context_ = NULL; | 201 instant_io_context_ = NULL; |
206 } | 202 } |
207 | 203 |
208 scoped_ptr<content::WebContents> InstantService::ReleaseNTPContents() { | |
209 return ntp_prerenderer_.ReleaseNTPContents(); | |
210 } | |
211 | |
212 content::WebContents* InstantService::GetNTPContents() const { | |
213 return ntp_prerenderer_.GetNTPContents(); | |
214 } | |
215 | |
216 void InstantService::OnBrowserInstantControllerCreated() { | |
217 if (profile_->IsOffTheRecord()) | |
218 return; | |
219 | |
220 ++browser_instant_controller_object_count_; | |
221 | |
222 if (browser_instant_controller_object_count_ == 1) | |
223 ntp_prerenderer_.ReloadInstantNTP(); | |
224 } | |
225 | |
226 void InstantService::OnBrowserInstantControllerDestroyed() { | |
227 if (profile_->IsOffTheRecord()) | |
228 return; | |
229 | |
230 DCHECK_GT(browser_instant_controller_object_count_, 0U); | |
231 --browser_instant_controller_object_count_; | |
232 | |
233 // All browser windows have closed, so release the InstantNTP resources to | |
234 // work around http://crbug.com/180810. | |
235 if (browser_instant_controller_object_count_ == 0) | |
236 ntp_prerenderer_.DeleteNTPContents(); | |
237 } | |
238 | |
239 void InstantService::Observe(int type, | 204 void InstantService::Observe(int type, |
240 const content::NotificationSource& source, | 205 const content::NotificationSource& source, |
241 const content::NotificationDetails& details) { | 206 const content::NotificationDetails& details) { |
242 switch (type) { | 207 switch (type) { |
243 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: | 208 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: |
244 SendSearchURLsToRenderer( | 209 SendSearchURLsToRenderer( |
245 content::Source<content::RenderProcessHost>(source).ptr()); | 210 content::Source<content::RenderProcessHost>(source).ptr()); |
246 break; | 211 break; |
247 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | 212 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
248 OnRendererProcessTerminated( | 213 OnRendererProcessTerminated( |
249 content::Source<content::RenderProcessHost>(source)->GetID()); | 214 content::Source<content::RenderProcessHost>(source)->GetID()); |
250 break; | 215 break; |
251 case chrome::NOTIFICATION_TOP_SITES_CHANGED: { | 216 case chrome::NOTIFICATION_TOP_SITES_CHANGED: { |
252 history::TopSites* top_sites = profile_->GetTopSites(); | 217 history::TopSites* top_sites = profile_->GetTopSites(); |
253 if (top_sites) { | 218 if (top_sites) { |
254 top_sites->GetMostVisitedURLs( | 219 top_sites->GetMostVisitedURLs( |
255 base::Bind(&InstantService::OnMostVisitedItemsReceived, | 220 base::Bind(&InstantService::OnMostVisitedItemsReceived, |
256 weak_ptr_factory_.GetWeakPtr()), false); | 221 weak_ptr_factory_.GetWeakPtr()), false); |
257 } | 222 } |
258 break; | 223 break; |
259 } | 224 } |
260 #if defined(ENABLE_THEMES) | 225 #if defined(ENABLE_THEMES) |
261 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { | 226 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { |
262 OnThemeChanged(content::Source<ThemeService>(source).ptr()); | 227 OnThemeChanged(content::Source<ThemeService>(source).ptr()); |
263 break; | 228 break; |
264 } | 229 } |
265 #endif // defined(ENABLE_THEMES) | 230 #endif // defined(ENABLE_THEMES) |
266 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | |
267 // Last chance to delete InstantNTP contents. We generally delete | |
268 // preloaded InstantNTP when the last BrowserInstantController object is | |
269 // destroyed. When the browser shutdown happens without closing browsers, | |
270 // there is a race condition between BrowserInstantController destruction | |
271 // and Profile destruction. | |
272 if (GetNTPContents()) | |
273 ntp_prerenderer_.DeleteNTPContents(); | |
274 break; | |
275 } | |
276 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: { | 231 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: { |
277 OnGoogleURLUpdated( | 232 OnGoogleURLUpdated( |
278 content::Source<Profile>(source).ptr(), | 233 content::Source<Profile>(source).ptr(), |
279 content::Details<GoogleURLTracker::UpdatedDetails>(details).ptr()); | 234 content::Details<GoogleURLTracker::UpdatedDetails>(details).ptr()); |
280 break; | 235 break; |
281 } | 236 } |
282 default: | 237 default: |
283 NOTREACHED() << "Unexpected notification type in InstantService."; | 238 NOTREACHED() << "Unexpected notification type in InstantService."; |
284 } | 239 } |
285 } | 240 } |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 // could cause that, neither of which we support. | 424 // could cause that, neither of which we support. |
470 return; | 425 return; |
471 } | 426 } |
472 | 427 |
473 ResetInstantSearchPrerenderer(); | 428 ResetInstantSearchPrerenderer(); |
474 | 429 |
475 FOR_EACH_OBSERVER( | 430 FOR_EACH_OBSERVER( |
476 InstantServiceObserver, observers_, DefaultSearchProviderChanged()); | 431 InstantServiceObserver, observers_, DefaultSearchProviderChanged()); |
477 } | 432 } |
478 | 433 |
479 InstantNTPPrerenderer* InstantService::ntp_prerenderer() { | |
480 return &ntp_prerenderer_; | |
481 } | |
482 | |
483 void InstantService::ResetInstantSearchPrerenderer() { | 434 void InstantService::ResetInstantSearchPrerenderer() { |
484 if (!chrome::ShouldPrefetchSearchResults()) | 435 if (!chrome::ShouldPrefetchSearchResults()) |
485 return; | 436 return; |
486 | 437 |
487 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); | 438 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); |
488 if (url.is_valid()) | 439 if (url.is_valid()) |
489 instant_prerenderer_.reset(new InstantSearchPrerenderer(profile_, url)); | 440 instant_prerenderer_.reset(new InstantSearchPrerenderer(profile_, url)); |
490 else | 441 else |
491 instant_prerenderer_.reset(); | 442 instant_prerenderer_.reset(); |
492 } | 443 } |
OLD | NEW |