| 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" | 11 #include "base/time/default_clock.h" |
| 7 #include "chrome/common/safe_browsing/permission_report.pb.h" | 12 #include "chrome/common/safe_browsing/permission_report.pb.h" |
| 8 #include "components/variations/active_field_trials.h" | 13 #include "components/variations/active_field_trials.h" |
| 9 #include "content/public/browser/permission_type.h" | 14 #include "content/public/browser/permission_type.h" |
| 10 #include "net/url_request/report_sender.h" | 15 #include "net/url_request/report_sender.h" |
| 11 | 16 |
| 12 using content::PermissionType; | 17 using content::PermissionType; |
| 13 | 18 |
| 14 namespace safe_browsing { | 19 namespace safe_browsing { |
| 15 | 20 |
| 16 namespace { | 21 namespace { |
| 17 // URL to upload permission action reports. | 22 // URL to upload permission action reports. |
| 18 const char kPermissionActionReportingUploadUrl[] = | 23 const char kPermissionActionReportingUploadUrl[] = |
| 19 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" | 24 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" |
| 20 "permission-action"; | 25 "permission-action"; |
| 21 | 26 |
| 27 const int kMaximumReportsPerOriginPerPermissionPerMinute = 5; |
| 28 |
| 22 PermissionReport::PermissionType PermissionTypeForReport( | 29 PermissionReport::PermissionType PermissionTypeForReport( |
| 23 PermissionType permission) { | 30 PermissionType permission) { |
| 24 switch (permission) { | 31 switch (permission) { |
| 25 case PermissionType::MIDI_SYSEX: | 32 case PermissionType::MIDI_SYSEX: |
| 26 return PermissionReport::MIDI_SYSEX; | 33 return PermissionReport::MIDI_SYSEX; |
| 27 case PermissionType::PUSH_MESSAGING: | 34 case PermissionType::PUSH_MESSAGING: |
| 28 return PermissionReport::PUSH_MESSAGING; | 35 return PermissionReport::PUSH_MESSAGING; |
| 29 case PermissionType::NOTIFICATIONS: | 36 case PermissionType::NOTIFICATIONS: |
| 30 return PermissionReport::NOTIFICATIONS; | 37 return PermissionReport::NOTIFICATIONS; |
| 31 case PermissionType::GEOLOCATION: | 38 case PermissionType::GEOLOCATION: |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 case PERMISSION_ACTION_NUM: | 75 case PERMISSION_ACTION_NUM: |
| 69 break; | 76 break; |
| 70 } | 77 } |
| 71 | 78 |
| 72 NOTREACHED(); | 79 NOTREACHED(); |
| 73 return PermissionReport::ACTION_UNSPECIFIED; | 80 return PermissionReport::ACTION_UNSPECIFIED; |
| 74 } | 81 } |
| 75 | 82 |
| 76 } // namespace | 83 } // namespace |
| 77 | 84 |
| 85 bool PermissionAndOrigin::operator==(const PermissionAndOrigin& other) const { |
| 86 return (permission == other.permission && origin == other.origin); |
| 87 } |
| 88 |
| 89 std::size_t PermissionAndOriginHash::operator()( |
| 90 const PermissionAndOrigin& permission_and_origin) const { |
| 91 std::size_t permission_hash = |
| 92 static_cast<std::size_t>(permission_and_origin.permission); |
| 93 std::size_t origin_hash = |
| 94 std::hash<std::string>()(permission_and_origin.origin.spec()); |
| 95 return base::HashInts(permission_hash, origin_hash); |
| 96 } |
| 97 |
| 78 PermissionReporter::PermissionReporter(net::URLRequestContext* request_context) | 98 PermissionReporter::PermissionReporter(net::URLRequestContext* request_context) |
| 79 : PermissionReporter(base::WrapUnique(new net::ReportSender( | 99 : PermissionReporter( |
| 80 request_context, | 100 base::WrapUnique(new net::ReportSender( |
| 81 net::ReportSender::CookiesPreference::DO_NOT_SEND_COOKIES))) {} | 101 request_context, |
| 102 net::ReportSender::CookiesPreference::DO_NOT_SEND_COOKIES)), |
| 103 base::WrapUnique(new base::DefaultClock)) {} |
| 82 | 104 |
| 83 PermissionReporter::PermissionReporter( | 105 PermissionReporter::PermissionReporter( |
| 84 std::unique_ptr<net::ReportSender> report_sender) | 106 std::unique_ptr<net::ReportSender> report_sender, |
| 85 : permission_report_sender_(std::move(report_sender)) {} | 107 std::unique_ptr<base::Clock> clock) |
| 108 : permission_report_sender_(std::move(report_sender)), |
| 109 clock_(std::move(clock)) {} |
| 86 | 110 |
| 87 PermissionReporter::~PermissionReporter() {} | 111 PermissionReporter::~PermissionReporter() {} |
| 88 | 112 |
| 89 void PermissionReporter::SendReport(const GURL& origin, | 113 void PermissionReporter::SendReport(const GURL& origin, |
| 90 content::PermissionType permission, | 114 content::PermissionType permission, |
| 91 PermissionAction action) { | 115 PermissionAction action) { |
| 116 if (IsReportThresholdExceeded(permission, origin)) |
| 117 return; |
| 92 std::string serialized_report; | 118 std::string serialized_report; |
| 93 BuildReport(origin, permission, action, &serialized_report); | 119 BuildReport(origin, permission, action, &serialized_report); |
| 94 permission_report_sender_->Send(GURL(kPermissionActionReportingUploadUrl), | 120 permission_report_sender_->Send(GURL(kPermissionActionReportingUploadUrl), |
| 95 serialized_report); | 121 serialized_report); |
| 96 } | 122 } |
| 97 | 123 |
| 98 // static | 124 // static |
| 99 bool PermissionReporter::BuildReport(const GURL& origin, | 125 bool PermissionReporter::BuildReport(const GURL& origin, |
| 100 PermissionType permission, | 126 PermissionType permission, |
| 101 PermissionAction action, | 127 PermissionAction action, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 119 std::vector<variations::ActiveGroupId> active_group_ids; | 145 std::vector<variations::ActiveGroupId> active_group_ids; |
| 120 variations::GetFieldTrialActiveGroupIds(&active_group_ids); | 146 variations::GetFieldTrialActiveGroupIds(&active_group_ids); |
| 121 for (auto active_group_id : active_group_ids) { | 147 for (auto active_group_id : active_group_ids) { |
| 122 PermissionReport::FieldTrial* field_trial = report.add_field_trials(); | 148 PermissionReport::FieldTrial* field_trial = report.add_field_trials(); |
| 123 field_trial->set_name_id(active_group_id.name); | 149 field_trial->set_name_id(active_group_id.name); |
| 124 field_trial->set_group_id(active_group_id.group); | 150 field_trial->set_group_id(active_group_id.group); |
| 125 } | 151 } |
| 126 return report.SerializeToString(output); | 152 return report.SerializeToString(output); |
| 127 } | 153 } |
| 128 | 154 |
| 155 bool PermissionReporter::IsReportThresholdExceeded( |
| 156 content::PermissionType permission, |
| 157 const GURL& origin) { |
| 158 std::queue<base::Time>& log = report_logs_[{permission, origin}]; |
| 159 base::Time current_time = clock_->Now(); |
| 160 // Remove entries that are sent more than one minute ago. |
| 161 while (!log.empty() && |
| 162 current_time - log.front() > base::TimeDelta::FromMinutes(1)) { |
| 163 log.pop(); |
| 164 } |
| 165 if (log.size() < kMaximumReportsPerOriginPerPermissionPerMinute) { |
| 166 log.push(current_time); |
| 167 return false; |
| 168 } else { |
| 169 return true; |
| 170 } |
| 171 } |
| 172 |
| 129 } // namespace safe_browsing | 173 } // namespace safe_browsing |
| OLD | NEW |