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