diff options
author | nirnimesh@chromium.org <nirnimesh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-16 07:35:57 +0000 |
---|---|---|
committer | nirnimesh@chromium.org <nirnimesh@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-16 07:35:57 +0000 |
commit | 3b9a9abc81f3223207ef9fc1d9c9104c0220d873 (patch) | |
tree | 142e4a7919c21f55d0f5fcb8cef122a7b43894f4 /chrome/browser/automation/automation_provider_json.cc | |
parent | 4d666e784754ed2f3f72bdafd875101dee021429 (diff) | |
download | chromium_src-3b9a9abc81f3223207ef9fc1d9c9104c0220d873.zip chromium_src-3b9a9abc81f3223207ef9fc1d9c9104c0220d873.tar.gz chromium_src-3b9a9abc81f3223207ef9fc1d9c9104c0220d873.tar.bz2 |
Refactor json automation interface for pyauto hooks.
Reduces the number of lines you need to add per new automation hook. Shaves
off several lines of code.
Refactor pyauto.py to obviate raising exception in case the json interfaces produces an error string.
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=52054
Review URL: http://codereview.chromium.org/2898001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52628 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/automation/automation_provider_json.cc')
-rw-r--r-- | chrome/browser/automation/automation_provider_json.cc | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/chrome/browser/automation/automation_provider_json.cc b/chrome/browser/automation/automation_provider_json.cc new file mode 100644 index 0000000..05f3a1b --- /dev/null +++ b/chrome/browser/automation/automation_provider_json.cc @@ -0,0 +1,55 @@ +// Copyright (c) 2010 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/automation/automation_provider_json.h" + +#include "base/json/json_writer.h" +#include "base/json/string_escape.h" +#include "chrome/test/automation/automation_messages.h" + +namespace { + +// Util for creating a JSON error return string (dict with key +// 'error' and error string value). No need to quote input. +std::string JSONErrorString(const std::string& err) { + std::string prefix = "{\"error\": \""; + std::string no_quote_err; + std::string suffix = "\"}"; + + base::JsonDoubleQuote(err, false, &no_quote_err); + return prefix + no_quote_err + suffix; +} + +} // namespace + +AutomationJSONReply::AutomationJSONReply(AutomationProvider* provider, + IPC::Message* reply_message) + : provider_(provider), + message_(reply_message) { +} + +AutomationJSONReply::~AutomationJSONReply() { + DCHECK(!message_) << "JSON automation request not replied!"; +} + +void AutomationJSONReply::SendSuccess(const Value* value) { + DCHECK(message_) << "Resending reply for JSON automation request"; + std::string json_string = "{}"; + if (value) + base::JSONWriter::Write(value, false, &json_string); + AutomationMsg_SendJSONRequest::WriteReplyParams( + message_, json_string, true); + provider_->Send(message_); + message_ = NULL; +} + +void AutomationJSONReply::SendError(const std::string& error_message) { + DCHECK(message_) << "Resending reply for JSON automation request"; + std::string json_string = JSONErrorString(error_message); + AutomationMsg_SendJSONRequest::WriteReplyParams( + message_, json_string, false); + provider_->Send(message_); + message_ = NULL; +} + |