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