• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 base::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& saveCallback,
62             const SaveCallback& cancelCallback);
63 
64   // Append |content| to the file that has been associated with given |url|.
65   // The |url| can be associated with a file via calling Save method.
66   // If the Save method has not been called for this |url|, then
67   // Append method does nothing.
68   void Append(const std::string& url,
69               const std::string& content,
70               const AppendCallback& callback);
71 
72   // Shows select folder dialog.
73   // If user cancels folder selection, passes empty FileSystem struct to
74   // |callback|.
75   // Shows infobar by means of |show_info_bar_callback| to let the user decide
76   // whether to grant security permissions or not.
77   // If user allows adding file system in infobar, grants renderer read/write
78   // permissions, registers isolated file system for it and passes FileSystem
79   // struct to |callback|. Saves file system path to prefs.
80   // If user denies adding file system in infobar, passes error string to
81   // |callback|.
82   void AddFileSystem(const AddFileSystemCallback& callback,
83                      const ShowInfoBarCallback& show_info_bar_callback);
84 
85   // Upgrades dragged file system permissions to a read-write access.
86   // Shows infobar by means of |show_info_bar_callback| to let the user decide
87   // whether to grant security permissions or not.
88   // If user allows adding file system in infobar, grants renderer read/write
89   // permissions, registers isolated file system for it and passes FileSystem
90   // struct to |callback|. Saves file system path to prefs.
91   // If user denies adding file system in infobar, passes error string to
92   // |callback|.
93   void UpgradeDraggedFileSystemPermissions(
94       const std::string& file_system_url,
95       const AddFileSystemCallback& callback,
96       const ShowInfoBarCallback& show_info_bar_callback);
97 
98   // Loads file system paths from prefs, grants permissions and registers
99   // isolated file system for those of them that contain magic file and passes
100   // FileSystem structs for registered file systems to |callback|.
101   void RequestFileSystems(const RequestFileSystemsCallback& callback);
102 
103   // Removes isolated file system for given |file_system_path|.
104   void RemoveFileSystem(const std::string& file_system_path);
105 
106   // Returns whether access to the folder on given |file_system_path| was
107   // granted.
108   bool IsFileSystemAdded(const std::string& file_system_path);
109 
110  private:
111   void SaveAsFileSelected(const std::string& url,
112                           const std::string& content,
113                           const SaveCallback& callback,
114                           const base::FilePath& path);
115   void SaveAsFileSelectionCanceled(const SaveCallback& callback);
116   void InnerAddFileSystem(
117       const AddFileSystemCallback& callback,
118       const ShowInfoBarCallback& show_info_bar_callback,
119       const base::FilePath& path);
120   void AddUserConfirmedFileSystem(
121       const AddFileSystemCallback& callback,
122       const base::FilePath& path,
123       bool allowed);
124   void RestoreValidatedFileSystems(
125       const RequestFileSystemsCallback& callback,
126       const std::vector<base::FilePath>& file_paths);
127 
128   content::WebContents* web_contents_;
129   Profile* profile_;
130   typedef std::map<std::string, base::FilePath> PathsMap;
131   PathsMap saved_files_;
132   base::WeakPtrFactory<DevToolsFileHelper> weak_factory_;
133   DISALLOW_COPY_AND_ASSIGN(DevToolsFileHelper);
134 };
135 
136 #endif  // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_HELPER_H_
137