| 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/memory/ptr_util.h" |
| 9 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "components/google/core/browser/google_util.h" | 17 #include "components/google/core/browser/google_util.h" |
| 17 #include "components/prefs/pref_service.h" | 18 #include "components/prefs/pref_service.h" |
| 18 #include "net/base/load_flags.h" | 19 #include "net/base/load_flags.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 // If resource requests are not allowed, we'll get a callback when they are. | 61 // If resource requests are not allowed, we'll get a callback when they are. |
| 61 if (resource_request_allowed_notifier_.ResourceRequestsAllowed()) | 62 if (resource_request_allowed_notifier_.ResourceRequestsAllowed()) |
| 62 OnResourceRequestsAllowed(); | 63 OnResourceRequestsAllowed(); |
| 63 } | 64 } |
| 64 | 65 |
| 65 WebResourceService::~WebResourceService() { | 66 WebResourceService::~WebResourceService() { |
| 66 } | 67 } |
| 67 | 68 |
| 68 void WebResourceService::OnURLFetchComplete(const net::URLFetcher* source) { | 69 void WebResourceService::OnURLFetchComplete(const net::URLFetcher* source) { |
| 69 // Delete the URLFetcher when this function exits. | 70 // Delete the URLFetcher when this function exits. |
| 70 scoped_ptr<net::URLFetcher> clean_up_fetcher(url_fetcher_.release()); | 71 std::unique_ptr<net::URLFetcher> clean_up_fetcher(url_fetcher_.release()); |
| 71 | 72 |
| 72 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { | 73 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { |
| 73 std::string data; | 74 std::string data; |
| 74 source->GetResponseAsString(&data); | 75 source->GetResponseAsString(&data); |
| 75 // Calls EndFetch() on completion. | 76 // Calls EndFetch() on completion. |
| 76 // Full JSON parsing might spawn a utility process (for security). | 77 // Full JSON parsing might spawn a utility process (for security). |
| 77 // To limit the the number of simultaneously active processes | 78 // To limit the the number of simultaneously active processes |
| 78 // (on Android in particular) we short-cut the full parsing in the case of | 79 // (on Android in particular) we short-cut the full parsing in the case of |
| 79 // trivially "empty" JSONs. | 80 // trivially "empty" JSONs. |
| 80 if (data.empty() || data == "{}") { | 81 if (data.empty() || data == "{}") { |
| 81 OnUnpackFinished(make_scoped_ptr(new base::DictionaryValue())); | 82 OnUnpackFinished(base::WrapUnique(new base::DictionaryValue())); |
| 82 } else { | 83 } else { |
| 83 parse_json_callback_.Run(data, | 84 parse_json_callback_.Run(data, |
| 84 base::Bind(&WebResourceService::OnUnpackFinished, | 85 base::Bind(&WebResourceService::OnUnpackFinished, |
| 85 weak_ptr_factory_.GetWeakPtr()), | 86 weak_ptr_factory_.GetWeakPtr()), |
| 86 base::Bind(&WebResourceService::OnUnpackError, | 87 base::Bind(&WebResourceService::OnUnpackError, |
| 87 weak_ptr_factory_.GetWeakPtr())); | 88 weak_ptr_factory_.GetWeakPtr())); |
| 88 } | 89 } |
| 89 } else { | 90 } else { |
| 90 // Don't parse data if attempt to download was unsuccessful. | 91 // Don't parse data if attempt to download was unsuccessful. |
| 91 // Stop loading new web resource data, and silently exit. | 92 // Stop loading new web resource data, and silently exit. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 net::LOAD_DO_NOT_SEND_COOKIES | | 135 net::LOAD_DO_NOT_SEND_COOKIES | |
| 135 net::LOAD_DO_NOT_SAVE_COOKIES); | 136 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 136 url_fetcher_->SetRequestContext(request_context_.get()); | 137 url_fetcher_->SetRequestContext(request_context_.get()); |
| 137 url_fetcher_->Start(); | 138 url_fetcher_->Start(); |
| 138 } | 139 } |
| 139 | 140 |
| 140 void WebResourceService::EndFetch() { | 141 void WebResourceService::EndFetch() { |
| 141 in_fetch_ = false; | 142 in_fetch_ = false; |
| 142 } | 143 } |
| 143 | 144 |
| 144 void WebResourceService::OnUnpackFinished(scoped_ptr<base::Value> value) { | 145 void WebResourceService::OnUnpackFinished(std::unique_ptr<base::Value> value) { |
| 145 if (!value) { | 146 if (!value) { |
| 146 // Page information not properly read, or corrupted. | 147 // Page information not properly read, or corrupted. |
| 147 OnUnpackError(kInvalidDataTypeError); | 148 OnUnpackError(kInvalidDataTypeError); |
| 148 return; | 149 return; |
| 149 } | 150 } |
| 150 const base::DictionaryValue* dict = nullptr; | 151 const base::DictionaryValue* dict = nullptr; |
| 151 if (!value->GetAsDictionary(&dict)) { | 152 if (!value->GetAsDictionary(&dict)) { |
| 152 OnUnpackError(kUnexpectedJSONFormatError); | 153 OnUnpackError(kUnexpectedJSONFormatError); |
| 153 return; | 154 return; |
| 154 } | 155 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 180 // Wait at least |start_fetch_delay_ms_|. | 181 // Wait at least |start_fetch_delay_ms_|. |
| 181 if (ms_until_update > start_fetch_delay_ms_) | 182 if (ms_until_update > start_fetch_delay_ms_) |
| 182 delay = ms_until_update; | 183 delay = ms_until_update; |
| 183 } | 184 } |
| 184 } | 185 } |
| 185 // Start fetch and wait for UpdateResourceCache. | 186 // Start fetch and wait for UpdateResourceCache. |
| 186 ScheduleFetch(delay); | 187 ScheduleFetch(delay); |
| 187 } | 188 } |
| 188 | 189 |
| 189 } // namespace web_resource | 190 } // namespace web_resource |
| OLD | NEW |