| 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 GetApplicationContext()->GetSystemURLRequestContext(), | |
| 44 nullptr) { | |
| 45 } | |
| 46 | |
| 47 IOSWebResourceService::~IOSWebResourceService() { | |
| 48 } | |
| 49 | |
| 50 // static | |
| 51 base::Closure IOSWebResourceService::ParseJSONOnBackgroundThread( | |
| 52 const std::string& data, | |
| 53 const SuccessCallback& success_callback, | |
| 54 const ErrorCallback& error_callback) { | |
| 55 if (data.empty()) | |
| 56 return base::Bind(error_callback, std::string(kInvalidDataTypeError)); | |
| 57 | |
| 58 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); | |
| 59 if (!value.get()) { | |
| 60 // Page information not properly read, or corrupted. | |
| 61 return base::Bind(error_callback, std::string(kInvalidDataTypeError)); | |
| 62 } | |
| 63 | |
| 64 if (!value->IsType(base::Value::TYPE_DICTIONARY)) | |
| 65 return base::Bind(error_callback, std::string(kUnexpectedJSONFormatError)); | |
| 66 | |
| 67 return base::Bind(success_callback, base::Passed(&value)); | |
| 68 } | |
| 69 | |
| 70 void IOSWebResourceService::ParseJSON(const std::string& data, | |
| 71 const SuccessCallback& success_callback, | |
| 72 const ErrorCallback& error_callback) { | |
| 73 base::PostTaskAndReplyWithResult( | |
| 74 web::WebThread::GetBlockingPool(), FROM_HERE, | |
| 75 base::Bind(&IOSWebResourceService::ParseJSONOnBackgroundThread, data, | |
| 76 success_callback, error_callback), | |
| 77 base::Bind(&RunClosure)); | |
| 78 } | |
| OLD | NEW |