Remove aura enum from DesktopMediaID to fix desktop mirroring audio (CrOS).
[chromium-blink-merge.git] / chrome / browser / safe_browsing / client_side_model_loader.cc
blobc73019e9e5f7c0c9569ad763d5a53523aa51e3c5
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.
5 #include "chrome/browser/safe_browsing/client_side_model_loader.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/time/time.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/safe_browsing/client_model.pb.h"
17 #include "chrome/common/safe_browsing/csd.pb.h"
18 #include "chrome/common/safe_browsing/safebrowsing_messages.h"
19 #include "components/variations/variations_associated_data.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/http/http_status_code.h"
22 #include "net/url_request/url_fetcher.h"
23 #include "net/url_request/url_request_context_getter.h"
24 #include "net/url_request/url_request_status.h"
25 #include "url/gurl.h"
27 namespace safe_browsing {
29 // Model Loader strings
30 const size_t ModelLoader::kMaxModelSizeBytes = 150 * 1024;
31 const int ModelLoader::kClientModelFetchIntervalMs = 3600 * 1000;
32 const char ModelLoader::kClientModelUrlPrefix[] =
33 "https://ssl.gstatic.com/safebrowsing/csd/";
34 const char ModelLoader::kClientModelNamePattern[] =
35 "client_model_v5%s_variation_%d.pb";
36 const char ModelLoader::kClientModelFinchExperiment[] =
37 "ClientSideDetectionModel";
38 const char ModelLoader::kClientModelFinchParam[] =
39 "ModelNum";
42 // static
43 int ModelLoader::GetModelNumber() {
44 std::string num_str = variations::GetVariationParamValue(
45 kClientModelFinchExperiment, kClientModelFinchParam);
46 int model_number = 0;
47 if (!base::StringToInt(num_str, &model_number)) {
48 model_number = 0; // Default model
50 return model_number;
53 // static
54 std::string ModelLoader::FillInModelName(bool is_extended_reporting,
55 int model_number) {
56 return base::StringPrintf(kClientModelNamePattern,
57 is_extended_reporting ? "_ext" : "", model_number);
60 // static
61 bool ModelLoader::ModelHasValidHashIds(const ClientSideModel& model) {
62 const int max_index = model.hashes_size() - 1;
63 for (int i = 0; i < model.rule_size(); ++i) {
64 for (int j = 0; j < model.rule(i).feature_size(); ++j) {
65 if (model.rule(i).feature(j) < 0 ||
66 model.rule(i).feature(j) > max_index) {
67 return false;
71 for (int i = 0; i < model.page_term_size(); ++i) {
72 if (model.page_term(i) < 0 || model.page_term(i) > max_index) {
73 return false;
76 return true;
79 // Model name and URL are a function of is_extended_reporting and Finch.
80 ModelLoader::ModelLoader(base::Closure update_renderers_callback,
81 net::URLRequestContextGetter* request_context_getter,
82 bool is_extended_reporting)
83 : name_(FillInModelName(is_extended_reporting, GetModelNumber())),
84 url_(kClientModelUrlPrefix + name_),
85 update_renderers_callback_(update_renderers_callback),
86 request_context_getter_(request_context_getter),
87 weak_factory_(this) {
88 DCHECK(url_.is_valid());
91 // For testing only
92 ModelLoader::ModelLoader(base::Closure update_renderers_callback,
93 const std::string model_name)
94 : name_(model_name),
95 url_(kClientModelUrlPrefix + name_),
96 update_renderers_callback_(update_renderers_callback),
97 request_context_getter_(NULL),
98 weak_factory_(this) {
99 DCHECK(url_.is_valid());
102 ModelLoader::~ModelLoader() {
105 void ModelLoader::StartFetch() {
106 // Start fetching the model either from the cache or possibly from the
107 // network if the model isn't in the cache.
109 // TODO(nparker): If no profile needs this model, we shouldn't fetch it.
110 // Then only re-fetch when a profile setting changes to need it.
111 // This will save on the order of ~50KB/week/client of bandwidth.
112 fetcher_ = net::URLFetcher::Create(0 /* ID used for testing */, url_,
113 net::URLFetcher::GET, this);
114 fetcher_->SetRequestContext(request_context_getter_);
115 fetcher_->Start();
118 void ModelLoader::OnURLFetchComplete(const net::URLFetcher* source) {
119 DCHECK_EQ(fetcher_, source);
120 DCHECK_EQ(url_, source->GetURL());
122 std::string data;
123 source->GetResponseAsString(&data);
124 const bool is_success = source->GetStatus().is_success();
125 const int response_code = source->GetResponseCode();
127 // max_age is valid iff !0.
128 base::TimeDelta max_age;
129 if (is_success && net::HTTP_OK == response_code &&
130 source->GetResponseHeaders()) {
131 source->GetResponseHeaders()->GetMaxAgeValue(&max_age);
133 scoped_ptr<ClientSideModel> model(new ClientSideModel());
134 ClientModelStatus model_status;
135 if (!is_success || net::HTTP_OK != response_code) {
136 model_status = MODEL_FETCH_FAILED;
137 } else if (data.empty()) {
138 model_status = MODEL_EMPTY;
139 } else if (data.size() > kMaxModelSizeBytes) {
140 model_status = MODEL_TOO_LARGE;
141 } else if (!model->ParseFromString(data)) {
142 model_status = MODEL_PARSE_ERROR;
143 } else if (!model->IsInitialized() || !model->has_version()) {
144 model_status = MODEL_MISSING_FIELDS;
145 } else if (!ModelHasValidHashIds(*model)) {
146 model_status = MODEL_BAD_HASH_IDS;
147 } else if (model->version() < 0 ||
148 (model_.get() && model->version() < model_->version())) {
149 model_status = MODEL_INVALID_VERSION_NUMBER;
150 } else if (model_.get() && model->version() == model_->version()) {
151 model_status = MODEL_NOT_CHANGED;
152 } else {
153 // The model is valid => replace the existing model with the new one.
154 model_str_.assign(data);
155 model_.swap(model);
156 model_status = MODEL_SUCCESS;
158 EndFetch(model_status, max_age);
161 void ModelLoader::EndFetch(ClientModelStatus status, base::TimeDelta max_age) {
162 // We don't differentiate models in the UMA stats.
163 UMA_HISTOGRAM_ENUMERATION("SBClientPhishing.ClientModelStatus",
164 status,
165 MODEL_STATUS_MAX);
166 if (status == MODEL_SUCCESS) {
167 update_renderers_callback_.Run();
169 int delay_ms = kClientModelFetchIntervalMs;
170 // If the most recently fetched model had a valid max-age and the model was
171 // valid we're scheduling the next model update for after the max-age expired.
172 if (!max_age.is_zero() &&
173 (status == MODEL_SUCCESS || status == MODEL_NOT_CHANGED)) {
174 // We're adding 60s of additional delay to make sure we're past
175 // the model's age.
176 max_age += base::TimeDelta::FromMinutes(1);
177 delay_ms = max_age.InMilliseconds();
180 // Schedule the next model reload.
181 ScheduleFetch(delay_ms);
184 void ModelLoader::ScheduleFetch(int64 delay_ms) {
185 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
186 switches::kSbDisableAutoUpdate))
187 return;
188 base::MessageLoop::current()->PostDelayedTask(
189 FROM_HERE,
190 base::Bind(&ModelLoader::StartFetch, weak_factory_.GetWeakPtr()),
191 base::TimeDelta::FromMilliseconds(delay_ms));
194 void ModelLoader::CancelFetcher() {
195 // Invalidate any scheduled request.
196 weak_factory_.InvalidateWeakPtrs();
197 // Cancel any request in progress.
198 fetcher_.reset();
201 } // namespace safe_browsing