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/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 "chrome/browser/browser_process.h"
16 #include "chrome/browser/google/google_util.h"
17 #include "net/base/load_flags.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_request_status.h"
22 WebResourceService::WebResourceService(
24 const GURL
& web_resource_server
,
25 bool apply_locale_to_url
,
26 const char* last_update_time_pref_name
,
27 int start_fetch_delay_ms
,
28 int cache_update_delay_ms
)
32 web_resource_server_(web_resource_server
),
33 apply_locale_to_url_(apply_locale_to_url
),
34 last_update_time_pref_name_(last_update_time_pref_name
),
35 start_fetch_delay_ms_(start_fetch_delay_ms
),
36 cache_update_delay_ms_(cache_update_delay_ms
),
37 weak_ptr_factory_(this) {
38 resource_request_allowed_notifier_
.Init(this);
42 WebResourceService::~WebResourceService() {
47 void WebResourceService::OnUnpackFinished(
48 const base::DictionaryValue
& parsed_json
) {
53 void WebResourceService::OnUnpackError(const std::string
& error_message
) {
54 LOG(ERROR
) << error_message
;
58 void WebResourceService::EndFetch() {
60 json_unpacker_
->ClearDelegate();
61 json_unpacker_
= NULL
;
66 void WebResourceService::StartAfterDelay() {
67 // If resource requests are not allowed, we'll get a callback when they are.
68 if (resource_request_allowed_notifier_
.ResourceRequestsAllowed())
69 OnResourceRequestsAllowed();
72 void WebResourceService::OnResourceRequestsAllowed() {
73 int64 delay
= start_fetch_delay_ms_
;
74 // Check whether we have ever put a value in the web resource cache;
75 // if so, pull it out and see if it's time to update again.
76 if (prefs_
->HasPrefPath(last_update_time_pref_name_
)) {
77 std::string last_update_pref
=
78 prefs_
->GetString(last_update_time_pref_name_
);
79 if (!last_update_pref
.empty()) {
80 double last_update_value
;
81 base::StringToDouble(last_update_pref
, &last_update_value
);
82 int64 ms_until_update
= cache_update_delay_ms_
-
83 static_cast<int64
>((base::Time::Now() - base::Time::FromDoubleT(
84 last_update_value
)).InMilliseconds());
85 // Wait at least |start_fetch_delay_ms_|.
86 if (ms_until_update
> start_fetch_delay_ms_
)
87 delay
= ms_until_update
;
90 // Start fetch and wait for UpdateResourceCache.
94 // Delay initial load of resource data into cache so as not to interfere
96 void WebResourceService::ScheduleFetch(int64 delay_ms
) {
97 base::MessageLoop::current()->PostDelayedTask(
99 base::Bind(&WebResourceService::StartFetch
,
100 weak_ptr_factory_
.GetWeakPtr()),
101 base::TimeDelta::FromMilliseconds(delay_ms
));
104 // Initializes the fetching of data from the resource server. Data
105 // load calls OnURLFetchComplete.
106 void WebResourceService::StartFetch() {
107 // First, put our next cache load on the MessageLoop.
108 ScheduleFetch(cache_update_delay_ms_
);
110 // Set cache update time in preferences.
111 prefs_
->SetString(last_update_time_pref_name_
,
112 base::DoubleToString(base::Time::Now().ToDoubleT()));
114 // If we are still fetching data, exit.
119 // Balanced in OnURLFetchComplete.
122 GURL web_resource_server
= apply_locale_to_url_
?
123 google_util::AppendGoogleLocaleParam(web_resource_server_
) :
124 web_resource_server_
;
126 DVLOG(1) << "WebResourceService StartFetch " << web_resource_server
;
127 url_fetcher_
.reset(net::URLFetcher::Create(
128 web_resource_server
, net::URLFetcher::GET
, this));
129 // Do not let url fetcher affect existing state in system context
130 // (by setting cookies, for example).
131 url_fetcher_
->SetLoadFlags(net::LOAD_DISABLE_CACHE
|
132 net::LOAD_DO_NOT_SEND_COOKIES
|
133 net::LOAD_DO_NOT_SAVE_COOKIES
);
134 net::URLRequestContextGetter
* url_request_context_getter
=
135 g_browser_process
->system_request_context();
136 url_fetcher_
->SetRequestContext(url_request_context_getter
);
137 url_fetcher_
->Start();
140 void WebResourceService::OnURLFetchComplete(const net::URLFetcher
* source
) {
141 // Delete the URLFetcher when this function exits.
142 scoped_ptr
<net::URLFetcher
> clean_up_fetcher(url_fetcher_
.release());
144 if (source
->GetStatus().is_success() && source
->GetResponseCode() == 200) {
146 source
->GetResponseAsString(&data
);
148 // UnpackerClient calls EndFetch and releases itself on completion.
149 json_unpacker_
= JSONAsynchronousUnpacker::Create(this);
150 json_unpacker_
->Start(data
);
152 // Don't parse data if attempt to download was unsuccessful.
153 // Stop loading new web resource data, and silently exit.
154 // We do not call UnpackerClient, so we need to call EndFetch ourselves.