summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/devtools/devtools_file_helper.h
blob: cb37572c0b81da39ed8860a40f332975e9fe18f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_HELPER_H_
#define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_HELPER_H_

#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
#include "chrome/browser/devtools/devtools_file_watcher.h"
#include "components/prefs/pref_change_registrar.h"

class Profile;

namespace base {
class FilePath;
class SequencedTaskRunner;
}

namespace content {
class WebContents;
}

class DevToolsFileHelper {
 public:
  struct FileSystem {
    FileSystem();
    FileSystem(const std::string& file_system_name,
               const std::string& root_url,
               const std::string& file_system_path);

    std::string file_system_name;
    std::string root_url;
    std::string file_system_path;
  };

  class Delegate {
   public:
    virtual ~Delegate() {}
    virtual void FileSystemAdded(const FileSystem& file_system) = 0;
    virtual void FileSystemRemoved(const std::string& file_system_path) = 0;
    virtual void FilePathsChanged(
        const std::vector<std::string>& changed_paths,
        const std::vector<std::string>& added_paths,
        const std::vector<std::string>& removed_paths) = 0;
  };

  DevToolsFileHelper(content::WebContents* web_contents, Profile* profile,
                     Delegate* delegate);
  ~DevToolsFileHelper();

  typedef base::Callback<void(void)> SaveCallback;
  typedef base::Callback<void(void)> AppendCallback;
  typedef base::Callback<void(const base::string16&,
                              const base::Callback<void(bool)>&)>
      ShowInfoBarCallback;

  // Saves |content| to the file and associates its path with given |url|.
  // If client is calling this method with given |url| for the first time
  // or |save_as| is true, confirmation dialog is shown to the user.
  void Save(const std::string& url,
            const std::string& content,
            bool save_as,
            const SaveCallback& saveCallback,
            const SaveCallback& cancelCallback);

  // Append |content| to the file that has been associated with given |url|.
  // The |url| can be associated with a file via calling Save method.
  // If the Save method has not been called for this |url|, then
  // Append method does nothing.
  void Append(const std::string& url,
              const std::string& content,
              const AppendCallback& callback);

  // 1. If empty |file_system_path| is passed, shows select folder dialog.
  //    If user cancels folder selection, passes empty FileSystem struct to
  //    |callback|.
  //    If non-empty |file_system_path| is passed, verifies that corresponding
  //    folder contains the ".devtools" project file, if not, passes empty
  //    FileSystem struct to |callback|.
  // 2. Shows infobar by means of |show_info_bar_callback| to let the user
  //    decide whether to grant security permissions or not.
  //    If user allows adding file system in infobar, grants renderer
  //    read/write permissions, registers isolated file system for it and
  //    passes FileSystem struct to |callback|. Saves file system path to prefs.
  //    If user denies adding file system in infobar, passes error string to
  //    |callback|.
  void AddFileSystem(const std::string& file_system_path,
                     const ShowInfoBarCallback& show_info_bar_callback);

  // Upgrades dragged file system permissions to a read-write access.
  // Shows infobar by means of |show_info_bar_callback| to let the user decide
  // whether to grant security permissions or not.
  // If user allows adding file system in infobar, grants renderer read/write
  // permissions, registers isolated file system for it and passes FileSystem
  // struct to |callback|. Saves file system path to prefs.
  // If user denies adding file system in infobar, passes error string to
  // |callback|.
  void UpgradeDraggedFileSystemPermissions(
      const std::string& file_system_url,
      const ShowInfoBarCallback& show_info_bar_callback);

  // Loads file system paths from prefs, grants permissions and registers
  // isolated file system for those of them that contain magic file and passes
  // FileSystem structs for registered file systems to |callback|.
  std::vector<FileSystem> GetFileSystems();

  // Removes isolated file system for given |file_system_path|.
  void RemoveFileSystem(const std::string& file_system_path);

  // Returns whether access to the folder on given |file_system_path| was
  // granted.
  bool IsFileSystemAdded(const std::string& file_system_path);

 private:
  void SaveAsFileSelected(const std::string& url,
                          const std::string& content,
                          const SaveCallback& callback,
                          const base::FilePath& path);
  void InnerAddFileSystem(
      const ShowInfoBarCallback& show_info_bar_callback,
      const base::FilePath& path);
  void AddUserConfirmedFileSystem(
      const base::FilePath& path,
      bool allowed);
  void FileSystemPathsSettingChanged();
  void FilePathsChanged(const std::vector<std::string>& changed_paths,
                        const std::vector<std::string>& added_paths,
                        const std::vector<std::string>& removed_paths);

  // This should only be called on the file sequence.
  static void CheckProjectFileExistsAndAddFileSystem(
      base::WeakPtr<DevToolsFileHelper> self,
      ShowInfoBarCallback show_info_bar_callback,
      base::FilePath path);

  content::WebContents* web_contents_;
  Profile* profile_;
  DevToolsFileHelper::Delegate* delegate_;
  typedef std::map<std::string, base::FilePath> PathsMap;
  PathsMap saved_files_;
  PrefChangeRegistrar pref_change_registrar_;
  std::set<std::string> file_system_paths_;
  std::unique_ptr<DevToolsFileWatcher, DevToolsFileWatcher::Deleter>
      file_watcher_;
  scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
  base::WeakPtrFactory<DevToolsFileHelper> weak_factory_;
  DISALLOW_COPY_AND_ASSIGN(DevToolsFileHelper);
};

#endif  // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_HELPER_H_