1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_HELPER_H_ 6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_HELPER_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/callback.h" 14 #include "base/memory/ref_counted.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/strings/string16.h" 17 18 class Profile; 19 20 namespace base { 21 class FilePath; 22 } 23 24 namespace content { 25 class WebContents; 26 } 27 28 class DevToolsFileHelper { 29 public: 30 struct FileSystem { 31 FileSystem(); 32 FileSystem(const std::string& file_system_name, 33 const std::string& root_url, 34 const std::string& file_system_path); 35 36 std::string file_system_name; 37 std::string root_url; 38 std::string file_system_path; 39 }; 40 41 DevToolsFileHelper(content::WebContents* web_contents, Profile* profile); 42 ~DevToolsFileHelper(); 43 44 typedef base::Callback<void(void)> SaveCallback; 45 typedef base::Callback<void(void)> AppendCallback; 46 typedef base::Callback< 47 void(const std::vector<DevToolsFileHelper::FileSystem>&)> 48 RequestFileSystemsCallback; 49 typedef base::Callback<void(const DevToolsFileHelper::FileSystem&)> 50 AddFileSystemCallback; 51 typedef base::Callback<void(const string16&, 52 const base::Callback<void(bool)>&)> 53 ShowInfoBarCallback; 54 55 // Saves |content| to the file and associates its path with given |url|. 56 // If client is calling this method with given |url| for the first time 57 // or |save_as| is true, confirmation dialog is shown to the user. 58 void Save(const std::string& url, 59 const std::string& content, 60 bool save_as, 61 const SaveCallback& callback); 62 63 // Append |content| to the file that has been associated with given |url|. 64 // The |url| can be associated with a file via calling Save method. 65 // If the Save method has not been called for this |url|, then 66 // Append method does nothing. 67 void Append(const std::string& url, 68 const std::string& content, 69 const AppendCallback& callback); 70 71 // Shows select folder dialog. 72 // If user cancels folder selection, passes empty FileSystem struct to 73 // |callback|. 74 // If selected folder contains magic file, grants renderer read/write 75 // permissions, registers isolated file system for it and passes FileSystem 76 // struct to |callback|. Saves file system path to prefs. 77 // If selected folder does not contain magic file, passes error string to 78 // |callback|. 79 void AddFileSystem(const AddFileSystemCallback& callback, 80 const ShowInfoBarCallback& show_info_bar_callback); 81 82 // Loads file system paths from prefs, grants permissions and registers 83 // isolated file system for those of them that contain magic file and passes 84 // FileSystem structs for registered file systems to |callback|. 85 void RequestFileSystems(const RequestFileSystemsCallback& callback); 86 87 // Removes isolated file system for given |file_system_path|. 88 void RemoveFileSystem(const std::string& file_system_path); 89 90 // Returns whether access to the folder on given |file_system_path| was 91 // granted. 92 bool IsFileSystemAdded(const std::string& file_system_path); 93 94 private: 95 void SaveAsFileSelected(const std::string& url, 96 const std::string& content, 97 const SaveCallback& callback, 98 const base::FilePath& path); 99 void SaveAsFileSelectionCanceled(); 100 void InnerAddFileSystem( 101 const AddFileSystemCallback& callback, 102 const ShowInfoBarCallback& show_info_bar_callback, 103 const base::FilePath& path); 104 void AddUserConfirmedFileSystem( 105 const AddFileSystemCallback& callback, 106 const base::FilePath& path, 107 bool allowed); 108 void RestoreValidatedFileSystems( 109 const RequestFileSystemsCallback& callback, 110 const std::vector<base::FilePath>& file_paths); 111 112 content::WebContents* web_contents_; 113 Profile* profile_; 114 base::WeakPtrFactory<DevToolsFileHelper> weak_factory_; 115 typedef std::map<std::string, base::FilePath> PathsMap; 116 PathsMap saved_files_; 117 DISALLOW_COPY_AND_ASSIGN(DevToolsFileHelper); 118 }; 119 120 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_HELPER_H_ 121