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.h" | |
6 | |
7 #include <cstring> | |
8 #include <ctime> | |
9 #include <sstream> | |
10 | |
11 #include "request.h" | |
12 #include "compressor_io_javascript_stream.h" | |
13 #include "compressor_archive_libarchive.h" | |
14 | |
15 namespace { | |
16 | |
17 // An internal implementation of JavaScriptCompressorRequestorInterface. | |
18 class JavaScriptCompressorRequestor : public JavaScriptCompressorRequestorInterf
ace { | |
19 public: | |
20 explicit JavaScriptCompressorRequestor(Compressor* compressor) : | |
21 compressor_(compressor) {} | |
22 | |
23 virtual void WriteChunkRequest(int64_t length, | |
24 const pp::VarArrayBuffer& buffer) { | |
25 compressor_->message_sender()->SendWriteChunk( | |
26 compressor_->compressor_id(), buffer, length); | |
27 } | |
28 | |
29 virtual void ReadFileChunkRequest(int64_t length) { | |
30 compressor_->message_sender()->SendReadFileChunk( | |
31 compressor_->compressor_id(), length); | |
32 } | |
33 | |
34 private: | |
35 Compressor* compressor_; | |
36 }; | |
37 | |
38 } // namespace | |
39 | |
40 Compressor::Compressor(const pp::InstanceHandle& instance_handle, | |
41 int compressor_id, | |
42 JavaScriptMessageSenderInterface* message_sender) | |
43 : compressor_id_(compressor_id), | |
44 message_sender_(message_sender), | |
45 worker_(instance_handle), | |
46 callback_factory_(this) { | |
47 requestor_ = new JavaScriptCompressorRequestor(this); | |
48 compressor_stream_ = | |
49 new CompressorIOJavaScriptStream(requestor_); | |
50 compressor_archive_ = | |
51 new CompressorArchiveLibarchive(compressor_stream_); | |
52 } | |
53 | |
54 Compressor::~Compressor() { | |
55 worker_.Join(); | |
56 delete compressor_archive_; | |
57 delete compressor_stream_; | |
58 delete requestor_; | |
59 } | |
60 | |
61 bool Compressor::Init() { | |
62 return worker_.Start(); | |
63 } | |
64 | |
65 void Compressor::CreateArchive() { | |
66 compressor_archive_->CreateArchive(); | |
67 message_sender_->SendCreateArchiveDone(compressor_id_); | |
68 } | |
69 | |
70 void Compressor::AddToArchive(const pp::VarDictionary& dictionary) { | |
71 worker_.message_loop().PostWork(callback_factory_.NewCallback( | |
72 &Compressor::AddToArchiveCallback, dictionary)); | |
73 } | |
74 | |
75 void Compressor::AddToArchiveCallback(int32_t, | |
76 const pp::VarDictionary& dictionary) { | |
77 PP_DCHECK(dictionary.Get(request::key::kPathname).is_string()); | |
78 std::string pathname = | |
79 dictionary.Get(request::key::kPathname).AsString(); | |
80 | |
81 PP_DCHECK(dictionary.Get(request::key::kFileSize).is_string()); | |
82 int64_t file_size = | |
83 request::GetInt64FromString(dictionary, request::key::kFileSize); | |
84 PP_DCHECK(file_size >= 0); | |
85 | |
86 PP_DCHECK(dictionary.Get(request::key::kIsDirectory).is_bool()); | |
87 bool is_directory = | |
88 dictionary.Get(request::key::kIsDirectory).AsBool(); | |
89 | |
90 PP_DCHECK(dictionary.Get(request::key::kModificationTime).is_string()); | |
91 std::string strtime = | |
92 dictionary.Get(request::key::kModificationTime).AsString(); | |
93 tm tm; | |
94 strptime(strtime.c_str(), "%m/%d/%Y %T", &tm); | |
95 time_t modification_time = mktime(&tm); | |
96 | |
97 compressor_archive_->AddToArchive( | |
98 pathname, file_size, modification_time, is_directory); | |
99 message_sender_->SendAddToArchiveDone(compressor_id_); | |
100 } | |
101 | |
102 void Compressor::ReadFileChunkDone(const pp::VarDictionary& dictionary) { | |
103 PP_DCHECK(dictionary.Get(request::key::kLength).is_string()); | |
104 int64_t read_bytes = | |
105 request::GetInt64FromString(dictionary, request::key::kLength); | |
106 | |
107 PP_DCHECK(dictionary.Get(request::key::kChunkBuffer).is_array_buffer()); | |
108 pp::VarArrayBuffer array_buffer(dictionary.Get(request::key::kChunkBuffer)); | |
109 | |
110 compressor_stream_->ReadFileChunkDone(read_bytes, &array_buffer); | |
111 } | |
112 | |
113 void Compressor::WriteChunkDone(const pp::VarDictionary& dictionary) { | |
114 PP_DCHECK(dictionary.Get(request::key::kLength).is_string()); | |
115 int64_t written_bytes = | |
116 request::GetInt64FromString(dictionary, request::key::kLength); | |
117 | |
118 compressor_stream_->WriteChunkDone(written_bytes); | |
119 } | |
120 | |
121 void Compressor::CloseArchive(const pp::VarDictionary& dictionary) { | |
122 PP_DCHECK(dictionary.Get(request::key::kHasError).is_bool()); | |
123 bool has_error = | |
124 dictionary.Get(request::key::kHasError).AsBool(); | |
125 | |
126 // If an error has occurred, no more write chunk requests are sent and | |
127 // CloseArchive() can be safely called in the main thread. | |
128 if (has_error) { | |
129 compressor_archive_->CloseArchive(has_error); | |
130 message_sender_->SendCloseArchiveDone(compressor_id_); | |
131 } else { | |
132 worker_.message_loop().PostWork(callback_factory_.NewCallback( | |
133 &Compressor::CloseArchiveCallback, has_error)); | |
134 } | |
135 } | |
136 | |
137 void Compressor::CloseArchiveCallback(int32_t, bool has_error) { | |
138 compressor_archive_->CloseArchive(has_error); | |
139 message_sender_->SendCloseArchiveDone(compressor_id_); | |
140 } | |
OLD | NEW |