automation_provider_json.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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. 4 5#include "chrome/browser/automation/automation_provider_json.h" 6 7#include "base/json/json_writer.h" 8#include "base/json/string_escape.h" 9#include "chrome/test/automation/automation_messages.h" 10 11namespace { 12 13// Util for creating a JSON error return string (dict with key 14// 'error' and error string value). No need to quote input. 15std::string JSONErrorString(const std::string& err) { 16 std::string prefix = "{\"error\": \""; 17 std::string no_quote_err; 18 std::string suffix = "\"}"; 19 20 base::JsonDoubleQuote(err, false, &no_quote_err); 21 return prefix + no_quote_err + suffix; 22} 23 24} // namespace 25 26AutomationJSONReply::AutomationJSONReply(AutomationProvider* provider, 27 IPC::Message* reply_message) 28 : provider_(provider), 29 message_(reply_message) { 30} 31 32AutomationJSONReply::~AutomationJSONReply() { 33 DCHECK(!message_) << "JSON automation request not replied!"; 34} 35 36void AutomationJSONReply::SendSuccess(const Value* value) { 37 DCHECK(message_) << "Resending reply for JSON automation request"; 38 std::string json_string = "{}"; 39 if (value) 40 base::JSONWriter::Write(value, false, &json_string); 41 AutomationMsg_SendJSONRequest::WriteReplyParams( 42 message_, json_string, true); 43 provider_->Send(message_); 44 message_ = NULL; 45} 46 47void AutomationJSONReply::SendError(const std::string& error_message) { 48 DCHECK(message_) << "Resending reply for JSON automation request"; 49 std::string json_string = JSONErrorString(error_message); 50 AutomationMsg_SendJSONRequest::WriteReplyParams( 51 message_, json_string, false); 52 provider_->Send(message_); 53 message_ = NULL; 54} 55 56