summaryrefslogtreecommitdiffstats
path: root/content/browser/devtools/devtools_io_context.cc
blob: 74f062b91d164d47a57349e07e63e97f14c54672 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2015 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.

#include "content/browser/devtools/devtools_io_context.h"

#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/third_party/icu/icu_utf.h"
#include "content/public/browser/browser_thread.h"

namespace content {
namespace devtools {

namespace {
unsigned s_last_stream_handle = 0;
}

using Stream = DevToolsIOContext::Stream;

Stream::Stream()
    : base::RefCountedDeleteOnMessageLoop<Stream>(
          BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)),
      handle_(base::UintToString(++s_last_stream_handle)),
      had_errors_(false),
      last_read_pos_(0) {}

Stream::~Stream() {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
}

bool Stream::InitOnFileThreadIfNeeded() {
  if (had_errors_)
    return false;
  if (file_.IsValid())
    return true;
  base::FilePath temp_path;
  if (!base::CreateTemporaryFile(&temp_path)) {
    LOG(ERROR) << "Failed to create temporary file";
    had_errors_ = true;
    return false;
  }
  const unsigned flags = base::File::FLAG_OPEN_TRUNCATED |
                         base::File::FLAG_WRITE | base::File::FLAG_READ |
                         base::File::FLAG_DELETE_ON_CLOSE;
  file_.Initialize(temp_path, flags);
  if (!file_.IsValid()) {
    LOG(ERROR) << "Failed to open temporary file: " << temp_path.value()
        << ", " << base::File::ErrorToString(file_.error_details());
    had_errors_ = true;
    DeleteFile(temp_path, false);
    return false;
  }
  return true;
}

void Stream::Read(off_t position, size_t max_size, ReadCallback callback) {
  BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
      base::Bind(&Stream::ReadOnFileThread, this, position, max_size,
                 callback));
}

void Stream::Append(const scoped_refptr<base::RefCountedString>& data) {
  BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
      base::Bind(&Stream::AppendOnFileThread, this, data));
}

void Stream::ReadOnFileThread(off_t position, size_t max_size,
                              ReadCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  Status status = StatusFailure;
  scoped_refptr<base::RefCountedString> data;

  if (file_.IsValid()) {
    std::string buffer;
    buffer.resize(max_size);
    if (position < 0)
      position = last_read_pos_;
    int size_got = file_.ReadNoBestEffort(position, &*buffer.begin(), max_size);
    if (size_got < 0) {
      LOG(ERROR) << "Failed to read temporary file";
      had_errors_ = true;
      file_.Close();
    } else {
      // Provided client has requested sufficient large block, make their
      // life easier by not truncating in the middle of a UTF-8 character.
      if (size_got > 6 && !CBU8_IS_SINGLE(buffer[size_got - 1])) {
        base::TruncateUTF8ToByteSize(buffer, size_got, &buffer);
        size_got = buffer.size();
      } else {
        buffer.resize(size_got);
      }
      data = base::RefCountedString::TakeString(&buffer);
      status = size_got ? StatusSuccess : StatusEOF;
      last_read_pos_ = position + size_got;
    }
  }
  BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                          base::Bind(callback, data, status));
}

void Stream::AppendOnFileThread(
    const scoped_refptr<base::RefCountedString>& data) {
  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
  if (!InitOnFileThreadIfNeeded())
    return;
  const std::string& buffer = data->data();
  int size_written = file_.WriteAtCurrentPos(&*buffer.begin(), buffer.size());
  if (size_written != static_cast<int>(buffer.size())) {
    LOG(ERROR) << "Failed to write temporary file";
    had_errors_ = true;
    file_.Close();
  }
}

DevToolsIOContext::DevToolsIOContext() {}

DevToolsIOContext::~DevToolsIOContext() {}

scoped_refptr<Stream> DevToolsIOContext::CreateTempFileBackedStream() {
  scoped_refptr<Stream> result = new Stream();
  bool inserted =
      streams_.insert(std::make_pair(result->handle(), result)).second;
  DCHECK(inserted);
  return result;
}

scoped_refptr<Stream>
    DevToolsIOContext::GetByHandle(const std::string& handle) {
  StreamsMap::const_iterator it = streams_.find(handle);
  return it == streams_.end() ? scoped_refptr<Stream>() : it->second;
}

bool DevToolsIOContext::Close(const std::string& handle) {
  return streams_.erase(handle) == 1;
}

void DevToolsIOContext::DiscardAllStreams() {
  return streams_.clear();
}

}  // namespace devtools
}  // namespace content