Home | History | Annotate | Download | only in devtools
      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 
     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 
     25   class Response;
     26 
     27   class Message : public base::RefCountedThreadSafe<Message> {
     28    public:
     29     std::string domain() { return domain_; }
     30     std::string method() { return method_; }
     31     base::DictionaryValue* params() { return params_.get(); }
     32     virtual std::string Serialize() = 0;
     33 
     34    protected:
     35     friend class base::RefCountedThreadSafe<Message>;
     36     virtual ~Message();
     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:
     50     int id() { return id_; }
     51 
     52     virtual std::string Serialize() OVERRIDE;
     53 
     54     // Creates success response. Takes ownership of |result|.
     55     scoped_refptr<Response> SuccessResponse(base::DictionaryValue* result);
     56 
     57     // Creates error response.
     58     scoped_refptr<Response> InternalErrorResponse(const std::string& message);
     59 
     60     // Creates error response.
     61     scoped_refptr<Response> InvalidParamResponse(const std::string& param);
     62 
     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();
     71 
     72    private:
     73     friend class DevToolsProtocol;
     74     Command(int id, const std::string& method,
     75             base::DictionaryValue* params);
     76 
     77     int id_;
     78 
     79     DISALLOW_COPY_AND_ASSIGN(Command);
     80   };
     81 
     82   class Response : public base::RefCountedThreadSafe<Response> {
     83    public:
     84     std::string Serialize();
     85 
     86     bool is_async_promise() { return is_async_promise_; }
     87 
     88    private:
     89     friend class base::RefCountedThreadSafe<Response>;
     90     friend class Command;
     91     friend class DevToolsProtocol;
     92     virtual  ~Response();
     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_;
    101     bool is_async_promise_;
    102 
    103     DISALLOW_COPY_AND_ASSIGN(Response);
    104   };
    105 
    106   class Notification : public Message {
    107    public:
    108 
    109     virtual std::string Serialize() OVERRIDE;
    110 
    111    private:
    112     friend class DevToolsProtocol;
    113     virtual ~Notification();
    114 
    115     // Takes ownership of |params|.
    116     Notification(const std::string& method,
    117                  base::DictionaryValue* params);
    118 
    119     DISALLOW_COPY_AND_ASSIGN(Notification);
    120   };
    121 
    122   class Handler {
    123    public:
    124     typedef base::Callback<scoped_refptr<DevToolsProtocol::Response>(
    125         scoped_refptr<DevToolsProtocol::Command> command)> CommandHandler;
    126 
    127     virtual ~Handler();
    128 
    129     virtual scoped_refptr<DevToolsProtocol::Response> HandleCommand(
    130         scoped_refptr<DevToolsProtocol::Command> command);
    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 
    144     // Sends message to client, the caller is presumed to properly
    145     // format the message.
    146     void SendRawMessage(const std::string& message);
    147 
    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 
    157   static scoped_refptr<Command> ParseCommand(const std::string& json,
    158                                              std::string* error_response);
    159 
    160   static scoped_refptr<Notification> ParseNotification(
    161       const std::string& json);
    162 
    163   static scoped_refptr<Notification> CreateNotification(
    164       const std::string& method, base::DictionaryValue* params);
    165 
    166  private:
    167   static base::DictionaryValue* ParseMessage(const std::string& json,
    168                                              std::string* error_response);
    169 
    170   DevToolsProtocol() {}
    171   ~DevToolsProtocol() {}
    172 };
    173 
    174 }  // namespace content
    175 
    176 #endif  // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_
    177