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