1 // 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.
5 #ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_
6 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/values.h"
16 #include "content/common/content_export.h"
20 // Utility classes for processing DevTools remote debugging messages.
21 // https://developers.google.com/chrome-developer-tools/docs/debugger-protocol
22 class DevToolsProtocol
{
24 typedef base::Callback
<void(const std::string
& message
)> Notifier
;
28 class Message
: public base::RefCountedThreadSafe
<Message
> {
30 std::string
domain() { return domain_
; }
31 std::string
method() { return method_
; }
32 base::DictionaryValue
* params() { return params_
.get(); }
33 virtual std::string
Serialize() = 0;
36 friend class base::RefCountedThreadSafe
<Message
>;
38 Message(const std::string
& method
,
39 base::DictionaryValue
* params
);
43 scoped_ptr
<base::DictionaryValue
> params_
;
46 DISALLOW_COPY_AND_ASSIGN(Message
);
49 class Command
: public Message
{
51 int id() { return id_
; }
53 virtual std::string
Serialize() OVERRIDE
;
55 // Creates success response. Takes ownership of |result|.
56 scoped_refptr
<Response
> SuccessResponse(base::DictionaryValue
* result
);
58 // Creates error response.
59 scoped_refptr
<Response
> InternalErrorResponse(const std::string
& message
);
61 // Creates error response.
62 scoped_refptr
<Response
> InvalidParamResponse(const std::string
& param
);
64 // Creates error response.
65 scoped_refptr
<Response
> NoSuchMethodErrorResponse();
67 // Creates error response.
68 scoped_refptr
<Response
> ServerErrorResponse(const std::string
& message
);
70 // Creates async response promise.
71 scoped_refptr
<Response
> AsyncResponsePromise();
77 friend class DevToolsProtocol
;
78 Command(int id
, const std::string
& method
,
79 base::DictionaryValue
* params
);
83 DISALLOW_COPY_AND_ASSIGN(Command
);
86 class Response
: public base::RefCountedThreadSafe
<Response
> {
88 std::string
Serialize();
90 bool is_async_promise() { return is_async_promise_
; }
93 friend class base::RefCountedThreadSafe
<Response
>;
95 friend class DevToolsProtocol
;
98 Response(int id
, base::DictionaryValue
* result
);
99 Response(int id
, int error_code
, const std::string
& error_message
);
102 scoped_ptr
<base::DictionaryValue
> result_
;
104 std::string error_message_
;
105 bool is_async_promise_
;
107 DISALLOW_COPY_AND_ASSIGN(Response
);
110 class Notification
: public Message
{
113 virtual std::string
Serialize() OVERRIDE
;
116 friend class DevToolsProtocol
;
117 virtual ~Notification();
119 // Takes ownership of |params|.
120 Notification(const std::string
& method
,
121 base::DictionaryValue
* params
);
123 DISALLOW_COPY_AND_ASSIGN(Notification
);
126 class CONTENT_EXPORT Handler
{
128 typedef base::Callback
<scoped_refptr
<DevToolsProtocol::Response
>(
129 scoped_refptr
<DevToolsProtocol::Command
> command
)> CommandHandler
;
130 typedef base::Callback
<void(
131 scoped_refptr
<DevToolsProtocol::Notification
> notification
)>
136 virtual scoped_refptr
<DevToolsProtocol::Response
> HandleCommand(
137 scoped_refptr
<DevToolsProtocol::Command
> command
);
139 virtual void HandleNotification(
140 scoped_refptr
<DevToolsProtocol::Notification
> notification
);
142 void SetNotifier(const Notifier
& notifier
);
147 void RegisterCommandHandler(const std::string
& command
,
148 const CommandHandler
& handler
);
150 void RegisterNotificationHandler(const std::string
& notification
,
151 const NotificationHandler
& handler
);
153 // Sends notification to the client. Takes ownership of |params|.
154 void SendNotification(const std::string
& method
,
155 base::DictionaryValue
* params
);
157 void SendAsyncResponse(scoped_refptr
<DevToolsProtocol::Response
> response
);
159 // Sends message to client, the caller is presumed to properly
160 // format the message.
161 void SendRawMessage(const std::string
& message
);
164 typedef std::map
<std::string
, CommandHandler
> CommandHandlers
;
165 typedef std::map
<std::string
, NotificationHandler
> NotificationHandlers
;
168 CommandHandlers command_handlers_
;
169 NotificationHandlers notification_handlers_
;
171 DISALLOW_COPY_AND_ASSIGN(Handler
);
174 CONTENT_EXPORT
static base::DictionaryValue
* ParseMessage(
175 const std::string
& json
,
176 std::string
* error_response
);
178 CONTENT_EXPORT
static scoped_refptr
<Command
> ParseCommand(
179 const std::string
& json
,
180 std::string
* error_response
);
182 CONTENT_EXPORT
static scoped_refptr
<Command
> ParseCommand(
183 base::DictionaryValue
* command_dict
,
184 std::string
* error_response
);
186 CONTENT_EXPORT
static scoped_refptr
<Command
> CreateCommand(
188 const std::string
& method
,
189 base::DictionaryValue
* params
);
191 CONTENT_EXPORT
static scoped_refptr
<Response
> ParseResponse(
192 base::DictionaryValue
* response_dict
);
194 static scoped_refptr
<Notification
> ParseNotification(
195 const std::string
& json
);
197 static scoped_refptr
<Notification
> CreateNotification(
198 const std::string
& method
, base::DictionaryValue
* params
);
200 DevToolsProtocol() {}
201 ~DevToolsProtocol() {}
204 } // namespace content
206 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_