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.
5 #include "ios/chrome/browser/web_resource/ios_web_resource_service.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"
15 const char kInvalidDataTypeError
[] =
16 "Data from web resource server is missing or not valid JSON.";
18 const char kUnexpectedJSONFormatError
[] =
19 "Data from web resource server does not have expected format.";
21 // Helper method to run a closure.
22 void RunClosure(const base::Closure
& closure
) {
28 IOSWebResourceService::IOSWebResourceService(
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(
38 apply_locale_to_url
? GetApplicationContext()->GetApplicationLocale()
40 last_update_time_pref_name
,
42 cache_update_delay_ms
,
43 GetApplicationContext()->GetSystemURLRequestContext(),
47 IOSWebResourceService::~IOSWebResourceService() {
51 base::Closure
IOSWebResourceService::ParseJSONOnBackgroundThread(
52 const std::string
& data
,
53 const SuccessCallback
& success_callback
,
54 const ErrorCallback
& error_callback
) {
56 return base::Bind(error_callback
, std::string(kInvalidDataTypeError
));
58 scoped_ptr
<base::Value
> value(base::JSONReader::Read(data
));
60 // Page information not properly read, or corrupted.
61 return base::Bind(error_callback
, std::string(kInvalidDataTypeError
));
64 if (!value
->IsType(base::Value::TYPE_DICTIONARY
))
65 return base::Bind(error_callback
, std::string(kUnexpectedJSONFormatError
));
67 return base::Bind(success_callback
, base::Passed(&value
));
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
));