Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(832)

Side by Side Diff: chrome/browser/safe_browsing/client_side_model_loader.h

Issue 2831183004: Bind ModelLoader to a sequence, not a thread. (Closed)
Patch Set: Add missed sequence check Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // Construct 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 // Cancel any pending model fetch. This must be called from the same sequence
64 // 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 14 matching lines...) Expand all
88 // available for download. 89 // available for download.
89 virtual void StartFetch(); 90 virtual void StartFetch();
90 91
91 // This method is called when we're done fetching the model either because 92 // This method is called when we're done fetching the model either because
92 // we hit an error somewhere or because we're actually done fetch and 93 // we hit an error somewhere or because we're actually done fetch and
93 // validating the model. If |max_age| is not 0, it's used to schedule the 94 // validating the model. If |max_age| is not 0, it's used to schedule the
94 // next fetch. 95 // next fetch.
95 virtual void EndFetch(ClientModelStatus status, base::TimeDelta max_age); 96 virtual void EndFetch(ClientModelStatus status, base::TimeDelta max_age);
96 97
97 private: 98 private:
99 // Common constructor called from both the public and test-only constructors.
100 ModelLoader(base::Closure update_renderers,
101 net::URLRequestContextGetter* request_context_getter,
102 const std::string& model_name);
103
98 // Use Finch to pick a model number. 104 // Use Finch to pick a model number.
99 static int GetModelNumber(); 105 static int GetModelNumber();
100 106
101 // Construct a model name from parameters. 107 // Construct a model name from parameters.
102 static std::string FillInModelName(bool is_extended_reporting, 108 static std::string FillInModelName(bool is_extended_reporting,
103 int model_number); 109 int model_number);
104 110
105 // Returns true iff all the hash id's in the client-side model point to 111 // Returns true iff all the hash id's in the client-side model point to
106 // valid hashes in the model. 112 // valid hashes in the model.
107 static bool ModelHasValidHashIds(const ClientSideModel& model); 113 static bool ModelHasValidHashIds(const ClientSideModel& model);
(...skipping 10 matching lines...) Expand all
118 124
119 // Callback to invoke when we've got a new model. CSD will send it around. 125 // Callback to invoke when we've got a new model. CSD will send it around.
120 base::Closure update_renderers_callback_; 126 base::Closure update_renderers_callback_;
121 127
122 // Not owned, must outlive this obj or be NULL. 128 // Not owned, must outlive this obj or be NULL.
123 net::URLRequestContextGetter* request_context_getter_; 129 net::URLRequestContextGetter* request_context_getter_;
124 130
125 // Used to protect the delayed callback to StartFetchModel() 131 // Used to protect the delayed callback to StartFetchModel()
126 base::WeakPtrFactory<ModelLoader> weak_factory_; 132 base::WeakPtrFactory<ModelLoader> weak_factory_;
127 133
134 // Used to check that ScheduleFetch and CancelFetcher are called on the same
135 // sequence.
136 base::SequenceChecker fetch_sequence_checker_;
137
128 friend class ClientSideDetectionServiceTest; 138 friend class ClientSideDetectionServiceTest;
129 friend class ModelLoaderTest; 139 friend class ModelLoaderTest;
130 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, FetchModelTest); 140 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, FetchModelTest);
131 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, ModelHasValidHashIds); 141 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, ModelHasValidHashIds);
132 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, ModelNamesTest); 142 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, ModelNamesTest);
133 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, RescheduleFetchTest); 143 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, RescheduleFetchTest);
134 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, UpdateRenderersTest); 144 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, UpdateRenderersTest);
135 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, 145 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest,
136 SetEnabledAndRefreshState); 146 SetEnabledAndRefreshState);
137 DISALLOW_COPY_AND_ASSIGN(ModelLoader); 147 DISALLOW_COPY_AND_ASSIGN(ModelLoader);
138 }; 148 };
139 149
140 } // namespace safe_browsing 150 } // namespace safe_browsing
141 151
142 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_ 152 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698