OLD | NEW |
| (Empty) |
1 // Copyright 2017 The Chromium OS 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 "compressor_io_javascript_stream.h" | |
6 | |
7 #include <limits> | |
8 #include <thread> | |
9 | |
10 #include "archive.h" | |
11 #include "ppapi/cpp/logging.h" | |
12 | |
13 CompressorIOJavaScriptStream::CompressorIOJavaScriptStream( | |
14 JavaScriptCompressorRequestorInterface* requestor) | |
15 : requestor_(requestor) { | |
16 pthread_mutex_init(&shared_state_lock_, NULL); | |
17 pthread_cond_init(&available_data_cond_, NULL); | |
18 pthread_cond_init(&data_written_cond_, NULL); | |
19 | |
20 pthread_mutex_lock(&shared_state_lock_); | |
21 available_data_ = false; | |
22 pthread_mutex_unlock(&shared_state_lock_); | |
23 } | |
24 | |
25 CompressorIOJavaScriptStream::~CompressorIOJavaScriptStream() { | |
26 pthread_cond_destroy(&data_written_cond_); | |
27 pthread_cond_destroy(&available_data_cond_); | |
28 pthread_mutex_destroy(&shared_state_lock_); | |
29 }; | |
30 | |
31 int64_t CompressorIOJavaScriptStream::Write(int64_t byte_to_write, | |
32 const pp::VarArrayBuffer& buffer) { | |
33 pthread_mutex_lock(&shared_state_lock_); | |
34 requestor_->WriteChunkRequest(byte_to_write, buffer); | |
35 | |
36 pthread_cond_wait(&data_written_cond_, &shared_state_lock_); | |
37 | |
38 int64_t written_bytes = written_bytes_; | |
39 pthread_mutex_unlock(&shared_state_lock_); | |
40 | |
41 return written_bytes; | |
42 } | |
43 | |
44 void CompressorIOJavaScriptStream::WriteChunkDone(int64_t written_bytes) { | |
45 pthread_mutex_lock(&shared_state_lock_); | |
46 written_bytes_ = written_bytes; | |
47 pthread_cond_signal(&data_written_cond_); | |
48 pthread_mutex_unlock(&shared_state_lock_); | |
49 } | |
50 | |
51 int64_t CompressorIOJavaScriptStream::Read(int64_t bytes_to_read, | |
52 char* destination_buffer) { | |
53 pthread_mutex_lock(&shared_state_lock_); | |
54 | |
55 destination_buffer_ = destination_buffer; | |
56 requestor_->ReadFileChunkRequest(bytes_to_read); | |
57 | |
58 while (!available_data_) { | |
59 pthread_cond_wait(&available_data_cond_, &shared_state_lock_); | |
60 } | |
61 | |
62 int64_t read_bytes = read_bytes_; | |
63 available_data_ = false; | |
64 pthread_mutex_unlock(&shared_state_lock_); | |
65 return read_bytes; | |
66 } | |
67 | |
68 void CompressorIOJavaScriptStream::ReadFileChunkDone(int64_t read_bytes, | |
69 pp::VarArrayBuffer* array_buffer) { | |
70 pthread_mutex_lock(&shared_state_lock_); | |
71 | |
72 // JavaScript sets a negative value in read_bytes if an error occurred while | |
73 // reading a chunk. | |
74 if (read_bytes >= 0) { | |
75 char* array_buffer_data = static_cast<char*>(array_buffer->Map()); | |
76 memcpy(destination_buffer_, array_buffer_data, read_bytes); | |
77 array_buffer->Unmap(); | |
78 } | |
79 | |
80 read_bytes_ = read_bytes; | |
81 available_data_ = true; | |
82 pthread_cond_signal(&available_data_cond_); | |
83 pthread_mutex_unlock(&shared_state_lock_); | |
84 } | |
OLD | NEW |