OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/safe_browsing/permission_reporter.h" | |
6 | |
7 #include <functional> | |
8 | |
9 #include "base/hash.h" | |
5 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
6 #include "chrome/browser/safe_browsing/permission_reporter.h" | |
7 #include "chrome/common/safe_browsing/permission_report.pb.h" | 11 #include "chrome/common/safe_browsing/permission_report.pb.h" |
8 #include "content/public/browser/permission_type.h" | 12 #include "content/public/browser/permission_type.h" |
9 #include "net/url_request/report_sender.h" | 13 #include "net/url_request/report_sender.h" |
10 | 14 |
11 using content::PermissionType; | 15 using content::PermissionType; |
12 | 16 |
13 namespace safe_browsing { | 17 namespace safe_browsing { |
14 | 18 |
15 namespace { | 19 namespace { |
16 // URL to upload permission action reports. | 20 // URL to upload permission action reports. |
17 const char kPermissionActionReportingUploadUrl[] = | 21 const char kPermissionActionReportingUploadUrl[] = |
18 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" | 22 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" |
19 "permission-action"; | 23 "permission-action"; |
20 | 24 |
25 const int kMaximumReportsPerOriginPerPermission = 5; | |
raymes
2016/06/27 06:26:56
...PerMinute?
stefanocs
2016/06/27 07:34:57
Done.
| |
26 | |
21 PermissionReport::PermissionType PermissionTypeForReport( | 27 PermissionReport::PermissionType PermissionTypeForReport( |
22 PermissionType permission) { | 28 PermissionType permission) { |
23 switch (permission) { | 29 switch (permission) { |
24 case PermissionType::MIDI_SYSEX: | 30 case PermissionType::MIDI_SYSEX: |
25 return PermissionReport::MIDI_SYSEX; | 31 return PermissionReport::MIDI_SYSEX; |
26 case PermissionType::PUSH_MESSAGING: | 32 case PermissionType::PUSH_MESSAGING: |
27 return PermissionReport::PUSH_MESSAGING; | 33 return PermissionReport::PUSH_MESSAGING; |
28 case PermissionType::NOTIFICATIONS: | 34 case PermissionType::NOTIFICATIONS: |
29 return PermissionReport::NOTIFICATIONS; | 35 return PermissionReport::NOTIFICATIONS; |
30 case PermissionType::GEOLOCATION: | 36 case PermissionType::GEOLOCATION: |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 case PERMISSION_ACTION_NUM: | 73 case PERMISSION_ACTION_NUM: |
68 break; | 74 break; |
69 } | 75 } |
70 | 76 |
71 NOTREACHED(); | 77 NOTREACHED(); |
72 return PermissionReport::ACTION_UNSPECIFIED; | 78 return PermissionReport::ACTION_UNSPECIFIED; |
73 } | 79 } |
74 | 80 |
75 } // namespace | 81 } // namespace |
76 | 82 |
83 bool PermissionAndOrigin::operator==(const PermissionAndOrigin& other) const { | |
84 return (permission == other.permission && origin == other.origin); | |
85 } | |
86 | |
87 std::size_t PermissionAndOriginHash::operator()( | |
88 const PermissionAndOrigin& permission_and_origin) const { | |
89 std::size_t permission_hash = | |
90 static_cast<std::size_t>(permission_and_origin.permission); | |
91 std::size_t origin_hash = | |
92 std::hash<std::string>()(permission_and_origin.origin.spec()); | |
93 return base::HashInts(permission_hash, origin_hash); | |
94 } | |
95 | |
77 PermissionReporter::PermissionReporter(net::URLRequestContext* request_context) | 96 PermissionReporter::PermissionReporter(net::URLRequestContext* request_context) |
78 : PermissionReporter(base::WrapUnique(new net::ReportSender( | 97 : PermissionReporter(base::WrapUnique(new net::ReportSender( |
79 request_context, | 98 request_context, |
80 net::ReportSender::CookiesPreference::DO_NOT_SEND_COOKIES))) {} | 99 net::ReportSender::CookiesPreference::DO_NOT_SEND_COOKIES))) {} |
81 | 100 |
82 PermissionReporter::PermissionReporter( | 101 PermissionReporter::PermissionReporter( |
83 std::unique_ptr<net::ReportSender> report_sender) | 102 std::unique_ptr<net::ReportSender> report_sender) |
84 : permission_report_sender_(std::move(report_sender)) {} | 103 : permission_report_sender_(std::move(report_sender)) {} |
85 | 104 |
86 PermissionReporter::~PermissionReporter() {} | 105 PermissionReporter::~PermissionReporter() {} |
87 | 106 |
88 void PermissionReporter::SendReport(const GURL& origin, | 107 void PermissionReporter::SendReport(const GURL& origin, |
89 content::PermissionType permission, | 108 content::PermissionType permission, |
90 PermissionAction action) { | 109 PermissionAction action) { |
110 if (!IsAllowedToSend(permission, origin)) | |
111 return; | |
91 std::string serialized_report; | 112 std::string serialized_report; |
92 BuildReport(origin, permission, action, &serialized_report); | 113 BuildReport(origin, permission, action, &serialized_report); |
93 permission_report_sender_->Send(GURL(kPermissionActionReportingUploadUrl), | 114 permission_report_sender_->Send(GURL(kPermissionActionReportingUploadUrl), |
94 serialized_report); | 115 serialized_report); |
95 } | 116 } |
96 | 117 |
97 // static | 118 // static |
98 bool PermissionReporter::BuildReport(const GURL& origin, | 119 bool PermissionReporter::BuildReport(const GURL& origin, |
99 PermissionType permission, | 120 PermissionType permission, |
100 PermissionAction action, | 121 PermissionAction action, |
101 std::string* output) { | 122 std::string* output) { |
102 PermissionReport report; | 123 PermissionReport report; |
103 report.set_origin(origin.spec()); | 124 report.set_origin(origin.spec()); |
104 report.set_permission(PermissionTypeForReport(permission)); | 125 report.set_permission(PermissionTypeForReport(permission)); |
105 report.set_action(PermissionActionForReport(action)); | 126 report.set_action(PermissionActionForReport(action)); |
106 // TODO(stefanocs): Collect field trials and platform type from global | 127 // TODO(stefanocs): Collect field trials and platform type from global |
107 // variables to the permission report. | 128 // variables to the permission report. |
108 return report.SerializeToString(output); | 129 return report.SerializeToString(output); |
109 } | 130 } |
110 | 131 |
132 bool PermissionReporter::IsAllowedToSend(content::PermissionType permission, | |
133 const GURL& origin) { | |
134 std::queue<base::Time>& history = sent_histories[{permission, origin}]; | |
135 base::Time current_time = base::Time::Now(); | |
136 // Remove entries that are sent more than one minute ago. | |
137 while (!history.empty() && | |
138 current_time - history.front() > base::TimeDelta::FromMinutes(1)) | |
139 history.pop(); | |
140 if (history.size() < kMaximumReportsPerOriginPerPermission) { | |
141 history.push(current_time); | |
142 return true; | |
143 } else { | |
144 return false; | |
145 } | |
146 } | |
147 | |
111 } // namespace safe_browsing | 148 } // namespace safe_browsing |
OLD | NEW |