Chromium Code Reviews| 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/ui/search/instant_ntp_prerenderer.h" | 5 #include "chrome/browser/ui/search/instant_ntp_prerenderer.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 PrefService* prefs) | 48 PrefService* prefs) |
| 49 : profile_(profile), | 49 : profile_(profile), |
| 50 extended_enabled_(chrome::IsInstantExtendedAPIEnabled()) { | 50 extended_enabled_(chrome::IsInstantExtendedAPIEnabled()) { |
| 51 DCHECK(profile); | 51 DCHECK(profile); |
| 52 | 52 |
| 53 // In unit tests, prefs may be NULL. | 53 // In unit tests, prefs may be NULL. |
| 54 if (prefs) { | 54 if (prefs) { |
| 55 profile_pref_registrar_.Init(prefs); | 55 profile_pref_registrar_.Init(prefs); |
| 56 profile_pref_registrar_.Add( | 56 profile_pref_registrar_.Add( |
| 57 prefs::kSearchSuggestEnabled, | 57 prefs::kSearchSuggestEnabled, |
| 58 base::Bind(&InstantNTPPrerenderer::ReloadStaleNTP, | 58 base::Bind(&InstantNTPPrerenderer::ReloadInstantNTP, |
| 59 base::Unretained(this))); | |
| 60 profile_pref_registrar_.Add( | |
| 61 prefs::kDefaultSearchProviderID, | |
| 62 base::Bind(&InstantNTPPrerenderer::OnDefaultSearchProviderChanged, | |
| 63 base::Unretained(this))); | 59 base::Unretained(this))); |
| 64 } | 60 } |
| 65 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 61 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 66 } | 62 } |
| 67 | 63 |
| 68 InstantNTPPrerenderer::~InstantNTPPrerenderer() { | 64 InstantNTPPrerenderer::~InstantNTPPrerenderer() { |
| 69 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 65 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 70 } | 66 } |
| 71 | 67 |
| 72 void InstantNTPPrerenderer::PreloadInstantNTP() { | 68 void InstantNTPPrerenderer::ReloadInstantNTP() { ResetNTP(GetInstantURL()); } |
|
samarth
2013/08/02 21:51:15
nit: please keep on its own line.
Anuj
2013/08/09 07:22:00
Done.
| |
| 73 DCHECK(!ntp()); | |
| 74 ReloadStaleNTP(); | |
| 75 } | |
| 76 | 69 |
| 77 scoped_ptr<content::WebContents> InstantNTPPrerenderer::ReleaseNTPContents() { | 70 scoped_ptr<content::WebContents> InstantNTPPrerenderer::ReleaseNTPContents() { |
| 78 if (!extended_enabled() || !profile_ || profile_->IsOffTheRecord() || | 71 if (!extended_enabled() || !profile_ || profile_->IsOffTheRecord() || |
| 79 !chrome::ShouldShowInstantNTP()) | 72 !chrome::ShouldShowInstantNTP()) |
| 80 return scoped_ptr<content::WebContents>(); | 73 return scoped_ptr<content::WebContents>(); |
| 81 | 74 |
| 82 if (ShouldSwitchToLocalNTP()) | 75 if (ShouldSwitchToLocalNTP()) |
| 83 ResetNTP(GetLocalInstantURL()); | 76 ResetNTP(GetLocalInstantURL()); |
| 84 | 77 |
| 85 scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents(); | 78 scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents(); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 void InstantNTPPrerenderer::InstantPageLoadFailed( | 231 void InstantNTPPrerenderer::InstantPageLoadFailed( |
| 239 content::WebContents* contents) { | 232 content::WebContents* contents) { |
| 240 DCHECK(ntp() && ntp()->contents() == contents); | 233 DCHECK(ntp() && ntp()->contents() == contents); |
| 241 | 234 |
| 242 bool is_local = ntp()->IsLocal(); | 235 bool is_local = ntp()->IsLocal(); |
| 243 DeleteNTPSoon(ntp_.Pass()); | 236 DeleteNTPSoon(ntp_.Pass()); |
| 244 if (!is_local) | 237 if (!is_local) |
| 245 ResetNTP(GetLocalInstantURL()); | 238 ResetNTP(GetLocalInstantURL()); |
| 246 } | 239 } |
| 247 | 240 |
| 248 void InstantNTPPrerenderer::OnDefaultSearchProviderChanged( | |
| 249 const std::string& pref_name) { | |
| 250 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); | |
| 251 if (!extended_enabled() || !ntp()) | |
| 252 return; | |
| 253 | |
| 254 ResetNTP(GetInstantURL()); | |
| 255 } | |
| 256 | |
| 257 void InstantNTPPrerenderer::ResetNTP(const std::string& instant_url) { | 241 void InstantNTPPrerenderer::ResetNTP(const std::string& instant_url) { |
| 258 // Instant NTP is only used in extended mode so we should always have a | 242 // Instant NTP is only used in extended mode so we should always have a |
| 259 // non-empty URL to use. | 243 // non-empty URL to use. |
| 260 DCHECK(!instant_url.empty()); | 244 DCHECK(!instant_url.empty()); |
| 261 ntp_.reset(new InstantNTP(this, instant_url, profile_)); | 245 ntp_.reset(new InstantNTP(this, instant_url, profile_)); |
| 262 ntp_->InitContents(base::Bind(&InstantNTPPrerenderer::ReloadStaleNTP, | 246 ntp_->InitContents(base::Bind(&InstantNTPPrerenderer::ReloadInstantNTP, |
| 263 base::Unretained(this))); | 247 base::Unretained(this))); |
| 264 } | 248 } |
| 265 | 249 |
| 266 void InstantNTPPrerenderer::ReloadStaleNTP() { | |
| 267 if (!extended_enabled()) | |
| 268 return; | |
| 269 | |
| 270 ResetNTP(GetInstantURL()); | |
| 271 } | |
| 272 | |
| 273 bool InstantNTPPrerenderer::PageIsCurrent() const { | 250 bool InstantNTPPrerenderer::PageIsCurrent() const { |
| 274 const std::string& instant_url = GetInstantURL(); | 251 const std::string& instant_url = GetInstantURL(); |
| 275 if (instant_url.empty() || | 252 if (instant_url.empty() || |
| 276 !chrome::MatchesOriginAndPath(GURL(ntp()->instant_url()), | 253 !chrome::MatchesOriginAndPath(GURL(ntp()->instant_url()), |
| 277 GURL(instant_url))) | 254 GURL(instant_url))) |
| 278 return false; | 255 return false; |
| 279 | 256 |
| 280 return ntp()->supports_instant(); | 257 return ntp()->supports_instant(); |
| 281 } | 258 } |
| 282 | 259 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 294 return false; | 271 return false; |
| 295 | 272 |
| 296 if (PageIsCurrent()) | 273 if (PageIsCurrent()) |
| 297 return false; | 274 return false; |
| 298 | 275 |
| 299 // The preloaded NTP does not support instant yet. If we're not in startup, | 276 // The preloaded NTP does not support instant yet. If we're not in startup, |
| 300 // always fall back to the local NTP. If we are in startup, use the local NTP | 277 // always fall back to the local NTP. If we are in startup, use the local NTP |
| 301 // (unless the finch flag to use the remote NTP is set). | 278 // (unless the finch flag to use the remote NTP is set). |
| 302 return !(InStartup() && chrome::ShouldPreferRemoteNTPOnStartup()); | 279 return !(InStartup() && chrome::ShouldPreferRemoteNTPOnStartup()); |
| 303 } | 280 } |
| 281 | |
| 282 //////////////////////////////////////////////////////////////////////////////// | |
| 283 // InstantNTPPrerenderer, InstantServiceObserver implementation: | |
| 284 | |
| 285 void InstantNTPPrerenderer::DefaultSearchProviderChanged() { | |
| 286 ReloadInstantNTP(); | |
| 287 } | |
| 288 | |
| 289 void InstantNTPPrerenderer::GoogleURLUpdated() { ReloadInstantNTP(); } | |
|
samarth
2013/08/02 21:51:15
also here
Anuj
2013/08/09 07:22:00
Done.
| |
| OLD | NEW |