1// Copyright (c) 2010 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_DEBUGGER_DEVTOOLS_REMOTE_MESSAGE_H_
6#define CHROME_BROWSER_DEBUGGER_DEVTOOLS_REMOTE_MESSAGE_H_
7#pragma once
8
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/hash_tables.h"
13
14// Contains DevTools protocol message header names
15// and the Flags header bit field constants.
16struct DevToolsRemoteMessageHeaders {
17  // The content length in decimal.
18  static const char kContentLength[];
19  // The tool that should handle the message.
20  static const char kTool[];
21  // The destination (inspected) object identifier (if any), like a TabID.
22  static const char kDestination[];
23};
24
25// Represents a Chrome remote debugging protocol message transferred
26// over the wire between the remote debugger and a Chrome instance.
27// Consider using DevToolsRemoteMessageBuilder (see end of this file) for easy
28// construction of outbound (Chrome -> remote debugger) messages.
29class DevToolsRemoteMessage {
30 public:
31  typedef base::hash_map<std::string, std::string> HeaderMap;
32
33  // Use this as the second parameter in a |GetHeader| call to use
34  // an empty string as the default value.
35  static const char kEmptyValue[];
36
37  // Constructs an empty message with no content or headers.
38  DevToolsRemoteMessage();
39  DevToolsRemoteMessage(const HeaderMap& headers, const std::string& content);
40  virtual ~DevToolsRemoteMessage();
41
42  const HeaderMap& headers() const {
43    return header_map_;
44  }
45
46  const std::string& content() const {
47    return content_;
48  }
49
50  int content_length() const {
51    return content_.size();
52  }
53
54  const std::string tool() const {
55    return GetHeaderWithEmptyDefault(DevToolsRemoteMessageHeaders::kTool);
56  }
57
58  const std::string destination() const {
59    return GetHeaderWithEmptyDefault(
60        DevToolsRemoteMessageHeaders::kDestination);
61  }
62
63  // Returns the header value providing default_value if the header is absent.
64  const std::string GetHeader(const std::string& header_name,
65                              const std::string& default_value) const;
66
67  // Returns the header value providing an empty string if the header is absent.
68  const std::string GetHeaderWithEmptyDefault(
69      const std::string& header_name) const;
70
71  // Returns a string representation of the message useful for the transfer to
72  // the remote debugger.
73  const std::string ToString() const;
74
75 private:
76  HeaderMap header_map_;
77  std::string content_;
78  // Cannot DISALLOW_COPY_AND_ASSIGN(DevToolsRemoteMessage) since it is passed
79  // as an IPC message argument and needs to be copied.
80};
81
82// Facilitates easy construction of outbound (Chrome -> remote debugger)
83// DevToolsRemote messages.
84class DevToolsRemoteMessageBuilder {
85 public:
86  // A singleton instance getter.
87  static DevToolsRemoteMessageBuilder& instance();
88  // Creates a message given the certain header values and a payload.
89  DevToolsRemoteMessage* Create(const std::string& tool,
90                                const std::string& destination,
91                                const std::string& payload);
92
93 private:
94  DevToolsRemoteMessageBuilder() {}
95  virtual ~DevToolsRemoteMessageBuilder() {}
96  DISALLOW_COPY_AND_ASSIGN(DevToolsRemoteMessageBuilder);
97};
98
99#endif  // CHROME_BROWSER_DEBUGGER_DEVTOOLS_REMOTE_MESSAGE_H_
100