forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In-process JSON parsing for web resources on iOS
BUG=447575 Review URL: https://codereview.chromium.org/880433002 Cr-Commit-Position: refs/heads/master@{#313063}
- Loading branch information
droger
authored and
Commit bot
committed
Jan 26, 2015
1 parent
c25902f
commit 37deaaa
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
ios/chrome/browser/web_resource/ios_web_resource_service.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2015 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "ios/chrome/browser/web_resource/ios_web_resource_service.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/json/json_reader.h" | ||
#include "base/values.h" | ||
#include "ios/chrome/browser/application_context.h" | ||
#include "ios/web/public/web_thread.h" | ||
|
||
namespace { | ||
|
||
const char kInvalidDataTypeError[] = | ||
"Data from web resource server is missing or not valid JSON."; | ||
|
||
const char kUnexpectedJSONFormatError[] = | ||
"Data from web resource server does not have expected format."; | ||
|
||
// Helper method to run a closure. | ||
void RunClosure(const base::Closure& closure) { | ||
closure.Run(); | ||
} | ||
|
||
} // namespace | ||
|
||
IOSWebResourceService::IOSWebResourceService( | ||
PrefService* prefs, | ||
const GURL& web_resource_server, | ||
bool apply_locale_to_url, | ||
const char* last_update_time_pref_name, | ||
int start_fetch_delay_ms, | ||
int cache_update_delay_ms) | ||
: web_resource::WebResourceService( | ||
prefs, | ||
web_resource_server, | ||
apply_locale_to_url ? GetApplicationContext()->GetApplicationLocale() | ||
: std::string(), | ||
last_update_time_pref_name, | ||
start_fetch_delay_ms, | ||
cache_update_delay_ms, | ||
nullptr) { | ||
} | ||
|
||
IOSWebResourceService::~IOSWebResourceService() { | ||
} | ||
|
||
// static | ||
base::Closure IOSWebResourceService::ParseJSONOnBackgroundThread( | ||
const std::string& data, | ||
const SuccessCallback& success_callback, | ||
const ErrorCallback& error_callback) { | ||
if (data.empty()) | ||
return base::Bind(error_callback, std::string(kInvalidDataTypeError)); | ||
|
||
scoped_ptr<base::Value> value(base::JSONReader::Read(data)); | ||
if (!value.get()) { | ||
// Page information not properly read, or corrupted. | ||
return base::Bind(error_callback, std::string(kInvalidDataTypeError)); | ||
} | ||
|
||
if (!value->IsType(base::Value::TYPE_DICTIONARY)) | ||
return base::Bind(error_callback, std::string(kUnexpectedJSONFormatError)); | ||
|
||
return base::Bind(success_callback, base::Passed(&value)); | ||
} | ||
|
||
void IOSWebResourceService::ParseJSON(const std::string& data, | ||
const SuccessCallback& success_callback, | ||
const ErrorCallback& error_callback) { | ||
base::PostTaskAndReplyWithResult( | ||
web::WebThread::GetBlockingPool(), FROM_HERE, | ||
base::Bind(&IOSWebResourceService::ParseJSONOnBackgroundThread, data, | ||
success_callback, error_callback), | ||
base::Bind(&RunClosure)); | ||
} | ||
|
||
net::URLRequestContextGetter* IOSWebResourceService::GetRequestContext() { | ||
return GetApplicationContext()->GetSystemURLRequestContext(); | ||
} |
45 changes: 45 additions & 0 deletions
45
ios/chrome/browser/web_resource/ios_web_resource_service.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2015 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef IOS_CHROME_BROWSER_WEB_RESOURCE_IOS_WEB_RESOURCE_SERVICE_H_ | ||
#define IOS_CHROME_BROWSER_WEB_RESOURCE_IOS_WEB_RESOURCE_SERVICE_H_ | ||
|
||
#include <string> | ||
|
||
#include "base/macros.h" | ||
#include "base/memory/scoped_ptr.h" | ||
#include "components/web_resource/web_resource_service.h" | ||
|
||
// iOS implementation of WebResourceService. | ||
// IOSWebResourceService does not parses JSON out of process, because of | ||
// platform limitations. | ||
class IOSWebResourceService : public web_resource::WebResourceService { | ||
public: | ||
IOSWebResourceService(PrefService* prefs, | ||
const GURL& web_resource_server, | ||
bool apply_locale_to_url, | ||
const char* last_update_time_pref_name, | ||
int start_fetch_delay_ms, | ||
int cache_update_delay_ms); | ||
|
||
protected: | ||
~IOSWebResourceService() override; | ||
|
||
private: | ||
// Parses JSON in a background thread. | ||
static base::Closure ParseJSONOnBackgroundThread( | ||
const std::string& data, | ||
const SuccessCallback& success_callback, | ||
const ErrorCallback& error_callback); | ||
|
||
// WebResourceService implementation: | ||
void ParseJSON(const std::string& data, | ||
const SuccessCallback& success_callback, | ||
const ErrorCallback& error_callback) override; | ||
net::URLRequestContextGetter* GetRequestContext() override; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(IOSWebResourceService); | ||
}; | ||
|
||
#endif // IOS_CHROME_BROWSER_WEB_RESOURCE_IOS_WEB_RESOURCE_SERVICE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters