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() { |
|
samarth
2013/08/12 19:07:28
Currently this function can be called multiple tim
Anuj
2013/08/13 00:18:09
Done.
| |
| 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::ReloadInstantNTP() { | |
| 75 ResetNTP(GetInstantURL()); | |
| 74 } | 76 } |
| 75 | 77 |
| 76 scoped_ptr<content::WebContents> InstantNTPPrerenderer::ReleaseNTPContents() { | 78 scoped_ptr<content::WebContents> InstantNTPPrerenderer::ReleaseNTPContents() { |
| 77 if (!profile_ || profile_->IsOffTheRecord() || | 79 if (!profile_ || profile_->IsOffTheRecord() || |
| 78 !chrome::ShouldShowInstantNTP()) | 80 !chrome::ShouldShowInstantNTP()) |
| 79 return scoped_ptr<content::WebContents>(); | 81 return scoped_ptr<content::WebContents>(); |
| 80 | 82 |
| 81 if (ShouldSwitchToLocalNTP()) | 83 if (ShouldSwitchToLocalNTP()) |
| 82 ResetNTP(GetLocalInstantURL()); | 84 ResetNTP(GetLocalInstantURL()); |
| 83 | 85 |
| 84 scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents(); | 86 scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents(); |
| 85 | 87 |
| 86 // Preload a new InstantNTP. | 88 // Preload a new InstantNTP. |
| 87 ResetNTP(GetInstantURL()); | 89 ReloadInstantNTP(); |
| 88 return ntp_contents.Pass(); | 90 return ntp_contents.Pass(); |
| 89 } | 91 } |
| 90 | 92 |
| 91 content::WebContents* InstantNTPPrerenderer::GetNTPContents() const { | 93 content::WebContents* InstantNTPPrerenderer::GetNTPContents() const { |
| 92 return ntp() ? ntp()->contents() : NULL; | 94 return ntp() ? ntp()->contents() : NULL; |
| 93 } | 95 } |
| 94 | 96 |
| 95 void InstantNTPPrerenderer::DeleteNTPContents() { | 97 void InstantNTPPrerenderer::DeleteNTPContents() { |
| 96 if (ntp_) | 98 if (ntp_) |
| 97 ntp_.reset(); | 99 ntp_.reset(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 return ntp_.get(); | 170 return ntp_.get(); |
| 169 } | 171 } |
| 170 | 172 |
| 171 void InstantNTPPrerenderer::OnNetworkChanged( | 173 void InstantNTPPrerenderer::OnNetworkChanged( |
| 172 net::NetworkChangeNotifier::ConnectionType type) { | 174 net::NetworkChangeNotifier::ConnectionType type) { |
| 173 // Not interested in events conveying change to offline. | 175 // Not interested in events conveying change to offline. |
| 174 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) | 176 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) |
| 175 return; | 177 return; |
| 176 | 178 |
| 177 if (!ntp() || ntp()->IsLocal()) | 179 if (!ntp() || ntp()->IsLocal()) |
| 178 ResetNTP(GetInstantURL()); | 180 ReloadInstantNTP(); |
| 179 } | 181 } |
| 180 | 182 |
| 181 void InstantNTPPrerenderer::InstantSupportDetermined( | 183 void InstantNTPPrerenderer::InstantSupportDetermined( |
| 182 const content::WebContents* contents, | 184 const content::WebContents* contents, |
| 183 bool supports_instant) { | 185 bool supports_instant) { |
| 184 DCHECK(ntp() && ntp()->contents() == contents); | 186 DCHECK(ntp() && ntp()->contents() == contents); |
| 185 | 187 |
| 186 if (!supports_instant) { | 188 if (!supports_instant) { |
| 187 bool is_local = ntp()->IsLocal(); | 189 bool is_local = ntp()->IsLocal(); |
| 188 DeleteNTPSoon(ntp_.Pass()); | 190 DeleteNTPSoon(ntp_.Pass()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 void InstantNTPPrerenderer::InstantPageLoadFailed( | 232 void InstantNTPPrerenderer::InstantPageLoadFailed( |
| 231 content::WebContents* contents) { | 233 content::WebContents* contents) { |
| 232 DCHECK(ntp() && ntp()->contents() == contents); | 234 DCHECK(ntp() && ntp()->contents() == contents); |
| 233 | 235 |
| 234 bool is_local = ntp()->IsLocal(); | 236 bool is_local = ntp()->IsLocal(); |
| 235 DeleteNTPSoon(ntp_.Pass()); | 237 DeleteNTPSoon(ntp_.Pass()); |
| 236 if (!is_local) | 238 if (!is_local) |
| 237 ResetNTP(GetLocalInstantURL()); | 239 ResetNTP(GetLocalInstantURL()); |
| 238 } | 240 } |
| 239 | 241 |
| 240 void InstantNTPPrerenderer::OnDefaultSearchProviderChanged( | |
| 241 const std::string& pref_name) { | |
| 242 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); | |
| 243 if (!ntp()) | |
| 244 return; | |
| 245 | |
| 246 ResetNTP(GetInstantURL()); | |
| 247 } | |
| 248 | |
| 249 void InstantNTPPrerenderer::ResetNTP(const std::string& instant_url) { | 242 void InstantNTPPrerenderer::ResetNTP(const std::string& instant_url) { |
| 250 // Instant NTP is only used in extended mode so we should always have a | 243 // Instant NTP is only used in extended mode so we should always have a |
| 251 // non-empty URL to use. | 244 // non-empty URL to use. |
| 252 DCHECK(!instant_url.empty()); | 245 DCHECK(!instant_url.empty()); |
| 253 ntp_.reset(new InstantNTP(this, instant_url, profile_)); | 246 ntp_.reset(new InstantNTP(this, instant_url, profile_)); |
| 254 ntp_->InitContents(base::Bind(&InstantNTPPrerenderer::ReloadStaleNTP, | 247 ntp_->InitContents(base::Bind(&InstantNTPPrerenderer::ReloadInstantNTP, |
| 255 base::Unretained(this))); | 248 base::Unretained(this))); |
| 256 } | 249 } |
| 257 | 250 |
| 258 void InstantNTPPrerenderer::ReloadStaleNTP() { | |
| 259 ResetNTP(GetInstantURL()); | |
| 260 } | |
| 261 | |
| 262 bool InstantNTPPrerenderer::PageIsCurrent() const { | 251 bool InstantNTPPrerenderer::PageIsCurrent() const { |
| 263 const std::string& instant_url = GetInstantURL(); | 252 const std::string& instant_url = GetInstantURL(); |
| 264 if (instant_url.empty() || | 253 if (instant_url.empty() || |
| 265 !chrome::MatchesOriginAndPath(GURL(ntp()->instant_url()), | 254 !chrome::MatchesOriginAndPath(GURL(ntp()->instant_url()), |
| 266 GURL(instant_url))) | 255 GURL(instant_url))) |
| 267 return false; | 256 return false; |
| 268 | 257 |
| 269 return ntp()->supports_instant(); | 258 return ntp()->supports_instant(); |
| 270 } | 259 } |
| 271 | 260 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 283 return false; | 272 return false; |
| 284 | 273 |
| 285 if (PageIsCurrent()) | 274 if (PageIsCurrent()) |
| 286 return false; | 275 return false; |
| 287 | 276 |
| 288 // The preloaded NTP does not support instant yet. If we're not in startup, | 277 // The preloaded NTP does not support instant yet. If we're not in startup, |
| 289 // always fall back to the local NTP. If we are in startup, use the local NTP | 278 // always fall back to the local NTP. If we are in startup, use the local NTP |
| 290 // (unless the finch flag to use the remote NTP is set). | 279 // (unless the finch flag to use the remote NTP is set). |
| 291 return !(InStartup() && chrome::ShouldPreferRemoteNTPOnStartup()); | 280 return !(InStartup() && chrome::ShouldPreferRemoteNTPOnStartup()); |
| 292 } | 281 } |
| 282 | |
| 283 //////////////////////////////////////////////////////////////////////////////// | |
| 284 // InstantNTPPrerenderer, InstantServiceObserver implementation: | |
| 285 | |
| 286 void InstantNTPPrerenderer::DefaultSearchProviderChanged() { | |
| 287 ReloadInstantNTP(); | |
| 288 } | |
| 289 | |
| 290 void InstantNTPPrerenderer::GoogleURLUpdated() { | |
| 291 ReloadInstantNTP(); | |
| 292 } | |
| OLD | NEW |