| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/google/google_url_tracker.h" | 5 #include "chrome/browser/google/google_url_tracker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/google/google_url_tracker_factory.h" | 12 #include "chrome/browser/google/google_url_tracker_factory.h" |
| 13 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" | 13 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" |
| 14 #include "chrome/browser/google/google_url_tracker_navigation_helper.h" | 14 #include "chrome/browser/google/google_url_tracker_navigation_helper.h" |
| 15 #include "chrome/browser/google/google_util.h" | 15 #include "chrome/browser/google/google_util.h" |
| 16 #include "chrome/browser/infobars/infobar_service.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 20 #include "components/google/core/browser/google_url_tracker_client.h" | 19 #include "components/google/core/browser/google_url_tracker_client.h" |
| 21 #include "components/infobars/core/infobar.h" | 20 #include "components/infobars/core/infobar.h" |
| 22 #include "content/public/browser/navigation_controller.h" | 21 #include "components/infobars/core/infobar_manager.h" |
| 23 #include "content/public/browser/navigation_entry.h" | |
| 24 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 25 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
| 26 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 27 #include "net/url_request/url_fetcher.h" | 25 #include "net/url_request/url_fetcher.h" |
| 28 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
| 29 | 27 |
| 30 | 28 |
| 31 const char GoogleURLTracker::kDefaultGoogleHomepage[] = | 29 const char GoogleURLTracker::kDefaultGoogleHomepage[] = |
| 32 "http://www.google.com/"; | 30 "http://www.google.com/"; |
| 33 const char GoogleURLTracker::kSearchDomainCheckURL[] = | 31 const char GoogleURLTracker::kSearchDomainCheckURL[] = |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 StartFetchIfDesirable(); | 197 StartFetchIfDesirable(); |
| 200 } | 198 } |
| 201 | 199 |
| 202 void GoogleURLTracker::Shutdown() { | 200 void GoogleURLTracker::Shutdown() { |
| 203 client_.reset(); | 201 client_.reset(); |
| 204 fetcher_.reset(); | 202 fetcher_.reset(); |
| 205 weak_ptr_factory_.InvalidateWeakPtrs(); | 203 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 206 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 204 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 207 } | 205 } |
| 208 | 206 |
| 209 void GoogleURLTracker::DeleteMapEntryForService( | 207 void GoogleURLTracker::DeleteMapEntryForManager( |
| 210 const InfoBarService* infobar_service) { | 208 const infobars::InfoBarManager* infobar_manager) { |
| 211 // WARNING: |infobar_service| may point to a deleted object. Do not | 209 // WARNING: |infobar_manager| may point to a deleted object. Do not |
| 212 // dereference it! See OnTabClosed(). | 210 // dereference it! See OnTabClosed(). |
| 213 EntryMap::iterator i(entry_map_.find(infobar_service)); | 211 EntryMap::iterator i(entry_map_.find(infobar_manager)); |
| 214 DCHECK(i != entry_map_.end()); | 212 DCHECK(i != entry_map_.end()); |
| 215 GoogleURLTrackerMapEntry* map_entry = i->second; | 213 GoogleURLTrackerMapEntry* map_entry = i->second; |
| 216 | 214 |
| 217 UnregisterForEntrySpecificNotifications(map_entry, false); | 215 UnregisterForEntrySpecificNotifications(map_entry, false); |
| 218 entry_map_.erase(i); | 216 entry_map_.erase(i); |
| 219 delete map_entry; | 217 delete map_entry; |
| 220 } | 218 } |
| 221 | 219 |
| 222 void GoogleURLTracker::SetNeedToFetch() { | 220 void GoogleURLTracker::SetNeedToFetch() { |
| 223 need_to_fetch_ = true; | 221 need_to_fetch_ = true; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 search_committed_ = true; | 268 search_committed_ = true; |
| 271 // These notifications will fire a bit later in the same call chain we're | 269 // These notifications will fire a bit later in the same call chain we're |
| 272 // currently in. | 270 // currently in. |
| 273 if (!client_->IsListeningForNavigationStart()) | 271 if (!client_->IsListeningForNavigationStart()) |
| 274 client_->SetListeningForNavigationStart(true); | 272 client_->SetListeningForNavigationStart(true); |
| 275 } | 273 } |
| 276 } | 274 } |
| 277 | 275 |
| 278 void GoogleURLTracker::OnNavigationPending( | 276 void GoogleURLTracker::OnNavigationPending( |
| 279 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper, | 277 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper, |
| 280 InfoBarService* infobar_service, | 278 infobars::InfoBarManager* infobar_manager, |
| 281 int pending_id) { | 279 int pending_id) { |
| 282 GoogleURLTrackerMapEntry* map_entry = NULL; | 280 GoogleURLTrackerMapEntry* map_entry = NULL; |
| 283 | 281 |
| 284 EntryMap::iterator i(entry_map_.find(infobar_service)); | 282 EntryMap::iterator i(entry_map_.find(infobar_manager)); |
| 285 if (i != entry_map_.end()) | 283 if (i != entry_map_.end()) |
| 286 map_entry = i->second; | 284 map_entry = i->second; |
| 287 | 285 |
| 288 if (search_committed_) { | 286 if (search_committed_) { |
| 289 search_committed_ = false; | 287 search_committed_ = false; |
| 290 if (!map_entry) { | 288 if (!map_entry) { |
| 291 // This is a search on a tab that doesn't have one of our infobars, so | 289 // This is a search on a tab that doesn't have one of our infobars, so |
| 292 // prepare to add one. Note that we only listen for the tab's destruction | 290 // prepare to add one. Note that we only listen for the tab's destruction |
| 293 // on this path; if there was already a map entry, then either it doesn't | 291 // on this path; if there was already a map entry, then either it doesn't |
| 294 // yet have an infobar and we're already registered for this, or it has an | 292 // yet have an infobar and we're already registered for this, or it has an |
| 295 // infobar and the infobar's owner will handle tearing it down when the | 293 // infobar and the infobar's owner will handle tearing it down when the |
| 296 // tab is destroyed. | 294 // tab is destroyed. |
| 297 map_entry = new GoogleURLTrackerMapEntry( | 295 map_entry = new GoogleURLTrackerMapEntry( |
| 298 this, infobar_service, nav_helper.Pass()); | 296 this, infobar_manager, nav_helper.Pass()); |
| 299 map_entry->navigation_helper()->SetListeningForTabDestruction(true); | 297 map_entry->navigation_helper()->SetListeningForTabDestruction(true); |
| 300 entry_map_.insert(std::make_pair(infobar_service, map_entry)); | 298 entry_map_.insert(std::make_pair(infobar_manager, map_entry)); |
| 301 } else if (map_entry->infobar_delegate()) { | 299 } else if (map_entry->infobar_delegate()) { |
| 302 // This is a new search on a tab where we already have an infobar. | 300 // This is a new search on a tab where we already have an infobar. |
| 303 map_entry->infobar_delegate()->set_pending_id(pending_id); | 301 map_entry->infobar_delegate()->set_pending_id(pending_id); |
| 304 } | 302 } |
| 305 | 303 |
| 306 // Whether there's an existing infobar or not, we need to listen for the | 304 // Whether there's an existing infobar or not, we need to listen for the |
| 307 // load to commit, so we can show and/or update the infobar when it does. | 305 // load to commit, so we can show and/or update the infobar when it does. |
| 308 // (We may already be registered for this if there is an existing infobar | 306 // (We may already be registered for this if there is an existing infobar |
| 309 // that had a previous pending search that hasn't yet committed.) | 307 // that had a previous pending search that hasn't yet committed.) |
| 310 if (!map_entry->navigation_helper()->IsListeningForNavigationCommit()) | 308 if (!map_entry->navigation_helper()->IsListeningForNavigationCommit()) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 327 // an infobar. This means the original search won't commit, so delete the | 325 // an infobar. This means the original search won't commit, so delete the |
| 328 // entry. | 326 // entry. |
| 329 map_entry->Close(false); | 327 map_entry->Close(false); |
| 330 } | 328 } |
| 331 } else { | 329 } else { |
| 332 // Non-search navigation on a tab without an infobars. This is irrelevant | 330 // Non-search navigation on a tab without an infobars. This is irrelevant |
| 333 // to us. | 331 // to us. |
| 334 } | 332 } |
| 335 } | 333 } |
| 336 | 334 |
| 337 void GoogleURLTracker::OnNavigationCommitted(InfoBarService* infobar_service, | 335 void GoogleURLTracker::OnNavigationCommitted( |
| 338 const GURL& search_url) { | 336 infobars::InfoBarManager* infobar_manager, |
| 339 EntryMap::iterator i(entry_map_.find(infobar_service)); | 337 const GURL& search_url) { |
| 338 EntryMap::iterator i(entry_map_.find(infobar_manager)); |
| 340 DCHECK(i != entry_map_.end()); | 339 DCHECK(i != entry_map_.end()); |
| 341 GoogleURLTrackerMapEntry* map_entry = i->second; | 340 GoogleURLTrackerMapEntry* map_entry = i->second; |
| 342 DCHECK(search_url.is_valid()); | 341 DCHECK(search_url.is_valid()); |
| 343 | 342 |
| 344 UnregisterForEntrySpecificNotifications(map_entry, true); | 343 UnregisterForEntrySpecificNotifications(map_entry, true); |
| 345 if (map_entry->has_infobar_delegate()) { | 344 if (map_entry->has_infobar_delegate()) { |
| 346 map_entry->infobar_delegate()->Update(search_url); | 345 map_entry->infobar_delegate()->Update(search_url); |
| 347 } else { | 346 } else { |
| 348 infobars::InfoBar* infobar = | 347 infobars::InfoBar* infobar = infobar_creator_.Run( |
| 349 infobar_creator_.Run(infobar_service, this, search_url); | 348 infobar_manager, this, search_url); |
| 350 if (infobar) { | 349 if (infobar) { |
| 351 map_entry->SetInfoBarDelegate( | 350 map_entry->SetInfoBarDelegate( |
| 352 static_cast<GoogleURLTrackerInfoBarDelegate*>(infobar->delegate())); | 351 static_cast<GoogleURLTrackerInfoBarDelegate*>(infobar->delegate())); |
| 353 } else { | 352 } else { |
| 354 map_entry->Close(false); | 353 map_entry->Close(false); |
| 355 } | 354 } |
| 356 } | 355 } |
| 357 } | 356 } |
| 358 | 357 |
| 359 void GoogleURLTracker::OnTabClosed( | 358 void GoogleURLTracker::OnTabClosed( |
| 360 GoogleURLTrackerNavigationHelper* nav_helper) { | 359 GoogleURLTrackerNavigationHelper* nav_helper) { |
| 361 // Because InfoBarService tears itself down on tab destruction, it's possible | 360 // Because InfoBarManager tears itself down on tab destruction, it's possible |
| 362 // to get a non-NULL InfoBarService pointer here, depending on which order | 361 // to get a non-NULL InfoBarManager pointer here, depending on which order |
| 363 // notifications fired in. Likewise, the pointer in |entry_map_| (and in its | 362 // notifications fired in. Likewise, the pointer in |entry_map_| (and in its |
| 364 // associated MapEntry) may point to deleted memory. Therefore, if we were to | 363 // associated MapEntry) may point to deleted memory. Therefore, if we were |
| 365 // access the InfoBarService* we have for this tab, we'd need to ensure we | 364 // to access the InfoBarManager* we have for this tab, we'd need to ensure we |
| 366 // just looked at the raw pointer value, and never dereferenced it. This | 365 // just looked at the raw pointer value, and never dereferenced it. This |
| 367 // function doesn't need to do even that, but others in the call chain from | 366 // function doesn't need to do even that, but others in the call chain from |
| 368 // here might (and have comments pointing back here). | 367 // here might (and have comments pointing back here). |
| 369 for (EntryMap::iterator i(entry_map_.begin()); i != entry_map_.end(); ++i) { | 368 for (EntryMap::iterator i(entry_map_.begin()); i != entry_map_.end(); ++i) { |
| 370 if (i->second->navigation_helper() == nav_helper) { | 369 if (i->second->navigation_helper() == nav_helper) { |
| 371 i->second->Close(false); | 370 i->second->Close(false); |
| 372 return; | 371 return; |
| 373 } | 372 } |
| 374 } | 373 } |
| 375 NOTREACHED(); | 374 NOTREACHED(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 } | 418 } |
| 420 if (client_->IsListeningForNavigationStart()) { | 419 if (client_->IsListeningForNavigationStart()) { |
| 421 DCHECK(!search_committed_); | 420 DCHECK(!search_committed_); |
| 422 client_->SetListeningForNavigationStart(false); | 421 client_->SetListeningForNavigationStart(false); |
| 423 } | 422 } |
| 424 } | 423 } |
| 425 | 424 |
| 426 void GoogleURLTracker::NotifyGoogleURLUpdated(GURL old_url, GURL new_url) { | 425 void GoogleURLTracker::NotifyGoogleURLUpdated(GURL old_url, GURL new_url) { |
| 427 callback_list_.Notify(old_url, new_url); | 426 callback_list_.Notify(old_url, new_url); |
| 428 } | 427 } |
| OLD | NEW |