blob: 2925cd92728060a3f3adcc513c04f27e202c7763 [file] [log] [blame]
[email protected]698601e2009-10-21 22:43:371// 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]2041cf342010-02-19 03:15:599#include "base/callback.h"
[email protected]698601e2009-10-21 22:43:3710#include "base/logging.h"
11#include "base/message_loop.h"
[email protected]07046ab2010-01-20 21:42:4412#include "base/path_service.h"
[email protected]5c4c7402009-10-30 19:58:0613#include "base/singleton.h"
[email protected]698601e2009-10-21 22:43:3714#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]9d6b9aff2009-12-11 17:39:1819#include "base/weak_ptr.h"
[email protected]698601e2009-10-21 22:43:3720#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]dcd23fe2009-11-12 20:21:1821#include "chrome/browser/browser.h"
[email protected]1717246f2010-02-10 17:08:1522#include "chrome/browser/browser_list.h"
[email protected]dcd23fe2009-11-12 20:21:1823#include "chrome/browser/browser_window.h"
[email protected]fae20792009-10-28 20:31:5824#include "chrome/browser/chrome_thread.h"
[email protected]698601e2009-10-21 22:43:3725#include "chrome/browser/dom_ui/dom_ui_favicon_source.h"
[email protected]274e42b2010-01-29 22:03:5726#include "chrome/browser/download/download_manager.h"
27#include "chrome/browser/download/download_util.h"
[email protected]1717246f2010-02-10 17:08:1528#include "chrome/browser/history/history_types.h"
[email protected]698601e2009-10-21 22:43:3729#include "chrome/browser/metrics/user_metrics.h"
[email protected]698601e2009-10-21 22:43:3730#include "chrome/browser/profile.h"
[email protected]1717246f2010-02-10 17:08:1531#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]68d2a05f2010-05-07 21:39:5532#include "chrome/browser/dom_ui/mediaplayer_ui.h"
[email protected]07046ab2010-01-20 21:42:4433#include "chrome/common/chrome_paths.h"
[email protected]698601e2009-10-21 22:43:3734#include "chrome/common/jstemplate_builder.h"
[email protected]68d2a05f2010-05-07 21:39:5535#include "chrome/common/net/url_fetcher.h"
[email protected]698601e2009-10-21 22:43:3736#include "chrome/common/time_format.h"
37#include "chrome/common/url_constants.h"
[email protected]698601e2009-10-21 22:43:3738#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]07046ab2010-01-20 21:42:4445#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:2446#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]268b02f2010-02-04 21:07:1547#include "chrome/browser/chromeos/cros/mount_library.h"
[email protected]d52bb8a2010-05-10 21:05:3548#include "chrome/browser/chromeos/login/user_manager.h"
[email protected]07046ab2010-01-20 21:42:4449#endif
50
[email protected]698601e2009-10-21 22:43:3751// Maximum number of search results to return in a given search. We should
52// eventually remove this.
53static const int kMaxSearchResults = 100;
54static const std::wstring kPropertyPath = L"path";
55static const std::wstring kPropertyTitle = L"title";
56static const std::wstring kPropertyDirectory = L"isDirectory";
[email protected]9d6b9aff2009-12-11 17:39:1857static const std::string kPicasawebUserPrefix =
58 "http://picasaweb.google.com/data/feed/api/user/";
59static const std::string kPicasawebDefault = "/albumid/default";
60static const std::string kPicasawebDropBox = "/DropBox";
61static const std::string kPicasawebBaseUrl = "http://picasaweb.google.com/";
[email protected]698601e2009-10-21 22:43:3762
[email protected]f5bf8ccf2010-02-05 18:19:2563static const char* kFilebrowseURLHash = "chrome://filebrowse#";
64static const int kPopupLeft = 0;
65static const int kPopupTop = 0;
[email protected]f5bf8ccf2010-02-05 18:19:2566
[email protected]698601e2009-10-21 22:43:3767class 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]f09d93792009-11-17 00:10:3673 virtual void StartDataRequest(const std::string& path,
74 bool is_off_the_record,
75 int request_id);
[email protected]698601e2009-10-21 22:43:3776 virtual std::string GetMimeType(const std::string&) const {
77 return "text/html";
78 }
79
80 private:
[email protected]8de85a62009-11-06 08:32:1781 ~FileBrowseUIHTMLSource() {}
82
[email protected]698601e2009-10-21 22:43:3783 DISALLOW_COPY_AND_ASSIGN(FileBrowseUIHTMLSource);
84};
85
[email protected]9d6b9aff2009-12-11 17:39:1886class TaskProxy;
87
[email protected]698601e2009-10-21 22:43:3788// The handler for Javascript messages related to the "filebrowse" view.
[email protected]dcd23fe2009-11-12 20:21:1889class FilebrowseHandler : public net::DirectoryLister::DirectoryListerDelegate,
[email protected]9d6b9aff2009-12-11 17:39:1890 public DOMMessageHandler,
[email protected]07046ab2010-01-20 21:42:4491#if defined(OS_CHROMEOS)
92 public chromeos::MountLibrary::Observer,
93#endif
[email protected]9d6b9aff2009-12-11 17:39:1894 public base::SupportsWeakPtr<FilebrowseHandler>,
[email protected]274e42b2010-01-29 22:03:5795 public URLFetcher::Delegate,
96 public DownloadManager::Observer,
97 public DownloadItem::Observer {
[email protected]698601e2009-10-21 22:43:3798 public:
[email protected]dcd23fe2009-11-12 20:21:1899 FilebrowseHandler();
100 virtual ~FilebrowseHandler();
[email protected]698601e2009-10-21 22:43:37101
[email protected]274e42b2010-01-29 22:03:57102 // Init work after Attach.
103 void Init();
104
[email protected]698601e2009-10-21 22:43:37105 // 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]07046ab2010-01-20 21:42:44113#if defined(OS_CHROMEOS)
114 void MountChanged(chromeos::MountLibrary* obj,
115 chromeos::MountEventType evt,
116 const std::string& path);
117#endif
118
[email protected]274e42b2010-01-29 22:03:57119 // 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]698601e2009-10-21 22:43:37128 // Callback for the "getRoots" message.
129 void HandleGetRoots(const Value* value);
130
[email protected]a67fa08e2010-02-12 20:43:55131 void GetChildrenForPath(FilePath& path, bool is_refresh);
132
[email protected]9d6b9aff2009-12-11 17:39:18133 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]698601e2009-10-21 22:43:37140 // Callback for the "getChildren" message.
141 void HandleGetChildren(const Value* value);
[email protected]c4a530b2010-03-08 17:33:03142 // Callback for the "refreshDirectory" message.
143 void HandleRefreshDirectory(const Value* value);
[email protected]698601e2009-10-21 22:43:37144
145 // Callback for the "getMetadata" message.
146 void HandleGetMetadata(const Value* value);
147
[email protected]9d6b9aff2009-12-11 17:39:18148 // Callback for the "openNewWindow" message.
[email protected]dcd23fe2009-11-12 20:21:18149 void OpenNewFullWindow(const Value* value);
150 void OpenNewPopupWindow(const Value* value);
151
[email protected]9d6b9aff2009-12-11 17:39:18152 // Callback for the "uploadToPicasaweb" message.
153 void UploadToPicasaweb(const Value* value);
154
[email protected]274e42b2010-01-29 22:03:57155 // Callback for the "getDownloads" message.
156 void HandleGetDownloads(const Value* value);
157
158 void HandleCreateNewFolder(const Value* value);
159
[email protected]17496bb2010-03-10 19:48:23160 void PlayMediaFile(const Value* value);
161
[email protected]a67fa08e2010-02-12 20:43:55162 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]c4a530b2010-03-08 17:33:03168 void HandleCancelDownload(const Value* value);
169 void HandleAllowDownload(const Value* value);
170
[email protected]9d6b9aff2009-12-11 17:39:18171 void ReadInFile();
172 void FireUploadComplete();
173
[email protected]d52bb8a2010-05-10 21:05:35174 void SendPicasawebRequest();
[email protected]698601e2009-10-21 22:43:37175 private:
[email protected]dcd23fe2009-11-12 20:21:18176
177 void OpenNewWindow(const Value* value, bool popup);
178
[email protected]274e42b2010-01-29 22:03:57179 // 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]377b4cc2010-05-18 17:25:19185 void SendNewDownload(DownloadItem* download);
186
[email protected]698601e2009-10-21 22:43:37187 scoped_ptr<ListValue> filelist_value_;
188 FilePath currentpath_;
[email protected]dcd23fe2009-11-12 20:21:18189 Profile* profile_;
[email protected]9d6b9aff2009-12-11 17:39:18190 std::string current_file_contents_;
191 std::string current_file_uploaded_;
192 int upload_response_code_;
[email protected]d52bb8a2010-05-10 21:05:35193 TaskProxy* current_task_;
[email protected]698601e2009-10-21 22:43:37194 scoped_refptr<net::DirectoryLister> lister_;
[email protected]a67fa08e2010-02-12 20:43:55195 bool is_refresh_;
[email protected]d52bb8a2010-05-10 21:05:35196 scoped_ptr<URLFetcher> fetch_;
[email protected]698601e2009-10-21 22:43:37197
[email protected]274e42b2010-01-29 22:03:57198 DownloadManager* download_manager_;
199 typedef std::vector<DownloadItem*> DownloadList;
[email protected]377b4cc2010-05-18 17:25:19200 DownloadList active_download_items_;
[email protected]274e42b2010-01-29 22:03:57201 DownloadList download_items_;
[email protected]377b4cc2010-05-18 17:25:19202 bool got_first_download_list_;
[email protected]dcd23fe2009-11-12 20:21:18203 DISALLOW_COPY_AND_ASSIGN(FilebrowseHandler);
[email protected]698601e2009-10-21 22:43:37204};
205
[email protected]9d6b9aff2009-12-11 17:39:18206class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> {
207 public:
[email protected]d52bb8a2010-05-10 21:05:35208 explicit TaskProxy(const base::WeakPtr<FilebrowseHandler>& handler,
209 FilePath& path)
[email protected]a67fa08e2010-02-12 20:43:55210 : handler_(handler),
211 path_(path) {}
[email protected]9d6b9aff2009-12-11 17:39:18212 void ReadInFileProxy() {
213 if (handler_) {
214 handler_->ReadInFile();
215 }
216 }
[email protected]068a71f2010-05-11 22:35:48217 void DeleteFetcher(URLFetcher* fetch) {
218 delete fetch;
219 }
[email protected]d52bb8a2010-05-10 21:05:35220 void SendPicasawebRequestProxy() {
221 if (handler_) {
222 handler_->SendPicasawebRequest();
223 }
224 }
[email protected]9d6b9aff2009-12-11 17:39:18225 void FireUploadCompleteProxy() {
226 if (handler_) {
227 handler_->FireUploadComplete();
228 }
229 }
[email protected]a67fa08e2010-02-12 20:43:55230
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]9d6b9aff2009-12-11 17:39:18242 private:
243 base::WeakPtr<FilebrowseHandler> handler_;
[email protected]a67fa08e2010-02-12 20:43:55244 FilePath path_;
[email protected]9d6b9aff2009-12-11 17:39:18245 friend class base::RefCountedThreadSafe<TaskProxy>;
[email protected]068a71f2010-05-11 22:35:48246 DISALLOW_COPY_AND_ASSIGN(TaskProxy);
[email protected]9d6b9aff2009-12-11 17:39:18247};
248
249
[email protected]698601e2009-10-21 22:43:37250////////////////////////////////////////////////////////////////////////////////
251//
252// FileBrowseHTMLSource
253//
254////////////////////////////////////////////////////////////////////////////////
255
256FileBrowseUIHTMLSource::FileBrowseUIHTMLSource()
257 : DataSource(chrome::kChromeUIFileBrowseHost, MessageLoop::current()) {
258}
259
260void FileBrowseUIHTMLSource::StartDataRequest(const std::string& path,
[email protected]e42ae9b2010-03-28 00:55:05261 bool is_off_the_record,
262 int request_id) {
[email protected]698601e2009-10-21 22:43:37263 DictionaryValue localized_strings;
[email protected]11f4857282009-11-13 19:56:17264 // TODO(dhg): Add stirings to localized strings, also add more strings
[email protected]dcd23fe2009-11-12 20:21:18265 // that are currently hardcoded.
[email protected]f95f0752010-03-09 16:47:10266 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]a25802d2010-04-21 16:18:20292 localized_strings.SetString(L"flickr",
293 l10n_util::GetString(IDS_FILEBROWSER_UPLOAD_FLICKR));
[email protected]f95f0752010-03-09 16:47:10294 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]698601e2009-10-21 22:43:37298
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]dcd23fe2009-11-12 20:21:18316// FilebrowseHandler
[email protected]698601e2009-10-21 22:43:37317//
318////////////////////////////////////////////////////////////////////////////////
[email protected]dcd23fe2009-11-12 20:21:18319FilebrowseHandler::FilebrowseHandler()
[email protected]274e42b2010-01-29 22:03:57320 : profile_(NULL),
[email protected]a67fa08e2010-02-12 20:43:55321 is_refresh_(false),
[email protected]d52bb8a2010-05-10 21:05:35322 fetch_(NULL),
[email protected]377b4cc2010-05-18 17:25:19323 download_manager_(NULL),
324 got_first_download_list_(false) {
[email protected]a67fa08e2010-02-12 20:43:55325 lister_ = NULL;
[email protected]07046ab2010-01-20 21:42:44326#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24327 chromeos::MountLibrary* lib =
328 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44329 lib->AddObserver(this);
330#endif
[email protected]698601e2009-10-21 22:43:37331}
332
[email protected]dcd23fe2009-11-12 20:21:18333FilebrowseHandler::~FilebrowseHandler() {
[email protected]07046ab2010-01-20 21:42:44334#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24335 chromeos::MountLibrary* lib =
336 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44337 lib->RemoveObserver(this);
338#endif
[email protected]698601e2009-10-21 22:43:37339 if (lister_.get()) {
340 lister_->Cancel();
341 lister_->set_delegate(NULL);
342 }
[email protected]274e42b2010-01-29 22:03:57343
344 ClearDownloadItems();
345 download_manager_->RemoveObserver(this);
[email protected]068a71f2010-05-11 22:35:48346 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]698601e2009-10-21 22:43:37355}
356
[email protected]dcd23fe2009-11-12 20:21:18357DOMMessageHandler* FilebrowseHandler::Attach(DOMUI* dom_ui) {
[email protected]698601e2009-10-21 22:43:37358 // Create our favicon data source.
[email protected]fae20792009-10-28 20:31:58359 ChromeThread::PostTask(
360 ChromeThread::IO, FROM_HERE,
361 NewRunnableMethod(
[email protected]576a4ca2009-11-05 01:41:09362 Singleton<ChromeURLDataManager>::get(),
[email protected]fae20792009-10-28 20:31:58363 &ChromeURLDataManager::AddDataSource,
[email protected]f8f82502009-11-20 23:14:23364 make_scoped_refptr(new DOMUIFavIconSource(dom_ui->GetProfile()))));
[email protected]dcd23fe2009-11-12 20:21:18365 profile_ = dom_ui->GetProfile();
[email protected]274e42b2010-01-29 22:03:57366
[email protected]698601e2009-10-21 22:43:37367 return DOMMessageHandler::Attach(dom_ui);
368}
369
[email protected]274e42b2010-01-29 22:03:57370void FilebrowseHandler::Init() {
371 download_manager_ = profile_->GetOriginalProfile()->GetDownloadManager();
372 download_manager_->AddObserver(this);
[email protected]d52bb8a2010-05-10 21:05:35373 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]274e42b2010-01-29 22:03:57386}
387
[email protected]dcd23fe2009-11-12 20:21:18388void FilebrowseHandler::RegisterMessages() {
[email protected]698601e2009-10-21 22:43:37389 dom_ui_->RegisterMessageCallback("getRoots",
[email protected]dcd23fe2009-11-12 20:21:18390 NewCallback(this, &FilebrowseHandler::HandleGetRoots));
[email protected]698601e2009-10-21 22:43:37391 dom_ui_->RegisterMessageCallback("getChildren",
[email protected]dcd23fe2009-11-12 20:21:18392 NewCallback(this, &FilebrowseHandler::HandleGetChildren));
[email protected]698601e2009-10-21 22:43:37393 dom_ui_->RegisterMessageCallback("getMetadata",
[email protected]dcd23fe2009-11-12 20:21:18394 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]9d6b9aff2009-12-11 17:39:18399 dom_ui_->RegisterMessageCallback("uploadToPicasaweb",
400 NewCallback(this, &FilebrowseHandler::UploadToPicasaweb));
[email protected]274e42b2010-01-29 22:03:57401 dom_ui_->RegisterMessageCallback("getDownloads",
402 NewCallback(this, &FilebrowseHandler::HandleGetDownloads));
403 dom_ui_->RegisterMessageCallback("createNewFolder",
404 NewCallback(this, &FilebrowseHandler::HandleCreateNewFolder));
[email protected]17496bb2010-03-10 19:48:23405 dom_ui_->RegisterMessageCallback("playMediaFile",
406 NewCallback(this, &FilebrowseHandler::PlayMediaFile));
[email protected]a67fa08e2010-02-12 20:43:55407 dom_ui_->RegisterMessageCallback("pauseToggleDownload",
408 NewCallback(this, &FilebrowseHandler::HandlePauseToggleDownload));
409 dom_ui_->RegisterMessageCallback("deleteFile",
410 NewCallback(this, &FilebrowseHandler::HandleDeleteFile));
[email protected]c4a530b2010-03-08 17:33:03411 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]9d6b9aff2009-12-11 17:39:18417}
418
[email protected]a67fa08e2010-02-12 20:43:55419
420void 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]9d6b9aff2009-12-11 17:39:18426void FilebrowseHandler::FireUploadComplete() {
[email protected]d52bb8a2010-05-10 21:05:35427#if defined(OS_CHROMEOS)
[email protected]9d6b9aff2009-12-11 17:39:18428 DictionaryValue info_value;
429 info_value.SetString(L"path", current_file_uploaded_);
430
431 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35432 chromeos::UserManager* user_man = chromeos::UserManager::Get();
433 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18434
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]d52bb8a2010-05-10 21:05:35452#endif
[email protected]9d6b9aff2009-12-11 17:39:18453}
454
[email protected]07046ab2010-01-20 21:42:44455#if defined(OS_CHROMEOS)
456void 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]9d6b9aff2009-12-11 17:39:18466void 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]d52bb8a2010-05-10 21:05:35473 LOG(INFO) << "Response code:" << response_code;
474 LOG(INFO) << "request url" << url;
[email protected]068a71f2010-05-11 22:35:48475 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]698601e2009-10-21 22:43:37481}
482
[email protected]dcd23fe2009-11-12 20:21:18483void FilebrowseHandler::HandleGetRoots(const Value* value) {
[email protected]698601e2009-10-21 22:43:37484 ListValue results_value;
485 DictionaryValue info_value;
[email protected]698601e2009-10-21 22:43:37486 // TODO(dhg): add other entries, make this more general
[email protected]07046ab2010-01-20 21:42:44487#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24488 chromeos::MountLibrary* lib =
489 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44490 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]698601e2009-10-21 22:43:37509 page_value->SetBoolean(kPropertyDirectory, true);
510
511 results_value.Append(page_value);
[email protected]07046ab2010-01-20 21:42:44512#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]698601e2009-10-21 22:43:37525
[email protected]9d6b9aff2009-12-11 17:39:18526 info_value.SetString(L"functionCall", "getRoots");
[email protected]07046ab2010-01-20 21:42:44527 info_value.SetString(kPropertyPath, "");
[email protected]9d6b9aff2009-12-11 17:39:18528 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37529 info_value, results_value);
530}
531
[email protected]274e42b2010-01-29 22:03:57532void 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]17496bb2010-03-10 19:48:23555void 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]c4a530b2010-03-08 17:33:03580void 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]a67fa08e2010-02-12 20:43:55601void 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]377b4cc2010-05-18 17:25:19610 DownloadItem* item = active_download_items_[id];
[email protected]a67fa08e2010-02-12 20:43:55611 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]c4a530b2010-03-08 17:33:03620void 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]377b4cc2010-05-18 17:25:19629 DownloadItem* item = active_download_items_[id];
[email protected]c4a530b2010-03-08 17:33:03630 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
639void 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]377b4cc2010-05-18 17:25:19648 DownloadItem* item = active_download_items_[id];
[email protected]c4a530b2010-03-08 17:33:03649 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]dcd23fe2009-11-12 20:21:18662void FilebrowseHandler::OpenNewFullWindow(const Value* value) {
663 OpenNewWindow(value, false);
664}
[email protected]698601e2009-10-21 22:43:37665
[email protected]dcd23fe2009-11-12 20:21:18666void FilebrowseHandler::OpenNewPopupWindow(const Value* value) {
667 OpenNewWindow(value, true);
668}
669
670void 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]9419bcf2010-04-12 17:26:10690 browser = BrowserList::GetLastActive();
[email protected]dcd23fe2009-11-12 20:21:18691 }
[email protected]50bd0c7322010-05-12 22:07:24692 TabContents* contents = browser->AddTabWithURL(
[email protected]715af7e2010-04-29 01:55:38693 GURL(path), GURL(), PageTransition::LINK, -1, Browser::ADD_SELECTED,
694 NULL, std::string());
[email protected]50bd0c7322010-05-12 22:07:24695 // 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]dcd23fe2009-11-12 20:21:18699 if (popup) {
700 // TODO(dhg): Remove these from being hardcoded. Allow javascript
701 // to specify.
[email protected]e561e202010-03-24 17:57:12702 browser->window()->SetBounds(gfx::Rect(0, 0, 400, 300));
[email protected]dcd23fe2009-11-12 20:21:18703 }
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]d52bb8a2010-05-10 21:05:35711void FilebrowseHandler::SendPicasawebRequest() {
[email protected]068a71f2010-05-11 22:35:48712#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]d52bb8a2010-05-10 21:05:35721 fetch_.reset(URLFetcher::Create(0,
722 GURL(kPicasawebBaseUrl),
723 URLFetcher::GET,
[email protected]068a71f2010-05-11 22:35:48724 this));
[email protected]d52bb8a2010-05-10 21:05:35725 fetch_->set_request_context(profile_->GetRequestContext());
726 fetch_->Start();
[email protected]068a71f2010-05-11 22:35:48727#endif
[email protected]d52bb8a2010-05-10 21:05:35728}
729
[email protected]9d6b9aff2009-12-11 17:39:18730void FilebrowseHandler::ReadInFile() {
731#if defined(OS_CHROMEOS)
732 // Get the users username
733 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35734 chromeos::UserManager* user_man = chromeos::UserManager::Get();
735 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18736
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]d52bb8a2010-05-10 21:05:35761 fetch_.reset(URLFetcher::Create(0,
762 GURL(url),
763 URLFetcher::POST,
764 this));
765 fetch_->set_upload_data("image/jpeg", filecontents);
[email protected]9d6b9aff2009-12-11 17:39:18766 // Set the filename on the server
767 std::string slug = "Slug: ";
768 slug += filename;
[email protected]d52bb8a2010-05-10 21:05:35769 fetch_->set_extra_request_headers(slug);
770 fetch_->set_request_context(profile_->GetRequestContext());
771 fetch_->Start();
[email protected]9d6b9aff2009-12-11 17:39:18772#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.
777void 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]a67fa08e2010-02-12 20:43:55798 FilePath current_path(path);
799 TaskProxy* task = new TaskProxy(AsWeakPtr(), current_path);
[email protected]9d6b9aff2009-12-11 17:39:18800 task->AddRef();
[email protected]d52bb8a2010-05-10 21:05:35801 current_task_ = task;
[email protected]9d6b9aff2009-12-11 17:39:18802 ChromeThread::PostTask(
803 ChromeThread::FILE, FROM_HERE,
804 NewRunnableMethod(
805 task, &TaskProxy::ReadInFileProxy));
806#endif
807}
808
[email protected]a67fa08e2010-02-12 20:43:55809void 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]dcd23fe2009-11-12 20:21:18824void FilebrowseHandler::HandleGetChildren(const Value* value) {
825 std::string path;
[email protected]698601e2009-10-21 22:43:37826 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]dcd23fe2009-11-12 20:21:18839 LOG(ERROR) << "Wasn't able to get the List if requested files.";
[email protected]698601e2009-10-21 22:43:37840 return;
841 }
842 filelist_value_.reset(new ListValue());
[email protected]a67fa08e2010-02-12 20:43:55843 FilePath currentpath;
[email protected]698601e2009-10-21 22:43:37844#if defined(OS_WIN)
[email protected]a67fa08e2010-02-12 20:43:55845 currentpath = FilePath(ASCIIToWide(path));
[email protected]698601e2009-10-21 22:43:37846#else
[email protected]a67fa08e2010-02-12 20:43:55847 currentpath = FilePath(path);
[email protected]698601e2009-10-21 22:43:37848#endif
849
[email protected]a67fa08e2010-02-12 20:43:55850 GetChildrenForPath(currentpath, false);
[email protected]698601e2009-10-21 22:43:37851}
852
[email protected]dcd23fe2009-11-12 20:21:18853void FilebrowseHandler::OnListFile(
[email protected]698601e2009-10-21 22:43:37854 const file_util::FileEnumerator::FindInfo& data) {
[email protected]07046ab2010-01-20 21:42:44855#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]698601e2009-10-21 22:43:37865 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]dcd23fe2009-11-12 20:21:18885void FilebrowseHandler::OnListDone(int error) {
[email protected]698601e2009-10-21 22:43:37886 DictionaryValue info_value;
[email protected]a67fa08e2010-02-12 20:43:55887 if (is_refresh_) {
888 info_value.SetString(L"functionCall", "refresh");
889 } else {
890 info_value.SetString(L"functionCall", "getChildren");
891 }
[email protected]698601e2009-10-21 22:43:37892 info_value.SetString(kPropertyPath, currentpath_.value());
[email protected]9d6b9aff2009-12-11 17:39:18893 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37894 info_value, *(filelist_value_.get()));
[email protected]a67fa08e2010-02-12 20:43:55895 SendCurrentDownloads();
[email protected]698601e2009-10-21 22:43:37896}
897
[email protected]dcd23fe2009-11-12 20:21:18898void FilebrowseHandler::HandleGetMetadata(const Value* value) {
[email protected]698601e2009-10-21 22:43:37899}
900
[email protected]274e42b2010-01-29 22:03:57901void FilebrowseHandler::HandleGetDownloads(const Value* value) {
902 ModelChanged();
903}
904
905void FilebrowseHandler::ModelChanged() {
906 ClearDownloadItems();
[email protected]377b4cc2010-05-18 17:25:19907 download_manager_->GetDownloads(this, std::wstring());
[email protected]274e42b2010-01-29 22:03:57908}
909
910void FilebrowseHandler::SetDownloads(std::vector<DownloadItem*>& downloads) {
911 ClearDownloadItems();
[email protected]377b4cc2010-05-18 17:25:19912 std::vector<DownloadItem*> new_downloads;
[email protected]274e42b2010-01-29 22:03:57913 // 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]377b4cc2010-05-18 17:25:19922 active_download_items_.push_back(download);
[email protected]274e42b2010-01-29 22:03:57923 }
[email protected]377b4cc2010-05-18 17:25:19924 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]274e42b2010-01-29 22:03:57931 }
[email protected]377b4cc2010-05-18 17:25:19932 download_items_.swap(new_downloads);
933 got_first_download_list_ = true;
[email protected]274e42b2010-01-29 22:03:57934 SendCurrentDownloads();
935}
936
[email protected]377b4cc2010-05-18 17:25:19937void 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]a67fa08e2010-02-12 20:43:55943void 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]d52bb8a2010-05-10 21:05:35949 NewRunnableMethod(current_task_, &TaskProxy::FireDeleteCompleteProxy));
[email protected]a67fa08e2010-02-12 20:43:55950}
951
952void 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]377b4cc2010-05-18 17:25:19963 for (unsigned int x = 0; x < active_download_items_.size(); x++) {
964 FilePath item = active_download_items_[x]->full_path();
[email protected]c4a530b2010-03-08 17:33:03965 if (item == currentpath) {
[email protected]377b4cc2010-05-18 17:25:19966 active_download_items_[x]->Cancel(true);
967 active_download_items_[x]->Remove(true);
[email protected]c4a530b2010-03-08 17:33:03968 FilePath dir_path = item.DirName();
969 GetChildrenForPath(dir_path, true);
970 return;
971 }
972 }
[email protected]a67fa08e2010-02-12 20:43:55973 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath);
974 task->AddRef();
[email protected]d52bb8a2010-05-10 21:05:35975 current_task_ = task;
[email protected]a67fa08e2010-02-12 20:43:55976 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]274e42b2010-01-29 22:03:57988void FilebrowseHandler::OnDownloadUpdated(DownloadItem* download) {
[email protected]377b4cc2010-05-18 17:25:19989 DownloadList::iterator it = find(active_download_items_.begin(),
990 active_download_items_.end(),
[email protected]274e42b2010-01-29 22:03:57991 download);
[email protected]377b4cc2010-05-18 17:25:19992 if (it == active_download_items_.end())
[email protected]274e42b2010-01-29 22:03:57993 return;
[email protected]377b4cc2010-05-18 17:25:19994 const int id = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:57995
996 ListValue results_value;
997 results_value.Append(download_util::CreateDownloadItemValue(download, id));
998 dom_ui_->CallJavascriptFunction(L"downloadUpdated", results_value);
999}
1000
1001void FilebrowseHandler::ClearDownloadItems() {
[email protected]377b4cc2010-05-18 17:25:191002 for (DownloadList::iterator it = active_download_items_.begin();
1003 it != active_download_items_.end(); ++it) {
[email protected]274e42b2010-01-29 22:03:571004 (*it)->RemoveObserver(this);
1005 }
[email protected]377b4cc2010-05-18 17:25:191006 active_download_items_.clear();
[email protected]274e42b2010-01-29 22:03:571007}
1008
1009void FilebrowseHandler::SendCurrentDownloads() {
1010 ListValue results_value;
[email protected]377b4cc2010-05-18 17:25:191011 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]274e42b2010-01-29 22:03:571014 results_value.Append(download_util::CreateDownloadItemValue(*it, index));
1015 }
1016
1017 dom_ui_->CallJavascriptFunction(L"downloadsList", results_value);
1018}
1019
[email protected]698601e2009-10-21 22:43:371020////////////////////////////////////////////////////////////////////////////////
1021//
[email protected]f5bf8ccf2010-02-05 18:19:251022// FileBrowseUI
[email protected]698601e2009-10-21 22:43:371023//
1024////////////////////////////////////////////////////////////////////////////////
1025
[email protected]274e42b2010-01-29 22:03:571026FileBrowseUI::FileBrowseUI(TabContents* contents) : HtmlDialogUI(contents) {
[email protected]9d6b9aff2009-12-11 17:39:181027 FilebrowseHandler* handler = new FilebrowseHandler();
1028 AddMessageHandler((handler)->Attach(this));
[email protected]274e42b2010-01-29 22:03:571029 handler->Init();
[email protected]698601e2009-10-21 22:43:371030 FileBrowseUIHTMLSource* html_source = new FileBrowseUIHTMLSource();
1031
1032 // Set up the chrome://filebrowse/ source.
[email protected]fae20792009-10-28 20:31:581033 ChromeThread::PostTask(
1034 ChromeThread::IO, FROM_HERE,
1035 NewRunnableMethod(
[email protected]576a4ca2009-11-05 01:41:091036 Singleton<ChromeURLDataManager>::get(),
[email protected]698601e2009-10-21 22:43:371037 &ChromeURLDataManager::AddDataSource,
[email protected]f8f82502009-11-20 23:14:231038 make_scoped_refptr(html_source)));
[email protected]698601e2009-10-21 22:43:371039}
[email protected]f5bf8ccf2010-02-05 18:19:251040
1041// static
[email protected]1717246f2010-02-10 17:08:151042Browser* FileBrowseUI::OpenPopup(Profile* profile,
[email protected]467293292010-04-16 19:41:561043 const std::string& hashArgument,
1044 int width,
1045 int height) {
[email protected]1717246f2010-02-10 17:08:151046 // Get existing pop up for given hashArgument.
1047 Browser* browser = GetPopupForPath(hashArgument);
[email protected]f5bf8ccf2010-02-05 18:19:251048
[email protected]1717246f2010-02-10 17:08:151049 // Create new browser if no matching pop up found.
1050 if (browser == NULL) {
1051 browser = Browser::CreateForPopup(profile);
[email protected]f59f1162010-04-23 21:19:291052 std::string url;
1053 if (hashArgument.empty()) {
1054 url = chrome::kChromeUIFileBrowseURL;
1055 } else {
1056 url = kFilebrowseURLHash;
1057 url.append(hashArgument);
1058 }
[email protected]f5bf8ccf2010-02-05 18:19:251059
[email protected]1717246f2010-02-10 17:08:151060 browser->AddTabWithURL(
[email protected]715af7e2010-04-29 01:55:381061 GURL(url), GURL(), PageTransition::LINK, -1, Browser::ADD_SELECTED,
1062 NULL, std::string());
[email protected]1717246f2010-02-10 17:08:151063 browser->window()->SetBounds(gfx::Rect(kPopupLeft,
1064 kPopupTop,
[email protected]467293292010-04-16 19:41:561065 width,
1066 height));
[email protected]1717246f2010-02-10 17:08:151067
1068 browser->window()->Show();
1069 }
[email protected]f5bf8ccf2010-02-05 18:19:251070
1071 return browser;
1072}
[email protected]1717246f2010-02-10 17:08:151073
1074Browser* 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]467293292010-04-16 19:41:561091
1092const int FileBrowseUI::kPopupWidth = 250;
1093const int FileBrowseUI::kPopupHeight = 300;
1094const int FileBrowseUI::kSmallPopupWidth = 250;
1095const int FileBrowseUI::kSmallPopupHeight = 50;