blob: 0012947382c95bd875dec6e7d07ad6a05305b3c2 [file] [log] [blame]
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001// Copyright (c) 2013 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#ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_
6#define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_
7
8#include <map>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
13#include "base/compiler_specific.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010014#include "base/memory/ref_counted.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000015#include "base/values.h"
16
17namespace content {
18
19// Utility classes for processing DevTools remote debugging messages.
20// https://developers.google.com/chrome-developer-tools/docs/debugger-protocol
21class DevToolsProtocol {
22 public:
23 typedef base::Callback<void(const std::string& message)> Notifier;
24
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000025 class Response;
26
Ben Murdochbb1529c2013-08-08 10:24:53 +010027 class Message : public base::RefCountedThreadSafe<Message> {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000028 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000029 std::string domain() { return domain_; }
30 std::string method() { return method_; }
31 base::DictionaryValue* params() { return params_.get(); }
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010032 virtual std::string Serialize() = 0;
33
34 protected:
Ben Murdochbb1529c2013-08-08 10:24:53 +010035 friend class base::RefCountedThreadSafe<Message>;
36 virtual ~Message();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010037 Message(const std::string& method,
38 base::DictionaryValue* params);
39
40 std::string domain_;
41 std::string method_;
42 scoped_ptr<base::DictionaryValue> params_;
43
44 private:
45 DISALLOW_COPY_AND_ASSIGN(Message);
46 };
47
48 class Command : public Message {
49 public:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010050 int id() { return id_; }
51
52 virtual std::string Serialize() OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000053
54 // Creates success response. Takes ownership of |result|.
Ben Murdochbb1529c2013-08-08 10:24:53 +010055 scoped_refptr<Response> SuccessResponse(base::DictionaryValue* result);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000056
Ben Murdochbb1529c2013-08-08 10:24:53 +010057 // Creates error response.
58 scoped_refptr<Response> InternalErrorResponse(const std::string& message);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010059
Ben Murdochbb1529c2013-08-08 10:24:53 +010060 // Creates error response.
61 scoped_refptr<Response> InvalidParamResponse(const std::string& param);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000062
Ben Murdochbb1529c2013-08-08 10:24:53 +010063 // Creates error response.
64 scoped_refptr<Response> NoSuchMethodErrorResponse();
65
66 // Creates async response promise.
67 scoped_refptr<Response> AsyncResponsePromise();
68
69 protected:
70 virtual ~Command();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000071
72 private:
73 friend class DevToolsProtocol;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010074 Command(int id, const std::string& method,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000075 base::DictionaryValue* params);
76
77 int id_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000078
79 DISALLOW_COPY_AND_ASSIGN(Command);
80 };
81
Ben Murdochbb1529c2013-08-08 10:24:53 +010082 class Response : public base::RefCountedThreadSafe<Response> {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000083 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000084 std::string Serialize();
85
Ben Murdochbb1529c2013-08-08 10:24:53 +010086 bool is_async_promise() { return is_async_promise_; }
87
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000088 private:
Ben Murdochbb1529c2013-08-08 10:24:53 +010089 friend class base::RefCountedThreadSafe<Response>;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000090 friend class Command;
91 friend class DevToolsProtocol;
Ben Murdochbb1529c2013-08-08 10:24:53 +010092 virtual ~Response();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000093
94 Response(int id, base::DictionaryValue* result);
95 Response(int id, int error_code, const std::string& error_message);
96
97 int id_;
98 scoped_ptr<base::DictionaryValue> result_;
99 int error_code_;
100 std::string error_message_;
Ben Murdochbb1529c2013-08-08 10:24:53 +0100101 bool is_async_promise_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000102
103 DISALLOW_COPY_AND_ASSIGN(Response);
104 };
105
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100106 class Notification : public Message {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000107 public:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000108
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100109 virtual std::string Serialize() OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000110
111 private:
112 friend class DevToolsProtocol;
Ben Murdochbb1529c2013-08-08 10:24:53 +0100113 virtual ~Notification();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000114
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100115 // Takes ownership of |params|.
116 Notification(const std::string& method,
117 base::DictionaryValue* params);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000118
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100119 DISALLOW_COPY_AND_ASSIGN(Notification);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000120 };
121
122 class Handler {
123 public:
Ben Murdochbb1529c2013-08-08 10:24:53 +0100124 typedef base::Callback<scoped_refptr<DevToolsProtocol::Response>(
125 scoped_refptr<DevToolsProtocol::Command> command)> CommandHandler;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000126
127 virtual ~Handler();
128
Ben Murdochbb1529c2013-08-08 10:24:53 +0100129 virtual scoped_refptr<DevToolsProtocol::Response> HandleCommand(
130 scoped_refptr<DevToolsProtocol::Command> command);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000131
132 void SetNotifier(const Notifier& notifier);
133
134 protected:
135 Handler();
136
137 void RegisterCommandHandler(const std::string& command,
138 const CommandHandler& handler);
139
140 // Sends notification to the client. Takes ownership of |params|.
141 void SendNotification(const std::string& method,
142 base::DictionaryValue* params);
143
Ben Murdoch558790d2013-07-30 15:19:42 +0100144 // Sends message to client, the caller is presumed to properly
145 // format the message.
146 void SendRawMessage(const std::string& message);
147
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000148 private:
149 typedef std::map<std::string, CommandHandler> CommandHandlers;
150
151 Notifier notifier_;
152 CommandHandlers command_handlers_;
153
154 DISALLOW_COPY_AND_ASSIGN(Handler);
155 };
156
Ben Murdochbb1529c2013-08-08 10:24:53 +0100157 static scoped_refptr<Command> ParseCommand(const std::string& json,
158 std::string* error_response);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000159
Ben Murdochbb1529c2013-08-08 10:24:53 +0100160 static scoped_refptr<Notification> ParseNotification(
161 const std::string& json);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100162
Ben Murdochbb1529c2013-08-08 10:24:53 +0100163 static scoped_refptr<Notification> CreateNotification(
164 const std::string& method, base::DictionaryValue* params);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000165
166 private:
Ben Murdocheb525c52013-07-10 11:40:50 +0100167 static base::DictionaryValue* ParseMessage(const std::string& json,
168 std::string* error_response);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100169
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000170 DevToolsProtocol() {}
171 ~DevToolsProtocol() {}
172};
173
174} // namespace content
175
176#endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_