| 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 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_HANDLER_UTIL_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_HANDLER_UTIL_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_HANDLER_UTIL_H_ | 6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_HANDLER_UTIL_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
| 12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/file_browser_handler.h" | 13 #include "chrome/common/extensions/file_browser_handler.h" |
| 14 #include "chrome/common/extensions/url_pattern_set.h" | 14 #include "chrome/common/extensions/url_pattern_set.h" |
| 15 | 15 |
| 16 class Browser; | 16 class Browser; |
| 17 class GURL; | 17 class GURL; |
| 18 class Profile; | 18 class Profile; |
| 19 | 19 |
| 20 namespace extensions { |
| 21 class Extension; |
| 22 } // namespace extensions |
| 23 |
| 20 namespace file_handler_util { | 24 namespace file_handler_util { |
| 21 | 25 |
| 26 // Specifies the task type for a task id that represents some file action, Drive |
| 27 // action, or Web Intent action. |
| 28 enum TaskType { |
| 29 TASK_FILE = 1, |
| 30 TASK_DRIVE, |
| 31 TASK_WEBINTENT |
| 32 }; |
| 33 |
| 34 void UpdateFileHandlerUsageStats(Profile* profile, const std::string& task_id); |
| 35 |
| 22 // Update the default file handler for the given sets of suffixes and MIME | 36 // Update the default file handler for the given sets of suffixes and MIME |
| 23 // types. | 37 // types. |
| 24 void UpdateDefaultTask(Profile* profile, | 38 void UpdateDefaultTask(Profile* profile, |
| 25 const std::string& task_id, | 39 const std::string& task_id, |
| 26 const std::set<std::string>& suffixes, | 40 const std::set<std::string>& suffixes, |
| 27 const std::set<std::string>& mime_types); | 41 const std::set<std::string>& mime_types); |
| 28 | 42 |
| 29 // Returns the task ID of the default task for the given |mime_type|/|suffix| | 43 // Returns the task ID of the default task for the given |mime_type|/|suffix| |
| 30 // combination. If it finds a MIME type match, then it prefers that over a | 44 // combination. If it finds a MIME type match, then it prefers that over a |
| 31 // suffix match. If it a default can't be found, then it returns the empty | 45 // suffix match. If it a default can't be found, then it returns the empty |
| 32 // string. | 46 // string. |
| 33 std::string GetDefaultTaskIdFromPrefs(Profile* profile, | 47 std::string GetDefaultTaskIdFromPrefs(Profile* profile, |
| 34 const std::string& mime_type, | 48 const std::string& mime_type, |
| 35 const std::string& suffix); | 49 const std::string& suffix); |
| 36 | 50 |
| 37 // Gets read-write file access permission flags. | 51 // Gets read-write file access permission flags. |
| 38 int GetReadWritePermissions(); | 52 int GetReadWritePermissions(); |
| 39 // Gets read-only file access permission flags. | 53 // Gets read-only file access permission flags. |
| 40 int GetReadOnlyPermissions(); | 54 int GetReadOnlyPermissions(); |
| 41 | 55 |
| 42 // Generates file task id for the file action specified by the extension. | 56 // Generates task id for the action specified by the extension. |
| 43 std::string MakeTaskID(const std::string& extension_id, | 57 std::string MakeTaskID(const std::string& extension_id, |
| 58 TaskType task_type, |
| 44 const std::string& action_id); | 59 const std::string& action_id); |
| 45 | 60 |
| 46 // Make a task id specific to drive apps instead of extensions. | 61 // Extracts action, type and extension id bound to the file task. Either |
| 47 std::string MakeDriveTaskID(const std::string& app_id, | |
| 48 const std::string& action_id); | |
| 49 | |
| 50 // Returns the |target_id| and |action_id| of a drive app or extension, given | |
| 51 // the drive |task_id| created by MakeDriveTaskID. If the |task_id| is a drive | |
| 52 // task_id then it will return true. If not, or if parsing fails, will return | |
| 53 // false and not set |app_id| or |action_id|. | |
| 54 bool CrackDriveTaskID(const std::string& task_id, | |
| 55 std::string* app_id, | |
| 56 std::string* action_id); | |
| 57 | |
| 58 // Extracts action and extension id bound to the file task. Either | |
| 59 // |target_extension_id| or |action_id| are allowed to be NULL if caller isn't | 62 // |target_extension_id| or |action_id| are allowed to be NULL if caller isn't |
| 60 // interested in those values. Returns false on failure to parse. | 63 // interested in those values. Returns false on failure to parse. |
| 61 bool CrackTaskID(const std::string& task_id, | 64 bool CrackTaskID(const std::string& task_id, |
| 62 std::string* target_extension_id, | 65 std::string* target_extension_id, |
| 66 TaskType* task_type, |
| 63 std::string* action_id); | 67 std::string* action_id); |
| 64 | 68 |
| 65 // This generates a list of default tasks (tasks set as default by the user in | 69 // This generates a list of default tasks (tasks set as default by the user in |
| 66 // prefs) from the |common_tasks|. | 70 // prefs) from the |common_tasks|. |
| 67 void FindDefaultTasks(Profile* profile, | 71 void FindDefaultTasks(Profile* profile, |
| 68 const std::vector<GURL>& files_list, | 72 const std::vector<GURL>& files_list, |
| 69 const std::set<const FileBrowserHandler*>& common_tasks, | 73 const std::set<const FileBrowserHandler*>& common_tasks, |
| 70 std::set<const FileBrowserHandler*>* default_tasks); | 74 std::set<const FileBrowserHandler*>* default_tasks); |
| 71 | 75 |
| 72 // This generates list of tasks common for all files in |file_list|. | 76 // This generates list of tasks common for all files in |file_list|. |
| 73 bool FindCommonTasks(Profile* profile, | 77 bool FindCommonTasks(Profile* profile, |
| 74 const std::vector<GURL>& files_list, | 78 const std::vector<GURL>& files_list, |
| 75 std::set<const FileBrowserHandler*>* common_tasks); | 79 std::set<const FileBrowserHandler*>* common_tasks); |
| 76 | 80 |
| 77 // Finds a task for a file whose URL is |url|. | 81 // Finds a task for a file whose URL is |url|. |
| 78 // Returns default task if one is defined (The default task is the task that is | 82 // Returns default task if one is defined (The default task is the task that is |
| 79 // assigned to file browser task button by default). If default task is not | 83 // assigned to file browser task button by default). If default task is not |
| 80 // found, tries to match the url with one of the builtin tasks. | 84 // found, tries to match the url with one of the builtin tasks. |
| 81 bool GetTaskForURL(Profile* profile, | 85 bool GetTaskForURL(Profile* profile, |
| 82 const GURL& url, | 86 const GURL& url, |
| 83 const FileBrowserHandler** handler); | 87 const FileBrowserHandler** handler); |
| 84 | 88 |
| 85 // Used for returning success or failure from task executions. | 89 // Used for returning success or failure from task executions. |
| 86 typedef base::Callback<void(bool)> FileTaskFinishedCallback; | 90 typedef base::Callback<void(bool)> FileTaskFinishedCallback; |
| 87 | 91 |
| 88 // Helper class for executing file browser file action. | 92 // Helper class for executing file browser file action. |
| 89 class FileTaskExecutor : public base::RefCountedThreadSafe<FileTaskExecutor> { | 93 class FileTaskExecutor : public base::RefCountedThreadSafe<FileTaskExecutor> { |
| 90 public: | 94 public: |
| 91 static const char kDriveTaskExtensionPrefix[]; | |
| 92 static const size_t kDriveTaskExtensionPrefixLength; | |
| 93 | |
| 94 // Creates the appropriate FileTaskExecutor for the given |extension_id|. | 95 // Creates the appropriate FileTaskExecutor for the given |extension_id|. |
| 95 static FileTaskExecutor* Create(Profile* profile, | 96 static FileTaskExecutor* Create(Profile* profile, |
| 96 const GURL source_url, | 97 const GURL source_url, |
| 97 const std::string& extension_id, | 98 const std::string& extension_id, |
| 99 TaskType task_type, |
| 98 const std::string& action_id); | 100 const std::string& action_id); |
| 99 | 101 |
| 100 // Same as ExecuteAndNotify, but no notification is performed. | 102 // Same as ExecuteAndNotify, but no notification is performed. |
| 101 virtual bool Execute(const std::vector<GURL>& file_urls); | 103 virtual bool Execute(const std::vector<GURL>& file_urls); |
| 102 | 104 |
| 103 // Initiates execution of file handler task for each element of |file_urls|. | 105 // Initiates execution of file handler task for each element of |file_urls|. |
| 104 // Return |false| if the execution cannot be initiated. Otherwise returns | 106 // Return |false| if the execution cannot be initiated. Otherwise returns |
| 105 // |true| and then eventually calls |done| when all the files have | 107 // |true| and then eventually calls |done| when all the files have |
| 106 // been handled. If there is an error during processing the list of files, the | 108 // been handled. If there is an error during processing the list of files, the |
| 107 // caller will be informed of the failure via |done|, and the rest of | 109 // caller will be informed of the failure via |done|, and the rest of |
| 108 // the files will not be processed. | 110 // the files will not be processed. |
| 109 virtual bool ExecuteAndNotify(const std::vector<GURL>& file_urls, | 111 virtual bool ExecuteAndNotify(const std::vector<GURL>& file_urls, |
| 110 const FileTaskFinishedCallback& done) = 0; | 112 const FileTaskFinishedCallback& done) = 0; |
| 111 | 113 |
| 112 protected: | 114 protected: |
| 113 explicit FileTaskExecutor(Profile* profile); | 115 explicit FileTaskExecutor(Profile* profile, const std::string& extension_id); |
| 114 virtual ~FileTaskExecutor(); | 116 virtual ~FileTaskExecutor(); |
| 115 | 117 |
| 116 // Returns the profile that this task was created with. | 118 // Returns the profile that this task was created with. |
| 117 Profile* profile() { return profile_; } | 119 Profile* profile() { return profile_; } |
| 118 | 120 |
| 119 // Returns a browser to use for the current browser. | 121 // Returns a browser to use for the current browser. |
| 120 Browser* GetBrowser() const; | 122 Browser* GetBrowser() const; |
| 123 |
| 124 // Returns the extension for this profile. |
| 125 const extensions::Extension* GetExtension(); |
| 126 |
| 127 // Returns the extension ID set for this FileTaskExecutor. |
| 128 const std::string& extension_id() { return extension_id_; } |
| 129 |
| 121 private: | 130 private: |
| 122 friend class base::RefCountedThreadSafe<FileTaskExecutor>; | 131 friend class base::RefCountedThreadSafe<FileTaskExecutor>; |
| 123 | 132 |
| 124 Profile* profile_; | 133 Profile* profile_; |
| 134 const std::string extension_id_; |
| 125 }; | 135 }; |
| 126 | 136 |
| 127 } // namespace file_handler_util | 137 } // namespace file_handler_util |
| 128 | 138 |
| 129 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_HANDLER_UTIL_H_ | 139 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_HANDLER_UTIL_H_ |
| OLD | NEW |