summaryrefslogtreecommitdiffstats
path: root/chrome/browser/debugger/devtools_remote_message.h
blob: bea9ae5408335a553112b43f9d4583df099c7d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_DEBUGGER_DEVTOOLS_REMOTE_MESSAGE_H_
#define CHROME_BROWSER_DEBUGGER_DEVTOOLS_REMOTE_MESSAGE_H_
#pragma once

#include <string>

#include "base/basictypes.h"
#include "base/hash_tables.h"

// Contains DevTools protocol message header names
// and the Flags header bit field constants.
struct DevToolsRemoteMessageHeaders {
  // The content length in decimal.
  static const char kContentLength[];
  // The tool that should handle the message.
  static const char kTool[];
  // The destination (inspected) object identifier (if any), like a TabID.
  static const char kDestination[];
};

// Represents a Chrome remote debugging protocol message transferred
// over the wire between the remote debugger and a Chrome instance.
// Consider using DevToolsRemoteMessageBuilder (see end of this file) for easy
// construction of outbound (Chrome -> remote debugger) messages.
class DevToolsRemoteMessage {
 public:
  typedef base::hash_map<std::string, std::string> HeaderMap;

  // Use this as the second parameter in a |GetHeader| call to use
  // an empty string as the default value.
  static const char kEmptyValue[];

  // Constructs an empty message with no content or headers.
  DevToolsRemoteMessage() {}
  DevToolsRemoteMessage(const HeaderMap& headers, const std::string& content)
      : header_map_(headers),
        content_(content) {}
  virtual ~DevToolsRemoteMessage() {}

  const HeaderMap& headers() const {
    return header_map_;
  }

  const std::string& content() const {
    return content_;
  }

  int content_length() const {
    return content_.size();
  }

  const std::string tool() const {
    return GetHeaderWithEmptyDefault(DevToolsRemoteMessageHeaders::kTool);
  }

  const std::string destination() const {
    return GetHeaderWithEmptyDefault(
        DevToolsRemoteMessageHeaders::kDestination);
  }

  // Returns the header value providing default_value if the header is absent.
  const std::string GetHeader(const std::string& header_name,
                              const std::string& default_value) const;

  // Returns the header value providing an empty string if the header is absent.
  const std::string GetHeaderWithEmptyDefault(
      const std::string& header_name) const;

  // Returns a string representation of the message useful for the transfer to
  // the remote debugger.
  const std::string ToString() const;

 private:
  HeaderMap header_map_;
  std::string content_;
  // Cannot DISALLOW_COPY_AND_ASSIGN(DevToolsRemoteMessage) since it is passed
  // as an IPC message argument and needs to be copied.
};

// Facilitates easy construction of outbound (Chrome -> remote debugger)
// DevToolsRemote messages.
class DevToolsRemoteMessageBuilder {
 public:
  // A singleton instance getter.
  static DevToolsRemoteMessageBuilder& instance();
  // Creates a message given the certain header values and a payload.
  DevToolsRemoteMessage* Create(const std::string& tool,
                                const std::string& destination,
                                const std::string& payload);

 private:
  DevToolsRemoteMessageBuilder() {}
  virtual ~DevToolsRemoteMessageBuilder() {}
  DISALLOW_COPY_AND_ASSIGN(DevToolsRemoteMessageBuilder);
};

#endif  // CHROME_BROWSER_DEBUGGER_DEVTOOLS_REMOTE_MESSAGE_H_