diff options
author | droger <droger@chromium.org> | 2015-09-08 09:14:00 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-09-08 16:14:44 +0000 |
commit | c3a6ec63ab20a10f2fec9baeb2251f0eafe19419 (patch) | |
tree | cfd2449906a01642063e97838af96b165dedcca8 /ios | |
parent | ee8fa02ac7d1456bb6c69b3a5130007a4046c5f2 (diff) | |
download | chromium_src-c3a6ec63ab20a10f2fec9baeb2251f0eafe19419.zip chromium_src-c3a6ec63ab20a10f2fec9baeb2251f0eafe19419.tar.gz chromium_src-c3a6ec63ab20a10f2fec9baeb2251f0eafe19419.tar.bz2 |
[iOS] Re-enable JSON parsing in PromoResourceService
Following the WebResource componentization, the parsing of JSON resource
has to be updated on iOS.
The JSON parsing code was removed:
https://codereview.chromium.org/1291543004/diff/120001/ios/chrome/browser/web_resource/ios_web_resource_service.cc
This CL re-adds the code, adapted to the new architecture.
BUG=526299
Review URL: https://codereview.chromium.org/1310173009
Cr-Commit-Position: refs/heads/master@{#347710}
Diffstat (limited to 'ios')
-rw-r--r-- | ios/chrome/browser/web_resource/OWNERS | 3 | ||||
-rw-r--r-- | ios/chrome/browser/web_resource/web_resource_util.cc | 79 | ||||
-rw-r--r-- | ios/chrome/browser/web_resource/web_resource_util.h | 18 | ||||
-rw-r--r-- | ios/chrome/browser/web_resource/web_resource_util_unittest.cc | 126 | ||||
-rw-r--r-- | ios/chrome/ios_chrome.gyp | 2 | ||||
-rw-r--r-- | ios/chrome/ios_chrome_tests.gyp | 1 |
6 files changed, 229 insertions, 0 deletions
diff --git a/ios/chrome/browser/web_resource/OWNERS b/ios/chrome/browser/web_resource/OWNERS new file mode 100644 index 0000000..58c6484 --- /dev/null +++ b/ios/chrome/browser/web_resource/OWNERS @@ -0,0 +1,3 @@ +achuith@chromium.org +dbeam@chromium.org +rsesek@chromium.org diff --git a/ios/chrome/browser/web_resource/web_resource_util.cc b/ios/chrome/browser/web_resource/web_resource_util.cc new file mode 100644 index 0000000..e11dedb --- /dev/null +++ b/ios/chrome/browser/web_resource/web_resource_util.cc @@ -0,0 +1,79 @@ +// 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/web_resource_util.h" + +#include "base/bind.h" +#include "base/json/json_reader.h" +#include "base/location.h" +#include "base/task_runner.h" +#include "base/task_runner_util.h" +#include "base/values.h" +#include "ios/web/public/web_thread.h" + +namespace web_resource { + +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."; + +// Runs |error_callback| with |error| on |task_runner|. +void PostErrorTask(base::TaskRunner* task_runner, + const WebResourceService::ErrorCallback& error_callback, + const char error[]) { + task_runner->PostTask(FROM_HERE, + base::Bind(error_callback, std::string(error))); +} + +// Parses |data| as a JSON string and calls back on |task_runner|. +// Must not be called on the UI thread, for performance reasons. +void ParseJSONOnBackgroundThread( + base::TaskRunner* task_runner, + const std::string& data, + const WebResourceService::SuccessCallback& success_callback, + const WebResourceService::ErrorCallback& error_callback) { + if (data.empty()) { + PostErrorTask(task_runner, error_callback, kInvalidDataTypeError); + return; + } + + scoped_ptr<base::Value> value(base::JSONReader::Read(data)); + if (!value.get()) { + // Page information not properly read, or corrupted. + PostErrorTask(task_runner, error_callback, kInvalidDataTypeError); + return; + } + + if (!value->IsType(base::Value::TYPE_DICTIONARY)) { + PostErrorTask(task_runner, error_callback, kUnexpectedJSONFormatError); + return; + } + + task_runner->PostTask(FROM_HERE, + base::Bind(success_callback, base::Passed(&value))); +} + +// Starts the parsing of |data| as a JSON string asynchronously on a background +// thread. Can be called from the UI thread. +void StartParseJSONAsync( + const std::string& data, + const WebResourceService::SuccessCallback& success_callback, + const WebResourceService::ErrorCallback& error_callback) { + web::WebThread::PostBlockingPoolTask( + FROM_HERE, base::Bind(&ParseJSONOnBackgroundThread, + base::ThreadTaskRunnerHandle::Get(), data, + success_callback, error_callback)); +} + +} // namespace + +WebResourceService::ParseJSONCallback GetIOSChromeParseJSONCallback() { + return base::Bind(&StartParseJSONAsync); +} + +} // namespace web_resource diff --git a/ios/chrome/browser/web_resource/web_resource_util.h b/ios/chrome/browser/web_resource/web_resource_util.h new file mode 100644 index 0000000..6b14203 --- /dev/null +++ b/ios/chrome/browser/web_resource/web_resource_util.h @@ -0,0 +1,18 @@ +// 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_WEB_RESOURCE_UTIL_H_ +#define IOS_CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_UTIL_H_ + +#include "components/web_resource/web_resource_service.h" + +namespace web_resource { + +// Returns a ParseJSONCallback that parses JSON on a background thread. +// Generates an error if the data is not a dictionary. +WebResourceService::ParseJSONCallback GetIOSChromeParseJSONCallback(); + +} // namespace web_resource + +#endif // IOS_CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_UTIL_H_ diff --git a/ios/chrome/browser/web_resource/web_resource_util_unittest.cc b/ios/chrome/browser/web_resource/web_resource_util_unittest.cc new file mode 100644 index 0000000..92a1585 --- /dev/null +++ b/ios/chrome/browser/web_resource/web_resource_util_unittest.cc @@ -0,0 +1,126 @@ +// 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/web_resource_util.h" + +#include <string> + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/message_loop/message_loop.h" +#include "base/strings/stringprintf.h" +#include "base/values.h" +#include "ios/web/public/web_thread.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace web_resource { + +class WebResourceUtilTest : public testing::Test { + protected: + WebResourceUtilTest() : error_called_(false), success_called_(false) {} + ~WebResourceUtilTest() override {} + + WebResourceService::SuccessCallback GetSuccessCallback() { + return base::Bind(&WebResourceUtilTest::OnParseSuccess, + base::Unretained(this)); + } + + WebResourceService::ErrorCallback GetErrorCallback() { + return base::Bind(&WebResourceUtilTest::OnParseError, + base::Unretained(this)); + } + + // Called on success. + void OnParseSuccess(scoped_ptr<base::Value> value) { + success_called_ = true; + value_ = value.Pass(); + } + + // Called on error. + void OnParseError(const std::string& error) { + error_called_ = true; + error_ = error; + } + + void FlushBackgroundTasks() { + // The function is not synchronous, callbacks are not called before flushing + // the tasks. + EXPECT_FALSE(success_called_); + EXPECT_FALSE(error_called_); + + web::WebThread::GetBlockingPool()->FlushForTesting(); + loop_.RunUntilIdle(); + } + + base::MessageLoop loop_; + std::string error_; + scoped_ptr<base::Value> value_; + bool error_called_; + bool success_called_; +}; + +TEST_F(WebResourceUtilTest, Success) { + const std::string kExpectedKey("foo"); + const std::string kExpectedValue("bar"); + std::string json = base::StringPrintf("{\"%s\":\"%s\"}", kExpectedKey.c_str(), + kExpectedValue.c_str()); + GetIOSChromeParseJSONCallback().Run(json, GetSuccessCallback(), + GetErrorCallback()); + + FlushBackgroundTasks(); + + // The success callback is called with the reference value. + EXPECT_FALSE(error_called_); + EXPECT_TRUE(success_called_); + + base::DictionaryValue* dictionary = nullptr; + ASSERT_TRUE(value_->GetAsDictionary(&dictionary)); + EXPECT_EQ(1u, dictionary->size()); + base::Value* actual_value = nullptr; + ASSERT_TRUE(dictionary->Get(kExpectedKey, &actual_value)); + std::string actual_value_as_string; + EXPECT_TRUE(actual_value->GetAsString(&actual_value_as_string)); + EXPECT_EQ(kExpectedValue, actual_value_as_string); +} + +// Only DictionartValues are expected. +TEST_F(WebResourceUtilTest, UnexpectedValue) { + GetIOSChromeParseJSONCallback().Run("foo", GetSuccessCallback(), + GetErrorCallback()); + + FlushBackgroundTasks(); + + // The error callback is called. + EXPECT_TRUE(error_called_); + EXPECT_FALSE(success_called_); + EXPECT_FALSE(error_.empty()); +} + +// Empty data is not expected. +TEST_F(WebResourceUtilTest, EmptyValue) { + GetIOSChromeParseJSONCallback().Run(std::string(), GetSuccessCallback(), + GetErrorCallback()); + + FlushBackgroundTasks(); + + // The error callback is called. + EXPECT_TRUE(error_called_); + EXPECT_FALSE(success_called_); + EXPECT_FALSE(error_.empty()); +} + +// Wrong syntax. +TEST_F(WebResourceUtilTest, SyntaxError) { + GetIOSChromeParseJSONCallback().Run("%$[", GetSuccessCallback(), + GetErrorCallback()); + + FlushBackgroundTasks(); + + // The error callback is called. + EXPECT_TRUE(error_called_); + EXPECT_FALSE(success_called_); + EXPECT_FALSE(error_.empty()); +} + +} // namespace web_resource diff --git a/ios/chrome/ios_chrome.gyp b/ios/chrome/ios_chrome.gyp index d20c369..8ce37d2 100644 --- a/ios/chrome/ios_chrome.gyp +++ b/ios/chrome/ios_chrome.gyp @@ -453,6 +453,8 @@ 'browser/web/web_view_type_util.mm', 'browser/web_data_service_factory.cc', 'browser/web_data_service_factory.h', + 'browser/web_resource/web_resource_util.cc', + 'browser/web_resource/web_resource_util.h', 'browser/xcallback_parameters.h', 'browser/xcallback_parameters.mm', ], diff --git a/ios/chrome/ios_chrome_tests.gyp b/ios/chrome/ios_chrome_tests.gyp index 2a148eb..2847f31 100644 --- a/ios/chrome/ios_chrome_tests.gyp +++ b/ios/chrome/ios_chrome_tests.gyp @@ -57,6 +57,7 @@ 'browser/ui/native_content_controller_unittest.mm', 'browser/ui/ui_util_unittest.mm', 'browser/ui/uikit_ui_util_unittest.mm', + 'browser/web_resource/web_resource_util_unittest.cc', 'common/string_util_unittest.mm', ], 'actions': [ |