Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / geolocation / geolocation_infobar_delegate.cc
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 "base/metrics/histogram.h"
8 #include "chrome/browser/content_settings/permission_queue_controller.h"
9 #include "chrome/browser/google/google_util.h"
10 #include "chrome/browser/infobars/infobar.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/web_contents.h"
15 #include "grit/generated_resources.h"
16 #include "grit/locale_settings.h"
17 #include "grit/theme_resources.h"
18 #include "net/base/net_util.h"
19 #include "ui/base/l10n/l10n_util.h"
20
21 #if defined(OS_ANDROID)
22 #include "chrome/browser/geolocation/geolocation_infobar_delegate_android.h"
23 typedef GeolocationInfoBarDelegateAndroid DelegateType;
24 #else
25 typedef GeolocationInfoBarDelegate DelegateType;
26 #endif
27
28 namespace {
29
30 enum GeolocationInfoBarDelegateEvent {
31   // NOTE: Do not renumber these as that would confuse interpretation of
32   // previously logged data. When making changes, also update the enum list
33   // in tools/metrics/histograms/histograms.xml to keep it in sync.
34
35   // The bar was created.
36   GEOLOCATION_INFO_BAR_DELEGATE_EVENT_CREATE = 0,
37
38   // User allowed use of geolocation.
39   GEOLOCATION_INFO_BAR_DELEGATE_EVENT_ALLOW = 1,
40
41   // User denied use of geolocation.
42   GEOLOCATION_INFO_BAR_DELEGATE_EVENT_DENY = 2,
43
44   // User dismissed the bar.
45   GEOLOCATION_INFO_BAR_DELEGATE_EVENT_DISMISS = 3,
46
47   // User clicked on link.
48   GEOLOCATION_INFO_BAR_DELEGATE_EVENT_LINK_CLICK = 4,
49
50   // User ignored the bar.
51   GEOLOCATION_INFO_BAR_DELEGATE_EVENT_IGNORED = 5,
52
53   // NOTE: Add entries only immediately above this line.
54   GEOLOCATION_INFO_BAR_DELEGATE_EVENT_COUNT = 6
55 };
56
57 void RecordUmaEvent(GeolocationInfoBarDelegateEvent event) {
58   UMA_HISTOGRAM_ENUMERATION("Geolocation.InfoBarDelegate.Event",
59       event, GEOLOCATION_INFO_BAR_DELEGATE_EVENT_COUNT);
60 }
61
62 }  // namespace
63
64 // static
65 InfoBar* GeolocationInfoBarDelegate::Create(
66     InfoBarService* infobar_service,
67     PermissionQueueController* controller,
68     const PermissionRequestID& id,
69     const GURL& requesting_frame,
70     const std::string& display_languages) {
71   RecordUmaEvent(GEOLOCATION_INFO_BAR_DELEGATE_EVENT_CREATE);
72   const content::NavigationEntry* committed_entry =
73       infobar_service->web_contents()->GetController().GetLastCommittedEntry();
74   return infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
75       scoped_ptr<ConfirmInfoBarDelegate>(new DelegateType(
76           controller, id, requesting_frame,
77           committed_entry ? committed_entry->GetUniqueID() : 0,
78           display_languages))));
79 }
80
81 GeolocationInfoBarDelegate::GeolocationInfoBarDelegate(
82     PermissionQueueController* controller,
83     const PermissionRequestID& id,
84     const GURL& requesting_frame,
85     int contents_unique_id,
86     const std::string& display_languages)
87     : ConfirmInfoBarDelegate(),
88       controller_(controller),
89       id_(id),
90       requesting_frame_(requesting_frame.GetOrigin()),
91       contents_unique_id_(contents_unique_id),
92       display_languages_(display_languages),
93       user_has_interacted_(false) {
94 }
95
96 GeolocationInfoBarDelegate::~GeolocationInfoBarDelegate() {
97   if (!user_has_interacted_)
98     RecordUmaEvent(GEOLOCATION_INFO_BAR_DELEGATE_EVENT_IGNORED);
99 }
100
101 bool GeolocationInfoBarDelegate::Accept() {
102   RecordUmaEvent(GEOLOCATION_INFO_BAR_DELEGATE_EVENT_ALLOW);
103   set_user_has_interacted();
104   SetPermission(true, true);
105   return true;
106 }
107
108 void GeolocationInfoBarDelegate::SetPermission(bool update_content_setting,
109                                                bool allowed) {
110   controller_->OnPermissionSet(
111         id_, requesting_frame_,
112         web_contents()->GetLastCommittedURL().GetOrigin(),
113         update_content_setting, allowed);
114 }
115
116 void GeolocationInfoBarDelegate::InfoBarDismissed() {
117   RecordUmaEvent(GEOLOCATION_INFO_BAR_DELEGATE_EVENT_DISMISS);
118   set_user_has_interacted();
119   SetPermission(false, false);
120 }
121
122 int GeolocationInfoBarDelegate::GetIconID() const {
123   return IDR_GEOLOCATION_INFOBAR_ICON;
124 }
125
126 InfoBarDelegate::Type GeolocationInfoBarDelegate::GetInfoBarType() const {
127   return PAGE_ACTION_TYPE;
128 }
129
130 bool GeolocationInfoBarDelegate::ShouldExpireInternal(
131     const content::LoadCommittedDetails& details) const {
132   // This implementation matches InfoBarDelegate::ShouldExpireInternal(), but
133   // uses the unique ID we set in the constructor instead of that stored in the
134   // base class.
135   return (contents_unique_id_ != details.entry->GetUniqueID()) ||
136       (content::PageTransitionStripQualifier(
137           details.entry->GetTransitionType()) ==
138               content::PAGE_TRANSITION_RELOAD);
139 }
140
141 base::string16 GeolocationInfoBarDelegate::GetMessageText() const {
142   return l10n_util::GetStringFUTF16(IDS_GEOLOCATION_INFOBAR_QUESTION,
143       net::FormatUrl(requesting_frame_, display_languages_));
144 }
145
146 base::string16 GeolocationInfoBarDelegate::GetButtonLabel(
147     InfoBarButton button) const {
148   return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
149       IDS_GEOLOCATION_ALLOW_BUTTON : IDS_GEOLOCATION_DENY_BUTTON);
150 }
151
152 bool GeolocationInfoBarDelegate::Cancel() {
153   RecordUmaEvent(GEOLOCATION_INFO_BAR_DELEGATE_EVENT_DENY);
154   set_user_has_interacted();
155   SetPermission(true, false);
156   return true;
157 }
158
159 base::string16 GeolocationInfoBarDelegate::GetLinkText() const {
160   return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
161 }
162
163 bool GeolocationInfoBarDelegate::LinkClicked(
164     WindowOpenDisposition disposition) {
165   RecordUmaEvent(GEOLOCATION_INFO_BAR_DELEGATE_EVENT_LINK_CLICK);
166   const char kGeolocationLearnMoreUrl[] =
167 #if defined(OS_CHROMEOS)
168       "https://www.google.com/support/chromeos/bin/answer.py?answer=142065";
169 #elif defined(OS_ANDROID)
170       "https://support.google.com/chrome/?p=mobile_location";
171 #else
172       "https://www.google.com/support/chrome/bin/answer.py?answer=142065";
173 #endif
174
175   web_contents()->OpenURL(content::OpenURLParams(
176       google_util::AppendGoogleLocaleParam(GURL(kGeolocationLearnMoreUrl)),
177       content::Referrer(),
178       (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
179       content::PAGE_TRANSITION_LINK, false));
180   return false;  // Do not dismiss the info bar.
181 }