• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
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"
14 #include "base/memory/ref_counted.h"
15 #include "base/values.h"
16 #include "content/common/content_export.h"
17 
18 namespace content {
19 
20 // Utility classes for processing DevTools remote debugging messages.
21 // https://developers.google.com/chrome-developer-tools/docs/debugger-protocol
22 class DevToolsProtocol {
23  public:
24   typedef base::Callback<void(const std::string& message)> Notifier;
25 
26   class Response;
27 
28   class Message : public base::RefCountedThreadSafe<Message> {
29    public:
domain()30     std::string domain() { return domain_; }
method()31     std::string method() { return method_; }
params()32     base::DictionaryValue* params() { return params_.get(); }
33     virtual std::string Serialize() = 0;
34 
35    protected:
36     friend class base::RefCountedThreadSafe<Message>;
37     virtual ~Message();
38     Message(const std::string& method,
39             base::DictionaryValue* params);
40 
41     std::string domain_;
42     std::string method_;
43     scoped_ptr<base::DictionaryValue> params_;
44 
45    private:
46     DISALLOW_COPY_AND_ASSIGN(Message);
47   };
48 
49   class Command : public Message {
50    public:
id()51     int id() { return id_; }
52 
53     virtual std::string Serialize() OVERRIDE;
54 
55     // Creates success response. Takes ownership of |result|.
56     scoped_refptr<Response> SuccessResponse(base::DictionaryValue* result);
57 
58     // Creates error response.
59     scoped_refptr<Response> InternalErrorResponse(const std::string& message);
60 
61     // Creates error response.
62     scoped_refptr<Response> InvalidParamResponse(const std::string& param);
63 
64     // Creates error response.
65     scoped_refptr<Response> NoSuchMethodErrorResponse();
66 
67     // Creates error response.
68     scoped_refptr<Response> ServerErrorResponse(const std::string& message);
69 
70     // Creates async response promise.
71     scoped_refptr<Response> AsyncResponsePromise();
72 
73    protected:
74     virtual  ~Command();
75 
76    private:
77     friend class DevToolsProtocol;
78     Command(int id, const std::string& method,
79             base::DictionaryValue* params);
80 
81     int id_;
82 
83     DISALLOW_COPY_AND_ASSIGN(Command);
84   };
85 
86   class Response : public base::RefCountedThreadSafe<Response> {
87    public:
88     std::string Serialize();
89 
is_async_promise()90     bool is_async_promise() { return is_async_promise_; }
91 
92    private:
93     friend class base::RefCountedThreadSafe<Response>;
94     friend class Command;
95     friend class DevToolsProtocol;
96     virtual  ~Response();
97 
98     Response(int id, base::DictionaryValue* result);
99     Response(int id, int error_code, const std::string& error_message);
100 
101     int id_;
102     scoped_ptr<base::DictionaryValue> result_;
103     int error_code_;
104     std::string error_message_;
105     bool is_async_promise_;
106 
107     DISALLOW_COPY_AND_ASSIGN(Response);
108   };
109 
110   class Notification : public Message {
111    public:
112 
113     virtual std::string Serialize() OVERRIDE;
114 
115    private:
116     friend class DevToolsProtocol;
117     virtual ~Notification();
118 
119     // Takes ownership of |params|.
120     Notification(const std::string& method,
121                  base::DictionaryValue* params);
122 
123     DISALLOW_COPY_AND_ASSIGN(Notification);
124   };
125 
126   class CONTENT_EXPORT Handler {
127    public:
128     typedef base::Callback<scoped_refptr<DevToolsProtocol::Response>(
129         scoped_refptr<DevToolsProtocol::Command> command)> CommandHandler;
130 
131     virtual ~Handler();
132 
133     virtual scoped_refptr<DevToolsProtocol::Response> HandleCommand(
134         scoped_refptr<DevToolsProtocol::Command> command);
135 
136     void SetNotifier(const Notifier& notifier);
137 
138    protected:
139     Handler();
140 
141     void RegisterCommandHandler(const std::string& command,
142                                 const CommandHandler& handler);
143 
144     // Sends notification to the client. Takes ownership of |params|.
145     void SendNotification(const std::string& method,
146                           base::DictionaryValue* params);
147 
148     void SendAsyncResponse(scoped_refptr<DevToolsProtocol::Response> response);
149 
150     // Sends message to client, the caller is presumed to properly
151     // format the message.
152     void SendRawMessage(const std::string& message);
153 
154    private:
155     typedef std::map<std::string, CommandHandler> CommandHandlers;
156 
157     Notifier notifier_;
158     CommandHandlers command_handlers_;
159 
160     DISALLOW_COPY_AND_ASSIGN(Handler);
161   };
162 
163   CONTENT_EXPORT static base::DictionaryValue* ParseMessage(
164       const std::string& json,
165       std::string* error_response);
166 
167   CONTENT_EXPORT static scoped_refptr<Command> ParseCommand(
168       const std::string& json,
169       std::string* error_response);
170 
171   CONTENT_EXPORT static scoped_refptr<Command> ParseCommand(
172       base::DictionaryValue* command_dict,
173       std::string* error_response);
174 
175   CONTENT_EXPORT static scoped_refptr<Command> CreateCommand(
176       int id,
177       const std::string& method,
178       base::DictionaryValue* params);
179 
180   CONTENT_EXPORT static scoped_refptr<Response> ParseResponse(
181       base::DictionaryValue* response_dict);
182 
183   static scoped_refptr<Notification> ParseNotification(
184       const std::string& json);
185 
186   static scoped_refptr<Notification> CreateNotification(
187       const std::string& method, base::DictionaryValue* params);
188 
DevToolsProtocol()189   DevToolsProtocol() {}
~DevToolsProtocol()190   ~DevToolsProtocol() {}
191 };
192 
193 }  // namespace content
194 
195 #endif  // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_
196