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/geolocation/geolocation_infobar_delegate.h"
6
7#include "chrome/browser/content_settings/permission_queue_controller.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/web_contents.h"
13#include "grit/generated_resources.h"
14#include "grit/locale_settings.h"
15#include "grit/theme_resources.h"
16#include "net/base/net_util.h"
17#include "ui/base/l10n/l10n_util.h"
18
19#if defined(OS_ANDROID)
20#include "chrome/browser/geolocation/geolocation_infobar_delegate_android.h"
21typedef GeolocationInfoBarDelegateAndroid DelegateType;
22#else
23typedef GeolocationInfoBarDelegate DelegateType;
24#endif
25
26
27// static
28InfoBarDelegate* GeolocationInfoBarDelegate::Create(
29    InfoBarService* infobar_service,
30    PermissionQueueController* controller,
31    const PermissionRequestID& id,
32    const GURL& requesting_frame,
33    const std::string& display_languages) {
34  const content::NavigationEntry* committed_entry =
35      infobar_service->web_contents()->GetController().GetLastCommittedEntry();
36  return infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>(
37      new DelegateType(infobar_service, controller, id, requesting_frame,
38                       committed_entry ? committed_entry->GetUniqueID() : 0,
39                       display_languages)));
40}
41
42GeolocationInfoBarDelegate::GeolocationInfoBarDelegate(
43    InfoBarService* infobar_service,
44    PermissionQueueController* controller,
45    const PermissionRequestID& id,
46    const GURL& requesting_frame,
47    int contents_unique_id,
48    const std::string& display_languages)
49    : ConfirmInfoBarDelegate(infobar_service),
50      controller_(controller),
51      id_(id),
52      requesting_frame_(requesting_frame),
53      contents_unique_id_(contents_unique_id),
54      display_languages_(display_languages) {
55}
56
57GeolocationInfoBarDelegate::~GeolocationInfoBarDelegate() {
58}
59
60bool GeolocationInfoBarDelegate::Accept() {
61  SetPermission(true, true);
62  return true;
63}
64
65void GeolocationInfoBarDelegate::SetPermission(bool update_content_setting,
66                                               bool allowed) {
67  if (web_contents()) {
68    controller_->OnPermissionSet(id_, requesting_frame_,
69                                 web_contents()->GetURL(),
70                                 update_content_setting, allowed);
71  }
72}
73
74int GeolocationInfoBarDelegate::GetIconID() const {
75  return IDR_GEOLOCATION_INFOBAR_ICON;
76}
77
78InfoBarDelegate::Type GeolocationInfoBarDelegate::GetInfoBarType() const {
79  return PAGE_ACTION_TYPE;
80}
81
82bool GeolocationInfoBarDelegate::ShouldExpireInternal(
83    const content::LoadCommittedDetails& details) const {
84  // This implementation matches InfoBarDelegate::ShouldExpireInternal(), but
85  // uses the unique ID we set in the constructor instead of that stored in the
86  // base class.
87  return (contents_unique_id_ != details.entry->GetUniqueID()) ||
88      (content::PageTransitionStripQualifier(
89          details.entry->GetTransitionType()) ==
90              content::PAGE_TRANSITION_RELOAD);
91}
92
93string16 GeolocationInfoBarDelegate::GetMessageText() const {
94  return l10n_util::GetStringFUTF16(IDS_GEOLOCATION_INFOBAR_QUESTION,
95      net::FormatUrl(requesting_frame_.GetOrigin(), display_languages_));
96}
97
98string16 GeolocationInfoBarDelegate::GetButtonLabel(
99    InfoBarButton button) const {
100  return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
101      IDS_GEOLOCATION_ALLOW_BUTTON : IDS_GEOLOCATION_DENY_BUTTON);
102}
103
104bool GeolocationInfoBarDelegate::Cancel() {
105  SetPermission(true, false);
106  return true;
107}
108
109string16 GeolocationInfoBarDelegate::GetLinkText() const {
110  return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
111}
112
113bool GeolocationInfoBarDelegate::LinkClicked(
114    WindowOpenDisposition disposition) {
115  const char kGeolocationLearnMoreUrl[] =
116#if defined(OS_CHROMEOS)
117      "https://www.google.com/support/chromeos/bin/answer.py?answer=142065";
118#else
119      "https://www.google.com/support/chrome/bin/answer.py?answer=142065";
120#endif
121
122  web_contents()->OpenURL(content::OpenURLParams(
123      google_util::AppendGoogleLocaleParam(GURL(kGeolocationLearnMoreUrl)),
124      content::Referrer(),
125      (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
126      content::PAGE_TRANSITION_LINK, false));
127  return false;  // Do not dismiss the info bar.
128}
129