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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 PrefService* prefs) | 46 PrefService* prefs) |
| 47 : profile_(profile), | 47 : profile_(profile), |
| 48 extended_enabled_(chrome::IsInstantExtendedAPIEnabled()) { | 48 extended_enabled_(chrome::IsInstantExtendedAPIEnabled()) { |
| 49 DCHECK(profile); | 49 DCHECK(profile); |
| 50 | 50 |
| 51 // In unit tests, prefs may be NULL. | 51 // In unit tests, prefs may be NULL. |
| 52 if (prefs) { | 52 if (prefs) { |
| 53 profile_pref_registrar_.Init(prefs); | 53 profile_pref_registrar_.Init(prefs); |
| 54 profile_pref_registrar_.Add( | 54 profile_pref_registrar_.Add( |
| 55 prefs::kSearchSuggestEnabled, | 55 prefs::kSearchSuggestEnabled, |
| 56 base::Bind(&InstantNTPPrerenderer::ReloadStaleNTP, | 56 base::Bind(&InstantNTPPrerenderer::ReloadInstantNTP, |
| 57 base::Unretained(this))); | |
| 58 profile_pref_registrar_.Add( | |
| 59 prefs::kDefaultSearchProviderID, | |
| 60 base::Bind(&InstantNTPPrerenderer::OnDefaultSearchProviderChanged, | |
| 61 base::Unretained(this))); | 57 base::Unretained(this))); |
| 62 } | 58 } |
| 63 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 59 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 64 } | 60 } |
| 65 | 61 |
| 66 InstantNTPPrerenderer::~InstantNTPPrerenderer() { | 62 InstantNTPPrerenderer::~InstantNTPPrerenderer() { |
| 67 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 63 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 68 } | 64 } |
| 69 | 65 |
| 70 void InstantNTPPrerenderer::PreloadInstantNTP() { | 66 void InstantNTPPrerenderer::ReloadInstantNTP() { |
| 71 DCHECK(!ntp()); | 67 DCHECK(!ntp()); |
|
samarth
2013/07/26 23:33:15
Aren't you going to trigger this DCHECK when it is
Anuj
2013/07/31 07:16:18
Done.
| |
| 72 ReloadStaleNTP(); | 68 ResetNTP(GetInstantURL()); |
| 73 } | 69 } |
| 74 | 70 |
| 75 scoped_ptr<content::WebContents> InstantNTPPrerenderer::ReleaseNTPContents() { | 71 scoped_ptr<content::WebContents> InstantNTPPrerenderer::ReleaseNTPContents() { |
| 76 if (!extended_enabled() || !profile_ || profile_->IsOffTheRecord() || | 72 if (!extended_enabled() || !profile_ || profile_->IsOffTheRecord() || |
| 77 !chrome::ShouldShowInstantNTP()) | 73 !chrome::ShouldShowInstantNTP()) |
| 78 return scoped_ptr<content::WebContents>(); | 74 return scoped_ptr<content::WebContents>(); |
| 79 | 75 |
| 80 if (ShouldSwitchToLocalNTP()) | 76 if (ShouldSwitchToLocalNTP()) |
| 81 ResetNTP(GetLocalInstantURL()); | 77 ResetNTP(GetLocalInstantURL()); |
| 82 | 78 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 void InstantNTPPrerenderer::InstantPageLoadFailed( | 215 void InstantNTPPrerenderer::InstantPageLoadFailed( |
| 220 content::WebContents* contents) { | 216 content::WebContents* contents) { |
| 221 DCHECK(ntp() && ntp()->contents() == contents); | 217 DCHECK(ntp() && ntp()->contents() == contents); |
| 222 | 218 |
| 223 bool is_local = ntp()->IsLocal(); | 219 bool is_local = ntp()->IsLocal(); |
| 224 DeleteNTPSoon(ntp_.Pass()); | 220 DeleteNTPSoon(ntp_.Pass()); |
| 225 if (!is_local) | 221 if (!is_local) |
| 226 ResetNTP(GetLocalInstantURL()); | 222 ResetNTP(GetLocalInstantURL()); |
| 227 } | 223 } |
| 228 | 224 |
| 229 void InstantNTPPrerenderer::OnDefaultSearchProviderChanged( | |
| 230 const std::string& pref_name) { | |
| 231 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); | |
| 232 if (!extended_enabled() || !ntp()) | |
| 233 return; | |
| 234 | |
| 235 ResetNTP(GetInstantURL()); | |
| 236 } | |
| 237 | |
| 238 void InstantNTPPrerenderer::ResetNTP(const std::string& instant_url) { | 225 void InstantNTPPrerenderer::ResetNTP(const std::string& instant_url) { |
| 239 // Instant NTP is only used in extended mode so we should always have a | 226 // Instant NTP is only used in extended mode so we should always have a |
| 240 // non-empty URL to use. | 227 // non-empty URL to use. |
| 241 DCHECK(!instant_url.empty()); | 228 DCHECK(!instant_url.empty()); |
| 242 ntp_.reset(new InstantNTP(this, instant_url, profile_)); | 229 ntp_.reset(new InstantNTP(this, instant_url, profile_)); |
| 243 ntp_->InitContents(base::Bind(&InstantNTPPrerenderer::ReloadStaleNTP, | 230 ntp_->InitContents(base::Bind(&InstantNTPPrerenderer::ReloadInstantNTP, |
| 244 base::Unretained(this))); | 231 base::Unretained(this))); |
| 245 } | 232 } |
| 246 | 233 |
| 247 void InstantNTPPrerenderer::ReloadStaleNTP() { | |
| 248 if (!extended_enabled()) | |
| 249 return; | |
| 250 | |
| 251 ResetNTP(GetInstantURL()); | |
| 252 } | |
| 253 | |
| 254 bool InstantNTPPrerenderer::PageIsCurrent() const { | 234 bool InstantNTPPrerenderer::PageIsCurrent() const { |
| 255 const std::string& instant_url = GetInstantURL(); | 235 const std::string& instant_url = GetInstantURL(); |
| 256 if (instant_url.empty() || | 236 if (instant_url.empty() || |
| 257 !chrome::MatchesOriginAndPath(GURL(ntp()->instant_url()), | 237 !chrome::MatchesOriginAndPath(GURL(ntp()->instant_url()), |
| 258 GURL(instant_url))) | 238 GURL(instant_url))) |
| 259 return false; | 239 return false; |
| 260 | 240 |
| 261 return ntp()->supports_instant(); | 241 return ntp()->supports_instant(); |
| 262 } | 242 } |
| 263 | 243 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 275 return false; | 255 return false; |
| 276 | 256 |
| 277 if (PageIsCurrent()) | 257 if (PageIsCurrent()) |
| 278 return false; | 258 return false; |
| 279 | 259 |
| 280 // The preloaded NTP does not support instant yet. If we're not in startup, | 260 // The preloaded NTP does not support instant yet. If we're not in startup, |
| 281 // always fall back to the local NTP. If we are in startup, use the local NTP | 261 // always fall back to the local NTP. If we are in startup, use the local NTP |
| 282 // (unless the finch flag to use the remote NTP is set). | 262 // (unless the finch flag to use the remote NTP is set). |
| 283 return !(InStartup() && chrome::ShouldPreferRemoteNTPOnStartup()); | 263 return !(InStartup() && chrome::ShouldPreferRemoteNTPOnStartup()); |
| 284 } | 264 } |
| 265 | |
| 266 //////////////////////////////////////////////////////////////////////////////// | |
| 267 // InstantNTPPrerenderer, InstantServiceObserver implementation: | |
| 268 | |
| 269 void InstantNTPPrerenderer::DefaultSearchProviderChanged() { | |
| 270 ReloadInstantNTP(); | |
| 271 } | |
| 272 | |
| 273 void InstantNTPPrerenderer::GoogleURLUpdated() { ReloadInstantNTP(); } | |
| OLD | NEW |