OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "request.h" | |
6 | |
7 #include <sstream> | |
8 | |
9 namespace { | |
10 | |
11 // Creates a basic request with the mandatory fields. | |
12 pp::VarDictionary CreateBasicRequest(const int operation, | |
13 const std::string& file_system_id, | |
14 const std::string& request_id) { | |
15 pp::VarDictionary request; | |
16 request.Set(request::key::kOperation, operation); | |
17 request.Set(request::key::kFileSystemId, file_system_id); | |
18 request.Set(request::key::kRequestId, request_id); | |
19 return request; | |
20 } | |
21 | |
22 } // namespace | |
23 | |
24 // Return true if the given operation is related to packing. | |
25 bool request::IsPackRequest(int operation) { | |
26 return request::MINIMUM_PACK_REQUEST_VALUE <= operation || | |
27 operation == request::COMPRESSOR_ERROR; | |
28 } | |
29 | |
30 pp::VarDictionary request::CreateReadMetadataDoneResponse( | |
31 const std::string& file_system_id, | |
32 const std::string& request_id, | |
33 const pp::VarDictionary& metadata) { | |
34 pp::VarDictionary response = | |
35 CreateBasicRequest(READ_METADATA_DONE, file_system_id, request_id); | |
36 response.Set(request::key::kMetadata, metadata); | |
37 return response; | |
38 } | |
39 | |
40 pp::VarDictionary request::CreateReadChunkRequest( | |
41 const std::string& file_system_id, | |
42 const std::string& request_id, | |
43 int64_t offset, | |
44 int64_t length) { | |
45 pp::VarDictionary request = | |
46 CreateBasicRequest(READ_CHUNK, file_system_id, request_id); | |
47 | |
48 std::stringstream ss_offset; | |
49 ss_offset << offset; | |
50 request.Set(request::key::kOffset, ss_offset.str()); | |
51 | |
52 std::stringstream ss_length; | |
53 ss_length << length; | |
54 request.Set(request::key::kLength, ss_length.str()); | |
55 return request; | |
56 } | |
57 | |
58 pp::VarDictionary request::CreateReadPassphraseRequest( | |
59 const std::string& file_system_id, | |
60 const std::string& request_id) { | |
61 pp::VarDictionary request = | |
62 CreateBasicRequest(READ_PASSPHRASE, file_system_id, request_id); | |
63 return request; | |
64 } | |
65 | |
66 pp::VarDictionary request::CreateOpenFileDoneResponse( | |
67 const std::string& file_system_id, | |
68 const std::string& request_id) { | |
69 return CreateBasicRequest(OPEN_FILE_DONE, file_system_id, request_id); | |
70 } | |
71 | |
72 pp::VarDictionary request::CreateCloseFileDoneResponse( | |
73 const std::string& file_system_id, | |
74 const std::string& request_id, | |
75 const std::string& open_request_id) { | |
76 pp::VarDictionary response = | |
77 CreateBasicRequest(CLOSE_FILE_DONE, file_system_id, request_id); | |
78 response.Set(request::key::kOpenRequestId, open_request_id); | |
79 return response; | |
80 } | |
81 | |
82 pp::VarDictionary request::CreateReadFileDoneResponse( | |
83 const std::string& file_system_id, | |
84 const std::string& request_id, | |
85 const pp::VarArrayBuffer& array_buffer, | |
86 bool has_more_data) { | |
87 pp::VarDictionary response = | |
88 CreateBasicRequest(READ_FILE_DONE, file_system_id, request_id); | |
89 response.Set(request::key::kReadFileData, array_buffer); | |
90 response.Set(request::key::kHasMoreData, has_more_data); | |
91 return response; | |
92 } | |
93 | |
94 pp::VarDictionary request::CreateCreateArchiveDoneResponse( | |
95 const int compressor_id) { | |
96 pp::VarDictionary request; | |
97 request.Set(request::key::kOperation, CREATE_ARCHIVE_DONE); | |
98 request.Set(request::key::kCompressorId, compressor_id); | |
99 return request; | |
100 } | |
101 | |
102 pp::VarDictionary request::CreateReadFileChunkRequest( | |
103 const int compressor_id, | |
104 const int64_t length) { | |
105 pp::VarDictionary request; | |
106 request.Set(request::key::kOperation, READ_FILE_CHUNK); | |
107 request.Set(request::key::kCompressorId, compressor_id); | |
108 | |
109 std::stringstream ss_length; | |
110 ss_length << length; | |
111 request.Set(request::key::kLength, ss_length.str()); | |
112 return request; | |
113 } | |
114 | |
115 pp::VarDictionary request::CreateWriteChunkRequest( | |
116 int compressor_id, | |
117 const pp::VarArrayBuffer& array_buffer, | |
118 int64_t length) { | |
119 pp::VarDictionary request; | |
120 request.Set(request::key::kOperation, WRITE_CHUNK); | |
121 request.Set(request::key::kCompressorId, compressor_id); | |
122 | |
123 request.Set(request::key::kChunkBuffer, array_buffer); | |
124 std::stringstream ss_length; | |
125 ss_length << length; | |
126 request.Set(request::key::kLength, ss_length.str()); | |
127 return request; | |
128 } | |
129 | |
130 pp::VarDictionary request::CreateAddToArchiveDoneResponse(int compressor_id) { | |
131 pp::VarDictionary request; | |
132 request.Set(request::key::kOperation, ADD_TO_ARCHIVE_DONE); | |
133 request.Set(request::key::kCompressorId, compressor_id); | |
134 return request; | |
135 } | |
136 | |
137 pp::VarDictionary request::CreateCloseArchiveDoneResponse(int compressor_id) { | |
138 pp::VarDictionary request; | |
139 request.Set(request::key::kOperation, CLOSE_ARCHIVE_DONE); | |
140 request.Set(request::key::kCompressorId, compressor_id); | |
141 return request; | |
142 } | |
143 | |
144 pp::VarDictionary request::CreateFileSystemError( | |
145 const std::string& file_system_id, | |
146 const std::string& request_id, | |
147 const std::string& error) { | |
148 pp::VarDictionary request = | |
149 CreateBasicRequest(FILE_SYSTEM_ERROR, file_system_id, request_id); | |
150 request.Set(request::key::kError, error); | |
151 return request; | |
152 } | |
153 | |
154 pp::VarDictionary request::CreateConsoleLog( | |
155 const std::string& file_system_id, | |
156 const std::string& request_id, | |
157 const std::string& src_file, | |
158 int src_line, | |
159 const std::string& src_func, | |
160 const std::string& message) { | |
161 pp::VarDictionary request = | |
162 CreateBasicRequest(CONSOLE_LOG, file_system_id, request_id); | |
163 request.Set(request::key::kSrcFile, src_file); | |
164 request.Set(request::key::kSrcLine, src_line); | |
165 request.Set(request::key::kSrcFunc, src_func); | |
166 request.Set(request::key::kMessage, message); | |
167 return request; | |
168 } | |
169 | |
170 pp::VarDictionary request::CreateCompressorError( | |
171 int compressor_id, | |
172 const std::string& error) { | |
173 pp::VarDictionary request; | |
174 request.Set(request::key::kOperation, COMPRESSOR_ERROR); | |
175 request.Set(request::key::kCompressorId, compressor_id); | |
176 request.Set(request::key::kError, error); | |
177 return request; | |
178 } | |
179 | |
180 int64_t request::GetInt64FromString(const pp::VarDictionary& dictionary, | |
181 const std::string& request_key) { | |
182 std::stringstream ss_int64(dictionary.Get(request_key).AsString()); | |
183 int64_t int64_value; | |
184 ss_int64 >> int64_value; | |
185 return int64_value; | |
186 } | |
OLD | NEW |