OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/safe_browsing/notification_image_reporter.h" | |
6 | |
7 #include <cmath> | |
8 #include <vector> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/logging.h" | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/memory/ref_counted_memory.h" | |
14 #include "base/rand_util.h" | |
15 #include "base/threading/sequenced_worker_pool.h" | |
16 #include "chrome/browser/browser_process.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
19 #include "chrome/common/safe_browsing/csd.pb.h" | |
20 #include "components/safe_browsing_db/database_manager.h" | |
21 #include "components/safe_browsing_db/safe_browsing_prefs.h" | |
22 #include "content/public/browser/browser_thread.h" | |
23 #include "net/url_request/report_sender.h" | |
24 #include "skia/ext/image_operations.h" | |
25 #include "third_party/skia/include/core/SkBitmap.h" | |
26 #include "ui/gfx/codec/png_codec.h" | |
27 #include "url/gurl.h" | |
28 | |
29 using content::BrowserThread; | |
30 | |
31 namespace safe_browsing { | |
32 | |
33 namespace { | |
34 const int kMaxReportsPerDay = 5; | |
Peter Beverloo
2017/01/11 18:23:56
micro nit: int -> size_t to match std::vector::siz
johnme
2017/01/11 20:05:07
Done.
| |
35 const char kReportingUploadUrl[] = | |
36 "https://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" | |
37 "chrome-notification-image"; // TODO(johnme): Confirm URL. | |
38 } // namespace | |
39 | |
40 NotificationImageReporter::NotificationImageReporter( | |
41 net::URLRequestContext* request_context) | |
42 : report_sender_(base::MakeUnique<net::ReportSender>( | |
43 request_context, | |
44 net::ReportSender::CookiesPreference::DO_NOT_SEND_COOKIES)) { | |
45 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
46 } | |
47 | |
48 NotificationImageReporter::~NotificationImageReporter() { | |
49 // Thread on which this is destroyed may vary. | |
50 } | |
51 | |
52 void NotificationImageReporter::ReportNotificationImageOnUI( | |
53 Profile* profile, | |
54 const GURL& origin, | |
55 const SkBitmap& image) { | |
56 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
57 DCHECK_EQ(origin, origin.GetOrigin()); | |
58 | |
59 // Skip reporting unless SBER2 Scout is enabled. | |
60 switch (GetExtendedReportingLevel(*profile->GetPrefs())) { | |
61 case SBER_LEVEL_OFF: | |
62 case SBER_LEVEL_LEGACY: | |
63 return; | |
64 case SBER_LEVEL_SCOUT: | |
65 break; | |
66 } | |
67 | |
68 // Sample a Finch-controlled fraction only. | |
69 double report_chance = 0.2; // TODO(johnme): Get this from Finch. | |
70 if (base::RandDouble() >= report_chance) | |
71 return; | |
72 | |
73 BrowserThread::PostTask( | |
74 BrowserThread::IO, FROM_HERE, | |
75 base::Bind(&NotificationImageReporter::ReportNotificationImageOnIO, this, | |
76 make_scoped_refptr(g_browser_process->safe_browsing_service()), | |
johnme
2017/01/11 17:50:53
I feel a little dirty for accessing this global he
Peter Beverloo
2017/01/11 18:23:56
This question is for Nathan, but I'd love to see a
johnme
2017/01/11 20:05:07
I've added a !safe_browsing_service to the conditi
Nathan Parker
2017/01/11 22:34:38
This is the common way to get the db_manager, so I
| |
77 origin, image)); | |
78 } | |
79 | |
80 void NotificationImageReporter::ReportNotificationImageOnIO( | |
81 scoped_refptr<SafeBrowsingService> safe_browsing_service, | |
82 const GURL& origin, | |
83 const SkBitmap& image) { | |
84 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
85 | |
86 // Skip whitelisted origins to cut down on report volume. | |
87 if (!safe_browsing_service->database_manager() || | |
88 safe_browsing_service->database_manager()->MatchCsdWhitelistUrl(origin)) | |
89 return; | |
Peter Beverloo
2017/01/11 18:23:56
micro nit: {} as this is considered a multi-line s
johnme
2017/01/11 20:05:07
Done.
| |
90 | |
91 // Avoid exceeding kMaxReportsPerDay. | |
92 base::Time a_day_ago = base::Time::Now() - base::TimeDelta::FromDays(1); | |
93 while (!report_times_.empty() && | |
94 report_times_.front() < /* older than */ a_day_ago) { | |
95 report_times_.pop(); | |
96 } | |
97 if (report_times_.size() >= kMaxReportsPerDay) | |
98 return; | |
99 report_times_.push(base::Time::Now()); | |
100 | |
101 BrowserThread::GetBlockingPool()->PostWorkerTask( | |
102 FROM_HERE, | |
103 base::Bind( | |
104 &NotificationImageReporter::DownscaleNotificationImageOnBlockingPool, | |
105 this, origin, image)); | |
106 } | |
107 | |
108 void NotificationImageReporter::DownscaleNotificationImageOnBlockingPool( | |
109 const GURL& origin, | |
110 const SkBitmap& image) { | |
111 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
112 | |
113 // Downscale to fit within 512x512. | |
114 const double MAX_SIZE = 512; | |
Peter Beverloo
2017/01/11 18:23:56
nit: maybe this should be finch controllable too?
johnme
2017/01/11 20:05:07
Added TODO.
| |
115 double scale = std::min(MAX_SIZE / image.width(), MAX_SIZE / image.height()); | |
116 SkBitmap downscaled_image = | |
117 scale >= 1.0 ? image // already small enough | |
118 : skia::ImageOperations::Resize( | |
119 image, skia::ImageOperations::RESIZE_GOOD, | |
120 std::lround(scale * image.width()), | |
121 std::lround(scale * image.height())); | |
122 | |
123 // Encode as PNG. | |
124 std::vector<unsigned char> png_bytes; | |
125 if (!gfx::PNGCodec::EncodeBGRASkBitmap(downscaled_image, false, &png_bytes)) { | |
126 NOTREACHED(); | |
Peter Beverloo
2017/01/11 18:23:56
q: this should only happen for OOM, right?
johnme
2017/01/11 20:05:07
I guess so; don't know enough about how libpng ope
| |
127 return; | |
128 } | |
129 | |
130 BrowserThread::PostTask( | |
131 BrowserThread::IO, FROM_HERE, | |
132 base::Bind( | |
133 &NotificationImageReporter::SendReportOnIO, this, origin, | |
134 base::RefCountedBytes::TakeVector(&png_bytes), | |
135 gfx::Size(image.width(), image.height()), | |
136 gfx::Size(downscaled_image.width(), downscaled_image.height()))); | |
137 } | |
138 | |
139 void NotificationImageReporter::SendReportOnIO( | |
140 const GURL& origin, | |
141 scoped_refptr<base::RefCountedMemory> png_data, | |
142 gfx::Size dimensions, | |
143 gfx::Size original_dimensions) { | |
144 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
145 | |
146 NotificationImageReportRequest report; | |
147 report.set_notification_origin(origin.spec()); | |
148 report.mutable_image()->set_png_data(png_data->front(), png_data->size()); | |
149 report.mutable_image()->mutable_dimensions()->set_width(dimensions.width()); | |
150 report.mutable_image()->mutable_dimensions()->set_height(dimensions.height()); | |
151 if (dimensions != original_dimensions) { | |
152 report.mutable_image()->mutable_original_dimensions()->set_width( | |
153 original_dimensions.width()); | |
154 report.mutable_image()->mutable_original_dimensions()->set_height( | |
155 original_dimensions.height()); | |
156 } | |
157 | |
158 std::string serialized_report; | |
159 report.SerializeToString(&serialized_report); | |
160 report_sender_->Send(GURL(kReportingUploadUrl), "application/octet-stream", | |
161 serialized_report, base::Closure(), | |
162 base::Callback<void(const GURL&, int)>()); | |
Peter Beverloo
2017/01/11 18:23:56
nit: it'd be cool to have UMA for logging (a) the
johnme
2017/01/11 20:05:07
Added TODO.
| |
163 } | |
164 | |
165 } // namespace safe_browsing | |
OLD | NEW |