OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Helper class which handles communication with the SafeBrowsing backends for | 5 // Helper class which handles communication with the SafeBrowsing backends for |
6 // client-side phishing detection. This class can be used to get a file | 6 // client-side phishing detection. This class can be used to get a file |
7 // descriptor to the client-side phishing model and also to send a ping back to | 7 // descriptor to the client-side phishing model and also to send a ping back to |
8 // Google to verify if a particular site is really phishing or not. | 8 // Google to verify if a particular site is really phishing or not. |
9 // | 9 // |
10 // This class is not thread-safe and expects all calls to GetModelFile() and | 10 // This class is not thread-safe and expects all calls to GetModelFile() and |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "base/file_path.h" | 26 #include "base/file_path.h" |
27 #include "base/gtest_prod_util.h" | 27 #include "base/gtest_prod_util.h" |
28 #include "base/platform_file.h" | 28 #include "base/platform_file.h" |
29 #include "base/ref_counted.h" | 29 #include "base/ref_counted.h" |
30 #include "base/scoped_callback_factory.h" | 30 #include "base/scoped_callback_factory.h" |
31 #include "base/scoped_ptr.h" | 31 #include "base/scoped_ptr.h" |
32 #include "base/task.h" | 32 #include "base/task.h" |
33 #include "base/time.h" | 33 #include "base/time.h" |
34 #include "chrome/browser/safe_browsing/csd.pb.h" | 34 #include "chrome/browser/safe_browsing/csd.pb.h" |
35 #include "chrome/common/net/url_fetcher.h" | 35 #include "chrome/common/net/url_fetcher.h" |
| 36 #include "chrome/common/notification_observer.h" |
| 37 #include "chrome/common/notification_registrar.h" |
36 #include "googleurl/src/gurl.h" | 38 #include "googleurl/src/gurl.h" |
37 | 39 |
38 class URLRequestContextGetter; | 40 class URLRequestContextGetter; |
39 | 41 |
40 namespace net { | 42 namespace net { |
41 class URLRequestStatus; | 43 class URLRequestStatus; |
42 } // namespace net | 44 } // namespace net |
43 | 45 |
44 namespace safe_browsing { | 46 namespace safe_browsing { |
45 | 47 |
46 class ClientSideDetectionService : public URLFetcher::Delegate { | 48 class ClientSideDetectionService : public URLFetcher::Delegate, |
| 49 public NotificationObserver { |
47 public: | 50 public: |
48 typedef Callback1<base::PlatformFile>::Type OpenModelDoneCallback; | 51 typedef Callback1<base::PlatformFile>::Type OpenModelDoneCallback; |
49 | 52 |
50 typedef Callback2<GURL /* phishing URL */, bool /* is phishing */>::Type | 53 typedef Callback2<GURL /* phishing URL */, bool /* is phishing */>::Type |
51 ClientReportPhishingRequestCallback; | 54 ClientReportPhishingRequestCallback; |
52 | 55 |
53 virtual ~ClientSideDetectionService(); | 56 virtual ~ClientSideDetectionService(); |
54 | 57 |
55 // Creates a client-side detection service and starts fetching the client-side | 58 // Creates a client-side detection service and starts fetching the client-side |
56 // detection model if necessary. The model will be stored in |model_path|. | 59 // detection model if necessary. The model will be stored in |model_path|. |
57 // The caller takes ownership of the object. This function may return NULL. | 60 // The caller takes ownership of the object. This function may return NULL. |
58 static ClientSideDetectionService* Create( | 61 static ClientSideDetectionService* Create( |
59 const FilePath& model_path, | 62 const FilePath& model_path, |
60 URLRequestContextGetter* request_context_getter); | 63 URLRequestContextGetter* request_context_getter); |
61 | 64 |
62 // From the URLFetcher::Delegate interface. | 65 // From the URLFetcher::Delegate interface. |
63 virtual void OnURLFetchComplete(const URLFetcher* source, | 66 virtual void OnURLFetchComplete(const URLFetcher* source, |
64 const GURL& url, | 67 const GURL& url, |
65 const net::URLRequestStatus& status, | 68 const net::URLRequestStatus& status, |
66 int response_code, | 69 int response_code, |
67 const ResponseCookies& cookies, | 70 const ResponseCookies& cookies, |
68 const std::string& data); | 71 const std::string& data); |
69 | 72 |
| 73 // From the NotificationObserver interface. |
| 74 virtual void Observe(NotificationType type, |
| 75 const NotificationSource& source, |
| 76 const NotificationDetails& details); |
| 77 |
70 // Gets the model file descriptor once the model is ready and stored | 78 // Gets the model file descriptor once the model is ready and stored |
71 // on disk. If there was an error the callback is called and the | 79 // on disk. If there was an error the callback is called and the |
72 // platform file is set to kInvalidPlatformFileValue. The | 80 // platform file is set to kInvalidPlatformFileValue. The |
73 // ClientSideDetectionService takes ownership of the |callback|. | 81 // ClientSideDetectionService takes ownership of the |callback|. |
74 // The callback is always called after GetModelFile() returns and on the | 82 // The callback is always called after GetModelFile() returns and on the |
75 // same thread as GetModelFile() was called. | 83 // same thread as GetModelFile() was called. |
76 void GetModelFile(OpenModelDoneCallback* callback); | 84 void GetModelFile(OpenModelDoneCallback* callback); |
77 | 85 |
78 // Sends a request to the SafeBrowsing servers with the potentially phishing | 86 // Sends a request to the SafeBrowsing servers with the potentially phishing |
79 // URL and the client-side phishing score. The |phishing_url| scheme should | 87 // URL and the client-side phishing score. The |phishing_url| scheme should |
80 // be HTTP. This method takes ownership of the |callback| and calls it once | 88 // be HTTP. This method takes ownership of the |callback| and calls it once |
81 // the result has come back from the server or if an error occurs during the | 89 // the result has come back from the server or if an error occurs during the |
82 // fetch. If an error occurs the phishing verdict will always be false. The | 90 // fetch. If an error occurs the phishing verdict will always be false. The |
83 // callback is always called after SendClientReportPhishingRequest() returns | 91 // callback is always called after SendClientReportPhishingRequest() returns |
84 // and on the same thread as SendClientReportPhishingRequest() was called. | 92 // and on the same thread as SendClientReportPhishingRequest() was called. |
85 void SendClientReportPhishingRequest( | 93 void SendClientReportPhishingRequest( |
86 const GURL& phishing_url, | 94 const GURL& phishing_url, |
87 double score, | 95 double score, |
88 ClientReportPhishingRequestCallback* callback); | 96 ClientReportPhishingRequestCallback* callback); |
89 | 97 |
90 private: | 98 private: |
91 friend class ClientSideDetectionServiceTest; | 99 friend class ClientSideDetectionServiceTest; |
| 100 friend class ClientSideDetectionServiceHooksTest; |
| 101 class ShouldClassifyUrlRequest; |
92 | 102 |
93 enum ModelStatus { | 103 enum ModelStatus { |
94 // It's unclear whether or not the model was already fetched. | 104 // It's unclear whether or not the model was already fetched. |
95 UNKNOWN_STATUS, | 105 UNKNOWN_STATUS, |
96 // Model is fetched and is stored on disk. | 106 // Model is fetched and is stored on disk. |
97 READY_STATUS, | 107 READY_STATUS, |
98 // Error occured during fetching or writing. | 108 // Error occured during fetching or writing. |
99 ERROR_STATUS, | 109 ERROR_STATUS, |
100 }; | 110 }; |
101 | 111 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 | 202 |
193 // The client-side detection service object (this) might go away before some | 203 // The client-side detection service object (this) might go away before some |
194 // of the callbacks are done (e.g., asynchronous file operations). The | 204 // of the callbacks are done (e.g., asynchronous file operations). The |
195 // callback factory will revoke all pending callbacks if this goes away to | 205 // callback factory will revoke all pending callbacks if this goes away to |
196 // avoid a crash. | 206 // avoid a crash. |
197 base::ScopedCallbackFactory<ClientSideDetectionService> callback_factory_; | 207 base::ScopedCallbackFactory<ClientSideDetectionService> callback_factory_; |
198 | 208 |
199 // The context we use to issue network requests. | 209 // The context we use to issue network requests. |
200 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 210 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
201 | 211 |
| 212 // Used to register for page load notifications. |
| 213 NotificationRegistrar registrar_; |
| 214 |
202 DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionService); | 215 DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionService); |
203 }; | 216 }; |
204 | 217 |
205 } // namepsace safe_browsing | 218 } // namepsace safe_browsing |
206 | 219 |
207 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 220 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
OLD | NEW |