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