-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcompressor_io_javascript_stream.cc
84 lines (66 loc) · 2.6 KB
/
compressor_io_javascript_stream.cc
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
// Copyright 2017 The Chromium OS 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 "compressor_io_javascript_stream.h"
#include <limits>
#include <thread>
#include "archive.h"
#include "ppapi/cpp/logging.h"
CompressorIOJavaScriptStream::CompressorIOJavaScriptStream(
JavaScriptCompressorRequestorInterface* requestor)
: requestor_(requestor) {
pthread_mutex_init(&shared_state_lock_, NULL);
pthread_cond_init(&available_data_cond_, NULL);
pthread_cond_init(&data_written_cond_, NULL);
pthread_mutex_lock(&shared_state_lock_);
available_data_ = false;
pthread_mutex_unlock(&shared_state_lock_);
}
CompressorIOJavaScriptStream::~CompressorIOJavaScriptStream() {
pthread_cond_destroy(&data_written_cond_);
pthread_cond_destroy(&available_data_cond_);
pthread_mutex_destroy(&shared_state_lock_);
};
int64_t CompressorIOJavaScriptStream::Write(int64_t byte_to_write,
const pp::VarArrayBuffer& buffer) {
pthread_mutex_lock(&shared_state_lock_);
requestor_->WriteChunkRequest(byte_to_write, buffer);
pthread_cond_wait(&data_written_cond_, &shared_state_lock_);
int64_t written_bytes = written_bytes_;
pthread_mutex_unlock(&shared_state_lock_);
return written_bytes;
}
void CompressorIOJavaScriptStream::WriteChunkDone(int64_t written_bytes) {
pthread_mutex_lock(&shared_state_lock_);
written_bytes_ = written_bytes;
pthread_cond_signal(&data_written_cond_);
pthread_mutex_unlock(&shared_state_lock_);
}
int64_t CompressorIOJavaScriptStream::Read(int64_t bytes_to_read,
char* destination_buffer) {
pthread_mutex_lock(&shared_state_lock_);
destination_buffer_ = destination_buffer;
requestor_->ReadFileChunkRequest(bytes_to_read);
while (!available_data_) {
pthread_cond_wait(&available_data_cond_, &shared_state_lock_);
}
int64_t read_bytes = read_bytes_;
available_data_ = false;
pthread_mutex_unlock(&shared_state_lock_);
return read_bytes;
}
void CompressorIOJavaScriptStream::ReadFileChunkDone(int64_t read_bytes,
pp::VarArrayBuffer* array_buffer) {
pthread_mutex_lock(&shared_state_lock_);
// JavaScript sets a negative value in read_bytes if an error occurred while
// reading a chunk.
if (read_bytes >= 0) {
char* array_buffer_data = static_cast<char*>(array_buffer->Map());
memcpy(destination_buffer_, array_buffer_data, read_bytes);
array_buffer->Unmap();
}
read_bytes_ = read_bytes;
available_data_ = true;
pthread_cond_signal(&available_data_cond_);
pthread_mutex_unlock(&shared_state_lock_);
}