Chromium Code Reviews| 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 // Get the percentage of images that should be reported from Finch. | |
| 61 virtual double GetReportChance() 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. This should be | |
| 70 // callable from any thread and will invoke a member function on the IO thread | |
|
johnme
2017/01/23 11:34:34
Nit: s/callable from any thread/called on the UI t
harkness
2017/01/23 11:45:56
Done.
| |
| 71 // using the IO-based WeapPtr. | |
| 72 static void ReportNotificationImageOnUI( | |
| 73 const base::WeakPtr<NotificationImageReporter>& weak_this_on_io, | |
| 74 Profile* profile, | |
| 75 const GURL& origin, | |
| 76 const SkBitmap& image); | |
| 77 | |
| 78 // Downscales image to fit within 512x512 if necessary, and encodes as it PNG, | |
| 79 // then invokes SendReportOnIO. This should be callable from any thread and | |
|
johnme
2017/01/23 11:34:34
Nit: s/callable from any thread/called on a blocki
harkness
2017/01/23 11:45:56
Done.
| |
| 80 // will invoke a member function on the IO thread using the IO-based WeakPtr. | |
| 81 static void DownscaleNotificationImageOnBlockingPool( | |
| 82 const base::WeakPtr<NotificationImageReporter>& weak_this_on_io, | |
| 83 const GURL& origin, | |
| 84 const SkBitmap& image); | |
| 85 | |
| 86 // Serializes report using NotificationImageReportRequest protobuf defined in | |
| 87 // chrome/common/safe_browsing/csd.proto and sends it to CSD server. | |
| 88 void SendReportOnIO(const GURL& origin, | |
| 89 scoped_refptr<base::RefCountedMemory> data, | |
| 90 const gfx::Size& dimensions, | |
| 91 const gfx::Size& original_dimensions); | |
| 92 | |
| 93 std::unique_ptr<net::ReportSender> report_sender_; | |
| 94 | |
| 95 // Timestamps of when we sent notification images. Used to limit the number | |
| 96 // of requests that we send in a day. Only access on the IO thread. | |
| 97 // TODO(johnme): Serialize this so that it doesn't reset on browser restart. | |
| 98 std::queue<base::Time> report_times_; | |
| 99 | |
| 100 // Keep this last. Only dereference these pointers on the IO thread. | |
| 101 base::WeakPtrFactory<NotificationImageReporter> weak_factory_on_io_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(NotificationImageReporter); | |
| 104 }; | |
| 105 | |
| 106 } // namespace safe_browsing | |
| 107 | |
| 108 #endif // CHROME_BROWSER_SAFE_BROWSING_NOTIFICATION_IMAGE_REPORTER_H_ | |
| OLD | NEW |