diff options
author | xiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-22 01:53:06 +0000 |
---|---|---|
committer | xiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-22 01:53:06 +0000 |
commit | 1f6a9a6af5403491e995350f14745aecbe970a7b (patch) | |
tree | d60cba91ae4675886a8d33038a8c4543c7585eae /chrome/browser/safe_json_parser.cc | |
parent | 81fd6164c73349aca50ce74e5594576621588d84 (diff) | |
download | chromium_src-1f6a9a6af5403491e995350f14745aecbe970a7b.zip chromium_src-1f6a9a6af5403491e995350f14745aecbe970a7b.tar.gz chromium_src-1f6a9a6af5403491e995350f14745aecbe970a7b.tar.bz2 |
app_list: Add web store search.
- Split SafeJsonParser out of WebstoreDataFetcher to share the code;
- Add a WebstoreSearchFetcher to retrieve results from web store server;
- Add a WebstoreResultIconSource that fetches icon when painted on screen;
- Add a WebStoreProvider that generates WebstoreResult for results fetched
from server or a placeholder "Search in webstore" result;
BUG=173430
Review URL: https://chromiumcodereview.appspot.com/15342003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@201422 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/safe_json_parser.cc')
-rw-r--r-- | chrome/browser/safe_json_parser.cc | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/chrome/browser/safe_json_parser.cc b/chrome/browser/safe_json_parser.cc new file mode 100644 index 0000000..df0ce00 --- /dev/null +++ b/chrome/browser/safe_json_parser.cc @@ -0,0 +1,85 @@ +// Copyright 2013 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 "chrome/browser/safe_json_parser.h" + +#include "chrome/common/chrome_utility_messages.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/utility_process_host.h" + +using content::BrowserThread; +using content::UtilityProcessHost; + +SafeJsonParser::SafeJsonParser(const std::string& unsafe_json, + const SuccessCallback& success_callback, + const ErrorCallback& error_callback) + : unsafe_json_(unsafe_json), + success_callback_(success_callback), + error_callback_(error_callback) {} + +void SafeJsonParser::Start() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + BrowserThread::PostTask( + BrowserThread::IO, + FROM_HERE, + base::Bind(&SafeJsonParser::StartWorkOnIOThread, this)); +} + +SafeJsonParser::~SafeJsonParser() {} + +void SafeJsonParser::StartWorkOnIOThread() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + UtilityProcessHost* host = + UtilityProcessHost::Create( + this, base::MessageLoopProxy::current()); + host->EnableZygote(); + host->Send(new ChromeUtilityMsg_ParseJSON(unsafe_json_)); +} + +void SafeJsonParser::OnJSONParseSucceeded(const base::ListValue& wrapper) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + const Value* value = NULL; + CHECK(wrapper.Get(0, &value)); + + parsed_json_.reset(value->DeepCopy()); + ReportResults(); +} + +void SafeJsonParser::OnJSONParseFailed(const std::string& error_message) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + error_ = error_message; + ReportResults(); +} + +void SafeJsonParser::ReportResults() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + + BrowserThread::PostTask( + BrowserThread::UI, + FROM_HERE, + base::Bind(&SafeJsonParser::ReportResultOnUIThread, this)); +} + +void SafeJsonParser::ReportResultOnUIThread() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + if (error_.empty() && parsed_json_) { + if (!success_callback_.is_null()) + success_callback_.Run(parsed_json_.Pass()); + } else { + if (!error_callback_.is_null()) + error_callback_.Run(error_); + } +} + +bool SafeJsonParser::OnMessageReceived(const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(SafeJsonParser, message) + IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded, + OnJSONParseSucceeded) + IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed, + OnJSONParseFailed) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} |