Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / google / google_url_tracker_infobar_delegate.cc
blob0b2036c9eb3d9e8f4f3b3dac3379f37f1a54cd88
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.
5 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h"
7 #include "chrome/browser/google/google_url_tracker.h"
8 #include "chrome/browser/google/google_util.h"
9 #include "chrome/browser/infobars/infobar.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "content/public/browser/navigation_details.h"
12 #include "content/public/browser/navigation_entry.h"
13 #include "content/public/browser/page_navigator.h"
14 #include "content/public/browser/web_contents.h"
15 #include "grit/generated_resources.h"
16 #include "net/base/net_util.h"
17 #include "ui/base/l10n/l10n_util.h"
20 // static
21 InfoBar* GoogleURLTrackerInfoBarDelegate::Create(
22 InfoBarService* infobar_service,
23 GoogleURLTracker* google_url_tracker,
24 const GURL& search_url) {
25 return infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
26 scoped_ptr<ConfirmInfoBarDelegate>(new GoogleURLTrackerInfoBarDelegate(
27 google_url_tracker, search_url))));
30 bool GoogleURLTrackerInfoBarDelegate::Accept() {
31 google_url_tracker_->AcceptGoogleURL(true);
32 return false;
35 bool GoogleURLTrackerInfoBarDelegate::Cancel() {
36 google_url_tracker_->CancelGoogleURL();
37 return false;
40 void GoogleURLTrackerInfoBarDelegate::Update(const GURL& search_url) {
41 StoreActiveEntryUniqueID();
42 search_url_ = search_url;
43 pending_id_ = 0;
46 void GoogleURLTrackerInfoBarDelegate::Close(bool redo_search) {
47 // Calling OpenURL() will auto-close us asynchronously. It's easier for
48 // various classes (e.g. GoogleURLTrackerMapEntry) to reason about things if
49 // the closure always happens synchronously, so we always call RemoveInfoBar()
50 // directly, then OpenURL() if desirable. (This calling order is safer if
51 // for some reason in the future OpenURL() were to close us synchronously.)
52 GURL new_search_url;
53 if (redo_search) {
54 // Re-do the user's search on the new domain.
55 DCHECK(search_url_.is_valid());
56 url_canon::Replacements<char> replacements;
57 const std::string& host(google_url_tracker_->fetched_google_url().host());
58 replacements.SetHost(host.data(), url_parse::Component(0, host.length()));
59 new_search_url = search_url_.ReplaceComponents(replacements);
62 content::WebContents* contents = web_contents();
63 infobar()->RemoveSelf();
64 // WARNING: |this| may be deleted at this point! Do not access any members!
66 if (new_search_url.is_valid()) {
67 contents->OpenURL(content::OpenURLParams(
68 new_search_url, content::Referrer(), CURRENT_TAB,
69 content::PAGE_TRANSITION_GENERATED, false));
73 GoogleURLTrackerInfoBarDelegate::GoogleURLTrackerInfoBarDelegate(
74 GoogleURLTracker* google_url_tracker,
75 const GURL& search_url)
76 : ConfirmInfoBarDelegate(),
77 google_url_tracker_(google_url_tracker),
78 search_url_(search_url),
79 pending_id_(0) {
82 GoogleURLTrackerInfoBarDelegate::~GoogleURLTrackerInfoBarDelegate() {
85 base::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()));
92 base::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()));
99 return l10n_util::GetStringFUTF16(
100 IDS_GOOGLE_URL_TRACKER_INFOBAR_DONT_SWITCH,
101 net::StripWWWFromHost(google_url_tracker_->google_url()));
104 base::string16 GoogleURLTrackerInfoBarDelegate::GetLinkText() const {
105 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
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;
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_);