OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/web_resource/web_resource_service.h" | 5 #include "components/web_resource/web_resource_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 namespace web_resource { | 32 namespace web_resource { |
33 | 33 |
34 WebResourceService::WebResourceService( | 34 WebResourceService::WebResourceService( |
35 PrefService* prefs, | 35 PrefService* prefs, |
36 const GURL& web_resource_server, | 36 const GURL& web_resource_server, |
37 const std::string& application_locale, | 37 const std::string& application_locale, |
38 const char* last_update_time_pref_name, | 38 const char* last_update_time_pref_name, |
39 int start_fetch_delay_ms, | 39 int start_fetch_delay_ms, |
40 int cache_update_delay_ms, | 40 int cache_update_delay_ms, |
41 net::URLRequestContextGetter* request_context, | 41 net::URLRequestContextGetter* request_context, |
42 const char* disable_network_switch) | 42 const char* disable_network_switch, |
| 43 const ParseJSONCallback& parse_json_callback) |
43 : prefs_(prefs), | 44 : prefs_(prefs), |
44 resource_request_allowed_notifier_(prefs, disable_network_switch), | 45 resource_request_allowed_notifier_(prefs, disable_network_switch), |
45 in_fetch_(false), | 46 in_fetch_(false), |
46 web_resource_server_(web_resource_server), | 47 web_resource_server_(web_resource_server), |
47 application_locale_(application_locale), | 48 application_locale_(application_locale), |
48 last_update_time_pref_name_(last_update_time_pref_name), | 49 last_update_time_pref_name_(last_update_time_pref_name), |
49 start_fetch_delay_ms_(start_fetch_delay_ms), | 50 start_fetch_delay_ms_(start_fetch_delay_ms), |
50 cache_update_delay_ms_(cache_update_delay_ms), | 51 cache_update_delay_ms_(cache_update_delay_ms), |
51 request_context_(request_context), | 52 request_context_(request_context), |
| 53 parse_json_callback_(parse_json_callback), |
52 weak_ptr_factory_(this) { | 54 weak_ptr_factory_(this) { |
53 resource_request_allowed_notifier_.Init(this); | 55 resource_request_allowed_notifier_.Init(this); |
54 DCHECK(prefs); | 56 DCHECK(prefs); |
55 } | 57 } |
56 | 58 |
57 void WebResourceService::StartAfterDelay() { | 59 void WebResourceService::StartAfterDelay() { |
58 // If resource requests are not allowed, we'll get a callback when they are. | 60 // If resource requests are not allowed, we'll get a callback when they are. |
59 if (resource_request_allowed_notifier_.ResourceRequestsAllowed()) | 61 if (resource_request_allowed_notifier_.ResourceRequestsAllowed()) |
60 OnResourceRequestsAllowed(); | 62 OnResourceRequestsAllowed(); |
61 } | 63 } |
62 | 64 |
63 WebResourceService::~WebResourceService() { | 65 WebResourceService::~WebResourceService() { |
64 } | 66 } |
65 | 67 |
66 void WebResourceService::OnURLFetchComplete(const net::URLFetcher* source) { | 68 void WebResourceService::OnURLFetchComplete(const net::URLFetcher* source) { |
67 // Delete the URLFetcher when this function exits. | 69 // Delete the URLFetcher when this function exits. |
68 scoped_ptr<net::URLFetcher> clean_up_fetcher(url_fetcher_.release()); | 70 scoped_ptr<net::URLFetcher> clean_up_fetcher(url_fetcher_.release()); |
69 | 71 |
70 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { | 72 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { |
71 std::string data; | 73 std::string data; |
72 source->GetResponseAsString(&data); | 74 source->GetResponseAsString(&data); |
73 // Calls EndFetch() on completion. | 75 // Calls EndFetch() on completion. |
74 // Full JSON parsing might spawn a utility process (for security). | 76 // Full JSON parsing might spawn a utility process (for security). |
75 // To limit the the number of simultaneously active processes | 77 // To limit the the number of simultaneously active processes |
76 // (on Android in particular) we short-cut the full parsing in the case of | 78 // (on Android in particular) we short-cut the full parsing in the case of |
77 // trivially "empty" JSONs. | 79 // trivially "empty" JSONs. |
78 if (data.empty() || data == "{}") { | 80 if (data.empty() || data == "{}") { |
79 OnUnpackFinished(make_scoped_ptr(new base::DictionaryValue()).Pass()); | 81 OnUnpackFinished(make_scoped_ptr(new base::DictionaryValue()).Pass()); |
80 } else { | 82 } else { |
81 ParseJSON(data, base::Bind(&WebResourceService::OnUnpackFinished, | 83 parse_json_callback_.Run(data, |
82 weak_ptr_factory_.GetWeakPtr()), | 84 base::Bind(&WebResourceService::OnUnpackFinished, |
83 base::Bind(&WebResourceService::OnUnpackError, | 85 weak_ptr_factory_.GetWeakPtr()), |
84 weak_ptr_factory_.GetWeakPtr())); | 86 base::Bind(&WebResourceService::OnUnpackError, |
| 87 weak_ptr_factory_.GetWeakPtr())); |
85 } | 88 } |
86 } else { | 89 } else { |
87 // Don't parse data if attempt to download was unsuccessful. | 90 // Don't parse data if attempt to download was unsuccessful. |
88 // Stop loading new web resource data, and silently exit. | 91 // Stop loading new web resource data, and silently exit. |
89 // We do not call ParseJSON(), so we need to call EndFetch() ourselves. | 92 // We do not call parse_json_callback_, so we need to call EndFetch() |
| 93 // ourselves. |
90 EndFetch(); | 94 EndFetch(); |
91 } | 95 } |
92 } | 96 } |
93 | 97 |
94 // Delay initial load of resource data into cache so as not to interfere | 98 // Delay initial load of resource data into cache so as not to interfere |
95 // with startup time. | 99 // with startup time. |
96 void WebResourceService::ScheduleFetch(int64 delay_ms) { | 100 void WebResourceService::ScheduleFetch(int64 delay_ms) { |
97 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 101 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
98 FROM_HERE, base::Bind(&WebResourceService::StartFetch, | 102 FROM_HERE, base::Bind(&WebResourceService::StartFetch, |
99 weak_ptr_factory_.GetWeakPtr()), | 103 weak_ptr_factory_.GetWeakPtr()), |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 // Wait at least |start_fetch_delay_ms_|. | 180 // Wait at least |start_fetch_delay_ms_|. |
177 if (ms_until_update > start_fetch_delay_ms_) | 181 if (ms_until_update > start_fetch_delay_ms_) |
178 delay = ms_until_update; | 182 delay = ms_until_update; |
179 } | 183 } |
180 } | 184 } |
181 // Start fetch and wait for UpdateResourceCache. | 185 // Start fetch and wait for UpdateResourceCache. |
182 ScheduleFetch(delay); | 186 ScheduleFetch(delay); |
183 } | 187 } |
184 | 188 |
185 } // namespace web_resource | 189 } // namespace web_resource |
OLD | NEW |