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"
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"
22 // No anonymous namespace, because const variables automatically get internal
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(
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
)
42 resource_request_allowed_notifier_(prefs
, disable_network_switch
),
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);
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) {
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()));
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.
84 // Delay initial load of resource data into cache so as not to interfere
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.
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
;
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() {
130 void WebResourceService::OnUnpackFinished(scoped_ptr
<base::Value
> value
) {
132 // Page information not properly read, or corrupted.
133 OnUnpackError(kInvalidDataTypeError
);
136 const base::DictionaryValue
* dict
= nullptr;
137 if (!value
->GetAsDictionary(&dict
)) {
138 OnUnpackError(kUnexpectedJSONFormatError
);
146 void WebResourceService::OnUnpackError(const std::string
& error_message
) {
147 LOG(ERROR
) << error_message
;
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_
-
164 (base::Time::Now() - base::Time::FromDoubleT(last_update_value
))
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