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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" 6 7 #include "chrome/browser/google/google_url_tracker.h" 8 #include "chrome/browser/google/google_util.h" 9 #include "chrome/browser/infobars/infobar_service.h" 10 #include "content/public/browser/navigation_details.h" 11 #include "content/public/browser/navigation_entry.h" 12 #include "content/public/browser/page_navigator.h" 13 #include "content/public/browser/web_contents.h" 14 #include "grit/generated_resources.h" 15 #include "net/base/net_util.h" 16 #include "ui/base/l10n/l10n_util.h" 17 18 19 // static 20 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerInfoBarDelegate::Create( 21 InfoBarService* infobar_service, 22 GoogleURLTracker* google_url_tracker, 23 const GURL& search_url) { 24 return static_cast<GoogleURLTrackerInfoBarDelegate*>( 25 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( 26 new GoogleURLTrackerInfoBarDelegate( 27 infobar_service, google_url_tracker, search_url)))); 28 } 29 30 bool GoogleURLTrackerInfoBarDelegate::Accept() { 31 google_url_tracker_->AcceptGoogleURL(true); 32 return false; 33 } 34 35 bool GoogleURLTrackerInfoBarDelegate::Cancel() { 36 google_url_tracker_->CancelGoogleURL(); 37 return false; 38 } 39 40 void GoogleURLTrackerInfoBarDelegate::Update(const GURL& search_url) { 41 StoreActiveEntryUniqueID(); 42 search_url_ = search_url; 43 pending_id_ = 0; 44 } 45 46 void GoogleURLTrackerInfoBarDelegate::Close(bool redo_search) { 47 // It's not obvious whether calling OpenURL() with a search URL would 48 // auto-close us or not. If it did, we wouldn't want to try to 49 // RemoveInfoBar() afterwards. So for safety, we always call RemoveInfoBar() 50 // directly, and then navigate if necessary afterwards. 51 GURL new_search_url; 52 if (redo_search) { 53 // Re-do the user's search on the new domain. 54 DCHECK(search_url_.is_valid()); 55 url_canon::Replacements<char> replacements; 56 const std::string& host(google_url_tracker_->fetched_google_url().host()); 57 replacements.SetHost(host.data(), url_parse::Component(0, host.length())); 58 new_search_url = search_url_.ReplaceComponents(replacements); 59 } 60 61 content::WebContents* contents = web_contents(); 62 owner()->RemoveInfoBar(this); 63 // WARNING: |this| may be deleted at this point! Do not access any members! 64 65 if (new_search_url.is_valid()) { 66 contents->OpenURL(content::OpenURLParams( 67 new_search_url, content::Referrer(), CURRENT_TAB, 68 content::PAGE_TRANSITION_GENERATED, false)); 69 } 70 } 71 72 GoogleURLTrackerInfoBarDelegate::GoogleURLTrackerInfoBarDelegate( 73 InfoBarService* infobar_service, 74 GoogleURLTracker* google_url_tracker, 75 const GURL& search_url) 76 : ConfirmInfoBarDelegate(infobar_service), 77 google_url_tracker_(google_url_tracker), 78 search_url_(search_url), 79 pending_id_(0) { 80 } 81 82 GoogleURLTrackerInfoBarDelegate::~GoogleURLTrackerInfoBarDelegate() { 83 } 84 85 string16 GoogleURLTrackerInfoBarDelegate::GetMessageText() const { 86 return l10n_util::GetStringFUTF16( 87 IDS_GOOGLE_URL_TRACKER_INFOBAR_MESSAGE, 88 net::StripWWWFromHost(google_url_tracker_->fetched_google_url()), 89 net::StripWWWFromHost(google_url_tracker_->google_url())); 90 } 91 92 string16 GoogleURLTrackerInfoBarDelegate::GetButtonLabel( 93 InfoBarButton button) const { 94 if (button == BUTTON_OK) { 95 return l10n_util::GetStringFUTF16( 96 IDS_GOOGLE_URL_TRACKER_INFOBAR_SWITCH, 97 net::StripWWWFromHost(google_url_tracker_->fetched_google_url())); 98 } 99 return l10n_util::GetStringFUTF16( 100 IDS_GOOGLE_URL_TRACKER_INFOBAR_DONT_SWITCH, 101 net::StripWWWFromHost(google_url_tracker_->google_url())); 102 } 103 104 string16 GoogleURLTrackerInfoBarDelegate::GetLinkText() const { 105 return l10n_util::GetStringUTF16(IDS_LEARN_MORE); 106 } 107 108 bool GoogleURLTrackerInfoBarDelegate::LinkClicked( 109 WindowOpenDisposition disposition) { 110 web_contents()->OpenURL(content::OpenURLParams( 111 google_util::AppendGoogleLocaleParam(GURL( 112 "https://www.google.com/support/chrome/bin/answer.py?" 113 "answer=1618699")), 114 content::Referrer(), 115 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition, 116 content::PAGE_TRANSITION_LINK, false)); 117 return false; 118 } 119 120 bool GoogleURLTrackerInfoBarDelegate::ShouldExpireInternal( 121 const content::LoadCommittedDetails& details) const { 122 int unique_id = details.entry->GetUniqueID(); 123 return (unique_id != contents_unique_id()) && (unique_id != pending_id_); 124 } 125