OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium 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 "nacl_io/googledrivefs/googledrivefs_util.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 #include <stdlib.h> |
| 9 #include <string.h> |
| 10 |
| 11 #include <limits> |
| 12 |
| 13 #include "ppapi/c/pp_completion_callback.h" |
| 14 |
| 15 #include "nacl_io/error.h" |
| 16 #include "nacl_io/filesystem.h" |
| 17 #include "nacl_io/path.h" |
| 18 #include "nacl_io/pepper_interface.h" |
| 19 #include "nacl_io/statuscode.h" |
| 20 #include "nacl_io/googledrivefs/googledrivefs.h" |
| 21 |
| 22 namespace nacl_io { |
| 23 |
| 24 #define ARRAY_SIZE_UNSAFE(arr) ((sizeof arr) / sizeof arr[0]) |
| 25 |
| 26 std::string ParentEqualClause(const std::string& parent_dir_id) { |
| 27 return "%27" + parent_dir_id + "%27+in+parents"; |
| 28 } |
| 29 |
| 30 std::string NameEqualClause(const std::string& name) { |
| 31 return "name+=+%27" + name + "%27"; |
| 32 } |
| 33 |
| 34 int ExtractYearFromRFC3339(const std::string& date_time) { |
| 35 return atoi(date_time.substr(0, 4).c_str()); |
| 36 } |
| 37 |
| 38 int ExtractMonthFromRFC3339(const std::string& date_time) { |
| 39 return atoi(date_time.substr(5, 2).c_str()); |
| 40 } |
| 41 |
| 42 int ExtractDayFromRFC3339(const std::string& date_time) { |
| 43 return atoi(date_time.substr(8, 2).c_str()); |
| 44 } |
| 45 |
| 46 int ExtractHourFromRFC3339(const std::string& date_time) { |
| 47 return atoi(date_time.substr(11, 2).c_str()); |
| 48 } |
| 49 |
| 50 int ExtractMinuteFromRFC3339(const std::string& date_time) { |
| 51 return atoi(date_time.substr(14, 2).c_str()); |
| 52 } |
| 53 |
| 54 int ExtractSecondFromRFC3339(const std::string& date_time) { |
| 55 return atoi(date_time.substr(17, 2).c_str()); |
| 56 } |
| 57 |
| 58 time_t ConvertDateTimeToEpochTime(int year, |
| 59 int month, |
| 60 int day, |
| 61 int hour, |
| 62 int minute, |
| 63 int second) { |
| 64 time_t epoch_time = 0; |
| 65 |
| 66 for (int i = 1970; i < year; ++i) { |
| 67 if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)) { |
| 68 epoch_time += 366 * 24 * 60 * 60; |
| 69 } else { |
| 70 epoch_time += 365 * 24 * 60 * 60; |
| 71 } |
| 72 } |
| 73 int month_to_number_of_days[] = {31, 28, 31, 30, 31, 30, |
| 74 31, 31, 30, 31, 30, 31}; |
| 75 |
| 76 for (int i = 1; i < month; ++i) { |
| 77 epoch_time += month_to_number_of_days[i - 1] * 24 * 60 * 60; |
| 78 if (i == 2 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))) { |
| 79 epoch_time += 24 * 60 * 60; |
| 80 } |
| 81 } |
| 82 |
| 83 epoch_time += (day - 1) * 24 * 60 * 60; |
| 84 |
| 85 if (hour > 0) { |
| 86 epoch_time += hour * 60 * 60; |
| 87 } |
| 88 |
| 89 if (minute > 0) { |
| 90 epoch_time += minute * 60; |
| 91 } |
| 92 |
| 93 epoch_time += second; |
| 94 |
| 95 return epoch_time; |
| 96 } |
| 97 |
| 98 void AddUrlPath(const std::string& path, std::string* out_url) { |
| 99 *out_url += "/"; |
| 100 *out_url += path; |
| 101 } |
| 102 |
| 103 void AddUrlQAttributeValue(std::string* array, |
| 104 size_t size, |
| 105 std::string* out_q_attribute_value) { |
| 106 *out_q_attribute_value = array[0]; |
| 107 for (size_t i = 1; i < size; ++i) { |
| 108 *out_q_attribute_value += "+and+"; |
| 109 *out_q_attribute_value += array[i]; |
| 110 } |
| 111 } |
| 112 |
| 113 void AddUrlFirstQueryParameter(const std::string& attribute, |
| 114 const std::string& value, |
| 115 std::string* out_url) { |
| 116 *out_url += "?"; |
| 117 *out_url += attribute; |
| 118 *out_url += "="; |
| 119 *out_url += value; |
| 120 } |
| 121 |
| 122 void AddUrlNextQueryParameter(const std::string& attribute, |
| 123 const std::string& value, |
| 124 std::string* out_url) { |
| 125 *out_url += "&"; |
| 126 *out_url += attribute; |
| 127 *out_url += "="; |
| 128 *out_url += value; |
| 129 } |
| 130 |
| 131 void AddHeaders(const std::string& header_field_key, |
| 132 const std::string& header_field_value, |
| 133 std::string* out_headers) { |
| 134 *out_headers += header_field_key; |
| 135 *out_headers += ": "; |
| 136 *out_headers += header_field_value; |
| 137 *out_headers += "\n"; |
| 138 } |
| 139 |
| 140 void AddBody(const std::string& data, std::string* out_body) { |
| 141 *out_body += data; |
| 142 *out_body += "\n"; |
| 143 } |
| 144 |
| 145 Error LoadUrl(PepperInterface* ppapi, |
| 146 const RequestUrlParams& params, |
| 147 ScopedResource* out_url_response_info_resource) { |
| 148 out_url_response_info_resource->Reset(0); |
| 149 |
| 150 URLLoaderInterface* url_loader_iface = ppapi->GetURLLoaderInterface(); |
| 151 ScopedResource url_loader_resource( |
| 152 ppapi, url_loader_iface->Create(ppapi->GetInstance())); |
| 153 if (!url_loader_resource.pp_resource()) { |
| 154 return EPERM; |
| 155 } |
| 156 |
| 157 URLRequestInfoInterface* url_request_info_iface = |
| 158 ppapi->GetURLRequestInfoInterface(); |
| 159 ScopedResource url_request_info_resource( |
| 160 ppapi, url_request_info_iface->Create(ppapi->GetInstance())); |
| 161 if (!url_request_info_resource.pp_resource()) { |
| 162 return EPERM; |
| 163 } |
| 164 |
| 165 VarInterface* var_iface = ppapi->GetVarInterface(); |
| 166 |
| 167 struct PP_Var pp_var = |
| 168 var_iface->VarFromUtf8(params.url.c_str(), params.url.size()); |
| 169 if (!url_request_info_iface->SetProperty( |
| 170 url_request_info_resource.pp_resource(), PP_URLREQUESTPROPERTY_URL, |
| 171 pp_var)) { |
| 172 return EPERM; |
| 173 } |
| 174 |
| 175 pp_var = var_iface->VarFromUtf8(params.method.c_str(), params.method.size()); |
| 176 if (!url_request_info_iface->SetProperty( |
| 177 url_request_info_resource.pp_resource(), PP_URLREQUESTPROPERTY_METHOD, |
| 178 pp_var)) { |
| 179 return EPERM; |
| 180 } |
| 181 |
| 182 pp_var = |
| 183 var_iface->VarFromUtf8(params.headers.c_str(), params.headers.size()); |
| 184 if (!url_request_info_iface->SetProperty( |
| 185 url_request_info_resource.pp_resource(), |
| 186 PP_URLREQUESTPROPERTY_HEADERS, pp_var)) { |
| 187 return EPERM; |
| 188 } |
| 189 |
| 190 if (!url_request_info_iface->AppendDataToBody( |
| 191 url_request_info_resource.pp_resource(), params.body.data(), |
| 192 params.body.size())) { |
| 193 return EPERM; |
| 194 } |
| 195 |
| 196 pp_var = PP_MakeBool(PP_TRUE); |
| 197 if (!url_request_info_iface->SetProperty( |
| 198 url_request_info_resource.pp_resource(), |
| 199 PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS, pp_var)) { |
| 200 return EPERM; |
| 201 } |
| 202 |
| 203 pp_var = PP_MakeBool(PP_TRUE); |
| 204 if (!url_request_info_iface->SetProperty( |
| 205 url_request_info_resource.pp_resource(), |
| 206 PP_URLREQUESTPROPERTY_STREAMTOFILE, pp_var)) { |
| 207 return EPERM; |
| 208 } |
| 209 |
| 210 int32_t err = url_loader_iface->Open(url_loader_resource.pp_resource(), |
| 211 url_request_info_resource.pp_resource(), |
| 212 PP_BlockUntilComplete()); |
| 213 if (err < 0) { |
| 214 return PPERROR_TO_ERRNO(err); |
| 215 } |
| 216 |
| 217 int32_t bytes_read = url_loader_iface->FinishStreamingToFile( |
| 218 url_loader_resource.pp_resource(), PP_BlockUntilComplete()); |
| 219 if (bytes_read < 0) { |
| 220 return PPERROR_TO_ERRNO(bytes_read); |
| 221 } |
| 222 |
| 223 out_url_response_info_resource->Reset( |
| 224 url_loader_iface->GetResponseInfo(url_loader_resource.pp_resource())); |
| 225 if (!out_url_response_info_resource->pp_resource()) { |
| 226 return EPERM; |
| 227 } |
| 228 |
| 229 return 0; |
| 230 } |
| 231 |
| 232 Error ReadResponseBody(PepperInterface* ppapi, |
| 233 PP_Resource url_response_info_object, |
| 234 int32_t bytes_to_read, |
| 235 std::string* out_output) { |
| 236 out_output->clear(); |
| 237 |
| 238 URLResponseInfoInterface* url_response_info_iface = |
| 239 ppapi->GetURLResponseInfoInterface(); |
| 240 FileIoInterface* file_io_iface = ppapi->GetFileIoInterface(); |
| 241 |
| 242 ScopedResource file_ref_resource( |
| 243 ppapi, |
| 244 url_response_info_iface->GetBodyAsFileRef(url_response_info_object)); |
| 245 if (!file_ref_resource.pp_resource()) { |
| 246 return EPERM; |
| 247 } |
| 248 |
| 249 ScopedResource file_io_resource(ppapi, |
| 250 file_io_iface->Create(ppapi->GetInstance())); |
| 251 if (!file_io_resource.pp_resource()) { |
| 252 return EPERM; |
| 253 } |
| 254 |
| 255 int32_t open_result = file_io_iface->Open( |
| 256 file_io_resource.pp_resource(), file_ref_resource.pp_resource(), |
| 257 PP_FILEOPENFLAG_READ, PP_BlockUntilComplete()); |
| 258 if (open_result < 0) { |
| 259 return PPERROR_TO_ERRNO(open_result); |
| 260 } |
| 261 |
| 262 std::string string_buffer(1024, '\0'); |
| 263 int32_t bytes_read = |
| 264 file_io_iface->Read(file_io_resource.pp_resource(), 0, &string_buffer[0], |
| 265 string_buffer.size(), PP_BlockUntilComplete()); |
| 266 |
| 267 while (bytes_read > 0) { |
| 268 *out_output += string_buffer.substr(0, bytes_read); |
| 269 |
| 270 bytes_read = file_io_iface->Read( |
| 271 file_io_resource.pp_resource(), out_output->size(), &string_buffer[0], |
| 272 string_buffer.size(), PP_BlockUntilComplete()); |
| 273 } |
| 274 |
| 275 if (bytes_read < 0) { |
| 276 return PPERROR_TO_ERRNO(bytes_read); |
| 277 } |
| 278 |
| 279 out_output->resize(std::min<size_t>(out_output->size(), bytes_to_read)); |
| 280 |
| 281 return 0; |
| 282 } |
| 283 |
| 284 int32_t ReadStatusCode(PepperInterface* ppapi, |
| 285 PP_Resource url_response_info_object) { |
| 286 URLResponseInfoInterface* url_response_info_iface = |
| 287 ppapi->GetURLResponseInfoInterface(); |
| 288 |
| 289 struct PP_Var status_code_var = url_response_info_iface->GetProperty( |
| 290 url_response_info_object, PP_URLRESPONSEPROPERTY_STATUSCODE); |
| 291 |
| 292 return status_code_var.value.as_int; |
| 293 } |
| 294 |
| 295 Error GetValueStringAndValuePos(const std::string& json, |
| 296 const std::string& key, |
| 297 size_t key_search_pos, |
| 298 std::string* out_value_string, |
| 299 size_t* out_value_pos) { |
| 300 out_value_string->clear(); |
| 301 *out_value_pos = std::string::npos; |
| 302 |
| 303 // json format is |
| 304 // .. |
| 305 // "<key1>": "<value1>" |
| 306 // .. |
| 307 // "<key2>": "<value2>" |
| 308 // .. |
| 309 // The variable key_location stores the index of the first character |
| 310 // of a target key. |
| 311 const char* s_quote_colon_quote = "\": \""; |
| 312 size_t key_location = json.find(key + s_quote_colon_quote, key_search_pos); |
| 313 if (key_location == std::string::npos) { |
| 314 return EINVAL; |
| 315 } |
| 316 |
| 317 size_t start_value_index = |
| 318 key_location + key.size() + strlen(s_quote_colon_quote); |
| 319 size_t end_value_index = json.find("\"", start_value_index); |
| 320 if (end_value_index == std::string::npos) { |
| 321 return EPERM; |
| 322 } |
| 323 |
| 324 *out_value_string = |
| 325 json.substr(start_value_index, end_value_index - start_value_index); |
| 326 *out_value_pos = start_value_index; |
| 327 |
| 328 return 0; |
| 329 } |
| 330 |
| 331 // A Google Drive item is a file or a directory. |
| 332 // RequestItemIdAndItemType requests to see if either a file |
| 333 // with item_name or a directory with item_name is there. |
| 334 Error RequestItemIdAndItemType(const std::string& parent_dir_id, |
| 335 const std::string& item_name, |
| 336 Filesystem* filesystem, |
| 337 std::string* out_item_id, |
| 338 bool* out_is_dir_type) { |
| 339 out_item_id->clear(); |
| 340 *out_is_dir_type = false; |
| 341 |
| 342 GoogleDriveFs* googledrivefs = static_cast<GoogleDriveFs*>(filesystem); |
| 343 |
| 344 RequestUrlParams p; |
| 345 |
| 346 p.url = DRIVE_URL; |
| 347 std::string q_attribute_value; |
| 348 std::string attribute_values[] = {ParentEqualClause(parent_dir_id), |
| 349 NameEqualClause(item_name)}; |
| 350 |
| 351 AddUrlQAttributeValue(attribute_values, ARRAY_SIZE_UNSAFE(attribute_values), |
| 352 &q_attribute_value); |
| 353 AddUrlFirstQueryParameter("q", q_attribute_value, &p.url); |
| 354 |
| 355 p.method = "GET"; |
| 356 |
| 357 AddHeaders("Content-type", "application/json", &p.headers); |
| 358 AddHeaders("Authorization", "Bearer " + googledrivefs->token(), &p.headers); |
| 359 |
| 360 ScopedResource url_response_info_resource(googledrivefs->ppapi()); |
| 361 Error error = LoadUrl(googledrivefs->ppapi(), p, &url_response_info_resource); |
| 362 if (error) { |
| 363 return error; |
| 364 } |
| 365 |
| 366 if (ReadStatusCode(googledrivefs->ppapi(), |
| 367 url_response_info_resource.pp_resource()) != |
| 368 STATUSCODE_OK) { |
| 369 return EPERM; |
| 370 } |
| 371 |
| 372 std::string output; |
| 373 error = ReadResponseBody(googledrivefs->ppapi(), |
| 374 url_response_info_resource.pp_resource(), |
| 375 std::numeric_limits<int32_t>::max(), &output); |
| 376 if (error) { |
| 377 return error; |
| 378 } |
| 379 |
| 380 size_t id_index; |
| 381 error = GetValueStringAndValuePos(output, "id", 0, out_item_id, &id_index); |
| 382 if (error == EINVAL) { |
| 383 return ENOENT; |
| 384 } else if (error) { |
| 385 return error; |
| 386 } |
| 387 |
| 388 std::string mime_type_value; |
| 389 size_t mime_type_index; |
| 390 error = GetValueStringAndValuePos(output, "mimeType", 0, &mime_type_value, |
| 391 &mime_type_index); |
| 392 if (error) { |
| 393 return EPERM; |
| 394 } |
| 395 |
| 396 *out_is_dir_type = (mime_type_value == FOLDER_MIME_TYPE); |
| 397 |
| 398 return 0; |
| 399 } |
| 400 |
| 401 Error RequestParentDirId(const Path& path, |
| 402 Filesystem* filesystem, |
| 403 std::string* out_parent_dir_id) { |
| 404 out_parent_dir_id->clear(); |
| 405 |
| 406 if (path.IsRoot()) { |
| 407 return EINVAL; |
| 408 } |
| 409 |
| 410 std::string helper_parent_dir_id = "root"; |
| 411 |
| 412 for (size_t i = 1; i < path.Size() - 1; ++i) { |
| 413 std::string dir_name = path.Range(i, i + 1); |
| 414 std::string dir_id; |
| 415 bool is_dir_type; |
| 416 Error error = RequestItemIdAndItemType(helper_parent_dir_id, dir_name, |
| 417 filesystem, &dir_id, &is_dir_type); |
| 418 if (error || !is_dir_type) { |
| 419 return EPERM; |
| 420 } |
| 421 |
| 422 helper_parent_dir_id = dir_id; |
| 423 } |
| 424 *out_parent_dir_id = helper_parent_dir_id; |
| 425 |
| 426 return 0; |
| 427 } |
| 428 |
| 429 } // namespace nacl_io |
OLD | NEW |