1// Copyright (c) 2011 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 "base/values.h"
10#include "chrome/browser/autocomplete/autocomplete_match.h"
11#include "chrome/browser/automation/automation_provider.h"
12#include "chrome/browser/automation/automation_util.h"
13#include "chrome/common/automation_messages.h"
14
15namespace {
16
17// Util for creating a JSON error return string (dict with key
18// 'error' and error string value).  No need to quote input.
19std::string JSONErrorString(const std::string& err) {
20  std::string prefix = "{\"error\": \"";
21  std::string no_quote_err;
22  std::string suffix = "\"}";
23
24  base::JsonDoubleQuote(err, false, &no_quote_err);
25  return prefix + no_quote_err + suffix;
26}
27
28}  // namespace
29
30AutomationJSONReply::AutomationJSONReply(AutomationProvider* provider,
31                                         IPC::Message* reply_message)
32  : provider_(provider),
33    message_(reply_message) {
34}
35
36AutomationJSONReply::~AutomationJSONReply() {
37  DCHECK(!message_) << "JSON automation request not replied!";
38}
39
40void AutomationJSONReply::SendSuccess(const Value* value) {
41  DCHECK(message_) << "Resending reply for JSON automation request";
42  std::string json_string = "{}";
43  if (value)
44    base::JSONWriter::Write(value, false, &json_string);
45  AutomationMsg_SendJSONRequest::WriteReplyParams(
46      message_, json_string, true);
47  provider_->Send(message_);
48  message_ = NULL;
49}
50
51void AutomationJSONReply::SendError(const std::string& error_message) {
52  DCHECK(message_) << "Resending reply for JSON automation request";
53  std::string json_string = JSONErrorString(error_message);
54  AutomationMsg_SendJSONRequest::WriteReplyParams(
55      message_, json_string, false);
56  provider_->Send(message_);
57  message_ = NULL;
58}
59
60bool GetBrowserFromJSONArgs(
61    DictionaryValue* args,
62    Browser** browser,
63    std::string* error) {
64  int browser_index;
65  if (!args->GetInteger("windex", &browser_index)) {
66    *error = "'windex' missing or invalid";
67    return false;
68  }
69  *browser = automation_util::GetBrowserAt(browser_index);
70  if (!*browser) {
71    *error = "Cannot locate browser from given index";
72    return false;
73  }
74  return true;
75}
76
77bool GetTabFromJSONArgs(
78    DictionaryValue* args,
79    TabContents** tab,
80    std::string* error) {
81  int browser_index, tab_index;
82  if (!args->GetInteger("windex", &browser_index)) {
83    *error = "'windex' missing or invalid";
84    return false;
85  }
86  if (!args->GetInteger("tab_index", &tab_index)) {
87    *error = "'tab_index' missing or invalid";
88    return false;
89  }
90  *tab = automation_util::GetTabContentsAt(browser_index, tab_index);
91  if (!*tab) {
92    *error = "Cannot locate tab from given indices";
93    return false;
94  }
95  return true;
96}
97
98bool GetBrowserAndTabFromJSONArgs(
99    DictionaryValue* args,
100    Browser** browser,
101    TabContents** tab,
102    std::string* error) {
103  return GetBrowserFromJSONArgs(args, browser, error) &&
104         GetTabFromJSONArgs(args, tab, error);
105}
106