Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / safe_browsing / client_side_model_loader.h
blob5a0c1b2439908de280e68e7c5d696e6ee1957b4c
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
3 // found in the LICENSE file.
4 //
5 // Helper class loads models for client-side phishing detection
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.
11 #ifndef CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_
12 #define CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_
14 #include <string>
16 #include "base/basictypes.h"
17 #include "base/callback.h"
18 #include "base/gtest_prod_util.h"
19 #include "base/memory/linked_ptr.h"
20 #include "base/memory/ref_counted.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/memory/weak_ptr.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "net/base/net_util.h"
25 #include "net/url_request/url_fetcher_delegate.h"
26 #include "url/gurl.h"
28 class SafeBrowsingService;
30 namespace base {
31 class TimeDelta;
34 namespace net {
35 class URLFetcher;
36 class URLRequestContextGetter;
37 } // namespace net
39 namespace safe_browsing {
40 class ClientSideModel;
42 // Class which owns and loads a single client-Side detection model.
43 // The ClientSideDetectionService uses this.
44 class ModelLoader : public net::URLFetcherDelegate {
45 public:
46 static const size_t kMaxModelSizeBytes;
47 static const int kClientModelFetchIntervalMs;
48 static const char kClientModelFinchExperiment[];
49 static const char kClientModelFinchParam[];
50 static const char kClientModelUrlPrefix[];
51 static const char kClientModelNamePattern[];
53 ModelLoader(base::Closure update_renderers,
54 net::URLRequestContextGetter* request_context_getter,
55 bool is_extended_reporting);
56 ~ModelLoader() override;
58 // From the net::URLFetcherDelegate interface.
59 void OnURLFetchComplete(const net::URLFetcher* source) override;
61 // Schedules the next fetch of the model.
62 virtual void ScheduleFetch(int64 delay_ms);
64 // Cancel any pending model fetch.
65 virtual void CancelFetcher();
67 const std::string& model_str() const { return model_str_; }
68 const std::string& name() const { return name_; }
70 protected:
71 // Enum used to keep stats about why we fail to get the client model.
72 enum ClientModelStatus {
73 MODEL_SUCCESS,
74 MODEL_NOT_CHANGED,
75 MODEL_FETCH_FAILED,
76 MODEL_EMPTY,
77 MODEL_TOO_LARGE,
78 MODEL_PARSE_ERROR,
79 MODEL_MISSING_FIELDS,
80 MODEL_INVALID_VERSION_NUMBER,
81 MODEL_BAD_HASH_IDS,
82 MODEL_STATUS_MAX // Always add new values before this one.
85 // For testing only.
86 ModelLoader(base::Closure update_renderers, const std::string model_name);
88 // This is called periodically to check whether a new client model is
89 // available for download.
90 virtual void StartFetch();
92 // This method is called when we're done fetching the model either because
93 // we hit an error somewhere or because we're actually done fetch and
94 // validating the model. If |max_age| is not 0, it's used to schedule the
95 // next fetch.
96 virtual void EndFetch(ClientModelStatus status, base::TimeDelta max_age);
98 private:
99 // Use Finch to pick a model number.
100 static int GetModelNumber();
102 // Construct a model name from parameters.
103 static std::string FillInModelName(bool is_extended_reporting,
104 int model_number);
106 // Returns true iff all the hash id's in the client-side model point to
107 // valid hashes in the model.
108 static bool ModelHasValidHashIds(const ClientSideModel& model);
110 // The name of the model is the last component of the URL path.
111 const std::string name_;
112 // Full URL of the model.
113 const GURL url_;
115 // If the model isn't yet loaded, model_str_ will be empty.
116 std::string model_str_;
117 scoped_ptr<ClientSideModel> model_;
118 scoped_ptr<net::URLFetcher> fetcher_;
120 // Callback to invoke when we've got a new model. CSD will send it around.
121 base::Closure update_renderers_callback_;
123 // Not owned, must outlive this obj or be NULL.
124 net::URLRequestContextGetter* request_context_getter_;
126 // Used to protect the delayed callback to StartFetchModel()
127 base::WeakPtrFactory<ModelLoader> weak_factory_;
129 friend class ClientSideDetectionServiceTest;
130 friend class ModelLoaderTest;
131 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, FetchModelTest);
132 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, ModelHasValidHashIds);
133 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, ModelNamesTest);
134 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, RescheduleFetchTest);
135 FRIEND_TEST_ALL_PREFIXES(ModelLoaderTest, UpdateRenderersTest);
136 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest,
137 SetEnabledAndRefreshState);
138 DISALLOW_COPY_AND_ASSIGN(ModelLoader);
141 } // namespace safe_browsing
143 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_MODEL_LOADER_H_