Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 14 | #include "base/memory/ref_counted.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 15 | #include "base/values.h" |
| 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | // Utility classes for processing DevTools remote debugging messages. |
| 20 | // https://developers.google.com/chrome-developer-tools/docs/debugger-protocol |
| 21 | class DevToolsProtocol { |
| 22 | public: |
| 23 | typedef base::Callback<void(const std::string& message)> Notifier; |
| 24 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 25 | class Response; |
| 26 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 27 | class Message : public base::RefCountedThreadSafe<Message> { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 28 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 29 | std::string domain() { return domain_; } |
| 30 | std::string method() { return method_; } |
| 31 | base::DictionaryValue* params() { return params_.get(); } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 32 | virtual std::string Serialize() = 0; |
| 33 | |
| 34 | protected: |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 35 | friend class base::RefCountedThreadSafe<Message>; |
| 36 | virtual ~Message(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 37 | 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) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 50 | int id() { return id_; } |
| 51 | |
| 52 | virtual std::string Serialize() OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 53 | |
| 54 | // Creates success response. Takes ownership of |result|. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 55 | scoped_refptr<Response> SuccessResponse(base::DictionaryValue* result); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 56 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 57 | // Creates error response. |
| 58 | scoped_refptr<Response> InternalErrorResponse(const std::string& message); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 59 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 60 | // Creates error response. |
| 61 | scoped_refptr<Response> InvalidParamResponse(const std::string& param); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 62 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 63 | // 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | friend class DevToolsProtocol; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 74 | Command(int id, const std::string& method, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 75 | base::DictionaryValue* params); |
| 76 | |
| 77 | int id_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 78 | |
| 79 | DISALLOW_COPY_AND_ASSIGN(Command); |
| 80 | }; |
| 81 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 82 | class Response : public base::RefCountedThreadSafe<Response> { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 83 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 84 | std::string Serialize(); |
| 85 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 86 | bool is_async_promise() { return is_async_promise_; } |
| 87 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 88 | private: |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 89 | friend class base::RefCountedThreadSafe<Response>; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 90 | friend class Command; |
| 91 | friend class DevToolsProtocol; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 92 | virtual ~Response(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 93 | |
| 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 Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 101 | bool is_async_promise_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 102 | |
| 103 | DISALLOW_COPY_AND_ASSIGN(Response); |
| 104 | }; |
| 105 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 106 | class Notification : public Message { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 107 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 108 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 109 | virtual std::string Serialize() OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | friend class DevToolsProtocol; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 113 | virtual ~Notification(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 114 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 115 | // Takes ownership of |params|. |
| 116 | Notification(const std::string& method, |
| 117 | base::DictionaryValue* params); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 118 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 119 | DISALLOW_COPY_AND_ASSIGN(Notification); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | class Handler { |
| 123 | public: |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 124 | typedef base::Callback<scoped_refptr<DevToolsProtocol::Response>( |
| 125 | scoped_refptr<DevToolsProtocol::Command> command)> CommandHandler; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 126 | |
| 127 | virtual ~Handler(); |
| 128 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 129 | virtual scoped_refptr<DevToolsProtocol::Response> HandleCommand( |
| 130 | scoped_refptr<DevToolsProtocol::Command> command); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 131 | |
| 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 Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 144 | // 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 148 | 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 Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 157 | static scoped_refptr<Command> ParseCommand(const std::string& json, |
| 158 | std::string* error_response); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 159 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 160 | static scoped_refptr<Notification> ParseNotification( |
| 161 | const std::string& json); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 162 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 163 | static scoped_refptr<Notification> CreateNotification( |
| 164 | const std::string& method, base::DictionaryValue* params); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 165 | |
| 166 | private: |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 167 | static base::DictionaryValue* ParseMessage(const std::string& json, |
| 168 | std::string* error_response); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 169 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 170 | DevToolsProtocol() {} |
| 171 | ~DevToolsProtocol() {} |
| 172 | }; |
| 173 | |
| 174 | } // namespace content |
| 175 | |
| 176 | #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_ |