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// Support utilities for the JSON automation interface used by PyAuto.
6
7#ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_JSON_H_
8#define CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_JSON_H_
9#pragma once
10
11#include <string>
12
13#include "base/compiler_specific.h"
14
15class AutomationProvider;
16class Browser;
17class DictionaryValue;
18class TabContents;
19class Value;
20
21namespace IPC {
22class Message;
23}
24
25// Helper to ensure we always send a reply message for JSON automation requests.
26class AutomationJSONReply {
27 public:
28  // Creates a new reply object for the IPC message |reply_message| for
29  // |provider|. The caller is expected to call SendSuccess() or SendError()
30  // before destroying this object.
31  AutomationJSONReply(AutomationProvider* provider,
32                      IPC::Message* reply_message);
33
34  ~AutomationJSONReply();
35
36  // Send a success reply along with data contained in |value|.
37  // An empty message will be sent if |value| is NULL.
38  void SendSuccess(const Value* value);
39
40  // Send an error reply along with error message |error_message|.
41  void SendError(const std::string& error_message);
42
43 private:
44  AutomationProvider* provider_;
45  IPC::Message* message_;
46};
47
48// Gets the browser specified by the given dictionary |args|. |args| should
49// contain a key 'windex' which refers to the index of the browser. Returns
50// true on success and sets |browser|. Otherwise, |error| will be set.
51bool GetBrowserFromJSONArgs(DictionaryValue* args,
52                            Browser** browser,
53                            std::string* error) WARN_UNUSED_RESULT;
54
55// Gets the tab specified by the given dictionary |args|. |args| should
56// contain a key 'windex' which refers to the index of the parent browser,
57// and a key 'tab_index' which refers to the index of the tab in that browser.
58// Returns true on success and sets |tab|. Otherwise, |error| will be set.
59bool GetTabFromJSONArgs(DictionaryValue* args,
60                        TabContents** tab,
61                        std::string* error) WARN_UNUSED_RESULT;
62
63// Gets the browser and tab specified by the given dictionary |args|. |args|
64// should contain a key 'windex' which refers to the index of the browser and
65// a key 'tab_index' which refers to the index of the tab in that browser.
66// Returns true on success and sets |browser| and |tab|. Otherwise, |error|
67// will be set.
68bool GetBrowserAndTabFromJSONArgs(DictionaryValue* args,
69                                  Browser** browser,
70                                  TabContents** tab,
71                                  std::string* error) WARN_UNUSED_RESULT;
72
73#endif  // CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_JSON_H_
74