Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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. | |
| 4 | |
| 5 #include "ios/chrome/browser/web_resource/ios_web_resource_service.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/json/json_reader.h" | |
| 9 #include "base/values.h" | |
| 10 #include "ios/chrome/browser/application_context.h" | |
| 11 #include "ios/web/public/web_thread.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 const char kInvalidDataTypeError[] = | |
| 16 "Data from web resource server is missing or not valid JSON."; | |
| 17 | |
| 18 const char kUnexpectedJSONFormatError[] = | |
| 19 "Data from web resource server does not have expected format."; | |
| 20 | |
| 21 // Helper method to run a closure. | |
| 22 void RunClosure(const base::Closure& closure) { | |
| 23 closure.Run(); | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 IOSWebResourceService::IOSWebResourceService( | |
| 29 PrefService* prefs, | |
| 30 const GURL& web_resource_server, | |
| 31 bool apply_locale_to_url, | |
| 32 const char* last_update_time_pref_name, | |
| 33 int start_fetch_delay_ms, | |
| 34 int cache_update_delay_ms) | |
| 35 : web_resource::WebResourceService( | |
| 36 prefs, | |
| 37 web_resource_server, | |
| 38 apply_locale_to_url ? GetApplicationContext()->GetApplicationLocale() | |
| 39 : std::string(), | |
| 40 last_update_time_pref_name, | |
| 41 start_fetch_delay_ms, | |
| 42 cache_update_delay_ms, | |
| 43 nullptr) { | |
| 44 } | |
| 45 | |
| 46 IOSWebResourceService::~IOSWebResourceService() { | |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 base::Closure IOSWebResourceService::ParseJSONOnBackgroundThread( | |
| 51 scoped_ptr<std::string> data, | |
| 52 const SuccessCallback& success_callback, | |
| 53 const ErrorCallback& error_callback) { | |
| 54 if (data->empty()) | |
| 55 return base::Bind(error_callback, std::string(kInvalidDataTypeError)); | |
| 56 | |
| 57 scoped_ptr<base::Value> value(base::JSONReader::Read(*data)); | |
| 58 if (!value.get()) { | |
| 59 // Page information not properly read, or corrupted. | |
| 60 return base::Bind(error_callback, std::string(kInvalidDataTypeError)); | |
| 61 } | |
| 62 | |
| 63 if (!value->IsType(base::Value::TYPE_DICTIONARY)) | |
| 64 return base::Bind(error_callback, std::string(kUnexpectedJSONFormatError)); | |
| 65 | |
| 66 return base::Bind(success_callback, base::Passed(&value)); | |
| 67 } | |
| 68 | |
| 69 void IOSWebResourceService::ParseJSON(const std::string& data, | |
| 70 const SuccessCallback& success_callback, | |
| 71 const ErrorCallback& error_callback) { | |
| 72 scoped_ptr<std::string> data_copy(new std::string(data)); | |
| 73 base::PostTaskAndReplyWithResult( | |
| 74 web::WebThread::GetBlockingPool(), FROM_HERE, | |
| 75 base::Bind(&IOSWebResourceService::ParseJSONOnBackgroundThread, | |
| 76 base::Passed(&data_copy), success_callback, error_callback), | |
|
Bernhard Bauer
2015/01/26 12:05:43
Could you just pass the data directly?
droger
2015/01/26 12:14:23
I used base::Passed to avoid making a copy. I am n
Bernhard Bauer
2015/01/26 12:22:49
Yeah, pretty much. You do already make a copy righ
droger
2015/01/26 12:28:19
Done.
| |
| 77 base::Bind(&RunClosure)); | |
| 78 } | |
| 79 | |
| 80 net::URLRequestContextGetter* IOSWebResourceService::GetRequestContext() { | |
| 81 return GetApplicationContext()->GetSystemURLRequestContext(); | |
| 82 } | |
| OLD | NEW |