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 #ifndef CHROME_BROWSER_SAFE_BROWSING_NOTIFICATION_IMAGE_REPORTER_H_ | |
6 #define CHROME_BROWSER_SAFE_BROWSING_NOTIFICATION_IMAGE_REPORTER_H_ | |
7 | |
8 #include <memory> | |
9 #include <queue> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/time/time.h" | |
15 | |
16 class GURL; | |
17 class Profile; | |
18 class SkBitmap; | |
19 | |
20 namespace base { | |
21 class RefCountedMemory; | |
22 } // namespace base | |
23 | |
24 namespace gfx { | |
25 class Size; | |
26 } // namespace gfx | |
27 | |
28 namespace net { | |
29 class ReportSender; | |
30 class URLRequestContext; | |
31 } // namespace net | |
32 | |
33 namespace safe_browsing { | |
34 | |
35 class SafeBrowsingDatabaseManager; | |
36 | |
37 // Provides functionality for building and sending reports about notification | |
38 // content images to the Safe Browsing CSD server. | |
39 class NotificationImageReporter { | |
40 public: | |
41 // CSD server URL to which notification image reports are sent. | |
42 static const char kReportingUploadUrl[]; | |
43 | |
44 explicit NotificationImageReporter(net::URLRequestContext* request_context); | |
45 virtual ~NotificationImageReporter(); | |
46 | |
47 // Report notification content image to SafeBrowsing CSD server if the user | |
48 // has opted in, the origin is not whitelisted, and it is randomly sampled. | |
49 // Can only be called on IO thread. | |
50 void ReportNotificationImageOnIO( | |
51 Profile* profile, | |
52 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager, | |
53 const GURL& origin, | |
54 const SkBitmap& image); | |
55 | |
56 protected: | |
57 explicit NotificationImageReporter( | |
58 std::unique_ptr<net::ReportSender> report_sender); | |
59 | |
60 // Whether reporting is enabled for this instance. | |
61 virtual bool IsReportingEnabled() const; | |
62 | |
63 // Tests may wish to override this to find out if reports are skipped. Called | |
64 // on the IO thread. | |
65 virtual void SkippedReporting(); | |
66 | |
67 private: | |
68 // Report notification content image to SafeBrowsing CSD server if necessary, | |
69 // by invoking DownscaleNotificationImageOnBlockingPool. | |
Nathan Parker
2017/01/20 00:25:43
Add a comment on why this is static and takes a we
harkness
2017/01/20 16:58:06
Done.
| |
70 static void ReportNotificationImageOnUI( | |
71 const base::WeakPtr<NotificationImageReporter>& weak_this_on_io, | |
72 Profile* profile, | |
73 const GURL& origin, | |
74 const SkBitmap& image); | |
75 | |
76 // Downscales image to fit within 512x512 if necessary, and encodes as it PNG, | |
77 // then invokes SendReportOnIO. | |
78 static void DownscaleNotificationImageOnBlockingPool( | |
79 const base::WeakPtr<NotificationImageReporter>& weak_this_on_io, | |
80 const GURL& origin, | |
81 const SkBitmap& image); | |
82 | |
83 // Serializes report using NotificationImageReportRequest protobuf defined in | |
84 // chrome/common/safe_browsing/csd.proto and sends it to CSD server. | |
85 void SendReportOnIO(const GURL& origin, | |
86 scoped_refptr<base::RefCountedMemory> data, | |
87 const gfx::Size& dimensions, | |
88 const gfx::Size& original_dimensions); | |
89 | |
90 std::unique_ptr<net::ReportSender> report_sender_; | |
91 | |
92 // Timestamps of when we sent notification images. Used to limit the number | |
93 // of requests that we send in a day. Only access on the IO thread. | |
94 // TODO(johnme): Serialize this so that it doesn't reset on browser restart. | |
95 std::queue<base::Time> report_times_; | |
96 | |
97 // Keep this last. Only dereference these pointers on the IO thread. | |
98 base::WeakPtrFactory<NotificationImageReporter> weak_factory_on_io_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(NotificationImageReporter); | |
101 }; | |
102 | |
103 } // namespace safe_browsing | |
104 | |
105 #endif // CHROME_BROWSER_SAFE_BROWSING_NOTIFICATION_IMAGE_REPORTER_H_ | |
OLD | NEW |