[Smart Lock] Measure clicks on the lock icon during Easy Unlock's trial run.
[chromium-blink-merge.git] / components / web_resource / web_resource_service.cc
blob40225b8c6bf755e9d3cc484e3e1924fc01048a3d
1 // Copyright 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 "components/web_resource/web_resource_service.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "components/google/core/browser/google_util.h"
16 #include "net/base/load_flags.h"
17 #include "net/url_request/url_fetcher.h"
18 #include "net/url_request/url_request_context_getter.h"
19 #include "net/url_request/url_request_status.h"
20 #include "url/gurl.h"
22 // No anonymous namespace, because const variables automatically get internal
23 // linkage.
24 const char kInvalidDataTypeError[] =
25 "Data from web resource server is missing or not valid JSON.";
27 const char kUnexpectedJSONFormatError[] =
28 "Data from web resource server does not have expected format.";
30 namespace web_resource {
32 WebResourceService::WebResourceService(
33 PrefService* prefs,
34 const GURL& web_resource_server,
35 const std::string& application_locale,
36 const char* last_update_time_pref_name,
37 int start_fetch_delay_ms,
38 int cache_update_delay_ms,
39 net::URLRequestContextGetter* request_context,
40 const char* disable_network_switch)
41 : prefs_(prefs),
42 resource_request_allowed_notifier_(prefs, disable_network_switch),
43 in_fetch_(false),
44 web_resource_server_(web_resource_server),
45 application_locale_(application_locale),
46 last_update_time_pref_name_(last_update_time_pref_name),
47 start_fetch_delay_ms_(start_fetch_delay_ms),
48 cache_update_delay_ms_(cache_update_delay_ms),
49 request_context_(request_context),
50 weak_ptr_factory_(this) {
51 resource_request_allowed_notifier_.Init(this);
52 DCHECK(prefs);
55 void WebResourceService::StartAfterDelay() {
56 // If resource requests are not allowed, we'll get a callback when they are.
57 if (resource_request_allowed_notifier_.ResourceRequestsAllowed())
58 OnResourceRequestsAllowed();
61 WebResourceService::~WebResourceService() {
64 void WebResourceService::OnURLFetchComplete(const net::URLFetcher* source) {
65 // Delete the URLFetcher when this function exits.
66 scoped_ptr<net::URLFetcher> clean_up_fetcher(url_fetcher_.release());
68 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) {
69 std::string data;
70 source->GetResponseAsString(&data);
71 // Calls EndFetch() on completion.
72 ParseJSON(data, base::Bind(&WebResourceService::OnUnpackFinished,
73 weak_ptr_factory_.GetWeakPtr()),
74 base::Bind(&WebResourceService::OnUnpackError,
75 weak_ptr_factory_.GetWeakPtr()));
76 } else {
77 // Don't parse data if attempt to download was unsuccessful.
78 // Stop loading new web resource data, and silently exit.
79 // We do not call ParseJSON(), so we need to call EndFetch() ourselves.
80 EndFetch();
84 // Delay initial load of resource data into cache so as not to interfere
85 // with startup time.
86 void WebResourceService::ScheduleFetch(int64 delay_ms) {
87 base::MessageLoop::current()->PostDelayedTask(
88 FROM_HERE, base::Bind(&WebResourceService::StartFetch,
89 weak_ptr_factory_.GetWeakPtr()),
90 base::TimeDelta::FromMilliseconds(delay_ms));
93 // Initializes the fetching of data from the resource server. Data
94 // load calls OnURLFetchComplete.
95 void WebResourceService::StartFetch() {
96 // First, put our next cache load on the MessageLoop.
97 ScheduleFetch(cache_update_delay_ms_);
99 // Set cache update time in preferences.
100 prefs_->SetString(last_update_time_pref_name_,
101 base::DoubleToString(base::Time::Now().ToDoubleT()));
103 // If we are still fetching data, exit.
104 if (in_fetch_)
105 return;
106 in_fetch_ = true;
108 GURL web_resource_server =
109 application_locale_.empty()
110 ? web_resource_server_
111 : google_util::AppendGoogleLocaleParam(web_resource_server_,
112 application_locale_);
114 DVLOG(1) << "WebResourceService StartFetch " << web_resource_server;
115 url_fetcher_.reset(
116 net::URLFetcher::Create(web_resource_server, net::URLFetcher::GET, this));
117 // Do not let url fetcher affect existing state in system context
118 // (by setting cookies, for example).
119 url_fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE |
120 net::LOAD_DO_NOT_SEND_COOKIES |
121 net::LOAD_DO_NOT_SAVE_COOKIES);
122 url_fetcher_->SetRequestContext(request_context_.get());
123 url_fetcher_->Start();
126 void WebResourceService::EndFetch() {
127 in_fetch_ = false;
130 void WebResourceService::OnUnpackFinished(scoped_ptr<base::Value> value) {
131 if (!value) {
132 // Page information not properly read, or corrupted.
133 OnUnpackError(kInvalidDataTypeError);
134 return;
136 const base::DictionaryValue* dict = nullptr;
137 if (!value->GetAsDictionary(&dict)) {
138 OnUnpackError(kUnexpectedJSONFormatError);
139 return;
141 Unpack(*dict);
143 EndFetch();
146 void WebResourceService::OnUnpackError(const std::string& error_message) {
147 LOG(ERROR) << error_message;
148 EndFetch();
151 void WebResourceService::OnResourceRequestsAllowed() {
152 int64 delay = start_fetch_delay_ms_;
153 // Check whether we have ever put a value in the web resource cache;
154 // if so, pull it out and see if it's time to update again.
155 if (prefs_->HasPrefPath(last_update_time_pref_name_)) {
156 std::string last_update_pref =
157 prefs_->GetString(last_update_time_pref_name_);
158 if (!last_update_pref.empty()) {
159 double last_update_value;
160 base::StringToDouble(last_update_pref, &last_update_value);
161 int64 ms_until_update =
162 cache_update_delay_ms_ -
163 static_cast<int64>(
164 (base::Time::Now() - base::Time::FromDoubleT(last_update_value))
165 .InMilliseconds());
166 // Wait at least |start_fetch_delay_ms_|.
167 if (ms_until_update > start_fetch_delay_ms_)
168 delay = ms_until_update;
171 // Start fetch and wait for UpdateResourceCache.
172 ScheduleFetch(delay);
175 } // namespace web_resource