OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/geolocation/geolocation_infobar_delegate.h" | 5 #include "chrome/browser/geolocation/geolocation_infobar_delegate.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "chrome/browser/content_settings/permission_queue_controller.h" | 8 #include "chrome/browser/content_settings/permission_queue_controller.h" |
9 #include "chrome/browser/infobars/infobar_service.h" | 9 #include "chrome/browser/infobars/infobar_service.h" |
10 #include "chrome/grit/generated_resources.h" | 10 #include "chrome/grit/generated_resources.h" |
11 #include "chrome/grit/locale_settings.h" | 11 #include "chrome/grit/locale_settings.h" |
12 #include "components/google/core/browser/google_util.h" | 12 #include "components/google/core/browser/google_util.h" |
13 #include "components/infobars/core/infobar.h" | 13 #include "components/infobars/core/infobar.h" |
14 #include "content/public/browser/navigation_details.h" | 14 #include "content/public/browser/navigation_details.h" |
15 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 17 #include "grit/generated_resources.h" |
| 18 #include "grit/locale_settings.h" |
17 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
18 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
19 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
20 | 22 |
21 | 23 |
22 namespace { | |
23 | |
24 enum GeolocationInfoBarDelegateEvent { | |
25 // NOTE: Do not renumber these as that would confuse interpretation of | |
26 // previously logged data. When making changes, also update the enum list | |
27 // in tools/metrics/histograms/histograms.xml to keep it in sync. | |
28 | |
29 // The bar was created. | |
30 GEOLOCATION_INFO_BAR_DELEGATE_EVENT_CREATE = 0, | |
31 | |
32 // User allowed use of geolocation. | |
33 GEOLOCATION_INFO_BAR_DELEGATE_EVENT_ALLOW = 1, | |
34 | |
35 // User denied use of geolocation. | |
36 GEOLOCATION_INFO_BAR_DELEGATE_EVENT_DENY = 2, | |
37 | |
38 // User dismissed the bar. | |
39 GEOLOCATION_INFO_BAR_DELEGATE_EVENT_DISMISS = 3, | |
40 | |
41 // User clicked on link. | |
42 DEPRECATED_GEOLOCATION_INFO_BAR_DELEGATE_EVENT_LINK_CLICK = 4, | |
43 | |
44 // User ignored the bar. | |
45 GEOLOCATION_INFO_BAR_DELEGATE_EVENT_IGNORED = 5, | |
46 | |
47 // NOTE: Add entries only immediately above this line. | |
48 GEOLOCATION_INFO_BAR_DELEGATE_EVENT_COUNT = 6 | |
49 }; | |
50 | |
51 void RecordUmaEvent(GeolocationInfoBarDelegateEvent event) { | |
52 UMA_HISTOGRAM_ENUMERATION("Geolocation.InfoBarDelegate.Event", | |
53 event, GEOLOCATION_INFO_BAR_DELEGATE_EVENT_COUNT); | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 // static | 24 // static |
59 infobars::InfoBar* GeolocationInfoBarDelegate::Create( | 25 infobars::InfoBar* GeolocationInfoBarDelegate::Create( |
60 InfoBarService* infobar_service, | 26 InfoBarService* infobar_service, |
61 PermissionQueueController* controller, | 27 PermissionQueueController* controller, |
62 const PermissionRequestID& id, | 28 const PermissionRequestID& id, |
63 const GURL& requesting_frame, | 29 const GURL& requesting_frame, |
64 const std::string& display_languages) { | 30 const std::string& display_languages) { |
65 RecordUmaEvent(GEOLOCATION_INFO_BAR_DELEGATE_EVENT_CREATE); | |
66 const content::NavigationEntry* committed_entry = | 31 const content::NavigationEntry* committed_entry = |
67 infobar_service->web_contents()->GetController().GetLastCommittedEntry(); | 32 infobar_service->web_contents()->GetController().GetLastCommittedEntry(); |
68 GeolocationInfoBarDelegate* const delegate = new GeolocationInfoBarDelegate( | 33 GeolocationInfoBarDelegate* const delegate = new GeolocationInfoBarDelegate( |
69 controller, id, requesting_frame, | 34 controller, id, requesting_frame, |
70 committed_entry ? committed_entry->GetUniqueID() : 0, | 35 committed_entry ? committed_entry->GetUniqueID() : 0, |
71 display_languages); | 36 display_languages); |
72 | 37 |
73 infobars::InfoBar* infobar = ConfirmInfoBarDelegate::CreateInfoBar( | 38 infobars::InfoBar* infobar = ConfirmInfoBarDelegate::CreateInfoBar( |
74 scoped_ptr<ConfirmInfoBarDelegate>(delegate)).release(); | 39 scoped_ptr<ConfirmInfoBarDelegate>(delegate)).release(); |
75 return infobar_service->AddInfoBar(scoped_ptr<infobars::InfoBar>(infobar)); | 40 return infobar_service->AddInfoBar(scoped_ptr<infobars::InfoBar>(infobar)); |
76 } | 41 } |
77 | 42 |
78 GeolocationInfoBarDelegate::GeolocationInfoBarDelegate( | 43 GeolocationInfoBarDelegate::GeolocationInfoBarDelegate( |
79 PermissionQueueController* controller, | 44 PermissionQueueController* controller, |
80 const PermissionRequestID& id, | 45 const PermissionRequestID& id, |
81 const GURL& requesting_frame, | 46 const GURL& requesting_frame, |
82 int contents_unique_id, | 47 int contents_unique_id, |
83 const std::string& display_languages) | 48 const std::string& display_languages) |
84 : ConfirmInfoBarDelegate(), | 49 : PermissionInfobarDelegate(controller, id, requesting_frame, |
85 controller_(controller), | 50 CONTENT_SETTINGS_TYPE_GEOLOCATION), |
86 id_(id), | 51 requesting_frame_(requesting_frame), |
87 requesting_frame_(requesting_frame.GetOrigin()), | 52 display_languages_(display_languages) { |
88 contents_unique_id_(contents_unique_id), | |
89 display_languages_(display_languages), | |
90 user_has_interacted_(false) { | |
91 } | 53 } |
92 | 54 |
93 GeolocationInfoBarDelegate::~GeolocationInfoBarDelegate() { | 55 GeolocationInfoBarDelegate::~GeolocationInfoBarDelegate() { |
94 if (!user_has_interacted_) | |
95 RecordUmaEvent(GEOLOCATION_INFO_BAR_DELEGATE_EVENT_IGNORED); | |
96 } | |
97 | |
98 bool GeolocationInfoBarDelegate::Accept() { | |
99 RecordUmaEvent(GEOLOCATION_INFO_BAR_DELEGATE_EVENT_ALLOW); | |
100 set_user_has_interacted(); | |
101 SetPermission(true, true); | |
102 return true; | |
103 } | |
104 | |
105 void GeolocationInfoBarDelegate::SetPermission(bool update_content_setting, | |
106 bool allowed) { | |
107 content::WebContents* web_contents = | |
108 InfoBarService::WebContentsFromInfoBar(infobar()); | |
109 controller_->OnPermissionSet( | |
110 id_, requesting_frame_, | |
111 web_contents->GetLastCommittedURL().GetOrigin(), | |
112 update_content_setting, allowed); | |
113 } | |
114 | |
115 void GeolocationInfoBarDelegate::InfoBarDismissed() { | |
116 RecordUmaEvent(GEOLOCATION_INFO_BAR_DELEGATE_EVENT_DISMISS); | |
117 set_user_has_interacted(); | |
118 SetPermission(false, false); | |
119 } | 56 } |
120 | 57 |
121 int GeolocationInfoBarDelegate::GetIconID() const { | 58 int GeolocationInfoBarDelegate::GetIconID() const { |
122 return IDR_INFOBAR_GEOLOCATION; | 59 return IDR_INFOBAR_GEOLOCATION; |
123 } | 60 } |
124 | 61 |
125 infobars::InfoBarDelegate::Type GeolocationInfoBarDelegate::GetInfoBarType() | |
126 const { | |
127 return PAGE_ACTION_TYPE; | |
128 } | |
129 | |
130 bool GeolocationInfoBarDelegate::ShouldExpireInternal( | |
131 const NavigationDetails& 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_id) || details.is_reload; | |
136 } | |
137 | |
138 base::string16 GeolocationInfoBarDelegate::GetMessageText() const { | 62 base::string16 GeolocationInfoBarDelegate::GetMessageText() const { |
139 return l10n_util::GetStringFUTF16(IDS_GEOLOCATION_INFOBAR_QUESTION, | 63 return l10n_util::GetStringFUTF16(IDS_GEOLOCATION_INFOBAR_QUESTION, |
140 net::FormatUrl(requesting_frame_, display_languages_, | 64 net::FormatUrl(requesting_frame_, display_languages_, |
141 net::kFormatUrlOmitUsernamePassword | | 65 net::kFormatUrlOmitUsernamePassword | |
142 net::kFormatUrlOmitTrailingSlashOnBareHostname, | 66 net::kFormatUrlOmitTrailingSlashOnBareHostname, |
143 net::UnescapeRule::SPACES, NULL, NULL, NULL)); | 67 net::UnescapeRule::SPACES, NULL, NULL, NULL)); |
144 } | 68 } |
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 } | |
OLD | NEW |