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