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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 InstantNTPPrerenderer::InstantNTPPrerenderer(Profile* profile, | 47 InstantNTPPrerenderer::InstantNTPPrerenderer(Profile* profile, |
| 48 PrefService* prefs) | 48 PrefService* prefs) |
| 49 : profile_(profile) { | 49 : profile_(profile) { |
| 50 DCHECK(profile); | 50 DCHECK(profile); |
| 51 | 51 |
| 52 // In unit tests, prefs may be NULL. | 52 // In unit tests, prefs may be NULL. |
| 53 if (prefs) { | 53 if (prefs) { |
| 54 profile_pref_registrar_.Init(prefs); | 54 profile_pref_registrar_.Init(prefs); |
| 55 profile_pref_registrar_.Add( | 55 profile_pref_registrar_.Add( |
| 56 prefs::kSearchSuggestEnabled, | 56 prefs::kSearchSuggestEnabled, |
| 57 base::Bind(&InstantNTPPrerenderer::ReloadStaleNTP, | 57 base::Bind(&InstantNTPPrerenderer::ReloadInstantNTP, |
| 58 base::Unretained(this))); | |
| 59 profile_pref_registrar_.Add( | |
| 60 prefs::kDefaultSearchProviderID, | |
| 61 base::Bind(&InstantNTPPrerenderer::OnDefaultSearchProviderChanged, | |
| 62 base::Unretained(this))); | 58 base::Unretained(this))); |
| 63 } | 59 } |
| 64 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 60 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 65 } | 61 } |
| 66 | 62 |
| 67 InstantNTPPrerenderer::~InstantNTPPrerenderer() { | 63 InstantNTPPrerenderer::~InstantNTPPrerenderer() { |
| 68 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 64 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 69 } | 65 } |
| 70 | 66 |
| 71 void InstantNTPPrerenderer::PreloadInstantNTP() { | 67 void InstantNTPPrerenderer::Init() { |
| 72 DCHECK(!ntp()); | 68 InstantService* instant_service = |
| 73 ReloadStaleNTP(); | 69 InstantServiceFactory::GetForProfile(profile_); |
| 70 instant_service->AddObserver(this); | |
| 71 ReloadInstantNTP(); | |
| 72 } | |
| 73 | |
| 74 void InstantNTPPrerenderer::Destroy() { | |
| 75 InstantService* instant_service = | |
| 76 InstantServiceFactory::GetForProfile(profile_); | |
| 77 instant_service->RemoveObserver(this); | |
| 78 DeleteNTPContents(); | |
| 79 } | |
| 80 | |
| 81 void InstantNTPPrerenderer::ReloadInstantNTP() { | |
| 82 ResetNTP(GetInstantURL()); | |
| 74 } | 83 } |
| 75 | 84 |
| 76 scoped_ptr<content::WebContents> InstantNTPPrerenderer::ReleaseNTPContents() { | 85 scoped_ptr<content::WebContents> InstantNTPPrerenderer::ReleaseNTPContents() { |
| 77 if (!profile_ || profile_->IsOffTheRecord() || | 86 if (!profile_ || profile_->IsOffTheRecord() || |
| 78 !chrome::ShouldShowInstantNTP()) | 87 !chrome::ShouldShowInstantNTP()) |
| 79 return scoped_ptr<content::WebContents>(); | 88 return scoped_ptr<content::WebContents>(); |
| 80 | 89 |
| 81 if (ShouldSwitchToLocalNTP()) | 90 if (ShouldSwitchToLocalNTP()) |
| 82 ResetNTP(GetLocalInstantURL()); | 91 ResetNTP(GetLocalInstantURL()); |
| 83 | 92 |
| 84 scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents(); | 93 scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents(); |
| 85 | 94 |
| 86 // Preload a new InstantNTP. | 95 // Preload a new InstantNTP. |
| 87 ResetNTP(GetInstantURL()); | 96 ReloadInstantNTP(); |
| 88 return ntp_contents.Pass(); | 97 return ntp_contents.Pass(); |
| 89 } | 98 } |
| 90 | 99 |
| 91 content::WebContents* InstantNTPPrerenderer::GetNTPContents() const { | 100 content::WebContents* InstantNTPPrerenderer::GetNTPContents() const { |
| 92 return ntp() ? ntp()->contents() : NULL; | 101 return ntp() ? ntp()->contents() : NULL; |
| 93 } | 102 } |
| 94 | 103 |
| 95 void InstantNTPPrerenderer::DeleteNTPContents() { | 104 void InstantNTPPrerenderer::DeleteNTPContents() { |
| 96 if (ntp_) | 105 if (ntp_) |
| 97 ntp_.reset(); | 106 ntp_.reset(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 return ntp_.get(); | 177 return ntp_.get(); |
| 169 } | 178 } |
| 170 | 179 |
| 171 void InstantNTPPrerenderer::OnNetworkChanged( | 180 void InstantNTPPrerenderer::OnNetworkChanged( |
| 172 net::NetworkChangeNotifier::ConnectionType type) { | 181 net::NetworkChangeNotifier::ConnectionType type) { |
| 173 // Not interested in events conveying change to offline. | 182 // Not interested in events conveying change to offline. |
| 174 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) | 183 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) |
| 175 return; | 184 return; |
| 176 | 185 |
| 177 if (!ntp() || ntp()->IsLocal()) | 186 if (!ntp() || ntp()->IsLocal()) |
| 178 ResetNTP(GetInstantURL()); | 187 ReloadInstantNTP(); |
| 179 } | 188 } |
| 180 | 189 |
| 181 void InstantNTPPrerenderer::InstantSupportDetermined( | 190 void InstantNTPPrerenderer::InstantSupportDetermined( |
| 182 const content::WebContents* contents, | 191 const content::WebContents* contents, |
| 183 bool supports_instant) { | 192 bool supports_instant) { |
| 184 DCHECK(ntp() && ntp()->contents() == contents); | 193 DCHECK(ntp() && ntp()->contents() == contents); |
| 185 | 194 |
| 186 if (!supports_instant) { | 195 if (!supports_instant) { |
| 187 bool is_local = ntp()->IsLocal(); | 196 bool is_local = ntp()->IsLocal(); |
| 188 DeleteNTPSoon(ntp_.Pass()); | 197 DeleteNTPSoon(ntp_.Pass()); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 void InstantNTPPrerenderer::InstantPageLoadFailed( | 247 void InstantNTPPrerenderer::InstantPageLoadFailed( |
| 239 content::WebContents* contents) { | 248 content::WebContents* contents) { |
| 240 DCHECK(ntp() && ntp()->contents() == contents); | 249 DCHECK(ntp() && ntp()->contents() == contents); |
| 241 | 250 |
| 242 bool is_local = ntp()->IsLocal(); | 251 bool is_local = ntp()->IsLocal(); |
| 243 DeleteNTPSoon(ntp_.Pass()); | 252 DeleteNTPSoon(ntp_.Pass()); |
| 244 if (!is_local) | 253 if (!is_local) |
| 245 ResetNTP(GetLocalInstantURL()); | 254 ResetNTP(GetLocalInstantURL()); |
| 246 } | 255 } |
| 247 | 256 |
| 248 void InstantNTPPrerenderer::OnDefaultSearchProviderChanged( | |
| 249 const std::string& pref_name) { | |
| 250 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); | |
| 251 if (!ntp()) | |
| 252 return; | |
| 253 | |
| 254 ResetNTP(GetInstantURL()); | |
| 255 } | |
| 256 | |
| 257 void InstantNTPPrerenderer::ResetNTP(const std::string& instant_url) { | 257 void InstantNTPPrerenderer::ResetNTP(const std::string& instant_url) { |
| 258 // Instant NTP is only used in extended mode so we should always have a | 258 // Instant NTP is only used in extended mode so we should always have a |
| 259 // non-empty URL to use. | 259 // non-empty URL to use. |
| 260 DCHECK(!instant_url.empty()); | 260 DCHECK(!instant_url.empty()); |
| 261 ntp_.reset(new InstantNTP(this, instant_url, profile_)); | 261 ntp_.reset(new InstantNTP(this, instant_url, profile_)); |
| 262 ntp_->InitContents(base::Bind(&InstantNTPPrerenderer::ReloadStaleNTP, | 262 ntp_->InitContents(base::Bind(&InstantNTPPrerenderer::ReloadInstantNTP, |
| 263 base::Unretained(this))); | 263 base::Unretained(this))); |
| 264 } | 264 } |
| 265 | 265 |
| 266 void InstantNTPPrerenderer::ReloadStaleNTP() { | |
| 267 ResetNTP(GetInstantURL()); | |
| 268 } | |
| 269 | |
| 270 bool InstantNTPPrerenderer::PageIsCurrent() const { | 266 bool InstantNTPPrerenderer::PageIsCurrent() const { |
| 271 const std::string& instant_url = GetInstantURL(); | 267 const std::string& instant_url = GetInstantURL(); |
| 272 if (instant_url.empty() || | 268 if (instant_url.empty() || |
| 273 !chrome::MatchesOriginAndPath(GURL(ntp()->instant_url()), | 269 !chrome::MatchesOriginAndPath(GURL(ntp()->instant_url()), |
| 274 GURL(instant_url))) | 270 GURL(instant_url))) |
| 275 return false; | 271 return false; |
| 276 | 272 |
| 277 return ntp()->supports_instant(); | 273 return ntp()->supports_instant(); |
| 278 } | 274 } |
| 279 | 275 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 291 return false; | 287 return false; |
| 292 | 288 |
| 293 if (PageIsCurrent()) | 289 if (PageIsCurrent()) |
| 294 return false; | 290 return false; |
| 295 | 291 |
| 296 // The preloaded NTP does not support instant yet. If we're not in startup, | 292 // The preloaded NTP does not support instant yet. If we're not in startup, |
| 297 // always fall back to the local NTP. If we are in startup, use the local NTP | 293 // always fall back to the local NTP. If we are in startup, use the local NTP |
| 298 // (unless the finch flag to use the remote NTP is set). | 294 // (unless the finch flag to use the remote NTP is set). |
| 299 return !(InStartup() && chrome::ShouldPreferRemoteNTPOnStartup()); | 295 return !(InStartup() && chrome::ShouldPreferRemoteNTPOnStartup()); |
| 300 } | 296 } |
| 297 | |
| 298 //////////////////////////////////////////////////////////////////////////////// | |
|
samarth
2013/08/14 00:24:14
nit: this file isn't really big enough to merit th
Anuj
2013/08/14 06:38:57
Done.
| |
| 299 // InstantNTPPrerenderer, InstantServiceObserver implementation: | |
| 300 | |
| 301 void InstantNTPPrerenderer::DefaultSearchProviderChanged() { | |
| 302 ReloadInstantNTP(); | |
| 303 } | |
| 304 | |
| 305 void InstantNTPPrerenderer::GoogleURLUpdated() { | |
| 306 ReloadInstantNTP(); | |
| 307 } | |
| OLD | NEW |