Home | History | Annotate | Download | only in shared_impl
      1 // Copyright (c) 2011 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 "ppapi/shared_impl/file_type_conversion.h"
      6 
      7 #include "base/logging.h"
      8 #include "ppapi/c/pp_errors.h"
      9 #include "ppapi/c/ppb_file_io.h"
     10 #include "ppapi/shared_impl/time_conversion.h"
     11 
     12 namespace ppapi {
     13 
     14 int FileErrorToPepperError(base::File::Error error_code) {
     15   switch (error_code) {
     16     case base::File::FILE_OK:
     17       return PP_OK;
     18     case base::File::FILE_ERROR_EXISTS:
     19       return PP_ERROR_FILEEXISTS;
     20     case base::File::FILE_ERROR_NOT_FOUND:
     21       return PP_ERROR_FILENOTFOUND;
     22     case base::File::FILE_ERROR_ACCESS_DENIED:
     23     case base::File::FILE_ERROR_SECURITY:
     24       return PP_ERROR_NOACCESS;
     25     case base::File::FILE_ERROR_NO_MEMORY:
     26       return PP_ERROR_NOMEMORY;
     27     case base::File::FILE_ERROR_NO_SPACE:
     28       return PP_ERROR_NOSPACE;
     29     case base::File::FILE_ERROR_NOT_A_DIRECTORY:
     30       return PP_ERROR_FAILED;
     31     case base::File::FILE_ERROR_NOT_A_FILE:
     32       return PP_ERROR_NOTAFILE;
     33     default:
     34       return PP_ERROR_FAILED;
     35   }
     36 }
     37 
     38 bool PepperFileOpenFlagsToPlatformFileFlags(int32_t pp_open_flags,
     39                                             int* flags_out) {
     40   bool pp_read = !!(pp_open_flags & PP_FILEOPENFLAG_READ);
     41   bool pp_write = !!(pp_open_flags & PP_FILEOPENFLAG_WRITE);
     42   bool pp_create = !!(pp_open_flags & PP_FILEOPENFLAG_CREATE);
     43   bool pp_truncate = !!(pp_open_flags & PP_FILEOPENFLAG_TRUNCATE);
     44   bool pp_exclusive = !!(pp_open_flags & PP_FILEOPENFLAG_EXCLUSIVE);
     45   bool pp_append = !!(pp_open_flags & PP_FILEOPENFLAG_APPEND);
     46 
     47   // Pepper allows Touch on any open file, so always set this Windows-only flag.
     48   int flags = base::File::FLAG_WRITE_ATTRIBUTES;
     49 
     50   if (pp_read)
     51     flags |= base::File::FLAG_READ;
     52   if (pp_write) {
     53     flags |= base::File::FLAG_WRITE;
     54   }
     55   if (pp_append) {
     56     if (pp_write)
     57       return false;
     58     flags |= base::File::FLAG_APPEND;
     59   }
     60 
     61   if (pp_truncate && !pp_write)
     62     return false;
     63 
     64   if (pp_create) {
     65     if (pp_exclusive) {
     66       flags |= base::File::FLAG_CREATE;
     67     } else if (pp_truncate) {
     68       flags |= base::File::FLAG_CREATE_ALWAYS;
     69     } else {
     70       flags |= base::File::FLAG_OPEN_ALWAYS;
     71     }
     72   } else if (pp_truncate) {
     73     flags |= base::File::FLAG_OPEN_TRUNCATED;
     74   } else {
     75     flags |= base::File::FLAG_OPEN;
     76   }
     77 
     78   if (flags_out)
     79     *flags_out = flags;
     80   return true;
     81 }
     82 
     83 void FileInfoToPepperFileInfo(const base::File::Info& info,
     84                               PP_FileSystemType fs_type,
     85                               PP_FileInfo* info_out) {
     86   DCHECK(info_out);
     87   info_out->size = info.size;
     88   info_out->creation_time = TimeToPPTime(info.creation_time);
     89   info_out->last_access_time = TimeToPPTime(info.last_accessed);
     90   info_out->last_modified_time = TimeToPPTime(info.last_modified);
     91   info_out->system_type = fs_type;
     92   if (info.is_directory) {
     93     info_out->type = PP_FILETYPE_DIRECTORY;
     94   } else if (info.is_symbolic_link) {
     95     DCHECK_EQ(PP_FILESYSTEMTYPE_EXTERNAL, fs_type);
     96     info_out->type = PP_FILETYPE_OTHER;
     97   } else {
     98     info_out->type = PP_FILETYPE_REGULAR;
     99   }
    100 }
    101 
    102 }  // namespace ppapi
    103