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 const 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 base::PostTaskAndReplyWithResult( |
| 73 web::WebThread::GetBlockingPool(), FROM_HERE, |
| 74 base::Bind(&IOSWebResourceService::ParseJSONOnBackgroundThread, data, |
| 75 success_callback, error_callback), |
| 76 base::Bind(&RunClosure)); |
| 77 } |
| 78 |
| 79 net::URLRequestContextGetter* IOSWebResourceService::GetRequestContext() { |
| 80 return GetApplicationContext()->GetSystemURLRequestContext(); |
| 81 } |
OLD | NEW |