devtools_protocol.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/values.h" 15 16namespace content { 17 18// Utility classes for processing DevTools remote debugging messages. 19// https://developers.google.com/chrome-developer-tools/docs/debugger-protocol 20class DevToolsProtocol { 21 public: 22 typedef base::Callback<void(const std::string& message)> Notifier; 23 24 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object 25 enum Error { 26 kErrorParseError = -32700, 27 kErrorInvalidRequest = -32600, 28 kErrorNoSuchMethod = -32601, 29 kErrorInvalidParams = -32602, 30 kErrorInternalError = -32603 31 }; 32 33 class Response; 34 35 class Command { 36 public: 37 ~Command(); 38 39 int id() { return id_; } 40 std::string domain() { return domain_; } 41 std::string method() { return method_; } 42 base::DictionaryValue* params() { return params_.get(); } 43 44 // Creates success response. Takes ownership of |result|. 45 scoped_ptr<Response> SuccessResponse(base::DictionaryValue* result); 46 47 // Creates error response. Caller takes ownership of the return value. 48 scoped_ptr<Response> ErrorResponse(int error_code, 49 const std::string& error_message); 50 51 // Creates error response. Caller takes ownership of the return value. 52 scoped_ptr<Response> NoSuchMethodErrorResponse(); 53 54 private: 55 friend class DevToolsProtocol; 56 Command(int id, const std::string& domain, const std::string& method, 57 base::DictionaryValue* params); 58 59 int id_; 60 std::string domain_; 61 std::string method_; 62 scoped_ptr<base::DictionaryValue> params_; 63 64 DISALLOW_COPY_AND_ASSIGN(Command); 65 }; 66 67 class Response { 68 public: 69 ~Response(); 70 71 std::string Serialize(); 72 73 private: 74 friend class Command; 75 friend class DevToolsProtocol; 76 77 Response(int id, base::DictionaryValue* result); 78 Response(int id, int error_code, const std::string& error_message); 79 80 int id_; 81 scoped_ptr<base::DictionaryValue> result_; 82 int error_code_; 83 std::string error_message_; 84 85 DISALLOW_COPY_AND_ASSIGN(Response); 86 }; 87 88 class Notification { 89 public: 90 // Takes ownership of |params|. 91 Notification(const std::string& method, base::DictionaryValue* params); 92 ~Notification(); 93 94 std::string Serialize(); 95 96 private: 97 std::string method_; 98 scoped_ptr<base::DictionaryValue> params_; 99 100 DISALLOW_COPY_AND_ASSIGN(Notification); 101 }; 102 103 class Event { 104 public: 105 ~Event(); 106 107 std::string Serialize(); 108 109 private: 110 friend class DevToolsProtocol; 111 112 // Takes ownership over |params|. 113 Event(const std::string& method, base::DictionaryValue* params); 114 115 std::string method_; 116 scoped_ptr<base::DictionaryValue> params_; 117 118 DISALLOW_COPY_AND_ASSIGN(Event); 119 }; 120 121 class Handler { 122 public: 123 typedef base::Callback<scoped_ptr<DevToolsProtocol::Response>( 124 DevToolsProtocol::Command* command)> CommandHandler; 125 126 virtual ~Handler(); 127 128 virtual scoped_ptr<DevToolsProtocol::Response> HandleCommand( 129 DevToolsProtocol::Command* command); 130 131 void SetNotifier(const Notifier& notifier); 132 133 protected: 134 Handler(); 135 136 void RegisterCommandHandler(const std::string& command, 137 const CommandHandler& handler); 138 139 // Sends notification to the client. Takes ownership of |params|. 140 void SendNotification(const std::string& method, 141 base::DictionaryValue* params); 142 143 private: 144 typedef std::map<std::string, CommandHandler> CommandHandlers; 145 146 Notifier notifier_; 147 CommandHandlers command_handlers_; 148 149 DISALLOW_COPY_AND_ASSIGN(Handler); 150 }; 151 152 static Command* ParseCommand(const std::string& json, 153 std::string* error_response); 154 155 static Event* CreateEvent(const std::string& method, 156 base::DictionaryValue* params); 157 158 private: 159 DevToolsProtocol() {} 160 ~DevToolsProtocol() {} 161}; 162 163} // namespace content 164 165#endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_ 166