| 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" | 5 #include "chrome/browser/safe_browsing/permission_reporter.h" |
| 6 | 6 |
| 7 #include <functional> | 7 #include <functional> |
| 8 | 8 |
| 9 #include "base/hash.h" | 9 #include "base/hash.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/time/default_clock.h" | 11 #include "base/time/default_clock.h" |
| 12 #include "chrome/browser/permissions/permission_request.h" |
| 12 #include "chrome/common/safe_browsing/permission_report.pb.h" | 13 #include "chrome/common/safe_browsing/permission_report.pb.h" |
| 13 #include "components/variations/active_field_trials.h" | 14 #include "components/variations/active_field_trials.h" |
| 14 #include "content/public/browser/permission_type.h" | 15 #include "content/public/browser/permission_type.h" |
| 15 #include "net/url_request/report_sender.h" | 16 #include "net/url_request/report_sender.h" |
| 16 | 17 |
| 17 using content::PermissionType; | 18 using content::PermissionType; |
| 18 | 19 |
| 19 namespace safe_browsing { | 20 namespace safe_browsing { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 case PermissionSourceUI::PAGE_ACTION: | 92 case PermissionSourceUI::PAGE_ACTION: |
| 92 return PermissionReport::PAGE_ACTION; | 93 return PermissionReport::PAGE_ACTION; |
| 93 case PermissionSourceUI::SOURCE_UI_NUM: | 94 case PermissionSourceUI::SOURCE_UI_NUM: |
| 94 break; | 95 break; |
| 95 } | 96 } |
| 96 | 97 |
| 97 NOTREACHED(); | 98 NOTREACHED(); |
| 98 return PermissionReport::SOURCE_UI_UNSPECIFIED; | 99 return PermissionReport::SOURCE_UI_UNSPECIFIED; |
| 99 } | 100 } |
| 100 | 101 |
| 102 PermissionReport::GestureType GestureTypeForReport( |
| 103 PermissionRequestGestureType gesture_type) { |
| 104 switch (gesture_type) { |
| 105 case PermissionRequestGestureType::UNKNOWN: |
| 106 return PermissionReport::GESTURE_TYPE_UNSPECIFIED; |
| 107 case PermissionRequestGestureType::GESTURE: |
| 108 return PermissionReport::GESTURE; |
| 109 case PermissionRequestGestureType::NO_GESTURE: |
| 110 return PermissionReport::NO_GESTURE; |
| 111 case PermissionRequestGestureType::NUM: |
| 112 break; |
| 113 } |
| 114 |
| 115 NOTREACHED(); |
| 116 return PermissionReport::GESTURE_TYPE_UNSPECIFIED; |
| 117 } |
| 118 |
| 101 } // namespace | 119 } // namespace |
| 102 | 120 |
| 103 bool PermissionAndOrigin::operator==(const PermissionAndOrigin& other) const { | 121 bool PermissionAndOrigin::operator==(const PermissionAndOrigin& other) const { |
| 104 return (permission == other.permission && origin == other.origin); | 122 return (permission == other.permission && origin == other.origin); |
| 105 } | 123 } |
| 106 | 124 |
| 107 std::size_t PermissionAndOriginHash::operator()( | 125 std::size_t PermissionAndOriginHash::operator()( |
| 108 const PermissionAndOrigin& permission_and_origin) const { | 126 const PermissionAndOrigin& permission_and_origin) const { |
| 109 std::size_t permission_hash = | 127 std::size_t permission_hash = |
| 110 static_cast<std::size_t>(permission_and_origin.permission); | 128 static_cast<std::size_t>(permission_and_origin.permission); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 125 std::unique_ptr<base::Clock> clock) | 143 std::unique_ptr<base::Clock> clock) |
| 126 : permission_report_sender_(std::move(report_sender)), | 144 : permission_report_sender_(std::move(report_sender)), |
| 127 clock_(std::move(clock)) {} | 145 clock_(std::move(clock)) {} |
| 128 | 146 |
| 129 PermissionReporter::~PermissionReporter() {} | 147 PermissionReporter::~PermissionReporter() {} |
| 130 | 148 |
| 131 void PermissionReporter::SendReport(const GURL& origin, | 149 void PermissionReporter::SendReport(const GURL& origin, |
| 132 content::PermissionType permission, | 150 content::PermissionType permission, |
| 133 PermissionAction action, | 151 PermissionAction action, |
| 134 PermissionSourceUI source_ui, | 152 PermissionSourceUI source_ui, |
| 135 bool user_gesture) { | 153 PermissionRequestGestureType gesture_type) { |
| 136 if (IsReportThresholdExceeded(permission, origin)) | 154 if (IsReportThresholdExceeded(permission, origin)) |
| 137 return; | 155 return; |
| 138 std::string serialized_report; | 156 std::string serialized_report; |
| 139 BuildReport(origin, permission, action, source_ui, user_gesture, | 157 BuildReport(origin, permission, action, source_ui, gesture_type, |
| 140 &serialized_report); | 158 &serialized_report); |
| 141 permission_report_sender_->Send(GURL(kPermissionActionReportingUploadUrl), | 159 permission_report_sender_->Send(GURL(kPermissionActionReportingUploadUrl), |
| 142 serialized_report); | 160 serialized_report); |
| 143 } | 161 } |
| 144 | 162 |
| 145 // static | 163 // static |
| 146 bool PermissionReporter::BuildReport(const GURL& origin, | 164 bool PermissionReporter::BuildReport(const GURL& origin, |
| 147 PermissionType permission, | 165 PermissionType permission, |
| 148 PermissionAction action, | 166 PermissionAction action, |
| 149 PermissionSourceUI source_ui, | 167 PermissionSourceUI source_ui, |
| 150 bool user_gesture, | 168 PermissionRequestGestureType gesture_type, |
| 151 std::string* output) { | 169 std::string* output) { |
| 152 PermissionReport report; | 170 PermissionReport report; |
| 153 report.set_origin(origin.spec()); | 171 report.set_origin(origin.spec()); |
| 154 report.set_permission(PermissionTypeForReport(permission)); | 172 report.set_permission(PermissionTypeForReport(permission)); |
| 155 report.set_action(PermissionActionForReport(action)); | 173 report.set_action(PermissionActionForReport(action)); |
| 156 report.set_source_ui(SourceUIForReport(source_ui)); | 174 report.set_source_ui(SourceUIForReport(source_ui)); |
| 157 if (user_gesture) | 175 report.set_gesture(GestureTypeForReport(gesture_type)); |
| 158 report.add_request_trigger(PermissionReport::AFTER_GESTURE); | |
| 159 | 176 |
| 160 // Collect platform data. | 177 // Collect platform data. |
| 161 #if defined(OS_ANDROID) | 178 #if defined(OS_ANDROID) |
| 162 report.set_platform_type(PermissionReport::ANDROID_PLATFORM); | 179 report.set_platform_type(PermissionReport::ANDROID_PLATFORM); |
| 163 #elif defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_CHROMEOS) || \ | 180 #elif defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_CHROMEOS) || \ |
| 164 defined(OS_LINUX) | 181 defined(OS_LINUX) |
| 165 report.set_platform_type(PermissionReport::DESKTOP_PLATFORM); | 182 report.set_platform_type(PermissionReport::DESKTOP_PLATFORM); |
| 166 #else | 183 #else |
| 167 #error Unsupported platform. | 184 #error Unsupported platform. |
| 168 #endif | 185 #endif |
| (...skipping 21 matching lines...) Expand all Loading... |
| 190 } | 207 } |
| 191 if (log.size() < kMaximumReportsPerOriginPerPermissionPerMinute) { | 208 if (log.size() < kMaximumReportsPerOriginPerPermissionPerMinute) { |
| 192 log.push(current_time); | 209 log.push(current_time); |
| 193 return false; | 210 return false; |
| 194 } else { | 211 } else { |
| 195 return true; | 212 return true; |
| 196 } | 213 } |
| 197 } | 214 } |
| 198 | 215 |
| 199 } // namespace safe_browsing | 216 } // namespace safe_browsing |
| OLD | NEW |