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#include "chrome/browser/debugger/devtools_remote_message.h"
6
7#include "base/string_number_conversions.h"
8
9const char DevToolsRemoteMessageHeaders::kContentLength[] = "Content-Length";
10const char DevToolsRemoteMessageHeaders::kTool[] = "Tool";
11const char DevToolsRemoteMessageHeaders::kDestination[] = "Destination";
12
13const char DevToolsRemoteMessage::kEmptyValue[] = "";
14
15DevToolsRemoteMessageBuilder& DevToolsRemoteMessageBuilder::instance() {
16  static DevToolsRemoteMessageBuilder instance_;
17  return instance_;
18}
19
20DevToolsRemoteMessage::DevToolsRemoteMessage() {}
21
22DevToolsRemoteMessage::DevToolsRemoteMessage(const HeaderMap& headers,
23                                             const std::string& content)
24    : header_map_(headers),
25      content_(content) {
26}
27
28DevToolsRemoteMessage::~DevToolsRemoteMessage() {}
29
30const std::string DevToolsRemoteMessage::GetHeader(
31    const std::string& header_name,
32    const std::string& default_value) const {
33  HeaderMap::const_iterator it = header_map_.find(header_name);
34  if (it == header_map_.end()) {
35    return default_value;
36  }
37  return it->second;
38}
39
40const std::string DevToolsRemoteMessage::GetHeaderWithEmptyDefault(
41    const std::string& header_name) const {
42  return GetHeader(header_name, DevToolsRemoteMessage::kEmptyValue);
43}
44
45const std::string DevToolsRemoteMessage::ToString() const {
46  std::string result;
47  for (HeaderMap::const_iterator it = header_map_.begin(),
48      end = header_map_.end(); it != end; ++it) {
49    result.append(it->first).append(":").append(it->second).append("\r\n");
50  }
51  result.append("\r\n").append(content_);
52  return result;
53}
54
55DevToolsRemoteMessage* DevToolsRemoteMessageBuilder::Create(
56    const std::string& tool,
57    const std::string& destination,
58    const std::string& content) {
59  DevToolsRemoteMessage::HeaderMap headers;
60  headers[DevToolsRemoteMessageHeaders::kContentLength] =
61      base::IntToString(content.size());
62  headers[DevToolsRemoteMessageHeaders::kTool] = tool;
63  headers[DevToolsRemoteMessageHeaders::kDestination] = destination;
64  return new DevToolsRemoteMessage(headers, content);
65}
66