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