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