| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 loads models for client-side phishing detection | 5 // Helper class loads models for client-side phishing detection |
| 6 // from the the SafeBrowsing backends. | 6 // from the the SafeBrowsing backends. |
| 7 // | |
| 8 // This class is not thread-safe and expects all calls to be made on the UI | |
| 9 // thread. | |
| 10 | 7 |
| 11 #ifndef CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_ | 8 #ifndef CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_ |
| 12 #define CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_ | 9 #define CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_ |
| 13 | 10 |
| 14 #include <stddef.h> | 11 #include <stddef.h> |
| 15 #include <stdint.h> | 12 #include <stdint.h> |
| 16 | 13 |
| 17 #include <memory> | 14 #include <memory> |
| 18 #include <string> | 15 #include <string> |
| 19 | 16 |
| 20 #include "base/callback.h" | 17 #include "base/callback.h" |
| 21 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
| 22 #include "base/macros.h" | 19 #include "base/macros.h" |
| 23 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 24 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 25 #include "content/public/browser/browser_thread.h" | 22 #include "base/sequence_checker.h" |
| 26 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
| 27 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 28 | 25 |
| 29 namespace base { | 26 namespace base { |
| 30 class TimeDelta; | 27 class TimeDelta; |
| 31 } | 28 } |
| 32 | 29 |
| 33 namespace net { | 30 namespace net { |
| 34 class URLFetcher; | 31 class URLFetcher; |
| 35 class URLRequestContextGetter; | 32 class URLRequestContextGetter; |
| 36 } // namespace net | 33 } // namespace net |
| 37 | 34 |
| 38 namespace safe_browsing { | 35 namespace safe_browsing { |
| 39 class ClientSideModel; | 36 class ClientSideModel; |
| 40 | 37 |
| 41 // Class which owns and loads a single client-Side detection model. | 38 // Class which owns and loads a single client-Side detection model. |
| 42 // The ClientSideDetectionService uses this. | 39 // The ClientSideDetectionService uses this. |
| 43 class ModelLoader : public net::URLFetcherDelegate { | 40 class ModelLoader : public net::URLFetcherDelegate { |
| 44 public: | 41 public: |
| 45 static const size_t kMaxModelSizeBytes; | 42 static const size_t kMaxModelSizeBytes; |
| 46 static const int kClientModelFetchIntervalMs; | 43 static const int kClientModelFetchIntervalMs; |
| 47 static const char kClientModelFinchExperiment[]; | 44 static const char kClientModelFinchExperiment[]; |
| 48 static const char kClientModelFinchParam[]; | 45 static const char kClientModelFinchParam[]; |
| 49 static const char kClientModelUrlPrefix[]; | 46 static const char kClientModelUrlPrefix[]; |
| 50 static const char kClientModelNamePattern[]; | 47 static const char kClientModelNamePattern[]; |
| 51 | 48 |
| 49 // Constructs a model loader to fetch a model using |request_context_getter|. |
| 50 // When ScheduleFetch is called, |update_renderers| will be called on the |
| 51 // same sequence if the fetch is successful. |
| 52 ModelLoader(base::Closure update_renderers, | 52 ModelLoader(base::Closure update_renderers, |
| 53 net::URLRequestContextGetter* request_context_getter, | 53 net::URLRequestContextGetter* request_context_getter, |
| 54 bool is_extended_reporting); | 54 bool is_extended_reporting); |
| 55 ~ModelLoader() override; | 55 ~ModelLoader() override; |
| 56 | 56 |
| 57 // From the net::URLFetcherDelegate interface. | 57 // From the net::URLFetcherDelegate interface. |
| 58 void OnURLFetchComplete(const net::URLFetcher* source) override; | 58 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 59 | 59 |
| 60 // Schedules the next fetch of the model. | 60 // Schedules the next fetch of the model. |
| 61 virtual void ScheduleFetch(int64_t delay_ms); | 61 virtual void ScheduleFetch(int64_t delay_ms); |
| 62 | 62 |
| 63 // Cancel any pending model fetch. | 63 // Cancels any pending model fetch. This must be called from the same |
| 64 // sequence as ScheduleFetch. |
| 64 virtual void CancelFetcher(); | 65 virtual void CancelFetcher(); |
| 65 | 66 |
| 66 const std::string& model_str() const { return model_str_; } | 67 const std::string& model_str() const { return model_str_; } |
| 67 const std::string& name() const { return name_; } | 68 const std::string& name() const { return name_; } |
| 68 | 69 |
| 69 protected: | 70 protected: |
| 70 // Enum used to keep stats about why we fail to get the client model. | 71 // Enum used to keep stats about why we fail to get the client model. |
| 71 enum ClientModelStatus { | 72 enum ClientModelStatus { |
| 72 MODEL_SUCCESS, | 73 MODEL_SUCCESS, |
| 73 MODEL_NOT_CHANGED, | 74 MODEL_NOT_CHANGED, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 std::string model_str_; | 116 std::string model_str_; |
| 116 std::unique_ptr<ClientSideModel> model_; | 117 std::unique_ptr<ClientSideModel> model_; |
| 117 std::unique_ptr<net::URLFetcher> fetcher_; | 118 std::unique_ptr<net::URLFetcher> fetcher_; |
| 118 | 119 |
| 119 // Callback to invoke when we've got a new model. CSD will send it around. | 120 // Callback to invoke when we've got a new model. CSD will send it around. |
| 120 base::Closure update_renderers_callback_; | 121 base::Closure update_renderers_callback_; |
| 121 | 122 |
| 122 // Not owned, must outlive this obj or be NULL. | 123 // Not owned, must outlive this obj or be NULL. |
| 123 net::URLRequestContextGetter* request_context_getter_; | 124 net::URLRequestContextGetter* request_context_getter_; |
| 124 | 125 |
| 126 // Used to check that ScheduleFetch and CancelFetcher are called on the same |
| 127 // sequence. |
| 128 base::SequenceChecker fetch_sequence_checker_; |
| 129 |
| 125 // Used to protect the delayed callback to StartFetchModel() | 130 // Used to protect the delayed callback to StartFetchModel() |
| 126 base::WeakPtrFactory<ModelLoader> weak_factory_; | 131 base::WeakPtrFactory<ModelLoader> weak_factory_; |
| 127 | 132 |
| 128 friend class ClientSideDetectionServiceTest; | 133 friend class ClientSideDetectionServiceTest; |
| 129 friend class ModelLoaderTest; | 134 friend class ModelLoaderTest; |
| 130 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, FetchModelTest); | 135 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, FetchModelTest); |
| 131 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, ModelHasValidHashIds); | 136 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, ModelHasValidHashIds); |
| 132 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, ModelNamesTest); | 137 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, ModelNamesTest); |
| 133 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, RescheduleFetchTest); | 138 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, RescheduleFetchTest); |
| 134 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, UpdateRenderersTest); | 139 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, UpdateRenderersTest); |
| 135 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, | 140 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, |
| 136 SetEnabledAndRefreshState); | 141 SetEnabledAndRefreshState); |
| 137 DISALLOW_COPY_AND_ASSIGN(ModelLoader); | 142 DISALLOW_COPY_AND_ASSIGN(ModelLoader); |
| 138 }; | 143 }; |
| 139 | 144 |
| 140 } // namespace safe_browsing | 145 } // namespace safe_browsing |
| 141 | 146 |
| 142 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_ | 147 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_ |
| OLD | NEW |