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 CHROME_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_
6#define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/values.h"
13
14// Utility class for processing DevTools remote debugging messages.
15// This is a stripped down clone of content::DevTools which is not accessible
16// from chrome component (see content/browser/devtools/devtools_protocol.*).
17class DevToolsProtocol {
18 public:
19  class Response;
20
21  class Message {
22   public:
23    virtual ~Message();
24
25    std::string method() { return method_; }
26    base::DictionaryValue* params() { return params_.get(); }
27
28   protected:
29    Message(const std::string& method, base::DictionaryValue* params);
30
31    std::string method_;
32    scoped_ptr<base::DictionaryValue> params_;
33
34   private:
35    DISALLOW_COPY_AND_ASSIGN(Message);
36  };
37
38  class Command : public Message {
39   public:
40    Command(int id, const std::string& method, base::DictionaryValue* params);
41    virtual ~Command();
42
43    int id() { return id_; }
44    std::string Serialize();
45
46    // Creates success response. Takes ownership of |result|.
47    scoped_ptr<Response> SuccessResponse(base::DictionaryValue* result);
48
49    // Creates error response.
50    scoped_ptr<Response> InvalidParamResponse(const std::string& param);
51
52   private:
53    int id_;
54
55    DISALLOW_COPY_AND_ASSIGN(Command);
56  };
57
58  class Response {
59   public:
60    virtual ~Response();
61
62    int id() { return id_; }
63    int error_code() { return error_code_; }
64
65    // Result ownership is passed to the caller.
66    base::DictionaryValue* Serialize();
67
68   private:
69    friend class DevToolsProtocol;
70
71    Response(int id, int error_code, const std::string error_message);
72    Response(int id, base::DictionaryValue* result);
73    int id_;
74    int error_code_;
75    std::string error_message_;
76    scoped_ptr<base::DictionaryValue> result_;
77
78    DISALLOW_COPY_AND_ASSIGN(Response);
79  };
80
81  class Notification : public Message {
82   public:
83    virtual ~Notification();
84
85   private:
86    friend class DevToolsProtocol;
87
88    // Takes ownership of |params|.
89    Notification(const std::string& method,
90                 base::DictionaryValue* params);
91
92    DISALLOW_COPY_AND_ASSIGN(Notification);
93  };
94
95  // Result ownership is passed to the caller.
96  static Command* ParseCommand(base::DictionaryValue* command_dict);
97
98  // Result ownership is passed to the caller.
99  static Notification* ParseNotification(const std::string& json);
100
101  // Result ownership is passed to the caller.
102  static Response* ParseResponse(const std::string& json);
103
104 private:
105
106  DevToolsProtocol() {}
107  ~DevToolsProtocol() {}
108};
109
110#endif  // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_H_
111