• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that can
3 // be found in the LICENSE file.
4 
5 #include "include/cef_file_util.h"
6 
7 #include "include/cef_task.h"
8 
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
12 #include "third_party/zlib/google/zip.h"
13 
14 namespace {
15 
AllowFileIO()16 bool AllowFileIO() {
17   if (CefCurrentlyOn(TID_UI) || CefCurrentlyOn(TID_IO)) {
18     NOTREACHED() << "file IO is not allowed on the current thread";
19     return false;
20   }
21   return true;
22 }
23 
24 }  // namespace
25 
CefCreateDirectory(const CefString & full_path)26 bool CefCreateDirectory(const CefString& full_path) {
27   if (!AllowFileIO())
28     return false;
29   return base::CreateDirectory(full_path);
30 }
31 
CefGetTempDirectory(CefString & temp_dir)32 bool CefGetTempDirectory(CefString& temp_dir) {
33   if (!AllowFileIO())
34     return false;
35   base::FilePath result;
36   if (base::GetTempDir(&result)) {
37     temp_dir = result.value();
38     return true;
39   }
40   return false;
41 }
42 
CefCreateNewTempDirectory(const CefString & prefix,CefString & new_temp_path)43 bool CefCreateNewTempDirectory(const CefString& prefix,
44                                CefString& new_temp_path) {
45   if (!AllowFileIO())
46     return false;
47   base::FilePath result;
48   if (base::CreateNewTempDirectory(prefix, &result)) {
49     new_temp_path = result.value();
50     return true;
51   }
52   return false;
53 }
54 
CefCreateTempDirectoryInDirectory(const CefString & base_dir,const CefString & prefix,CefString & new_dir)55 bool CefCreateTempDirectoryInDirectory(const CefString& base_dir,
56                                        const CefString& prefix,
57                                        CefString& new_dir) {
58   if (!AllowFileIO())
59     return false;
60   base::FilePath result;
61   if (base::CreateTemporaryDirInDir(base_dir, prefix, &result)) {
62     new_dir = result.value();
63     return true;
64   }
65   return false;
66 }
67 
CefDirectoryExists(const CefString & path)68 bool CefDirectoryExists(const CefString& path) {
69   if (!AllowFileIO())
70     return false;
71   return base::DirectoryExists(path);
72 }
73 
CefDeleteFile(const CefString & path,bool recursive)74 bool CefDeleteFile(const CefString& path, bool recursive) {
75   if (!AllowFileIO())
76     return false;
77   if (recursive) {
78     return base::DeletePathRecursively(path);
79   } else {
80     return base::DeleteFile(path);
81   }
82 }
83 
CefZipDirectory(const CefString & src_dir,const CefString & dest_file,bool include_hidden_files)84 bool CefZipDirectory(const CefString& src_dir,
85                      const CefString& dest_file,
86                      bool include_hidden_files) {
87   if (!AllowFileIO())
88     return false;
89   return zip::Zip(src_dir, dest_file, include_hidden_files);
90 }
91