15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/google/google_url_tracker_infobar_delegate.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/google/google_url_tracker.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/google/google_util.h" 9a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/infobars/infobar.h" 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/infobars/infobar_service.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_details.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_entry.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/page_navigator.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/generated_resources.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static 21a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)InfoBar* GoogleURLTrackerInfoBarDelegate::Create( 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) InfoBarService* infobar_service, 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GoogleURLTracker* google_url_tracker, 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL& search_url) { 25a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) return infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar( 26a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) scoped_ptr<ConfirmInfoBarDelegate>(new GoogleURLTrackerInfoBarDelegate( 27a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) google_url_tracker, search_url)))); 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool GoogleURLTrackerInfoBarDelegate::Accept() { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) google_url_tracker_->AcceptGoogleURL(true); 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool GoogleURLTrackerInfoBarDelegate::Cancel() { 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) google_url_tracker_->CancelGoogleURL(); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GoogleURLTrackerInfoBarDelegate::Update(const GURL& search_url) { 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) StoreActiveEntryUniqueID(); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) search_url_ = search_url; 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pending_id_ = 0; 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GoogleURLTrackerInfoBarDelegate::Close(bool redo_search) { 47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Calling OpenURL() will auto-close us asynchronously. It's easier for 48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // various classes (e.g. GoogleURLTrackerMapEntry) to reason about things if 49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // the closure always happens synchronously, so we always call RemoveInfoBar() 50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // directly, then OpenURL() if desirable. (This calling order is safer if 51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // for some reason in the future OpenURL() were to close us synchronously.) 527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch GURL new_search_url; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (redo_search) { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Re-do the user's search on the new domain. 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(search_url_.is_valid()); 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) url_canon::Replacements<char> replacements; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& host(google_url_tracker_->fetched_google_url().host()); 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) replacements.SetHost(host.data(), url_parse::Component(0, host.length())); 597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch new_search_url = search_url_.ReplaceComponents(replacements); 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch content::WebContents* contents = web_contents(); 63a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) infobar()->RemoveSelf(); 647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // WARNING: |this| may be deleted at this point! Do not access any members! 657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (new_search_url.is_valid()) { 677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch contents->OpenURL(content::OpenURLParams( 687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch new_search_url, content::Referrer(), CURRENT_TAB, 697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch content::PAGE_TRANSITION_GENERATED, false)); 707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch } 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GoogleURLTrackerInfoBarDelegate::GoogleURLTrackerInfoBarDelegate( 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GoogleURLTracker* google_url_tracker, 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL& search_url) 76a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) : ConfirmInfoBarDelegate(), 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) google_url_tracker_(google_url_tracker), 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) search_url_(search_url), 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) pending_id_(0) { 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GoogleURLTrackerInfoBarDelegate::~GoogleURLTrackerInfoBarDelegate() { 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)base::string16 GoogleURLTrackerInfoBarDelegate::GetMessageText() const { 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return l10n_util::GetStringFUTF16( 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IDS_GOOGLE_URL_TRACKER_INFOBAR_MESSAGE, 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::StripWWWFromHost(google_url_tracker_->fetched_google_url()), 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::StripWWWFromHost(google_url_tracker_->google_url())); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 92d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)base::string16 GoogleURLTrackerInfoBarDelegate::GetButtonLabel( 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) InfoBarButton button) const { 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (button == BUTTON_OK) { 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return l10n_util::GetStringFUTF16( 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IDS_GOOGLE_URL_TRACKER_INFOBAR_SWITCH, 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::StripWWWFromHost(google_url_tracker_->fetched_google_url())); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return l10n_util::GetStringFUTF16( 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IDS_GOOGLE_URL_TRACKER_INFOBAR_DONT_SWITCH, 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::StripWWWFromHost(google_url_tracker_->google_url())); 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 104d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)base::string16 GoogleURLTrackerInfoBarDelegate::GetLinkText() const { 105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return l10n_util::GetStringUTF16(IDS_LEARN_MORE); 106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool GoogleURLTrackerInfoBarDelegate::LinkClicked( 109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) WindowOpenDisposition disposition) { 1107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch web_contents()->OpenURL(content::OpenURLParams( 1117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch google_util::AppendGoogleLocaleParam(GURL( 1127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch "https://www.google.com/support/chrome/bin/answer.py?" 1137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch "answer=1618699")), 114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) content::Referrer(), 115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition, 1167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch content::PAGE_TRANSITION_LINK, false)); 117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return false; 118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool GoogleURLTrackerInfoBarDelegate::ShouldExpireInternal( 121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const content::LoadCommittedDetails& details) const { 122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int unique_id = details.entry->GetUniqueID(); 123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return (unique_id != contents_unique_id()) && (unique_id != pending_id_); 124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 125