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