Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "compressor.h" | 5 #include "compressor.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 #include <ctime> | 8 #include <ctime> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 | 10 |
| 11 #include "compressor_archive_libarchive.h" | |
|
mtomasz
2017/04/10 07:15:08
Let's rename files in a separate CL.
| |
| 12 #include "compressor_io_javascript_stream.h" | |
| 11 #include "request.h" | 13 #include "request.h" |
| 12 #include "compressor_io_javascript_stream.h" | |
| 13 #include "compressor_archive_libarchive.h" | |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // An internal implementation of JavaScriptCompressorRequestorInterface. | 17 // An internal implementation of JavaScriptCompressorRequestorInterface. |
| 18 class JavaScriptCompressorRequestor : public JavaScriptCompressorRequestorInterf ace { | 18 class JavaScriptCompressorRequestor : public JavaScriptCompressorRequestorInterf ace { |
| 19 public: | 19 public: |
| 20 explicit JavaScriptCompressorRequestor(Compressor* compressor) : | 20 explicit JavaScriptCompressorRequestor(Compressor* compressor) : |
| 21 compressor_(compressor) {} | 21 compressor_(compressor) {} |
| 22 | 22 |
| 23 virtual void WriteChunkRequest(int64_t length, | 23 virtual void WriteChunkRequest(int64_t offset, |
| 24 int64_t length, | |
| 24 const pp::VarArrayBuffer& buffer) { | 25 const pp::VarArrayBuffer& buffer) { |
| 25 compressor_->message_sender()->SendWriteChunk( | 26 compressor_->message_sender()->SendWriteChunk(compressor_->compressor_id(), |
| 26 compressor_->compressor_id(), buffer, length); | 27 buffer, offset, length); |
| 27 } | 28 } |
| 28 | 29 |
| 29 virtual void ReadFileChunkRequest(int64_t length) { | 30 virtual void ReadFileChunkRequest(int64_t length) { |
| 30 compressor_->message_sender()->SendReadFileChunk( | 31 compressor_->message_sender()->SendReadFileChunk( |
| 31 compressor_->compressor_id(), length); | 32 compressor_->compressor_id(), length); |
| 32 } | 33 } |
| 33 | 34 |
| 34 private: | 35 private: |
| 35 Compressor* compressor_; | 36 Compressor* compressor_; |
| 36 }; | 37 }; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 56 delete compressor_archive_; | 57 delete compressor_archive_; |
| 57 delete compressor_stream_; | 58 delete compressor_stream_; |
| 58 delete requestor_; | 59 delete requestor_; |
| 59 } | 60 } |
| 60 | 61 |
| 61 bool Compressor::Init() { | 62 bool Compressor::Init() { |
| 62 return worker_.Start(); | 63 return worker_.Start(); |
| 63 } | 64 } |
| 64 | 65 |
| 65 void Compressor::CreateArchive() { | 66 void Compressor::CreateArchive() { |
| 66 compressor_archive_->CreateArchive(); | 67 if (!compressor_archive_->CreateArchive()) { |
| 68 message_sender_->SendCompressorError( | |
| 69 compressor_id_, | |
| 70 compressor_archive_->error_message()); | |
| 71 return; | |
| 72 } | |
| 67 message_sender_->SendCreateArchiveDone(compressor_id_); | 73 message_sender_->SendCreateArchiveDone(compressor_id_); |
| 68 } | 74 } |
| 69 | 75 |
| 70 void Compressor::AddToArchive(const pp::VarDictionary& dictionary) { | 76 void Compressor::AddToArchive(const pp::VarDictionary& dictionary) { |
| 71 worker_.message_loop().PostWork(callback_factory_.NewCallback( | 77 worker_.message_loop().PostWork(callback_factory_.NewCallback( |
| 72 &Compressor::AddToArchiveCallback, dictionary)); | 78 &Compressor::AddToArchiveCallback, dictionary)); |
| 73 } | 79 } |
| 74 | 80 |
| 75 void Compressor::AddToArchiveCallback(int32_t, | 81 void Compressor::AddToArchiveCallback(int32_t, |
| 76 const pp::VarDictionary& dictionary) { | 82 const pp::VarDictionary& dictionary) { |
| 77 PP_DCHECK(dictionary.Get(request::key::kPathname).is_string()); | 83 PP_DCHECK(dictionary.Get(request::key::kPathname).is_string()); |
| 78 std::string pathname = | 84 std::string pathname = |
| 79 dictionary.Get(request::key::kPathname).AsString(); | 85 dictionary.Get(request::key::kPathname).AsString(); |
| 80 | 86 |
| 81 PP_DCHECK(dictionary.Get(request::key::kFileSize).is_string()); | 87 PP_DCHECK(dictionary.Get(request::key::kFileSize).is_string()); |
| 82 int64_t file_size = | 88 int64_t file_size = |
| 83 request::GetInt64FromString(dictionary, request::key::kFileSize); | 89 request::GetInt64FromString(dictionary, request::key::kFileSize); |
| 84 PP_DCHECK(file_size >= 0); | 90 PP_DCHECK(file_size >= 0); |
| 85 | 91 |
| 86 PP_DCHECK(dictionary.Get(request::key::kIsDirectory).is_bool()); | 92 PP_DCHECK(dictionary.Get(request::key::kIsDirectory).is_bool()); |
| 87 bool is_directory = | 93 bool is_directory = |
| 88 dictionary.Get(request::key::kIsDirectory).AsBool(); | 94 dictionary.Get(request::key::kIsDirectory).AsBool(); |
| 89 | 95 |
| 90 PP_DCHECK(dictionary.Get(request::key::kModificationTime).is_string()); | 96 PP_DCHECK(dictionary.Get(request::key::kModificationTime).is_string()); |
| 91 std::string strtime = | 97 // Since modification_time is milliseconds, we hold the value in int64_t. |
| 92 dictionary.Get(request::key::kModificationTime).AsString(); | 98 int64_t modification_time = |
| 93 tm tm; | 99 static_cast<int64_t>(request::GetInt64FromString(dictionary, |
| 94 strptime(strtime.c_str(), "%m/%d/%Y %T", &tm); | 100 request::key::kModificationTime)) ; |
| 95 time_t modification_time = mktime(&tm); | |
| 96 | 101 |
| 97 compressor_archive_->AddToArchive( | 102 if (!compressor_archive_->AddToArchive( |
| 98 pathname, file_size, modification_time, is_directory); | 103 pathname, file_size, modification_time, is_directory)) { |
| 104 message_sender_->SendCompressorError( | |
| 105 compressor_id_, | |
| 106 compressor_archive_->error_message()); | |
| 107 return; | |
| 108 } | |
| 99 message_sender_->SendAddToArchiveDone(compressor_id_); | 109 message_sender_->SendAddToArchiveDone(compressor_id_); |
| 100 } | 110 } |
| 101 | 111 |
| 102 void Compressor::ReadFileChunkDone(const pp::VarDictionary& dictionary) { | 112 void Compressor::ReadFileChunkDone(const pp::VarDictionary& dictionary) { |
| 103 PP_DCHECK(dictionary.Get(request::key::kLength).is_string()); | 113 PP_DCHECK(dictionary.Get(request::key::kLength).is_string()); |
| 104 int64_t read_bytes = | 114 int64_t read_bytes = |
| 105 request::GetInt64FromString(dictionary, request::key::kLength); | 115 request::GetInt64FromString(dictionary, request::key::kLength); |
| 106 | 116 |
| 107 PP_DCHECK(dictionary.Get(request::key::kChunkBuffer).is_array_buffer()); | 117 PP_DCHECK(dictionary.Get(request::key::kChunkBuffer).is_array_buffer()); |
| 108 pp::VarArrayBuffer array_buffer(dictionary.Get(request::key::kChunkBuffer)); | 118 pp::VarArrayBuffer array_buffer(dictionary.Get(request::key::kChunkBuffer)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 119 } | 129 } |
| 120 | 130 |
| 121 void Compressor::CloseArchive(const pp::VarDictionary& dictionary) { | 131 void Compressor::CloseArchive(const pp::VarDictionary& dictionary) { |
| 122 PP_DCHECK(dictionary.Get(request::key::kHasError).is_bool()); | 132 PP_DCHECK(dictionary.Get(request::key::kHasError).is_bool()); |
| 123 bool has_error = | 133 bool has_error = |
| 124 dictionary.Get(request::key::kHasError).AsBool(); | 134 dictionary.Get(request::key::kHasError).AsBool(); |
| 125 | 135 |
| 126 // If an error has occurred, no more write chunk requests are sent and | 136 // If an error has occurred, no more write chunk requests are sent and |
| 127 // CloseArchive() can be safely called in the main thread. | 137 // CloseArchive() can be safely called in the main thread. |
| 128 if (has_error) { | 138 if (has_error) { |
| 129 compressor_archive_->CloseArchive(has_error); | 139 if (!compressor_archive_->CloseArchive(has_error)) { |
| 140 message_sender_->SendCompressorError( | |
| 141 compressor_id_, | |
| 142 compressor_archive_->error_message()); | |
| 143 return; | |
| 144 } | |
| 130 message_sender_->SendCloseArchiveDone(compressor_id_); | 145 message_sender_->SendCloseArchiveDone(compressor_id_); |
| 131 } else { | 146 } else { |
| 132 worker_.message_loop().PostWork(callback_factory_.NewCallback( | 147 worker_.message_loop().PostWork(callback_factory_.NewCallback( |
| 133 &Compressor::CloseArchiveCallback, has_error)); | 148 &Compressor::CloseArchiveCallback, has_error)); |
| 134 } | 149 } |
| 135 } | 150 } |
| 136 | 151 |
| 137 void Compressor::CloseArchiveCallback(int32_t, bool has_error) { | 152 void Compressor::CloseArchiveCallback(int32_t, bool has_error) { |
| 138 compressor_archive_->CloseArchive(has_error); | 153 if (!compressor_archive_->CloseArchive(has_error)) { |
| 154 message_sender_->SendCompressorError( | |
| 155 compressor_id_, | |
| 156 compressor_archive_->error_message()); | |
| 157 return; | |
| 158 } | |
| 139 message_sender_->SendCloseArchiveDone(compressor_id_); | 159 message_sender_->SendCloseArchiveDone(compressor_id_); |
| 140 } | 160 } |
| OLD | NEW |