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