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 PlatformFileErrorToPepperError(base::PlatformFileError error_code) {
     15   switch (error_code) {
     16     case base::PLATFORM_FILE_OK:
     17       return PP_OK;
     18     case base::PLATFORM_FILE_ERROR_EXISTS:
     19       return PP_ERROR_FILEEXISTS;
     20     case base::PLATFORM_FILE_ERROR_NOT_FOUND:
     21       return PP_ERROR_FILENOTFOUND;
     22     case base::PLATFORM_FILE_ERROR_ACCESS_DENIED:
     23     case base::PLATFORM_FILE_ERROR_SECURITY:
     24       return PP_ERROR_NOACCESS;
     25     case base::PLATFORM_FILE_ERROR_NO_MEMORY:
     26       return PP_ERROR_NOMEMORY;
     27     case base::PLATFORM_FILE_ERROR_NO_SPACE:
     28       return PP_ERROR_NOSPACE;
     29     case base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY:
     30       return PP_ERROR_FAILED;
     31     case base::PLATFORM_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   int flags = 0;
     48   if (pp_read)
     49     flags |= base::PLATFORM_FILE_READ;
     50   if (pp_write) {
     51     flags |= base::PLATFORM_FILE_WRITE;
     52     flags |= base::PLATFORM_FILE_WRITE_ATTRIBUTES;
     53   }
     54   if (pp_append) {
     55     if (pp_write)
     56       return false;
     57     flags |= base::PLATFORM_FILE_APPEND;
     58   }
     59 
     60   if (pp_truncate && !pp_write)
     61     return false;
     62 
     63   if (pp_create) {
     64     if (pp_exclusive) {
     65       flags |= base::PLATFORM_FILE_CREATE;
     66     } else if (pp_truncate) {
     67       flags |= base::PLATFORM_FILE_CREATE_ALWAYS;
     68     } else {
     69       flags |= base::PLATFORM_FILE_OPEN_ALWAYS;
     70     }
     71   } else if (pp_truncate) {
     72     flags |= base::PLATFORM_FILE_OPEN_TRUNCATED;
     73   } else {
     74     flags |= base::PLATFORM_FILE_OPEN;
     75   }
     76 
     77   if (flags_out)
     78     *flags_out = flags;
     79   return true;
     80 }
     81 
     82 void PlatformFileInfoToPepperFileInfo(const base::PlatformFileInfo& info,
     83                                       PP_FileSystemType fs_type,
     84                                       PP_FileInfo* info_out) {
     85   DCHECK(info_out);
     86   info_out->size = info.size;
     87   info_out->creation_time = TimeToPPTime(info.creation_time);
     88   info_out->last_access_time = TimeToPPTime(info.last_accessed);
     89   info_out->last_modified_time = TimeToPPTime(info.last_modified);
     90   info_out->system_type = fs_type;
     91   if (info.is_directory) {
     92     info_out->type = PP_FILETYPE_DIRECTORY;
     93   } else if (info.is_symbolic_link) {
     94     DCHECK_EQ(PP_FILESYSTEMTYPE_EXTERNAL, fs_type);
     95     info_out->type = PP_FILETYPE_OTHER;
     96   } else {
     97     info_out->type = PP_FILETYPE_REGULAR;
     98   }
     99 }
    100 
    101 }  // namespace ppapi
    102