File: alternate_nav_infobar_delegate.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (113 lines) | stat: -rw-r--r-- 4,207 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/omnibox/alternate_nav_infobar_delegate.h"

#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/autocomplete/shortcuts_backend_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/grit/generated_resources.h"
#include "components/history/core/browser/history_service.h"
#include "components/infobars/core/infobar.h"
#include "components/omnibox/browser/shortcuts_backend.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/vector_icons_public.h"

AlternateNavInfoBarDelegate::~AlternateNavInfoBarDelegate() {
}

// static
void AlternateNavInfoBarDelegate::Create(
    content::WebContents* web_contents,
    const base::string16& text,
    const AutocompleteMatch& match,
    const GURL& search_url) {
  InfoBarService* infobar_service =
      InfoBarService::FromWebContents(web_contents);
  infobar_service->AddInfoBar(AlternateNavInfoBarDelegate::CreateInfoBar(
      std::unique_ptr<AlternateNavInfoBarDelegate>(
          new AlternateNavInfoBarDelegate(
              Profile::FromBrowserContext(web_contents->GetBrowserContext()),
              text, match, search_url))));
}

AlternateNavInfoBarDelegate::AlternateNavInfoBarDelegate(
    Profile* profile,
    const base::string16& text,
    const AutocompleteMatch& match,
    const GURL& search_url)
    : infobars::InfoBarDelegate(),
      profile_(profile),
      text_(text),
      match_(match),
      search_url_(search_url) {
  DCHECK(match_.destination_url.is_valid());
  DCHECK(search_url_.is_valid());
}

// AlternateNavInfoBarDelegate::CreateInfoBar() is implemented in
// platform-specific files.

base::string16 AlternateNavInfoBarDelegate::GetMessageTextWithOffset(
    size_t* link_offset) const {
  const base::string16 label = l10n_util::GetStringFUTF16(
      IDS_ALTERNATE_NAV_URL_VIEW_LABEL, base::string16(), link_offset);
  return label;
}

base::string16 AlternateNavInfoBarDelegate::GetLinkText() const {
  return base::UTF8ToUTF16(match_.destination_url.spec());
}

GURL AlternateNavInfoBarDelegate::GetLinkURL() const {
  return match_.destination_url;
}

bool AlternateNavInfoBarDelegate::LinkClicked(
    WindowOpenDisposition disposition) {
  // Tell the shortcuts backend to remove the shortcut it added for the original
  // search and instead add one reflecting this navigation.
  scoped_refptr<ShortcutsBackend> shortcuts_backend(
      ShortcutsBackendFactory::GetForProfile(profile_));
  if (shortcuts_backend.get()) {  // May be NULL in incognito.
    shortcuts_backend->DeleteShortcutsWithURL(search_url_);
    shortcuts_backend->AddOrUpdateShortcut(text_, match_);
  }

  // Tell the history system to remove any saved search term for the search.
  history::HistoryService* const history_service =
      HistoryServiceFactory::GetForProfile(profile_,
                                           ServiceAccessType::IMPLICIT_ACCESS);
  if (history_service)
    history_service->DeleteKeywordSearchTermForURL(search_url_);

  // Pretend the user typed this URL, so that navigating to it will be the
  // default action when it's typed again in the future.
  InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
      content::OpenURLParams(match_.destination_url, content::Referrer(),
                             disposition, ui::PAGE_TRANSITION_TYPED,
                             false));

  // We should always close, even if the navigation did not occur within this
  // WebContents.
  return true;
}

infobars::InfoBarDelegate::Type
AlternateNavInfoBarDelegate::GetInfoBarType() const {
  return PAGE_ACTION_TYPE;
}

infobars::InfoBarDelegate::InfoBarIdentifier
AlternateNavInfoBarDelegate::GetIdentifier() const {
  return ALTERNATE_NAV_INFOBAR_DELEGATE;
}

gfx::VectorIconId AlternateNavInfoBarDelegate::GetVectorIconId() const {
  return gfx::VectorIconId::GLOBE;
}