OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // File contains the fileBrowserHandlerInternal.selectFile extension function. | 5 // File contains the fileBrowserHandlerInternal.selectFile extension function. |
6 // The function prompts user to select a file path to be used by the caller. It | 6 // The function prompts user to select a file path to be used by the caller. It |
7 // will fail if it isn't invoked by a user gesture (e.g. a mouse click or a | 7 // will fail if it isn't invoked by a user gesture (e.g. a mouse click or a |
8 // keyboard key press). | 8 // keyboard key press). |
9 // Note that the target file is never actually created by this function, even | 9 // Note that the target file is never actually created by this function, even |
10 // if the selected path doesn't exist. | 10 // if the selected path doesn't exist. |
11 | 11 |
12 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_HANDLER_API_H_ | 12 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_HANDLER_API_H_ |
13 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_HANDLER_API_H_ | 13 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_HANDLER_API_H_ |
14 | 14 |
15 #include <string> | 15 #include <string> |
16 #include <vector> | 16 #include <vector> |
17 | 17 |
18 #include "base/file_path.h" | 18 #include "base/file_path.h" |
19 #include "chrome/browser/extensions/extension_function.h" | 19 #include "chrome/browser/extensions/extension_function.h" |
20 | 20 |
21 class Browser; | 21 class Browser; |
22 class FileBrowserHandlerInternalSelectFileFunction; | 22 class FileHandlerSelectFileFunction; |
23 | 23 |
24 namespace file_handler { | 24 namespace file_handler { |
25 | 25 |
26 // Interface that is used by FileBrowserHandlerInternalSelectFileFunction to | 26 // Interface that is used by FileHandlerSelectFileFunction to select the file |
27 // select the file path that should be reported back to the extension function | 27 // path that should be reported back to the extension function caller. |
28 // caller. Nobody will take the ownership of the interface implementation, so | 28 // Nobody will take the ownership of the interface implementation, so it should |
29 // it should delete itself once it's done. | 29 // delete itself once it's done. |
30 class FileSelector { | 30 class FileSelector { |
31 public: | 31 public: |
32 virtual ~FileSelector() {} | 32 virtual ~FileSelector() {} |
33 | 33 |
34 // Starts the file selection. It should prompt user to select a file path. | 34 // Starts the file selection. It should prompt user to select a file path. |
35 // Once the selection is made it should asynchronously call | 35 // Once the selection is made it should asynchronously call |
36 // |function_->OnFilePathSelected| with the selection information. | 36 // |function_->OnFilePathSelected| with the selection information. |
37 // User should be initially suggested to select file named |suggested_name|. | 37 // User should be initially suggested to select file named |suggested_name|. |
38 // |allowed_extensions| specifies the file extensions allowed to be shown, | 38 // |allowed_extensions| specifies the file extensions allowed to be shown, |
39 // and selected. Extensions should not include '.'. This spec comes from | 39 // and selected. Extensions should not include '.'. This spec comes from |
40 // ui::SelectFileDialog() which takes extensions without '.'. | 40 // ui::SelectFileDialog() which takes extensions without '.'. |
41 // | 41 // |
42 // Selection UI should be displayed using |browser|. |browser| should outlive | 42 // Selection UI should be displayed using |browser|. |browser| should outlive |
43 // the interface implementation. | 43 // the interface implementation. |
44 // |function| if the extension function that called the method and needs to | 44 // |function| if the extension function that called the method and needs to |
45 // be notified of user action. The interface implementation should keep a | 45 // be notified of user action. The interface implementation should keep a |
46 // reference to the function until it is notified (extension function | 46 // reference to the function until it is notified (extension function |
47 // implementations are ref counted). | 47 // implementations are ref counted). |
48 // |SelectFile| will be called at most once by a single extension function. | 48 // |SelectFile| will be called at most once by a single extension function. |
49 // The interface implementation should delete itself after the extension | 49 // The interface implementation should delete itself after the extension |
50 // function is notified of file selection result. | 50 // function is notified of file selection result. |
51 virtual void SelectFile( | 51 virtual void SelectFile(const FilePath& suggested_name, |
52 const FilePath& suggested_name, | 52 const std::vector<std::string>& allowed_extensions, |
53 const std::vector<std::string>& allowed_extensions, | 53 Browser* browser, |
54 Browser* browser, | 54 FileHandlerSelectFileFunction* function) = 0; |
55 FileBrowserHandlerInternalSelectFileFunction* function) = 0; | |
56 }; | 55 }; |
57 | 56 |
58 // Interface that is used by FileBrowserHandlerInternalSelectFileFunction to | 57 // Interface that is used by FileHandlerSelectFileFunction to create a |
59 // create a FileSelector it can use to select a file path. | 58 // FileSelector it can use to select a file path. |
60 class FileSelectorFactory { | 59 class FileSelectorFactory { |
61 public: | 60 public: |
62 virtual ~FileSelectorFactory() {} | 61 virtual ~FileSelectorFactory() {} |
63 | 62 |
64 // Creates a FileSelector instance for the | 63 // Creates a FileSelector instance for the FileHandlerSelectFileFunction. |
65 // FileBrowserHandlerInternalSelectFileFunction. | |
66 virtual FileSelector* CreateFileSelector() const = 0; | 64 virtual FileSelector* CreateFileSelector() const = 0; |
67 }; | 65 }; |
68 | 66 |
69 } // namespace file_handler | 67 } // namespace file_handler |
70 | 68 |
71 // The fileBrowserHandlerInternal.selectFile extension function implementation. | 69 // The fileBrowserHandlerInternal.selectFile extension function implementation. |
72 // See the file description for more info. | 70 // See the file description for more info. |
73 class FileBrowserHandlerInternalSelectFileFunction | 71 class FileHandlerSelectFileFunction : public AsyncExtensionFunction { |
74 : public AsyncExtensionFunction { | |
75 public: | 72 public: |
76 // Default constructor used in production code. | 73 // Default constructor used in production code. |
77 // It will create its own FileSelectorFactory implementation, and set the | 74 // It will create its own FileSelectorFactory implementation, and set the |
78 // value of |user_gesture_check_enabled| to true. | 75 // value of |user_gesture_check_enabled| to true. |
79 FileBrowserHandlerInternalSelectFileFunction(); | 76 FileHandlerSelectFileFunction(); |
80 | 77 |
81 // This constructor should be used only in tests to inject test file selector | 78 // This constructor should be used only in tests to inject test file selector |
82 // factory and to allow extension function to run even if it hasn't been | 79 // factory and to allow extension function to run even if it hasn't been |
83 // invoked by user gesture. | 80 // invoked by user gesture. |
84 // Created object will take the ownership of the |file_selector_factory|. | 81 // Created object will take the ownership of the |file_selector_factory|. |
85 FileBrowserHandlerInternalSelectFileFunction( | 82 FileHandlerSelectFileFunction( |
86 file_handler::FileSelectorFactory* file_selector_factory, | 83 file_handler::FileSelectorFactory* file_selector_factory, |
87 bool enable_user_gesture_check); | 84 bool enable_user_gesture_check); |
88 | 85 |
89 // Called by FileSelector implementation when the user selects the file's | 86 // Called by FileSelector implementation when the user selects the file's |
90 // file path. File access permissions for the selected file are granted and | 87 // file path. File access permissions for the selected file are granted and |
91 // caller is notified of the selection result after this method is called. | 88 // caller is notified of the selection result after this method is called. |
92 // |success| Whether the path was selected. | 89 // |success| Whether the path was selected. |
93 // |full_path| The selected file path if one was selected. It is ignored if | 90 // |full_path| The selected file path if one was selected. It is ignored if |
94 // the selection did not succeed. | 91 // the selection did not succeed. |
95 void OnFilePathSelected(bool success, const FilePath& full_path); | 92 void OnFilePathSelected(bool success, const FilePath& full_path); |
96 | 93 |
97 protected: | 94 protected: |
98 // The class is ref counted, so destructor should not be public. | 95 // The class is ref counted, so destructor should not be public. |
99 virtual ~FileBrowserHandlerInternalSelectFileFunction(); | 96 virtual ~FileHandlerSelectFileFunction() OVERRIDE; |
100 | 97 |
101 // AsyncExtensionFunction implementation. | 98 // AsyncExtensionFunction implementation. |
102 // Runs the extension function implementation. | 99 // Runs the extension function implementation. |
103 virtual bool RunImpl() OVERRIDE; | 100 virtual bool RunImpl() OVERRIDE; |
104 | 101 |
105 private: | 102 private: |
106 // Called when the external file system is opened for the extension function | 103 // Called when the external file system is opened for the extension function |
107 // caller in the browser context. It saves opened file system's parameters. | 104 // caller in the browser context. It saves opened file system's parameters. |
108 // The file system is needed to create FileEntry object for the selection | 105 // The file system is needed to create FileEntry object for the selection |
109 // result. | 106 // result. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 GURL file_system_root_; | 152 GURL file_system_root_; |
156 | 153 |
157 // List of permissions and paths that have to be granted for the selected | 154 // List of permissions and paths that have to be granted for the selected |
158 // files. | 155 // files. |
159 std::vector<std::pair<FilePath, int> > permissions_to_grant_; | 156 std::vector<std::pair<FilePath, int> > permissions_to_grant_; |
160 | 157 |
161 DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserHandlerInternal.selectFile"); | 158 DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserHandlerInternal.selectFile"); |
162 }; | 159 }; |
163 | 160 |
164 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_HANDLER_API_H_ | 161 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_BROWSER_HANDLER_API_H_ |
OLD | NEW |