[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/dom_ui/filebrowse_ui.h" |
| 6 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 7 | #include <algorithm> |
| 8 | #include <vector> |
| 9 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | ba816424 | 2010-11-16 21:31:00 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 12 | #include "base/file_util.h" |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 13 | #include "base/logging.h" |
| 14 | #include "base/message_loop.h" |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 15 | #include "base/path_service.h" |
[email protected] | 5c4c740 | 2009-10-30 19:58:06 | [diff] [blame] | 16 | #include "base/singleton.h" |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 17 | #include "base/string_piece.h" |
| 18 | #include "base/string_util.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 19 | #include "base/threading/thread.h" |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 20 | #include "base/time.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 21 | #include "base/utf_string_conversions.h" |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 22 | #include "base/values.h" |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 23 | #include "base/weak_ptr.h" |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 24 | #include "chrome/browser/bookmarks/bookmark_model.h" |
[email protected] | facac153 | 2010-10-11 22:59:50 | [diff] [blame] | 25 | #include "chrome/browser/browser_thread.h" |
[email protected] | c848d3d9 | 2010-09-16 21:57:45 | [diff] [blame] | 26 | #include "chrome/browser/dom_ui/mediaplayer_ui.h" |
[email protected] | e0813a39 | 2011-02-01 22:57:25 | [diff] [blame] | 27 | #include "chrome/browser/dom_ui/web_ui_favicon_source.h" |
[email protected] | 7598661 | 2010-07-16 22:23:57 | [diff] [blame] | 28 | #include "chrome/browser/download/download_item.h" |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 29 | #include "chrome/browser/download/download_manager.h" |
| 30 | #include "chrome/browser/download/download_util.h" |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 31 | #include "chrome/browser/history/history_types.h" |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 32 | #include "chrome/browser/metrics/user_metrics.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 33 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 34 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | c848d3d9 | 2010-09-16 21:57:45 | [diff] [blame] | 35 | #include "chrome/browser/tabs/tab_strip_model.h" |
[email protected] | 2ad4a90 | 2010-11-17 06:05:13 | [diff] [blame] | 36 | #include "chrome/browser/ui/browser.h" |
| 37 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 339d6dd | 2010-11-12 00:41:58 | [diff] [blame] | 38 | #include "chrome/browser/ui/browser_navigator.h" |
[email protected] | 2ad4a90 | 2010-11-17 06:05:13 | [diff] [blame] | 39 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 40 | #include "chrome/common/chrome_paths.h" |
[email protected] | ba816424 | 2010-11-16 21:31:00 | [diff] [blame] | 41 | #include "chrome/common/chrome_switches.h" |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 42 | #include "chrome/common/jstemplate_builder.h" |
[email protected] | 68d2a05f | 2010-05-07 21:39:55 | [diff] [blame] | 43 | #include "chrome/common/net/url_fetcher.h" |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 44 | #include "chrome/common/time_format.h" |
| 45 | #include "chrome/common/url_constants.h" |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 46 | #include "grit/browser_resources.h" |
| 47 | #include "grit/chromium_strings.h" |
| 48 | #include "grit/generated_resources.h" |
| 49 | #include "grit/locale_settings.h" |
[email protected] | c848d3d9 | 2010-09-16 21:57:45 | [diff] [blame] | 50 | #include "net/base/escape.h" |
[email protected] | df53d2d | 2010-11-19 08:56:12 | [diff] [blame] | 51 | #include "net/url_request/url_request_file_job.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 52 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 42ce29d | 2011-01-20 23:19:46 | [diff] [blame] | 53 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 54 | |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 55 | #if defined(OS_CHROMEOS) |
[email protected] | 62c7ef3 | 2010-03-23 23:44:24 | [diff] [blame] | 56 | #include "chrome/browser/chromeos/cros/cros_library.h" |
[email protected] | 268b02f | 2010-02-04 21:07:15 | [diff] [blame] | 57 | #include "chrome/browser/chromeos/cros/mount_library.h" |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 58 | #include "chrome/browser/chromeos/login/user_manager.h" |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 59 | #endif |
| 60 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 61 | // Maximum number of search results to return in a given search. We should |
| 62 | // eventually remove this. |
| 63 | static const int kMaxSearchResults = 100; |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 64 | static const char kPropertyPath[] = "path"; |
| 65 | static const char kPropertyTitle[] = "title"; |
| 66 | static const char kPropertyDirectory[] = "isDirectory"; |
| 67 | static const char kPicasawebUserPrefix[] = |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 68 | "http://picasaweb.google.com/data/feed/api/user/"; |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 69 | static const char kPicasawebDefault[] = "/albumid/default"; |
| 70 | static const char kPicasawebDropBox[] = "/home"; |
| 71 | static const char kPicasawebBaseUrl[] = "http://picasaweb.google.com/"; |
| 72 | static const char kMediaPath[] = "/media"; |
| 73 | static const char kFilebrowseURLHash[] = "chrome://filebrowse#"; |
[email protected] | f5bf8ccf | 2010-02-05 18:19:25 | [diff] [blame] | 74 | static const int kPopupLeft = 0; |
| 75 | static const int kPopupTop = 0; |
[email protected] | f5bf8ccf | 2010-02-05 18:19:25 | [diff] [blame] | 76 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 77 | class FileBrowseUIHTMLSource : public ChromeURLDataManager::DataSource { |
| 78 | public: |
| 79 | FileBrowseUIHTMLSource(); |
| 80 | |
| 81 | // Called when the network layer has requested a resource underneath |
| 82 | // the path we registered. |
[email protected] | f09d9379 | 2009-11-17 00:10:36 | [diff] [blame] | 83 | virtual void StartDataRequest(const std::string& path, |
| 84 | bool is_off_the_record, |
| 85 | int request_id); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 86 | virtual std::string GetMimeType(const std::string&) const { |
| 87 | return "text/html"; |
| 88 | } |
| 89 | |
| 90 | private: |
[email protected] | 8de85a6 | 2009-11-06 08:32:17 | [diff] [blame] | 91 | ~FileBrowseUIHTMLSource() {} |
| 92 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 93 | DISALLOW_COPY_AND_ASSIGN(FileBrowseUIHTMLSource); |
| 94 | }; |
| 95 | |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 96 | class TaskProxy; |
| 97 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 98 | // The handler for Javascript messages related to the "filebrowse" view. |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 99 | class FilebrowseHandler : public net::DirectoryLister::DirectoryListerDelegate, |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 100 | public WebUIMessageHandler, |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 101 | #if defined(OS_CHROMEOS) |
| 102 | public chromeos::MountLibrary::Observer, |
| 103 | #endif |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 104 | public base::SupportsWeakPtr<FilebrowseHandler>, |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 105 | public URLFetcher::Delegate, |
| 106 | public DownloadManager::Observer, |
| 107 | public DownloadItem::Observer { |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 108 | public: |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 109 | FilebrowseHandler(); |
| 110 | virtual ~FilebrowseHandler(); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 111 | |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 112 | // Init work after Attach. |
| 113 | void Init(); |
| 114 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 115 | // DirectoryLister::DirectoryListerDelegate methods: |
[email protected] | 9ee2c8d | 2010-08-21 23:25:22 | [diff] [blame] | 116 | virtual void OnListFile( |
| 117 | const net::DirectoryLister::DirectoryListerData& data); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 118 | virtual void OnListDone(int error); |
| 119 | |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 120 | // WebUIMessageHandler implementation. |
| 121 | virtual WebUIMessageHandler* Attach(DOMUI* dom_ui); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 122 | virtual void RegisterMessages(); |
| 123 | |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 124 | #if defined(OS_CHROMEOS) |
| 125 | void MountChanged(chromeos::MountLibrary* obj, |
| 126 | chromeos::MountEventType evt, |
| 127 | const std::string& path); |
| 128 | #endif |
| 129 | |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 130 | // DownloadItem::Observer interface |
| 131 | virtual void OnDownloadUpdated(DownloadItem* download); |
[email protected] | ebdd201 | 2011-01-19 11:06:57 | [diff] [blame] | 132 | virtual void OnDownloadFileCompleted(DownloadItem* download); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 133 | virtual void OnDownloadOpened(DownloadItem* download) { } |
| 134 | |
| 135 | // DownloadManager::Observer interface |
| 136 | virtual void ModelChanged(); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 137 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 138 | // Callback for the "getRoots" message. |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 139 | void HandleGetRoots(const ListValue* args); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 140 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 141 | void GetChildrenForPath(const FilePath& path, bool is_refresh); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 142 | |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 143 | void OnURLFetchComplete(const URLFetcher* source, |
| 144 | const GURL& url, |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 145 | const net::URLRequestStatus& status, |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 146 | int response_code, |
| 147 | const ResponseCookies& cookies, |
| 148 | const std::string& data); |
| 149 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 150 | // Callback for the "getChildren" message. |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 151 | void HandleGetChildren(const ListValue* args); |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 152 | // Callback for the "refreshDirectory" message. |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 153 | void HandleRefreshDirectory(const ListValue* args); |
| 154 | void HandleIsAdvancedEnabled(const ListValue* args); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 155 | |
| 156 | // Callback for the "getMetadata" message. |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 157 | void HandleGetMetadata(const ListValue* args); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 158 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 159 | // Callback for the "openNewWindow" message. |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 160 | void OpenNewFullWindow(const ListValue* args); |
| 161 | void OpenNewPopupWindow(const ListValue* args); |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 162 | |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 163 | // Callback for the "uploadToPicasaweb" message. |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 164 | void UploadToPicasaweb(const ListValue* args); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 165 | |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 166 | // Callback for the "getDownloads" message. |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 167 | void HandleGetDownloads(const ListValue* args); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 168 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 169 | void HandleCreateNewFolder(const ListValue* args); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 170 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 171 | void PlayMediaFile(const ListValue* args); |
| 172 | void EnqueueMediaFile(const ListValue* args); |
[email protected] | 17496bb | 2010-03-10 19:48:23 | [diff] [blame] | 173 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 174 | void HandleDeleteFile(const ListValue* args); |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 175 | void HandleCopyFile(const ListValue* value); |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 176 | void CopyFile(const FilePath& src, const FilePath& dest, TaskProxy* task); |
| 177 | void DeleteFile(const FilePath& path, TaskProxy* task); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 178 | void FireDeleteComplete(const FilePath& path); |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 179 | void FireCopyComplete(const FilePath& src, const FilePath& dest); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 180 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 181 | void HandlePauseToggleDownload(const ListValue* args); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 182 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 183 | void HandleCancelDownload(const ListValue* args); |
| 184 | void HandleAllowDownload(const ListValue* args); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 185 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 186 | void CreateNewFolder(const FilePath& path) const; |
| 187 | |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 188 | void ReadInFile(); |
| 189 | void FireUploadComplete(); |
| 190 | |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 191 | void SendPicasawebRequest(); |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 192 | |
| 193 | // Callback for the "validateSavePath" message. |
| 194 | void HandleValidateSavePath(const ListValue* args); |
| 195 | |
| 196 | // Validate a save path on file thread. |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 197 | void ValidateSavePathOnFileThread(const FilePath& save_path, TaskProxy* task); |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 198 | |
| 199 | // Fire save path validation result to JS onValidatedSavePath. |
| 200 | void FireOnValidatedSavePathOnUIThread(bool valid, const FilePath& save_path); |
| 201 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 202 | private: |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 203 | |
[email protected] | ebdd201 | 2011-01-19 11:06:57 | [diff] [blame] | 204 | // Retrieves downloads from the DownloadManager and updates the page. |
| 205 | void UpdateDownloadList(); |
| 206 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 207 | void OpenNewWindow(const ListValue* args, bool popup); |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 208 | |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 209 | // Clear all download items and their observers. |
| 210 | void ClearDownloadItems(); |
| 211 | |
| 212 | // Send the current list of downloads to the page. |
| 213 | void SendCurrentDownloads(); |
| 214 | |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 215 | void SendNewDownload(DownloadItem* download); |
| 216 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 217 | bool ValidateSaveDir(const FilePath& save_dir, bool exists) const; |
| 218 | bool AccessDisabled(const FilePath& path) const; |
| 219 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 220 | scoped_ptr<ListValue> filelist_value_; |
| 221 | FilePath currentpath_; |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 222 | Profile* profile_; |
[email protected] | 93a58bf7 | 2010-06-04 23:01:20 | [diff] [blame] | 223 | TabContents* tab_contents_; |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 224 | std::string current_file_contents_; |
| 225 | std::string current_file_uploaded_; |
| 226 | int upload_response_code_; |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 227 | TaskProxy* current_task_; |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 228 | scoped_refptr<net::DirectoryLister> lister_; |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 229 | bool is_refresh_; |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 230 | scoped_ptr<URLFetcher> fetch_; |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 231 | |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 232 | DownloadManager* download_manager_; |
| 233 | typedef std::vector<DownloadItem*> DownloadList; |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 234 | DownloadList active_download_items_; |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 235 | DownloadList download_items_; |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 236 | bool got_first_download_list_; |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 237 | DISALLOW_COPY_AND_ASSIGN(FilebrowseHandler); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 238 | }; |
| 239 | |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 240 | class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { |
| 241 | public: |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 242 | TaskProxy(const base::WeakPtr<FilebrowseHandler>& handler, |
| 243 | const FilePath& path, const FilePath& dest) |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 244 | : handler_(handler), |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 245 | src_(path), |
| 246 | dest_(dest) {} |
| 247 | TaskProxy(const base::WeakPtr<FilebrowseHandler>& handler, |
| 248 | const FilePath& path) |
| 249 | : handler_(handler), |
| 250 | src_(path) {} |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 251 | |
| 252 | // TaskProxy is created on the UI thread, so in some cases, |
| 253 | // we need to post back to the UI thread for destruction. |
| 254 | void DeleteOnUIThread() { |
| 255 | BrowserThread::PostTask( |
| 256 | BrowserThread::UI, FROM_HERE, |
| 257 | NewRunnableMethod(this, &TaskProxy::DoNothing)); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 258 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 259 | |
| 260 | void DoNothing() {} |
| 261 | |
| 262 | void ReadInFileProxy() { |
| 263 | if (handler_) |
| 264 | handler_->ReadInFile(); |
| 265 | DeleteOnUIThread(); |
| 266 | } |
| 267 | |
[email protected] | 068a71f | 2010-05-11 22:35:48 | [diff] [blame] | 268 | void DeleteFetcher(URLFetcher* fetch) { |
| 269 | delete fetch; |
| 270 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 271 | |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 272 | void SendPicasawebRequestProxy() { |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 273 | if (handler_) |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 274 | handler_->SendPicasawebRequest(); |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 275 | DeleteOnUIThread(); |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 276 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 277 | |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 278 | void FireUploadCompleteProxy() { |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 279 | if (handler_) |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 280 | handler_->FireUploadComplete(); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 281 | } |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 282 | |
| 283 | void DeleteFileProxy() { |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 284 | if (handler_) |
| 285 | handler_->DeleteFile(src_, this); |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void CopyFileProxy() { |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 289 | if (handler_) |
| 290 | handler_->CopyFile(src_, dest_, this); |
| 291 | } |
| 292 | |
| 293 | void CreateNewFolderProxy() { |
| 294 | if (handler_) |
| 295 | handler_->CreateNewFolder(src_); |
| 296 | DeleteOnUIThread(); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void FireDeleteCompleteProxy() { |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 300 | if (handler_) |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 301 | handler_->FireDeleteComplete(src_); |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 302 | } |
| 303 | void FireCopyCompleteProxy() { |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 304 | if (handler_) |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 305 | handler_->FireCopyComplete(src_, dest_); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 306 | } |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 307 | |
| 308 | void ValidateSavePathOnFileThread() { |
| 309 | if (handler_) |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 310 | handler_->ValidateSavePathOnFileThread(src_, this); |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 311 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 312 | |
| 313 | void FireOnValidatedSavePathOnUIThread(bool valid) { |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 314 | if (handler_) |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 315 | handler_->FireOnValidatedSavePathOnUIThread(valid, src_); |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 316 | } |
| 317 | |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 318 | private: |
| 319 | base::WeakPtr<FilebrowseHandler> handler_; |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 320 | FilePath src_; |
| 321 | FilePath dest_; |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 322 | friend class base::RefCountedThreadSafe<TaskProxy>; |
[email protected] | 068a71f | 2010-05-11 22:35:48 | [diff] [blame] | 323 | DISALLOW_COPY_AND_ASSIGN(TaskProxy); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 327 | //////////////////////////////////////////////////////////////////////////////// |
| 328 | // |
| 329 | // FileBrowseHTMLSource |
| 330 | // |
| 331 | //////////////////////////////////////////////////////////////////////////////// |
| 332 | |
| 333 | FileBrowseUIHTMLSource::FileBrowseUIHTMLSource() |
| 334 | : DataSource(chrome::kChromeUIFileBrowseHost, MessageLoop::current()) { |
| 335 | } |
| 336 | |
| 337 | void FileBrowseUIHTMLSource::StartDataRequest(const std::string& path, |
[email protected] | e42ae9b | 2010-03-28 00:55:05 | [diff] [blame] | 338 | bool is_off_the_record, |
| 339 | int request_id) { |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 340 | DictionaryValue localized_strings; |
[email protected] | 11f485728 | 2009-11-13 19:56:17 | [diff] [blame] | 341 | // TODO(dhg): Add stirings to localized strings, also add more strings |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 342 | // that are currently hardcoded. |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 343 | localized_strings.SetString("title", |
| 344 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_TITLE)); |
| 345 | localized_strings.SetString("pause", |
| 346 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_PAUSE)); |
| 347 | localized_strings.SetString("resume", |
| 348 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_RESUME)); |
| 349 | localized_strings.SetString("scanning", |
| 350 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_SCANNING)); |
| 351 | localized_strings.SetString("confirmdelete", |
| 352 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_DELETE)); |
| 353 | localized_strings.SetString("confirmyes", |
| 354 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_YES)); |
| 355 | localized_strings.SetString("confirmcancel", |
| 356 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_CANCEL)); |
| 357 | localized_strings.SetString("allowdownload", |
| 358 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_DOWNLOAD)); |
| 359 | localized_strings.SetString("filenameprompt", |
| 360 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_PROMPT_FILENAME)); |
| 361 | localized_strings.SetString("save", |
| 362 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_SAVE)); |
| 363 | localized_strings.SetString("newfolder", |
| 364 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_NEW_FOLDER)); |
| 365 | localized_strings.SetString("open", |
| 366 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_OPEN)); |
| 367 | localized_strings.SetString("picasaweb", |
| 368 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_PICASAWEB)); |
| 369 | localized_strings.SetString("flickr", |
| 370 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_FLICKR)); |
| 371 | localized_strings.SetString("email", |
| 372 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_EMAIL)); |
| 373 | localized_strings.SetString("delete", |
| 374 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_DELETE)); |
| 375 | localized_strings.SetString("enqueue", |
| 376 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_ENQUEUE)); |
| 377 | localized_strings.SetString("mediapath", kMediaPath); |
[email protected] | 88f03da0 | 2010-06-10 16:21:13 | [diff] [blame] | 378 | FilePath default_download_path; |
| 379 | if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, |
| 380 | &default_download_path)) { |
| 381 | NOTREACHED(); |
| 382 | } |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 383 | // TODO(viettrungluu): this is wrong -- FilePath's need not be Unicode. |
| 384 | localized_strings.SetString("downloadpath", default_download_path.value()); |
| 385 | localized_strings.SetString("error_unknown_file_type", |
| 386 | l10n_util::GetStringUTF16(IDS_FILEBROWSER_ERROR_UNKNOWN_FILE_TYPE)); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 387 | SetFontAndTextDirection(&localized_strings); |
| 388 | |
| 389 | static const base::StringPiece filebrowse_html( |
| 390 | ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 391 | IDR_FILEBROWSE_HTML)); |
| 392 | const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( |
| 393 | filebrowse_html, &localized_strings); |
| 394 | |
| 395 | scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 396 | html_bytes->data.resize(full_html.size()); |
| 397 | std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
| 398 | |
| 399 | SendResponse(request_id, html_bytes); |
| 400 | } |
| 401 | |
| 402 | //////////////////////////////////////////////////////////////////////////////// |
| 403 | // |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 404 | // FilebrowseHandler |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 405 | // |
| 406 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 407 | FilebrowseHandler::FilebrowseHandler() |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 408 | : profile_(NULL), |
[email protected] | 93a58bf7 | 2010-06-04 23:01:20 | [diff] [blame] | 409 | tab_contents_(NULL), |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 410 | is_refresh_(false), |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 411 | fetch_(NULL), |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 412 | download_manager_(NULL), |
| 413 | got_first_download_list_(false) { |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 414 | lister_ = NULL; |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 415 | #if defined(OS_CHROMEOS) |
[email protected] | 62c7ef3 | 2010-03-23 23:44:24 | [diff] [blame] | 416 | chromeos::MountLibrary* lib = |
| 417 | chromeos::CrosLibrary::Get()->GetMountLibrary(); |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 418 | lib->AddObserver(this); |
| 419 | #endif |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 420 | } |
| 421 | |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 422 | FilebrowseHandler::~FilebrowseHandler() { |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 423 | #if defined(OS_CHROMEOS) |
[email protected] | 62c7ef3 | 2010-03-23 23:44:24 | [diff] [blame] | 424 | chromeos::MountLibrary* lib = |
| 425 | chromeos::CrosLibrary::Get()->GetMountLibrary(); |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 426 | lib->RemoveObserver(this); |
| 427 | #endif |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 428 | if (lister_.get()) { |
| 429 | lister_->Cancel(); |
| 430 | lister_->set_delegate(NULL); |
| 431 | } |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 432 | |
| 433 | ClearDownloadItems(); |
| 434 | download_manager_->RemoveObserver(this); |
[email protected] | 068a71f | 2010-05-11 22:35:48 | [diff] [blame] | 435 | URLFetcher* fetch = fetch_.release(); |
| 436 | if (fetch) { |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 437 | scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), currentpath_); |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 438 | BrowserThread::PostTask( |
| 439 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 068a71f | 2010-05-11 22:35:48 | [diff] [blame] | 440 | NewRunnableMethod( |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 441 | task.get(), &TaskProxy::DeleteFetcher, fetch)); |
[email protected] | 068a71f | 2010-05-11 22:35:48 | [diff] [blame] | 442 | } |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 445 | WebUIMessageHandler* FilebrowseHandler::Attach(DOMUI* dom_ui) { |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 446 | // Create our favicon data source. |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 447 | profile_ = dom_ui->GetProfile(); |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 448 | profile_->GetChromeURLDataManager()->AddDataSource( |
| 449 | new WebUIFavIconSource(profile_)); |
[email protected] | 93a58bf7 | 2010-06-04 23:01:20 | [diff] [blame] | 450 | tab_contents_ = dom_ui->tab_contents(); |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 451 | return WebUIMessageHandler::Attach(dom_ui); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 452 | } |
| 453 | |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 454 | void FilebrowseHandler::Init() { |
[email protected] | f7e9fd6 | 2010-09-28 15:45:06 | [diff] [blame] | 455 | download_manager_ = profile_->GetDownloadManager(); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 456 | download_manager_->AddObserver(this); |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 457 | static bool sent_request = false; |
| 458 | if (!sent_request) { |
| 459 | // If we have not sent a request before, we should do one in order to |
| 460 | // ensure that we have the correct cookies. This is for uploads. |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 461 | scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), currentpath_); |
| 462 | current_task_ = task; |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 463 | BrowserThread::PostTask( |
| 464 | BrowserThread::FILE, FROM_HERE, |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 465 | NewRunnableMethod( |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 466 | task.get(), &TaskProxy::SendPicasawebRequestProxy)); |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 467 | sent_request = true; |
| 468 | } |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 469 | } |
| 470 | |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 471 | void FilebrowseHandler::RegisterMessages() { |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 472 | dom_ui_->RegisterMessageCallback("getRoots", |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 473 | NewCallback(this, &FilebrowseHandler::HandleGetRoots)); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 474 | dom_ui_->RegisterMessageCallback("getChildren", |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 475 | NewCallback(this, &FilebrowseHandler::HandleGetChildren)); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 476 | dom_ui_->RegisterMessageCallback("getMetadata", |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 477 | NewCallback(this, &FilebrowseHandler::HandleGetMetadata)); |
| 478 | dom_ui_->RegisterMessageCallback("openNewPopupWindow", |
| 479 | NewCallback(this, &FilebrowseHandler::OpenNewPopupWindow)); |
| 480 | dom_ui_->RegisterMessageCallback("openNewFullWindow", |
| 481 | NewCallback(this, &FilebrowseHandler::OpenNewFullWindow)); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 482 | dom_ui_->RegisterMessageCallback("uploadToPicasaweb", |
| 483 | NewCallback(this, &FilebrowseHandler::UploadToPicasaweb)); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 484 | dom_ui_->RegisterMessageCallback("getDownloads", |
| 485 | NewCallback(this, &FilebrowseHandler::HandleGetDownloads)); |
| 486 | dom_ui_->RegisterMessageCallback("createNewFolder", |
| 487 | NewCallback(this, &FilebrowseHandler::HandleCreateNewFolder)); |
[email protected] | 17496bb | 2010-03-10 19:48:23 | [diff] [blame] | 488 | dom_ui_->RegisterMessageCallback("playMediaFile", |
| 489 | NewCallback(this, &FilebrowseHandler::PlayMediaFile)); |
[email protected] | 89a7e381 | 2010-06-02 19:38:07 | [diff] [blame] | 490 | dom_ui_->RegisterMessageCallback("enqueueMediaFile", |
| 491 | NewCallback(this, &FilebrowseHandler::EnqueueMediaFile)); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 492 | dom_ui_->RegisterMessageCallback("pauseToggleDownload", |
| 493 | NewCallback(this, &FilebrowseHandler::HandlePauseToggleDownload)); |
| 494 | dom_ui_->RegisterMessageCallback("deleteFile", |
| 495 | NewCallback(this, &FilebrowseHandler::HandleDeleteFile)); |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 496 | dom_ui_->RegisterMessageCallback("copyFile", |
| 497 | NewCallback(this, &FilebrowseHandler::HandleCopyFile)); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 498 | dom_ui_->RegisterMessageCallback("cancelDownload", |
| 499 | NewCallback(this, &FilebrowseHandler::HandleCancelDownload)); |
| 500 | dom_ui_->RegisterMessageCallback("allowDownload", |
| 501 | NewCallback(this, &FilebrowseHandler::HandleAllowDownload)); |
| 502 | dom_ui_->RegisterMessageCallback("refreshDirectory", |
| 503 | NewCallback(this, &FilebrowseHandler::HandleRefreshDirectory)); |
[email protected] | 6ba198f | 2010-07-02 20:32:05 | [diff] [blame] | 504 | dom_ui_->RegisterMessageCallback("isAdvancedEnabled", |
| 505 | NewCallback(this, &FilebrowseHandler::HandleIsAdvancedEnabled)); |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 506 | dom_ui_->RegisterMessageCallback("validateSavePath", |
| 507 | NewCallback(this, &FilebrowseHandler::HandleValidateSavePath)); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 508 | } |
| 509 | |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 510 | |
| 511 | void FilebrowseHandler::FireDeleteComplete(const FilePath& path) { |
| 512 | // We notify the UI by telling it to refresh its contents. |
| 513 | FilePath dir_path = path.DirName(); |
| 514 | GetChildrenForPath(dir_path, true); |
| 515 | }; |
| 516 | |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 517 | void FilebrowseHandler::FireCopyComplete(const FilePath& src, |
| 518 | const FilePath& dest) { |
| 519 | // Notify the UI somehow. |
| 520 | FilePath dir_path = dest.DirName(); |
| 521 | GetChildrenForPath(dir_path, true); |
| 522 | }; |
| 523 | |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 524 | void FilebrowseHandler::FireUploadComplete() { |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 525 | #if defined(OS_CHROMEOS) |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 526 | DictionaryValue info_value; |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 527 | info_value.SetString("path", current_file_uploaded_); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 528 | |
| 529 | std::string username; |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 530 | chromeos::UserManager* user_man = chromeos::UserManager::Get(); |
| 531 | username = user_man->logged_in_user().email(); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 532 | |
| 533 | if (username.empty()) { |
| 534 | LOG(ERROR) << "Unable to get username"; |
| 535 | return; |
| 536 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 537 | int location = username.find_first_of('@', 0); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 538 | if (location <= 0) { |
| 539 | LOG(ERROR) << "Username not formatted correctly"; |
| 540 | return; |
| 541 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 542 | username = username.erase(username.find_first_of('@', 0)); |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 543 | std::string picture_url = kPicasawebBaseUrl; |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 544 | picture_url += username; |
| 545 | picture_url += kPicasawebDropBox; |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 546 | info_value.SetString("url", picture_url); |
| 547 | info_value.SetInteger("status_code", upload_response_code_); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 548 | dom_ui_->CallJavascriptFunction(L"uploadComplete", info_value); |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 549 | #endif |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 550 | } |
| 551 | |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 552 | #if defined(OS_CHROMEOS) |
| 553 | void FilebrowseHandler::MountChanged(chromeos::MountLibrary* obj, |
| 554 | chromeos::MountEventType evt, |
| 555 | const std::string& path) { |
| 556 | if (evt == chromeos::DISK_REMOVED || |
| 557 | evt == chromeos::DISK_CHANGED) { |
| 558 | dom_ui_->CallJavascriptFunction(L"rootsChanged"); |
| 559 | } |
| 560 | } |
| 561 | #endif |
| 562 | |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 563 | void FilebrowseHandler::OnURLFetchComplete(const URLFetcher* source, |
| 564 | const GURL& url, |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 565 | const net::URLRequestStatus& status, |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 566 | int response_code, |
| 567 | const ResponseCookies& cookies, |
| 568 | const std::string& data) { |
| 569 | upload_response_code_ = response_code; |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 570 | VLOG(1) << "Response code: " << response_code; |
| 571 | VLOG(1) << "Request url: " << url; |
[email protected] | 068a71f | 2010-05-11 22:35:48 | [diff] [blame] | 572 | if (StartsWithASCII(url.spec(), kPicasawebUserPrefix, true)) { |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 573 | BrowserThread::PostTask( |
| 574 | BrowserThread::UI, FROM_HERE, |
[email protected] | 068a71f | 2010-05-11 22:35:48 | [diff] [blame] | 575 | NewRunnableMethod(current_task_, &TaskProxy::FireUploadCompleteProxy)); |
| 576 | } |
| 577 | fetch_.reset(); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 578 | } |
| 579 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 580 | void FilebrowseHandler::HandleGetRoots(const ListValue* args) { |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 581 | ListValue results_value; |
| 582 | DictionaryValue info_value; |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 583 | // TODO(dhg): add other entries, make this more general |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 584 | #if defined(OS_CHROMEOS) |
[email protected] | 62c7ef3 | 2010-03-23 23:44:24 | [diff] [blame] | 585 | chromeos::MountLibrary* lib = |
| 586 | chromeos::CrosLibrary::Get()->GetMountLibrary(); |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 587 | const chromeos::MountLibrary::DiskVector& disks = lib->disks(); |
| 588 | |
| 589 | for (size_t i = 0; i < disks.size(); ++i) { |
| 590 | if (!disks[i].mount_path.empty()) { |
| 591 | DictionaryValue* page_value = new DictionaryValue(); |
| 592 | page_value->SetString(kPropertyPath, disks[i].mount_path); |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 593 | FilePath currentpath(disks[i].mount_path); |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 594 | std::string filename; |
| 595 | filename = currentpath.BaseName().value(); |
| 596 | page_value->SetString(kPropertyTitle, filename); |
| 597 | page_value->SetBoolean(kPropertyDirectory, true); |
| 598 | results_value.Append(page_value); |
| 599 | } |
| 600 | } |
| 601 | #else |
| 602 | DictionaryValue* page_value = new DictionaryValue(); |
| 603 | page_value->SetString(kPropertyPath, "/media"); |
| 604 | page_value->SetString(kPropertyTitle, "Removeable"); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 605 | page_value->SetBoolean(kPropertyDirectory, true); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 606 | results_value.Append(page_value); |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 607 | #endif |
| 608 | FilePath default_download_path; |
| 609 | if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, |
| 610 | &default_download_path)) { |
| 611 | NOTREACHED(); |
| 612 | } |
| 613 | |
| 614 | DictionaryValue* download_value = new DictionaryValue(); |
| 615 | download_value->SetString(kPropertyPath, default_download_path.value()); |
| 616 | download_value->SetString(kPropertyTitle, "File Shelf"); |
| 617 | download_value->SetBoolean(kPropertyDirectory, true); |
| 618 | |
| 619 | results_value.Append(download_value); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 620 | |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 621 | info_value.SetString("functionCall", "getRoots"); |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 622 | info_value.SetString(kPropertyPath, ""); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 623 | dom_ui_->CallJavascriptFunction(L"browseFileResult", |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 624 | info_value, results_value); |
| 625 | } |
| 626 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 627 | void FilebrowseHandler::HandleCreateNewFolder(const ListValue* args) { |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 628 | #if defined(OS_CHROMEOS) |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 629 | std::string path = WideToUTF8(ExtractStringValue(args)); |
| 630 | FilePath currentpath(path); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 631 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 632 | scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), currentpath); |
| 633 | BrowserThread::PostTask( |
| 634 | BrowserThread::FILE, FROM_HERE, |
| 635 | NewRunnableMethod( |
| 636 | task.get(), &TaskProxy::CreateNewFolderProxy)); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 637 | #endif |
| 638 | } |
| 639 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 640 | void FilebrowseHandler::CreateNewFolder(const FilePath& currentpath) const { |
| 641 | if (!ValidateSaveDir(currentpath, false) || |
| 642 | !file_util::CreateDirectory(currentpath)) |
| 643 | LOG(ERROR) << "Unable to create directory " << currentpath.value(); |
| 644 | } |
| 645 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 646 | void FilebrowseHandler::PlayMediaFile(const ListValue* args) { |
[email protected] | 17496bb | 2010-03-10 19:48:23 | [diff] [blame] | 647 | #if defined(OS_CHROMEOS) |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 648 | std::string url = WideToUTF8(ExtractStringValue(args)); |
| 649 | GURL gurl(url); |
[email protected] | 17496bb | 2010-03-10 19:48:23 | [diff] [blame] | 650 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 651 | Browser* browser = Browser::GetBrowserForController( |
| 652 | &tab_contents_->controller(), NULL); |
[email protected] | dc8caba | 2010-12-13 16:52:35 | [diff] [blame] | 653 | MediaPlayer* mediaplayer = MediaPlayer::GetInstance(); |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 654 | mediaplayer->ForcePlayMediaURL(gurl, browser); |
[email protected] | 89a7e381 | 2010-06-02 19:38:07 | [diff] [blame] | 655 | #endif |
| 656 | } |
| 657 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 658 | void FilebrowseHandler::EnqueueMediaFile(const ListValue* args) { |
[email protected] | 89a7e381 | 2010-06-02 19:38:07 | [diff] [blame] | 659 | #if defined(OS_CHROMEOS) |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 660 | std::string url = WideToUTF8(ExtractStringValue(args)); |
| 661 | GURL gurl(url); |
[email protected] | 89a7e381 | 2010-06-02 19:38:07 | [diff] [blame] | 662 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 663 | Browser* browser = Browser::GetBrowserForController( |
| 664 | &tab_contents_->controller(), NULL); |
[email protected] | dc8caba | 2010-12-13 16:52:35 | [diff] [blame] | 665 | MediaPlayer* mediaplayer = MediaPlayer::GetInstance(); |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 666 | mediaplayer->EnqueueMediaURL(gurl, browser); |
[email protected] | 17496bb | 2010-03-10 19:48:23 | [diff] [blame] | 667 | #endif |
| 668 | } |
| 669 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 670 | void FilebrowseHandler::HandleIsAdvancedEnabled(const ListValue* args) { |
[email protected] | 6ba198f | 2010-07-02 20:32:05 | [diff] [blame] | 671 | #if defined(OS_CHROMEOS) |
[email protected] | ba816424 | 2010-11-16 21:31:00 | [diff] [blame] | 672 | bool is_enabled = CommandLine::ForCurrentProcess()->HasSwitch( |
| 673 | switches::kEnableAdvancedFileSystem); |
| 674 | bool mp_enabled = CommandLine::ForCurrentProcess()->HasSwitch( |
| 675 | switches::kEnableMediaPlayer); |
[email protected] | 6ba198f | 2010-07-02 20:32:05 | [diff] [blame] | 676 | DictionaryValue info_value; |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 677 | info_value.SetBoolean("enabled", is_enabled); |
| 678 | info_value.SetBoolean("mpEnabled", mp_enabled); |
[email protected] | 6ba198f | 2010-07-02 20:32:05 | [diff] [blame] | 679 | dom_ui_->CallJavascriptFunction(L"enabledResult", |
| 680 | info_value); |
[email protected] | f872b3e | 2010-08-13 16:26:10 | [diff] [blame] | 681 | |
[email protected] | 6ba198f | 2010-07-02 20:32:05 | [diff] [blame] | 682 | #endif |
| 683 | } |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 684 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 685 | void FilebrowseHandler::HandleRefreshDirectory(const ListValue* args) { |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 686 | #if defined(OS_CHROMEOS) |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 687 | std::string path = WideToUTF8(ExtractStringValue(args)); |
| 688 | FilePath currentpath(path); |
| 689 | GetChildrenForPath(currentpath, true); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 690 | #endif |
| 691 | } |
| 692 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 693 | void FilebrowseHandler::HandlePauseToggleDownload(const ListValue* args) { |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 694 | #if defined(OS_CHROMEOS) |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 695 | int id; |
| 696 | ExtractIntegerValue(args, &id); |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 697 | if ((id - 1) >= static_cast<int>(active_download_items_.size())) { |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 698 | return; |
| 699 | } |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 700 | DownloadItem* item = active_download_items_[id]; |
| 701 | item->TogglePause(); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 702 | #endif |
| 703 | } |
| 704 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 705 | void FilebrowseHandler::HandleAllowDownload(const ListValue* args) { |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 706 | #if defined(OS_CHROMEOS) |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 707 | int id; |
| 708 | ExtractIntegerValue(args, &id); |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 709 | if ((id - 1) >= static_cast<int>(active_download_items_.size())) { |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 710 | return; |
| 711 | } |
| 712 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 713 | DownloadItem* item = active_download_items_[id]; |
| 714 | download_manager_->DangerousDownloadValidated(item); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 715 | #endif |
| 716 | } |
| 717 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 718 | void FilebrowseHandler::HandleCancelDownload(const ListValue* args) { |
| 719 | #if defined(OS_CHROMEOS) |
| 720 | int id; |
| 721 | ExtractIntegerValue(args, &id); |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 722 | if ((id - 1) >= static_cast<int>(active_download_items_.size())) { |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 723 | return; |
| 724 | } |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 725 | DownloadItem* item = active_download_items_[id]; |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 726 | FilePath path = item->full_path(); |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 727 | item->Cancel(true); |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 728 | FilePath dir_path = path.DirName(); |
| 729 | item->Remove(true); |
| 730 | GetChildrenForPath(dir_path, true); |
| 731 | #endif |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 732 | } |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 733 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 734 | void FilebrowseHandler::OpenNewFullWindow(const ListValue* args) { |
| 735 | OpenNewWindow(args, false); |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 736 | } |
| 737 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 738 | void FilebrowseHandler::OpenNewPopupWindow(const ListValue* args) { |
| 739 | OpenNewWindow(args, true); |
| 740 | } |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 741 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 742 | void FilebrowseHandler::OpenNewWindow(const ListValue* args, bool popup) { |
| 743 | std::string url = WideToUTF8(ExtractStringValue(args)); |
| 744 | Browser* browser = popup ? |
| 745 | Browser::CreateForType(Browser::TYPE_APP_PANEL, profile_) : |
| 746 | BrowserList::GetLastActive(); |
[email protected] | 616381f0 | 2010-11-02 15:15:33 | [diff] [blame] | 747 | browser::NavigateParams params(browser, GURL(url), PageTransition::LINK); |
| 748 | params.disposition = NEW_FOREGROUND_TAB; |
| 749 | browser::Navigate(¶ms); |
| 750 | // TODO(beng): The following two calls should be automatic by Navigate(). |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 751 | if (popup) { |
| 752 | // TODO(dhg): Remove these from being hardcoded. Allow javascript |
| 753 | // to specify. |
[email protected] | 616381f0 | 2010-11-02 15:15:33 | [diff] [blame] | 754 | params.browser->window()->SetBounds(gfx::Rect(0, 0, 400, 300)); |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 755 | } |
[email protected] | 616381f0 | 2010-11-02 15:15:33 | [diff] [blame] | 756 | params.browser->window()->Show(); |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 757 | } |
| 758 | |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 759 | void FilebrowseHandler::SendPicasawebRequest() { |
[email protected] | 068a71f | 2010-05-11 22:35:48 | [diff] [blame] | 760 | #if defined(OS_CHROMEOS) |
| 761 | chromeos::UserManager* user_man = chromeos::UserManager::Get(); |
| 762 | std::string username = user_man->logged_in_user().email(); |
| 763 | |
| 764 | if (username.empty()) { |
| 765 | LOG(ERROR) << "Unable to get username"; |
| 766 | return; |
| 767 | } |
| 768 | |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 769 | fetch_.reset(URLFetcher::Create(0, |
| 770 | GURL(kPicasawebBaseUrl), |
| 771 | URLFetcher::GET, |
[email protected] | 068a71f | 2010-05-11 22:35:48 | [diff] [blame] | 772 | this)); |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 773 | fetch_->set_request_context(profile_->GetRequestContext()); |
| 774 | fetch_->Start(); |
[email protected] | 068a71f | 2010-05-11 22:35:48 | [diff] [blame] | 775 | #endif |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 776 | } |
| 777 | |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 778 | void FilebrowseHandler::ReadInFile() { |
| 779 | #if defined(OS_CHROMEOS) |
| 780 | // Get the users username |
| 781 | std::string username; |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 782 | chromeos::UserManager* user_man = chromeos::UserManager::Get(); |
| 783 | username = user_man->logged_in_user().email(); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 784 | |
| 785 | if (username.empty()) { |
| 786 | LOG(ERROR) << "Unable to get username"; |
| 787 | return; |
| 788 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 789 | int location = username.find_first_of('@', 0); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 790 | if (location <= 0) { |
| 791 | LOG(ERROR) << "Username not formatted correctly"; |
| 792 | return; |
| 793 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 794 | username = username.erase(username.find_first_of('@', 0)); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 795 | std::string url = kPicasawebUserPrefix; |
| 796 | url += username; |
| 797 | url += kPicasawebDefault; |
| 798 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 799 | FilePath currentpath(current_file_uploaded_); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 800 | // Get the filename |
| 801 | std::string filename; |
| 802 | filename = currentpath.BaseName().value(); |
| 803 | std::string filecontents; |
| 804 | if (!file_util::ReadFileToString(currentpath, &filecontents)) { |
| 805 | LOG(ERROR) << "Unable to read this file:" << currentpath.value(); |
| 806 | return; |
| 807 | } |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 808 | fetch_.reset(URLFetcher::Create(0, |
| 809 | GURL(url), |
| 810 | URLFetcher::POST, |
| 811 | this)); |
| 812 | fetch_->set_upload_data("image/jpeg", filecontents); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 813 | // Set the filename on the server |
| 814 | std::string slug = "Slug: "; |
| 815 | slug += filename; |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 816 | fetch_->set_extra_request_headers(slug); |
| 817 | fetch_->set_request_context(profile_->GetRequestContext()); |
| 818 | fetch_->Start(); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 819 | #endif |
| 820 | } |
| 821 | |
| 822 | // This is just a prototype for allowing generic uploads to various sites |
| 823 | // TODO(dhg): Remove this and implement general upload. |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 824 | void FilebrowseHandler::UploadToPicasaweb(const ListValue* args) { |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 825 | #if defined(OS_CHROMEOS) |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 826 | std::string search_string = WideToUTF8(ExtractStringValue(args)); |
| 827 | current_file_uploaded_ = search_string; |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 828 | // ReadInFile(); |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 829 | FilePath current_path(search_string); |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 830 | scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), current_path); |
[email protected] | d52bb8a | 2010-05-10 21:05:35 | [diff] [blame] | 831 | current_task_ = task; |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 832 | BrowserThread::PostTask( |
| 833 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 834 | NewRunnableMethod( |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 835 | task.get(), &TaskProxy::ReadInFileProxy)); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 836 | #endif |
| 837 | } |
| 838 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 839 | void FilebrowseHandler::GetChildrenForPath(const FilePath& path, |
| 840 | bool is_refresh) { |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 841 | filelist_value_.reset(new ListValue()); |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 842 | currentpath_ = path; |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 843 | |
| 844 | if (lister_.get()) { |
| 845 | lister_->Cancel(); |
| 846 | lister_->set_delegate(NULL); |
| 847 | lister_ = NULL; |
| 848 | } |
| 849 | |
| 850 | is_refresh_ = is_refresh; |
[email protected] | df53d2d | 2010-11-19 08:56:12 | [diff] [blame] | 851 | |
| 852 | #if defined(OS_CHROMEOS) |
| 853 | // Don't allow listing files in inaccessible dirs. |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 854 | if (AccessDisabled(path)) |
[email protected] | df53d2d | 2010-11-19 08:56:12 | [diff] [blame] | 855 | return; |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 856 | #endif |
[email protected] | df53d2d | 2010-11-19 08:56:12 | [diff] [blame] | 857 | |
[email protected] | f872b3e | 2010-08-13 16:26:10 | [diff] [blame] | 858 | FilePath default_download_path; |
| 859 | if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, |
| 860 | &default_download_path)) { |
| 861 | NOTREACHED(); |
| 862 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 863 | |
[email protected] | f872b3e | 2010-08-13 16:26:10 | [diff] [blame] | 864 | if (currentpath_ == default_download_path) { |
| 865 | lister_ = new net::DirectoryLister(currentpath_, |
[email protected] | 9ee2c8d | 2010-08-21 23:25:22 | [diff] [blame] | 866 | false, |
[email protected] | f872b3e | 2010-08-13 16:26:10 | [diff] [blame] | 867 | net::DirectoryLister::DATE, |
| 868 | this); |
| 869 | } else { |
| 870 | lister_ = new net::DirectoryLister(currentpath_, this); |
| 871 | } |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 872 | lister_->Start(); |
| 873 | } |
| 874 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 875 | void FilebrowseHandler::HandleGetChildren(const ListValue* args) { |
| 876 | #if defined(OS_CHROMEOS) |
| 877 | std::string path = WideToUTF8(ExtractStringValue(args)); |
| 878 | FilePath currentpath(path); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 879 | filelist_value_.reset(new ListValue()); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 880 | |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 881 | GetChildrenForPath(currentpath, false); |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 882 | #endif |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 883 | } |
| 884 | |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 885 | void FilebrowseHandler::OnListFile( |
[email protected] | 9ee2c8d | 2010-08-21 23:25:22 | [diff] [blame] | 886 | const net::DirectoryLister::DirectoryListerData& data) { |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 887 | #if defined(OS_WIN) |
[email protected] | 9ee2c8d | 2010-08-21 23:25:22 | [diff] [blame] | 888 | if (data.info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) { |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | #elif defined(OS_POSIX) |
[email protected] | 9ee2c8d | 2010-08-21 23:25:22 | [diff] [blame] | 892 | if (data.info.filename[0] == '.') { |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 893 | return; |
| 894 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 895 | |
| 896 | // Suppress .crdownload files. |
| 897 | static const char crdownload[] = (".crdownload"); |
| 898 | static const size_t crdownload_size = arraysize(crdownload); |
| 899 | const std::string& filename = data.info.filename; |
| 900 | if ((filename.size() > crdownload_size) && |
| 901 | (filename.rfind(crdownload) == (filename.size() - crdownload_size))) |
| 902 | return; |
[email protected] | 07046ab | 2010-01-20 21:42:44 | [diff] [blame] | 903 | #endif |
| 904 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 905 | DictionaryValue* file_value = new DictionaryValue(); |
| 906 | |
| 907 | #if defined(OS_WIN) |
[email protected] | 9ee2c8d | 2010-08-21 23:25:22 | [diff] [blame] | 908 | int64 size = (static_cast<int64>(data.info.nFileSizeHigh) << 32) | |
| 909 | data.info.nFileSizeLow; |
| 910 | file_value->SetString(kPropertyTitle, data.info.cFileName); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 911 | file_value->SetString(kPropertyPath, |
[email protected] | 9ee2c8d | 2010-08-21 23:25:22 | [diff] [blame] | 912 | currentpath_.Append(data.info.cFileName).value()); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 913 | file_value->SetBoolean(kPropertyDirectory, |
[email protected] | 9ee2c8d | 2010-08-21 23:25:22 | [diff] [blame] | 914 | (data.info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 915 | #elif defined(OS_POSIX) |
[email protected] | 9ee2c8d | 2010-08-21 23:25:22 | [diff] [blame] | 916 | file_value->SetString(kPropertyTitle, data.info.filename); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 917 | file_value->SetString(kPropertyPath, |
[email protected] | 9ee2c8d | 2010-08-21 23:25:22 | [diff] [blame] | 918 | currentpath_.Append(data.info.filename).value()); |
| 919 | file_value->SetBoolean(kPropertyDirectory, S_ISDIR(data.info.stat.st_mode)); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 920 | #endif |
| 921 | filelist_value_->Append(file_value); |
| 922 | } |
| 923 | |
[email protected] | dcd23fe | 2009-11-12 20:21:18 | [diff] [blame] | 924 | void FilebrowseHandler::OnListDone(int error) { |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 925 | DictionaryValue info_value; |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 926 | if (is_refresh_) { |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 927 | info_value.SetString("functionCall", "refresh"); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 928 | } else { |
[email protected] | e4fbe8e | 2010-08-07 07:00:46 | [diff] [blame] | 929 | info_value.SetString("functionCall", "getChildren"); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 930 | } |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 931 | info_value.SetString(kPropertyPath, currentpath_.value()); |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 932 | dom_ui_->CallJavascriptFunction(L"browseFileResult", |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 933 | info_value, *(filelist_value_.get())); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 934 | } |
| 935 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 936 | void FilebrowseHandler::HandleGetMetadata(const ListValue* args) { |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 937 | } |
| 938 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 939 | void FilebrowseHandler::HandleGetDownloads(const ListValue* args) { |
[email protected] | ebdd201 | 2011-01-19 11:06:57 | [diff] [blame] | 940 | UpdateDownloadList(); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | void FilebrowseHandler::ModelChanged() { |
[email protected] | ebdd201 | 2011-01-19 11:06:57 | [diff] [blame] | 944 | if (!currentpath_.empty()) |
| 945 | GetChildrenForPath(currentpath_, true); |
| 946 | else |
| 947 | UpdateDownloadList(); |
| 948 | } |
| 949 | |
| 950 | void FilebrowseHandler::UpdateDownloadList() { |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 951 | ClearDownloadItems(); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 952 | |
[email protected] | 0e2b59a | 2010-07-29 22:27:05 | [diff] [blame] | 953 | std::vector<DownloadItem*> downloads; |
| 954 | download_manager_->GetAllDownloads(FilePath(), &downloads); |
| 955 | |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 956 | std::vector<DownloadItem*> new_downloads; |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 957 | // Scan for any in progress downloads and add ourself to them as an observer. |
| 958 | for (DownloadList::iterator it = downloads.begin(); |
| 959 | it != downloads.end(); ++it) { |
| 960 | DownloadItem* download = *it; |
| 961 | // We want to know what happens as the download progresses and be notified |
| 962 | // when the user validates the dangerous download. |
| 963 | if (download->state() == DownloadItem::IN_PROGRESS || |
| 964 | download->safety_state() == DownloadItem::DANGEROUS) { |
| 965 | download->AddObserver(this); |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 966 | active_download_items_.push_back(download); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 967 | } |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 968 | DownloadList::iterator item = find(download_items_.begin(), |
| 969 | download_items_.end(), |
| 970 | download); |
| 971 | if (item == download_items_.end() && got_first_download_list_) { |
| 972 | SendNewDownload(download); |
| 973 | } |
| 974 | new_downloads.push_back(download); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 975 | } |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 976 | download_items_.swap(new_downloads); |
| 977 | got_first_download_list_ = true; |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 978 | SendCurrentDownloads(); |
| 979 | } |
| 980 | |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 981 | void FilebrowseHandler::SendNewDownload(DownloadItem* download) { |
| 982 | ListValue results_value; |
| 983 | results_value.Append(download_util::CreateDownloadItemValue(download, -1)); |
| 984 | dom_ui_->CallJavascriptFunction(L"newDownload", results_value); |
| 985 | } |
| 986 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 987 | void FilebrowseHandler::DeleteFile(const FilePath& path, TaskProxy* task) { |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 988 | if (!file_util::Delete(path, true)) { |
| 989 | LOG(ERROR) << "unable to delete directory"; |
| 990 | } |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 991 | BrowserThread::PostTask( |
| 992 | BrowserThread::UI, FROM_HERE, |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 993 | NewRunnableMethod(task, &TaskProxy::FireDeleteCompleteProxy)); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 994 | } |
| 995 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 996 | void FilebrowseHandler::CopyFile(const FilePath& src, |
| 997 | const FilePath& dest, |
| 998 | TaskProxy* task) { |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 999 | if (file_util::DirectoryExists(src)) { |
| 1000 | if (!file_util::CopyDirectory(src, dest, true)) { |
| 1001 | LOG(ERROR) << "unable to copy directory:" << src.value(); |
| 1002 | } |
| 1003 | } else { |
| 1004 | if (!file_util::CopyFile(src, dest)) { |
| 1005 | LOG(ERROR) << "unable to copy file" << src.value(); |
| 1006 | } |
| 1007 | } |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 1008 | BrowserThread::PostTask( |
| 1009 | BrowserThread::UI, FROM_HERE, |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1010 | NewRunnableMethod(task, &TaskProxy::FireCopyCompleteProxy)); |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 1011 | } |
| 1012 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 1013 | void FilebrowseHandler::HandleDeleteFile(const ListValue* args) { |
| 1014 | #if defined(OS_CHROMEOS) |
| 1015 | std::string path = WideToUTF8(ExtractStringValue(args)); |
| 1016 | FilePath currentpath(path); |
[email protected] | df53d2d | 2010-11-19 08:56:12 | [diff] [blame] | 1017 | |
| 1018 | // Don't allow file deletion in inaccessible dirs. |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1019 | if (AccessDisabled(currentpath)) |
[email protected] | df53d2d | 2010-11-19 08:56:12 | [diff] [blame] | 1020 | return; |
| 1021 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 1022 | for (unsigned int x = 0; x < active_download_items_.size(); x++) { |
| 1023 | FilePath item = active_download_items_[x]->full_path(); |
| 1024 | if (item == currentpath) { |
| 1025 | active_download_items_[x]->Cancel(true); |
| 1026 | active_download_items_[x]->Remove(true); |
| 1027 | FilePath dir_path = item.DirName(); |
| 1028 | GetChildrenForPath(dir_path, true); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | } |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1032 | scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), currentpath); |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 1033 | BrowserThread::PostTask( |
| 1034 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 1035 | NewRunnableMethod( |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1036 | task.get(), &TaskProxy::DeleteFileProxy)); |
[email protected] | a67fa08e | 2010-02-12 20:43:55 | [diff] [blame] | 1037 | #endif |
| 1038 | } |
| 1039 | |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 1040 | void FilebrowseHandler::HandleCopyFile(const ListValue* value) { |
| 1041 | #if defined(OS_CHROMEOS) |
| 1042 | if (value && value->GetType() == Value::TYPE_LIST) { |
| 1043 | const ListValue* list_value = static_cast<const ListValue*>(value); |
| 1044 | std::string src; |
| 1045 | std::string dest; |
| 1046 | |
| 1047 | // Get path string. |
| 1048 | if (list_value->GetString(0, &src) && |
| 1049 | list_value->GetString(1, &dest)) { |
| 1050 | FilePath SrcPath = FilePath(src); |
| 1051 | FilePath DestPath = FilePath(dest); |
| 1052 | |
[email protected] | df53d2d | 2010-11-19 08:56:12 | [diff] [blame] | 1053 | // Don't allow file copy to inaccessible dirs. |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1054 | if (AccessDisabled(DestPath)) |
[email protected] | df53d2d | 2010-11-19 08:56:12 | [diff] [blame] | 1055 | return; |
| 1056 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1057 | scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), |
| 1058 | SrcPath, DestPath); |
[email protected] | 2dc383ce | 2010-10-09 03:54:00 | [diff] [blame] | 1059 | BrowserThread::PostTask( |
| 1060 | BrowserThread::FILE, FROM_HERE, |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 1061 | NewRunnableMethod( |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1062 | task.get(), &TaskProxy::CopyFileProxy)); |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 1063 | } else { |
| 1064 | LOG(ERROR) << "Unable to get string"; |
| 1065 | return; |
| 1066 | } |
| 1067 | } |
| 1068 | #endif |
| 1069 | } |
| 1070 | |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 1071 | void FilebrowseHandler::HandleValidateSavePath(const ListValue* args) { |
| 1072 | std::string string_path; |
| 1073 | if (!args || !args->GetString(0, &string_path)) { |
| 1074 | FireOnValidatedSavePathOnUIThread(false, FilePath()); // Invalid save path. |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | FilePath save_path(string_path); |
| 1079 | |
| 1080 | #if defined(OS_CHROMEOS) |
| 1081 | scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), save_path); |
| 1082 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 1083 | NewRunnableMethod(task.get(), &TaskProxy::ValidateSavePathOnFileThread)); |
| 1084 | #else |
| 1085 | // No save path checking for non-ChromeOS platforms. |
| 1086 | FireOnValidatedSavePathOnUIThread(true, save_path); |
| 1087 | #endif |
| 1088 | } |
| 1089 | |
| 1090 | void FilebrowseHandler::ValidateSavePathOnFileThread( |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1091 | const FilePath& save_path, TaskProxy* task) { |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 1092 | #if defined(OS_CHROMEOS) |
| 1093 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 1094 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1095 | const bool valid = ValidateSaveDir(save_path.DirName(), true); |
| 1096 | |
| 1097 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 1098 | NewRunnableMethod(task, |
| 1099 | &TaskProxy::FireOnValidatedSavePathOnUIThread, |
| 1100 | valid)); |
| 1101 | #endif |
| 1102 | } |
| 1103 | |
| 1104 | bool FilebrowseHandler::ValidateSaveDir(const FilePath& save_dir, |
| 1105 | bool exists) const { |
| 1106 | #if defined(OS_CHROMEOS) |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 1107 | FilePath default_download_path; |
| 1108 | if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, |
| 1109 | &default_download_path)) { |
| 1110 | NOTREACHED(); |
| 1111 | } |
| 1112 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1113 | // Valid save dir must be inside default download dir. |
| 1114 | if (default_download_path == save_dir) |
| 1115 | return true; |
| 1116 | if (exists) { |
| 1117 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 1118 | return file_util::ContainsPath(default_download_path, save_dir); |
| 1119 | } else { |
| 1120 | return default_download_path.IsParent(save_dir); |
| 1121 | } |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 1122 | #endif |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1123 | return false; |
[email protected] | 23ebd230 | 2010-11-04 16:23:20 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | void FilebrowseHandler::FireOnValidatedSavePathOnUIThread(bool valid, |
| 1127 | const FilePath& save_path) { |
| 1128 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1129 | |
| 1130 | FundamentalValue valid_value(valid); |
| 1131 | StringValue path_value(save_path.value()); |
| 1132 | dom_ui_->CallJavascriptFunction(L"onValidatedSavePath", |
| 1133 | valid_value, path_value); |
| 1134 | } |
[email protected] | d3d9b4fb | 2010-09-24 16:24:27 | [diff] [blame] | 1135 | |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 1136 | void FilebrowseHandler::OnDownloadUpdated(DownloadItem* download) { |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 1137 | DownloadList::iterator it = find(active_download_items_.begin(), |
| 1138 | active_download_items_.end(), |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 1139 | download); |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 1140 | if (it == active_download_items_.end()) |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 1141 | return; |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 1142 | const int id = static_cast<int>(it - active_download_items_.begin()); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 1143 | |
[email protected] | 499d97c | 2010-10-25 23:12:15 | [diff] [blame] | 1144 | scoped_ptr<DictionaryValue> download_item( |
| 1145 | download_util::CreateDownloadItemValue(download, id)); |
| 1146 | dom_ui_->CallJavascriptFunction(L"downloadUpdated", *download_item.get()); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | void FilebrowseHandler::ClearDownloadItems() { |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 1150 | for (DownloadList::iterator it = active_download_items_.begin(); |
| 1151 | it != active_download_items_.end(); ++it) { |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 1152 | (*it)->RemoveObserver(this); |
| 1153 | } |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 1154 | active_download_items_.clear(); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | void FilebrowseHandler::SendCurrentDownloads() { |
| 1158 | ListValue results_value; |
[email protected] | 377b4cc | 2010-05-18 17:25:19 | [diff] [blame] | 1159 | for (DownloadList::iterator it = active_download_items_.begin(); |
| 1160 | it != active_download_items_.end(); ++it) { |
| 1161 | int index = static_cast<int>(it - active_download_items_.begin()); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 1162 | results_value.Append(download_util::CreateDownloadItemValue(*it, index)); |
| 1163 | } |
| 1164 | |
| 1165 | dom_ui_->CallJavascriptFunction(L"downloadsList", results_value); |
| 1166 | } |
| 1167 | |
[email protected] | ebdd201 | 2011-01-19 11:06:57 | [diff] [blame] | 1168 | void FilebrowseHandler::OnDownloadFileCompleted(DownloadItem* download) { |
| 1169 | GetChildrenForPath(currentpath_, true); |
| 1170 | } |
| 1171 | |
[email protected] | 9fec6ff | 2011-02-10 23:47:10 | [diff] [blame^] | 1172 | bool FilebrowseHandler::AccessDisabled(const FilePath& path) const { |
| 1173 | return !ValidateSaveDir(path, false) && |
| 1174 | net::URLRequestFileJob::AccessDisabled(path); |
| 1175 | } |
| 1176 | |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 1177 | //////////////////////////////////////////////////////////////////////////////// |
| 1178 | // |
[email protected] | f5bf8ccf | 2010-02-05 18:19:25 | [diff] [blame] | 1179 | // FileBrowseUI |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 1180 | // |
| 1181 | //////////////////////////////////////////////////////////////////////////////// |
| 1182 | |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 1183 | FileBrowseUI::FileBrowseUI(TabContents* contents) : HtmlDialogUI(contents) { |
[email protected] | 9d6b9aff | 2009-12-11 17:39:18 | [diff] [blame] | 1184 | FilebrowseHandler* handler = new FilebrowseHandler(); |
| 1185 | AddMessageHandler((handler)->Attach(this)); |
[email protected] | 274e42b | 2010-01-29 22:03:57 | [diff] [blame] | 1186 | handler->Init(); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 1187 | FileBrowseUIHTMLSource* html_source = new FileBrowseUIHTMLSource(); |
| 1188 | |
| 1189 | // Set up the chrome://filebrowse/ source. |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 1190 | contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); |
[email protected] | 698601e | 2009-10-21 22:43:37 | [diff] [blame] | 1191 | } |
[email protected] | f5bf8ccf | 2010-02-05 18:19:25 | [diff] [blame] | 1192 | |
| 1193 | // static |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 1194 | Browser* FileBrowseUI::OpenPopup(Profile* profile, |
[email protected] | 46729329 | 2010-04-16 19:41:56 | [diff] [blame] | 1195 | const std::string& hashArgument, |
| 1196 | int width, |
| 1197 | int height) { |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 1198 | // Get existing pop up for given hashArgument. |
[email protected] | f7e9fd6 | 2010-09-28 15:45:06 | [diff] [blame] | 1199 | Browser* browser = GetPopupForPath(hashArgument, profile); |
[email protected] | f5bf8ccf | 2010-02-05 18:19:25 | [diff] [blame] | 1200 | |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 1201 | // Create new browser if no matching pop up found. |
| 1202 | if (browser == NULL) { |
[email protected] | 9ee17e8c | 2010-08-10 19:17:31 | [diff] [blame] | 1203 | browser = Browser::CreateForType(Browser::TYPE_APP_PANEL, profile); |
[email protected] | f59f116 | 2010-04-23 21:19:29 | [diff] [blame] | 1204 | std::string url; |
| 1205 | if (hashArgument.empty()) { |
| 1206 | url = chrome::kChromeUIFileBrowseURL; |
| 1207 | } else { |
| 1208 | url = kFilebrowseURLHash; |
| 1209 | url.append(hashArgument); |
| 1210 | } |
[email protected] | f5bf8ccf | 2010-02-05 18:19:25 | [diff] [blame] | 1211 | |
[email protected] | 616381f0 | 2010-11-02 15:15:33 | [diff] [blame] | 1212 | browser::NavigateParams params(browser, GURL(url), PageTransition::LINK); |
| 1213 | params.disposition = NEW_FOREGROUND_TAB; |
| 1214 | browser::Navigate(¶ms); |
| 1215 | // TODO(beng): The following two calls should be automatic by Navigate(). |
| 1216 | params.browser->window()->SetBounds(gfx::Rect(kPopupLeft, |
| 1217 | kPopupTop, |
| 1218 | width, |
| 1219 | height)); |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 1220 | |
[email protected] | 616381f0 | 2010-11-02 15:15:33 | [diff] [blame] | 1221 | params.browser->window()->Show(); |
[email protected] | 9ee17e8c | 2010-08-10 19:17:31 | [diff] [blame] | 1222 | } else { |
| 1223 | browser->window()->Show(); |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 1224 | } |
[email protected] | f5bf8ccf | 2010-02-05 18:19:25 | [diff] [blame] | 1225 | |
| 1226 | return browser; |
| 1227 | } |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 1228 | |
[email protected] | f7e9fd6 | 2010-09-28 15:45:06 | [diff] [blame] | 1229 | Browser* FileBrowseUI::GetPopupForPath(const std::string& path, |
| 1230 | Profile* profile) { |
[email protected] | 9ee17e8c | 2010-08-10 19:17:31 | [diff] [blame] | 1231 | std::string current_path = path; |
| 1232 | if (current_path.empty()) { |
[email protected] | ba816424 | 2010-11-16 21:31:00 | [diff] [blame] | 1233 | bool is_enabled = CommandLine::ForCurrentProcess()->HasSwitch( |
| 1234 | switches::kEnableAdvancedFileSystem); |
[email protected] | 9ee17e8c | 2010-08-10 19:17:31 | [diff] [blame] | 1235 | if (!is_enabled) { |
| 1236 | FilePath default_download_path; |
| 1237 | if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, |
| 1238 | &default_download_path)) { |
| 1239 | NOTREACHED(); |
| 1240 | } |
| 1241 | current_path = default_download_path.value(); |
| 1242 | } |
| 1243 | } |
| 1244 | |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 1245 | for (BrowserList::const_iterator it = BrowserList::begin(); |
| 1246 | it != BrowserList::end(); ++it) { |
[email protected] | 9ee17e8c | 2010-08-10 19:17:31 | [diff] [blame] | 1247 | if (((*it)->type() == Browser::TYPE_APP_PANEL)) { |
[email protected] | d519bed | 2010-05-28 18:42:03 | [diff] [blame] | 1248 | TabContents* tab_contents = (*it)->GetSelectedTabContents(); |
| 1249 | DCHECK(tab_contents); |
| 1250 | if (!tab_contents) |
| 1251 | continue; |
| 1252 | const GURL& url = tab_contents->GetURL(); |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 1253 | |
| 1254 | if (url.SchemeIs(chrome::kChromeUIScheme) && |
| 1255 | url.host() == chrome::kChromeUIFileBrowseHost && |
[email protected] | f7e9fd6 | 2010-09-28 15:45:06 | [diff] [blame] | 1256 | url.ref() == current_path && |
| 1257 | (*it)->profile() == profile) { |
[email protected] | 1717246f | 2010-02-10 17:08:15 | [diff] [blame] | 1258 | return (*it); |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | return NULL; |
| 1264 | } |
[email protected] | 46729329 | 2010-04-16 19:41:56 | [diff] [blame] | 1265 | |
| 1266 | const int FileBrowseUI::kPopupWidth = 250; |
| 1267 | const int FileBrowseUI::kPopupHeight = 300; |
| 1268 | const int FileBrowseUI::kSmallPopupWidth = 250; |
| 1269 | const int FileBrowseUI::kSmallPopupHeight = 50; |