Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / ios / chrome / browser / web_resource / ios_web_resource_service.cc
blob44458bb7db8c62ae78a0cc43d39cc4273236f8c9
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"
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"
13 namespace {
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) {
23 closure.Run();
26 } // namespace
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) {
47 IOSWebResourceService::~IOSWebResourceService() {
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));
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));
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));