blob: 6d78005bbbd00f9d2b88549ead9515423d063f55 [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"
[email protected]be1ce6a72010-08-03 14:35:2218#include "base/utf_string_conversions.h"
[email protected]698601e2009-10-21 22:43:3719#include "base/values.h"
[email protected]9d6b9aff2009-12-11 17:39:1820#include "base/weak_ptr.h"
[email protected]698601e2009-10-21 22:43:3721#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]dcd23fe2009-11-12 20:21:1822#include "chrome/browser/browser.h"
[email protected]1717246f2010-02-10 17:08:1523#include "chrome/browser/browser_list.h"
[email protected]dcd23fe2009-11-12 20:21:1824#include "chrome/browser/browser_window.h"
[email protected]fae20792009-10-28 20:31:5825#include "chrome/browser/chrome_thread.h"
[email protected]698601e2009-10-21 22:43:3726#include "chrome/browser/dom_ui/dom_ui_favicon_source.h"
[email protected]75986612010-07-16 22:23:5727#include "chrome/browser/download/download_item.h"
[email protected]274e42b2010-01-29 22:03:5728#include "chrome/browser/download/download_manager.h"
29#include "chrome/browser/download/download_util.h"
[email protected]1717246f2010-02-10 17:08:1530#include "chrome/browser/history/history_types.h"
[email protected]698601e2009-10-21 22:43:3731#include "chrome/browser/metrics/user_metrics.h"
[email protected]6ba198f2010-07-02 20:32:0532#include "chrome/browser/pref_service.h"
[email protected]698601e2009-10-21 22:43:3733#include "chrome/browser/profile.h"
[email protected]1717246f2010-02-10 17:08:1534#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]68d2a05f2010-05-07 21:39:5535#include "chrome/browser/dom_ui/mediaplayer_ui.h"
[email protected]07046ab2010-01-20 21:42:4436#include "chrome/common/chrome_paths.h"
[email protected]698601e2009-10-21 22:43:3737#include "chrome/common/jstemplate_builder.h"
[email protected]68d2a05f2010-05-07 21:39:5538#include "chrome/common/net/url_fetcher.h"
[email protected]6ba198f2010-07-02 20:32:0539#include "chrome/common/pref_names.h"
[email protected]698601e2009-10-21 22:43:3740#include "chrome/common/time_format.h"
41#include "chrome/common/url_constants.h"
42#include "net/base/escape.h"
43
44#include "grit/browser_resources.h"
45#include "grit/chromium_strings.h"
46#include "grit/generated_resources.h"
47#include "grit/locale_settings.h"
48
[email protected]07046ab2010-01-20 21:42:4449#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:2450#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]268b02f2010-02-04 21:07:1551#include "chrome/browser/chromeos/cros/mount_library.h"
[email protected]d52bb8a2010-05-10 21:05:3552#include "chrome/browser/chromeos/login/user_manager.h"
[email protected]07046ab2010-01-20 21:42:4453#endif
54
[email protected]698601e2009-10-21 22:43:3755// Maximum number of search results to return in a given search. We should
56// eventually remove this.
57static const int kMaxSearchResults = 100;
[email protected]e4fbe8e2010-08-07 07:00:4658static const char kPropertyPath[] = "path";
59static const char kPropertyTitle[] = "title";
60static const char kPropertyDirectory[] = "isDirectory";
61static const char kPicasawebUserPrefix[] =
[email protected]9d6b9aff2009-12-11 17:39:1862 "http://picasaweb.google.com/data/feed/api/user/";
[email protected]e4fbe8e2010-08-07 07:00:4663static const char kPicasawebDefault[] = "/albumid/default";
64static const char kPicasawebDropBox[] = "/home";
65static const char kPicasawebBaseUrl[] = "http://picasaweb.google.com/";
66static const char kMediaPath[] = "/media";
67static const char kFilebrowseURLHash[] = "chrome://filebrowse#";
[email protected]f5bf8ccf2010-02-05 18:19:2568static const int kPopupLeft = 0;
69static const int kPopupTop = 0;
[email protected]f5bf8ccf2010-02-05 18:19:2570
[email protected]698601e2009-10-21 22:43:3771class FileBrowseUIHTMLSource : public ChromeURLDataManager::DataSource {
72 public:
73 FileBrowseUIHTMLSource();
74
75 // Called when the network layer has requested a resource underneath
76 // the path we registered.
[email protected]f09d93792009-11-17 00:10:3677 virtual void StartDataRequest(const std::string& path,
78 bool is_off_the_record,
79 int request_id);
[email protected]698601e2009-10-21 22:43:3780 virtual std::string GetMimeType(const std::string&) const {
81 return "text/html";
82 }
83
84 private:
[email protected]8de85a62009-11-06 08:32:1785 ~FileBrowseUIHTMLSource() {}
86
[email protected]698601e2009-10-21 22:43:3787 DISALLOW_COPY_AND_ASSIGN(FileBrowseUIHTMLSource);
88};
89
[email protected]9d6b9aff2009-12-11 17:39:1890class TaskProxy;
91
[email protected]698601e2009-10-21 22:43:3792// The handler for Javascript messages related to the "filebrowse" view.
[email protected]dcd23fe2009-11-12 20:21:1893class FilebrowseHandler : public net::DirectoryLister::DirectoryListerDelegate,
[email protected]9d6b9aff2009-12-11 17:39:1894 public DOMMessageHandler,
[email protected]07046ab2010-01-20 21:42:4495#if defined(OS_CHROMEOS)
96 public chromeos::MountLibrary::Observer,
97#endif
[email protected]9d6b9aff2009-12-11 17:39:1898 public base::SupportsWeakPtr<FilebrowseHandler>,
[email protected]274e42b2010-01-29 22:03:5799 public URLFetcher::Delegate,
100 public DownloadManager::Observer,
101 public DownloadItem::Observer {
[email protected]698601e2009-10-21 22:43:37102 public:
[email protected]dcd23fe2009-11-12 20:21:18103 FilebrowseHandler();
104 virtual ~FilebrowseHandler();
[email protected]698601e2009-10-21 22:43:37105
[email protected]274e42b2010-01-29 22:03:57106 // Init work after Attach.
107 void Init();
108
[email protected]698601e2009-10-21 22:43:37109 // DirectoryLister::DirectoryListerDelegate methods:
[email protected]9ee2c8d2010-08-21 23:25:22110 virtual void OnListFile(
111 const net::DirectoryLister::DirectoryListerData& data);
[email protected]698601e2009-10-21 22:43:37112 virtual void OnListDone(int error);
113
114 // DOMMessageHandler implementation.
115 virtual DOMMessageHandler* Attach(DOMUI* dom_ui);
116 virtual void RegisterMessages();
117
[email protected]07046ab2010-01-20 21:42:44118#if defined(OS_CHROMEOS)
119 void MountChanged(chromeos::MountLibrary* obj,
120 chromeos::MountEventType evt,
121 const std::string& path);
122#endif
123
[email protected]274e42b2010-01-29 22:03:57124 // DownloadItem::Observer interface
125 virtual void OnDownloadUpdated(DownloadItem* download);
126 virtual void OnDownloadFileCompleted(DownloadItem* download) { }
127 virtual void OnDownloadOpened(DownloadItem* download) { }
128
129 // DownloadManager::Observer interface
130 virtual void ModelChanged();
[email protected]82f37b02010-07-29 22:04:57131
[email protected]698601e2009-10-21 22:43:37132 // Callback for the "getRoots" message.
[email protected]88942a22010-08-19 20:34:43133 void HandleGetRoots(const ListValue* args);
[email protected]698601e2009-10-21 22:43:37134
[email protected]a67fa08e2010-02-12 20:43:55135 void GetChildrenForPath(FilePath& path, bool is_refresh);
136
[email protected]9d6b9aff2009-12-11 17:39:18137 void OnURLFetchComplete(const URLFetcher* source,
138 const GURL& url,
139 const URLRequestStatus& status,
140 int response_code,
141 const ResponseCookies& cookies,
142 const std::string& data);
143
[email protected]698601e2009-10-21 22:43:37144 // Callback for the "getChildren" message.
[email protected]88942a22010-08-19 20:34:43145 void HandleGetChildren(const ListValue* args);
[email protected]c4a530b2010-03-08 17:33:03146 // Callback for the "refreshDirectory" message.
[email protected]88942a22010-08-19 20:34:43147 void HandleRefreshDirectory(const ListValue* args);
148 void HandleIsAdvancedEnabled(const ListValue* args);
[email protected]698601e2009-10-21 22:43:37149
150 // Callback for the "getMetadata" message.
[email protected]88942a22010-08-19 20:34:43151 void HandleGetMetadata(const ListValue* args);
[email protected]698601e2009-10-21 22:43:37152
[email protected]9d6b9aff2009-12-11 17:39:18153 // Callback for the "openNewWindow" message.
[email protected]88942a22010-08-19 20:34:43154 void OpenNewFullWindow(const ListValue* args);
155 void OpenNewPopupWindow(const ListValue* args);
[email protected]dcd23fe2009-11-12 20:21:18156
[email protected]9d6b9aff2009-12-11 17:39:18157 // Callback for the "uploadToPicasaweb" message.
[email protected]88942a22010-08-19 20:34:43158 void UploadToPicasaweb(const ListValue* args);
[email protected]9d6b9aff2009-12-11 17:39:18159
[email protected]274e42b2010-01-29 22:03:57160 // Callback for the "getDownloads" message.
[email protected]88942a22010-08-19 20:34:43161 void HandleGetDownloads(const ListValue* args);
[email protected]274e42b2010-01-29 22:03:57162
[email protected]88942a22010-08-19 20:34:43163 void HandleCreateNewFolder(const ListValue* args);
[email protected]274e42b2010-01-29 22:03:57164
[email protected]88942a22010-08-19 20:34:43165 void PlayMediaFile(const ListValue* args);
166 void EnqueueMediaFile(const ListValue* args);
[email protected]17496bb2010-03-10 19:48:23167
[email protected]88942a22010-08-19 20:34:43168 void HandleDeleteFile(const ListValue* args);
[email protected]a67fa08e2010-02-12 20:43:55169 void DeleteFile(const FilePath& path);
170 void FireDeleteComplete(const FilePath& path);
171
[email protected]88942a22010-08-19 20:34:43172 void HandlePauseToggleDownload(const ListValue* args);
[email protected]a67fa08e2010-02-12 20:43:55173
[email protected]88942a22010-08-19 20:34:43174 void HandleCancelDownload(const ListValue* args);
175 void HandleAllowDownload(const ListValue* args);
[email protected]c4a530b2010-03-08 17:33:03176
[email protected]9d6b9aff2009-12-11 17:39:18177 void ReadInFile();
178 void FireUploadComplete();
179
[email protected]d52bb8a2010-05-10 21:05:35180 void SendPicasawebRequest();
[email protected]698601e2009-10-21 22:43:37181 private:
[email protected]dcd23fe2009-11-12 20:21:18182
[email protected]88942a22010-08-19 20:34:43183 void OpenNewWindow(const ListValue* args, bool popup);
[email protected]dcd23fe2009-11-12 20:21:18184
[email protected]274e42b2010-01-29 22:03:57185 // Clear all download items and their observers.
186 void ClearDownloadItems();
187
188 // Send the current list of downloads to the page.
189 void SendCurrentDownloads();
190
[email protected]377b4cc2010-05-18 17:25:19191 void SendNewDownload(DownloadItem* download);
192
[email protected]698601e2009-10-21 22:43:37193 scoped_ptr<ListValue> filelist_value_;
194 FilePath currentpath_;
[email protected]dcd23fe2009-11-12 20:21:18195 Profile* profile_;
[email protected]93a58bf72010-06-04 23:01:20196 TabContents* tab_contents_;
[email protected]9d6b9aff2009-12-11 17:39:18197 std::string current_file_contents_;
198 std::string current_file_uploaded_;
199 int upload_response_code_;
[email protected]d52bb8a2010-05-10 21:05:35200 TaskProxy* current_task_;
[email protected]698601e2009-10-21 22:43:37201 scoped_refptr<net::DirectoryLister> lister_;
[email protected]a67fa08e2010-02-12 20:43:55202 bool is_refresh_;
[email protected]d52bb8a2010-05-10 21:05:35203 scoped_ptr<URLFetcher> fetch_;
[email protected]698601e2009-10-21 22:43:37204
[email protected]274e42b2010-01-29 22:03:57205 DownloadManager* download_manager_;
206 typedef std::vector<DownloadItem*> DownloadList;
[email protected]377b4cc2010-05-18 17:25:19207 DownloadList active_download_items_;
[email protected]274e42b2010-01-29 22:03:57208 DownloadList download_items_;
[email protected]377b4cc2010-05-18 17:25:19209 bool got_first_download_list_;
[email protected]dcd23fe2009-11-12 20:21:18210 DISALLOW_COPY_AND_ASSIGN(FilebrowseHandler);
[email protected]698601e2009-10-21 22:43:37211};
212
[email protected]9d6b9aff2009-12-11 17:39:18213class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> {
214 public:
[email protected]d52bb8a2010-05-10 21:05:35215 explicit TaskProxy(const base::WeakPtr<FilebrowseHandler>& handler,
216 FilePath& path)
[email protected]a67fa08e2010-02-12 20:43:55217 : handler_(handler),
218 path_(path) {}
[email protected]9d6b9aff2009-12-11 17:39:18219 void ReadInFileProxy() {
220 if (handler_) {
221 handler_->ReadInFile();
222 }
223 }
[email protected]068a71f2010-05-11 22:35:48224 void DeleteFetcher(URLFetcher* fetch) {
225 delete fetch;
226 }
[email protected]d52bb8a2010-05-10 21:05:35227 void SendPicasawebRequestProxy() {
228 if (handler_) {
229 handler_->SendPicasawebRequest();
230 }
231 }
[email protected]9d6b9aff2009-12-11 17:39:18232 void FireUploadCompleteProxy() {
233 if (handler_) {
234 handler_->FireUploadComplete();
235 }
236 }
[email protected]a67fa08e2010-02-12 20:43:55237
238 void DeleteFileProxy() {
239 if (handler_) {
240 handler_->DeleteFile(path_);
241 }
242 }
243
244 void FireDeleteCompleteProxy() {
245 if (handler_) {
246 handler_->FireDeleteComplete(path_);
247 }
248 }
[email protected]9d6b9aff2009-12-11 17:39:18249 private:
250 base::WeakPtr<FilebrowseHandler> handler_;
[email protected]a67fa08e2010-02-12 20:43:55251 FilePath path_;
[email protected]9d6b9aff2009-12-11 17:39:18252 friend class base::RefCountedThreadSafe<TaskProxy>;
[email protected]068a71f2010-05-11 22:35:48253 DISALLOW_COPY_AND_ASSIGN(TaskProxy);
[email protected]9d6b9aff2009-12-11 17:39:18254};
255
256
[email protected]698601e2009-10-21 22:43:37257////////////////////////////////////////////////////////////////////////////////
258//
259// FileBrowseHTMLSource
260//
261////////////////////////////////////////////////////////////////////////////////
262
263FileBrowseUIHTMLSource::FileBrowseUIHTMLSource()
264 : DataSource(chrome::kChromeUIFileBrowseHost, MessageLoop::current()) {
265}
266
267void FileBrowseUIHTMLSource::StartDataRequest(const std::string& path,
[email protected]e42ae9b2010-03-28 00:55:05268 bool is_off_the_record,
269 int request_id) {
[email protected]698601e2009-10-21 22:43:37270 DictionaryValue localized_strings;
[email protected]11f4857282009-11-13 19:56:17271 // TODO(dhg): Add stirings to localized strings, also add more strings
[email protected]dcd23fe2009-11-12 20:21:18272 // that are currently hardcoded.
[email protected]e4fbe8e2010-08-07 07:00:46273 localized_strings.SetString("title",
274 l10n_util::GetStringUTF16(IDS_FILEBROWSER_TITLE));
275 localized_strings.SetString("pause",
276 l10n_util::GetStringUTF16(IDS_FILEBROWSER_PAUSE));
277 localized_strings.SetString("resume",
278 l10n_util::GetStringUTF16(IDS_FILEBROWSER_RESUME));
279 localized_strings.SetString("scanning",
280 l10n_util::GetStringUTF16(IDS_FILEBROWSER_SCANNING));
281 localized_strings.SetString("confirmdelete",
282 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_DELETE));
283 localized_strings.SetString("confirmyes",
284 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_YES));
285 localized_strings.SetString("confirmcancel",
286 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_CANCEL));
287 localized_strings.SetString("allowdownload",
288 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_DOWNLOAD));
289 localized_strings.SetString("filenameprompt",
290 l10n_util::GetStringUTF16(IDS_FILEBROWSER_PROMPT_FILENAME));
291 localized_strings.SetString("save",
292 l10n_util::GetStringUTF16(IDS_FILEBROWSER_SAVE));
293 localized_strings.SetString("newfolder",
294 l10n_util::GetStringUTF16(IDS_FILEBROWSER_NEW_FOLDER));
295 localized_strings.SetString("open",
296 l10n_util::GetStringUTF16(IDS_FILEBROWSER_OPEN));
297 localized_strings.SetString("picasaweb",
298 l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_PICASAWEB));
299 localized_strings.SetString("flickr",
300 l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_FLICKR));
301 localized_strings.SetString("email",
302 l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_EMAIL));
303 localized_strings.SetString("delete",
304 l10n_util::GetStringUTF16(IDS_FILEBROWSER_DELETE));
305 localized_strings.SetString("enqueue",
306 l10n_util::GetStringUTF16(IDS_FILEBROWSER_ENQUEUE));
307 localized_strings.SetString("mediapath", kMediaPath);
[email protected]88f03da02010-06-10 16:21:13308 FilePath default_download_path;
309 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
310 &default_download_path)) {
311 NOTREACHED();
312 }
[email protected]e4fbe8e2010-08-07 07:00:46313 // TODO(viettrungluu): this is wrong -- FilePath's need not be Unicode.
314 localized_strings.SetString("downloadpath", default_download_path.value());
315 localized_strings.SetString("error_unknown_file_type",
316 l10n_util::GetStringUTF16(IDS_FILEBROWSER_ERROR_UNKNOWN_FILE_TYPE));
[email protected]698601e2009-10-21 22:43:37317 SetFontAndTextDirection(&localized_strings);
318
319 static const base::StringPiece filebrowse_html(
320 ResourceBundle::GetSharedInstance().GetRawDataResource(
321 IDR_FILEBROWSE_HTML));
322 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml(
323 filebrowse_html, &localized_strings);
324
325 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
326 html_bytes->data.resize(full_html.size());
327 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
328
329 SendResponse(request_id, html_bytes);
330}
331
332////////////////////////////////////////////////////////////////////////////////
333//
[email protected]dcd23fe2009-11-12 20:21:18334// FilebrowseHandler
[email protected]698601e2009-10-21 22:43:37335//
336////////////////////////////////////////////////////////////////////////////////
[email protected]dcd23fe2009-11-12 20:21:18337FilebrowseHandler::FilebrowseHandler()
[email protected]274e42b2010-01-29 22:03:57338 : profile_(NULL),
[email protected]93a58bf72010-06-04 23:01:20339 tab_contents_(NULL),
[email protected]a67fa08e2010-02-12 20:43:55340 is_refresh_(false),
[email protected]d52bb8a2010-05-10 21:05:35341 fetch_(NULL),
[email protected]377b4cc2010-05-18 17:25:19342 download_manager_(NULL),
343 got_first_download_list_(false) {
[email protected]a67fa08e2010-02-12 20:43:55344 lister_ = NULL;
[email protected]07046ab2010-01-20 21:42:44345#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24346 chromeos::MountLibrary* lib =
347 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44348 lib->AddObserver(this);
349#endif
[email protected]698601e2009-10-21 22:43:37350}
351
[email protected]dcd23fe2009-11-12 20:21:18352FilebrowseHandler::~FilebrowseHandler() {
[email protected]07046ab2010-01-20 21:42:44353#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24354 chromeos::MountLibrary* lib =
355 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44356 lib->RemoveObserver(this);
357#endif
[email protected]698601e2009-10-21 22:43:37358 if (lister_.get()) {
359 lister_->Cancel();
360 lister_->set_delegate(NULL);
361 }
[email protected]274e42b2010-01-29 22:03:57362
363 ClearDownloadItems();
364 download_manager_->RemoveObserver(this);
[email protected]068a71f2010-05-11 22:35:48365 URLFetcher* fetch = fetch_.release();
366 if (fetch) {
367 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath_);
368 task->AddRef();
369 ChromeThread::PostTask(
370 ChromeThread::FILE, FROM_HERE,
371 NewRunnableMethod(
372 task, &TaskProxy::DeleteFetcher, fetch));
373 }
[email protected]698601e2009-10-21 22:43:37374}
375
[email protected]dcd23fe2009-11-12 20:21:18376DOMMessageHandler* FilebrowseHandler::Attach(DOMUI* dom_ui) {
[email protected]698601e2009-10-21 22:43:37377 // Create our favicon data source.
[email protected]fae20792009-10-28 20:31:58378 ChromeThread::PostTask(
379 ChromeThread::IO, FROM_HERE,
380 NewRunnableMethod(
[email protected]576a4ca2009-11-05 01:41:09381 Singleton<ChromeURLDataManager>::get(),
[email protected]fae20792009-10-28 20:31:58382 &ChromeURLDataManager::AddDataSource,
[email protected]f8f82502009-11-20 23:14:23383 make_scoped_refptr(new DOMUIFavIconSource(dom_ui->GetProfile()))));
[email protected]dcd23fe2009-11-12 20:21:18384 profile_ = dom_ui->GetProfile();
[email protected]93a58bf72010-06-04 23:01:20385 tab_contents_ = dom_ui->tab_contents();
[email protected]698601e2009-10-21 22:43:37386 return DOMMessageHandler::Attach(dom_ui);
387}
388
[email protected]274e42b2010-01-29 22:03:57389void FilebrowseHandler::Init() {
390 download_manager_ = profile_->GetOriginalProfile()->GetDownloadManager();
391 download_manager_->AddObserver(this);
[email protected]d52bb8a2010-05-10 21:05:35392 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath_);
393 task->AddRef();
394 current_task_ = task;
395 static bool sent_request = false;
396 if (!sent_request) {
397 // If we have not sent a request before, we should do one in order to
398 // ensure that we have the correct cookies. This is for uploads.
399 ChromeThread::PostTask(
400 ChromeThread::FILE, FROM_HERE,
401 NewRunnableMethod(
402 task, &TaskProxy::SendPicasawebRequestProxy));
403 sent_request = true;
404 }
[email protected]274e42b2010-01-29 22:03:57405}
406
[email protected]dcd23fe2009-11-12 20:21:18407void FilebrowseHandler::RegisterMessages() {
[email protected]698601e2009-10-21 22:43:37408 dom_ui_->RegisterMessageCallback("getRoots",
[email protected]dcd23fe2009-11-12 20:21:18409 NewCallback(this, &FilebrowseHandler::HandleGetRoots));
[email protected]698601e2009-10-21 22:43:37410 dom_ui_->RegisterMessageCallback("getChildren",
[email protected]dcd23fe2009-11-12 20:21:18411 NewCallback(this, &FilebrowseHandler::HandleGetChildren));
[email protected]698601e2009-10-21 22:43:37412 dom_ui_->RegisterMessageCallback("getMetadata",
[email protected]dcd23fe2009-11-12 20:21:18413 NewCallback(this, &FilebrowseHandler::HandleGetMetadata));
414 dom_ui_->RegisterMessageCallback("openNewPopupWindow",
415 NewCallback(this, &FilebrowseHandler::OpenNewPopupWindow));
416 dom_ui_->RegisterMessageCallback("openNewFullWindow",
417 NewCallback(this, &FilebrowseHandler::OpenNewFullWindow));
[email protected]9d6b9aff2009-12-11 17:39:18418 dom_ui_->RegisterMessageCallback("uploadToPicasaweb",
419 NewCallback(this, &FilebrowseHandler::UploadToPicasaweb));
[email protected]274e42b2010-01-29 22:03:57420 dom_ui_->RegisterMessageCallback("getDownloads",
421 NewCallback(this, &FilebrowseHandler::HandleGetDownloads));
422 dom_ui_->RegisterMessageCallback("createNewFolder",
423 NewCallback(this, &FilebrowseHandler::HandleCreateNewFolder));
[email protected]17496bb2010-03-10 19:48:23424 dom_ui_->RegisterMessageCallback("playMediaFile",
425 NewCallback(this, &FilebrowseHandler::PlayMediaFile));
[email protected]89a7e3812010-06-02 19:38:07426 dom_ui_->RegisterMessageCallback("enqueueMediaFile",
427 NewCallback(this, &FilebrowseHandler::EnqueueMediaFile));
[email protected]a67fa08e2010-02-12 20:43:55428 dom_ui_->RegisterMessageCallback("pauseToggleDownload",
429 NewCallback(this, &FilebrowseHandler::HandlePauseToggleDownload));
430 dom_ui_->RegisterMessageCallback("deleteFile",
431 NewCallback(this, &FilebrowseHandler::HandleDeleteFile));
[email protected]c4a530b2010-03-08 17:33:03432 dom_ui_->RegisterMessageCallback("cancelDownload",
433 NewCallback(this, &FilebrowseHandler::HandleCancelDownload));
434 dom_ui_->RegisterMessageCallback("allowDownload",
435 NewCallback(this, &FilebrowseHandler::HandleAllowDownload));
436 dom_ui_->RegisterMessageCallback("refreshDirectory",
437 NewCallback(this, &FilebrowseHandler::HandleRefreshDirectory));
[email protected]6ba198f2010-07-02 20:32:05438 dom_ui_->RegisterMessageCallback("isAdvancedEnabled",
439 NewCallback(this, &FilebrowseHandler::HandleIsAdvancedEnabled));
[email protected]9d6b9aff2009-12-11 17:39:18440}
441
[email protected]a67fa08e2010-02-12 20:43:55442
443void FilebrowseHandler::FireDeleteComplete(const FilePath& path) {
444 // We notify the UI by telling it to refresh its contents.
445 FilePath dir_path = path.DirName();
446 GetChildrenForPath(dir_path, true);
447};
448
[email protected]9d6b9aff2009-12-11 17:39:18449void FilebrowseHandler::FireUploadComplete() {
[email protected]d52bb8a2010-05-10 21:05:35450#if defined(OS_CHROMEOS)
[email protected]9d6b9aff2009-12-11 17:39:18451 DictionaryValue info_value;
[email protected]e4fbe8e2010-08-07 07:00:46452 info_value.SetString("path", current_file_uploaded_);
[email protected]9d6b9aff2009-12-11 17:39:18453
454 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35455 chromeos::UserManager* user_man = chromeos::UserManager::Get();
456 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18457
458 if (username.empty()) {
459 LOG(ERROR) << "Unable to get username";
460 return;
461 }
462 int location = username.find_first_of('@',0);
463 if (location <= 0) {
464 LOG(ERROR) << "Username not formatted correctly";
465 return;
466 }
467 username = username.erase(username.find_first_of('@',0));
[email protected]e4fbe8e2010-08-07 07:00:46468 std::string picture_url = kPicasawebBaseUrl;
[email protected]9d6b9aff2009-12-11 17:39:18469 picture_url += username;
470 picture_url += kPicasawebDropBox;
[email protected]e4fbe8e2010-08-07 07:00:46471 info_value.SetString("url", picture_url);
472 info_value.SetInteger("status_code", upload_response_code_);
[email protected]9d6b9aff2009-12-11 17:39:18473 dom_ui_->CallJavascriptFunction(L"uploadComplete", info_value);
[email protected]d52bb8a2010-05-10 21:05:35474#endif
[email protected]9d6b9aff2009-12-11 17:39:18475}
476
[email protected]07046ab2010-01-20 21:42:44477#if defined(OS_CHROMEOS)
478void FilebrowseHandler::MountChanged(chromeos::MountLibrary* obj,
479 chromeos::MountEventType evt,
480 const std::string& path) {
481 if (evt == chromeos::DISK_REMOVED ||
482 evt == chromeos::DISK_CHANGED) {
483 dom_ui_->CallJavascriptFunction(L"rootsChanged");
484 }
485}
486#endif
487
[email protected]9d6b9aff2009-12-11 17:39:18488void FilebrowseHandler::OnURLFetchComplete(const URLFetcher* source,
489 const GURL& url,
490 const URLRequestStatus& status,
491 int response_code,
492 const ResponseCookies& cookies,
493 const std::string& data) {
494 upload_response_code_ = response_code;
[email protected]d52bb8a2010-05-10 21:05:35495 LOG(INFO) << "Response code:" << response_code;
496 LOG(INFO) << "request url" << url;
[email protected]068a71f2010-05-11 22:35:48497 if (StartsWithASCII(url.spec(), kPicasawebUserPrefix, true)) {
498 ChromeThread::PostTask(
499 ChromeThread::UI, FROM_HERE,
500 NewRunnableMethod(current_task_, &TaskProxy::FireUploadCompleteProxy));
501 }
502 fetch_.reset();
[email protected]698601e2009-10-21 22:43:37503}
504
[email protected]88942a22010-08-19 20:34:43505void FilebrowseHandler::HandleGetRoots(const ListValue* args) {
[email protected]698601e2009-10-21 22:43:37506 ListValue results_value;
507 DictionaryValue info_value;
[email protected]698601e2009-10-21 22:43:37508 // TODO(dhg): add other entries, make this more general
[email protected]07046ab2010-01-20 21:42:44509#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24510 chromeos::MountLibrary* lib =
511 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44512 const chromeos::MountLibrary::DiskVector& disks = lib->disks();
513
514 for (size_t i = 0; i < disks.size(); ++i) {
515 if (!disks[i].mount_path.empty()) {
516 DictionaryValue* page_value = new DictionaryValue();
517 page_value->SetString(kPropertyPath, disks[i].mount_path);
[email protected]88942a22010-08-19 20:34:43518 FilePath currentpath(disks[i].mount_path);
[email protected]07046ab2010-01-20 21:42:44519 std::string filename;
520 filename = currentpath.BaseName().value();
521 page_value->SetString(kPropertyTitle, filename);
522 page_value->SetBoolean(kPropertyDirectory, true);
523 results_value.Append(page_value);
524 }
525 }
526#else
527 DictionaryValue* page_value = new DictionaryValue();
528 page_value->SetString(kPropertyPath, "/media");
529 page_value->SetString(kPropertyTitle, "Removeable");
[email protected]698601e2009-10-21 22:43:37530 page_value->SetBoolean(kPropertyDirectory, true);
531
532 results_value.Append(page_value);
[email protected]07046ab2010-01-20 21:42:44533#endif
534 FilePath default_download_path;
535 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
536 &default_download_path)) {
537 NOTREACHED();
538 }
539
540 DictionaryValue* download_value = new DictionaryValue();
541 download_value->SetString(kPropertyPath, default_download_path.value());
542 download_value->SetString(kPropertyTitle, "File Shelf");
543 download_value->SetBoolean(kPropertyDirectory, true);
544
545 results_value.Append(download_value);
[email protected]698601e2009-10-21 22:43:37546
[email protected]e4fbe8e2010-08-07 07:00:46547 info_value.SetString("functionCall", "getRoots");
[email protected]07046ab2010-01-20 21:42:44548 info_value.SetString(kPropertyPath, "");
[email protected]9d6b9aff2009-12-11 17:39:18549 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37550 info_value, results_value);
551}
552
[email protected]88942a22010-08-19 20:34:43553void FilebrowseHandler::HandleCreateNewFolder(const ListValue* args) {
[email protected]274e42b2010-01-29 22:03:57554#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43555 std::string path = WideToUTF8(ExtractStringValue(args));
556 FilePath currentpath(path);
[email protected]274e42b2010-01-29 22:03:57557
[email protected]88942a22010-08-19 20:34:43558 if (!file_util::CreateDirectory(currentpath))
559 LOG(ERROR) << "unable to create directory";
[email protected]274e42b2010-01-29 22:03:57560#endif
561}
562
[email protected]88942a22010-08-19 20:34:43563void FilebrowseHandler::PlayMediaFile(const ListValue* args) {
[email protected]17496bb2010-03-10 19:48:23564#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43565 std::string url = WideToUTF8(ExtractStringValue(args));
566 GURL gurl(url);
[email protected]17496bb2010-03-10 19:48:23567
[email protected]88942a22010-08-19 20:34:43568 Browser* browser = Browser::GetBrowserForController(
569 &tab_contents_->controller(), NULL);
570 MediaPlayer* mediaplayer = MediaPlayer::Get();
571 mediaplayer->ForcePlayMediaURL(gurl, browser);
[email protected]89a7e3812010-06-02 19:38:07572#endif
573}
574
[email protected]88942a22010-08-19 20:34:43575void FilebrowseHandler::EnqueueMediaFile(const ListValue* args) {
[email protected]89a7e3812010-06-02 19:38:07576#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43577 std::string url = WideToUTF8(ExtractStringValue(args));
578 GURL gurl(url);
[email protected]89a7e3812010-06-02 19:38:07579
[email protected]88942a22010-08-19 20:34:43580 Browser* browser = Browser::GetBrowserForController(
581 &tab_contents_->controller(), NULL);
582 MediaPlayer* mediaplayer = MediaPlayer::Get();
583 mediaplayer->EnqueueMediaURL(gurl, browser);
[email protected]17496bb2010-03-10 19:48:23584#endif
585}
586
[email protected]88942a22010-08-19 20:34:43587void FilebrowseHandler::HandleIsAdvancedEnabled(const ListValue* args) {
[email protected]6ba198f2010-07-02 20:32:05588#if defined(OS_CHROMEOS)
[email protected]f872b3e2010-08-13 16:26:10589 Browser* browser = BrowserList::GetLastActive();
590 bool is_enabled = false;
591 bool mp_enabled = false;
592 if (browser) {
593 Profile* profile = browser->profile();
594 PrefService* pref_service = profile->GetPrefs();
595 is_enabled = pref_service->GetBoolean(
596 prefs::kLabsAdvancedFilesystemEnabled);
597 mp_enabled = pref_service->GetBoolean(prefs::kLabsMediaplayerEnabled);
598 }
[email protected]6ba198f2010-07-02 20:32:05599 DictionaryValue info_value;
[email protected]e4fbe8e2010-08-07 07:00:46600 info_value.SetBoolean("enabled", is_enabled);
601 info_value.SetBoolean("mpEnabled", mp_enabled);
[email protected]6ba198f2010-07-02 20:32:05602 dom_ui_->CallJavascriptFunction(L"enabledResult",
603 info_value);
[email protected]f872b3e2010-08-13 16:26:10604
[email protected]6ba198f2010-07-02 20:32:05605#endif
606}
[email protected]c4a530b2010-03-08 17:33:03607
[email protected]88942a22010-08-19 20:34:43608void FilebrowseHandler::HandleRefreshDirectory(const ListValue* args) {
[email protected]a67fa08e2010-02-12 20:43:55609#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43610 std::string path = WideToUTF8(ExtractStringValue(args));
611 FilePath currentpath(path);
612 GetChildrenForPath(currentpath, true);
[email protected]a67fa08e2010-02-12 20:43:55613#endif
614}
615
[email protected]88942a22010-08-19 20:34:43616void FilebrowseHandler::HandlePauseToggleDownload(const ListValue* args) {
[email protected]c4a530b2010-03-08 17:33:03617#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43618 int id;
619 ExtractIntegerValue(args, &id);
620 DownloadItem* item = active_download_items_[id];
621 item->TogglePause();
[email protected]c4a530b2010-03-08 17:33:03622#endif
623}
624
[email protected]88942a22010-08-19 20:34:43625void FilebrowseHandler::HandleAllowDownload(const ListValue* args) {
[email protected]c4a530b2010-03-08 17:33:03626#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43627 int id;
628 ExtractIntegerValue(args, &id);
629 DownloadItem* item = active_download_items_[id];
630 download_manager_->DangerousDownloadValidated(item);
[email protected]c4a530b2010-03-08 17:33:03631#endif
632}
633
[email protected]88942a22010-08-19 20:34:43634void FilebrowseHandler::HandleCancelDownload(const ListValue* args) {
635#if defined(OS_CHROMEOS)
636 int id;
637 ExtractIntegerValue(args, &id);
638 DownloadItem* item = active_download_items_[id];
639 item->Cancel(true);
640 FilePath path = item->full_path();
641 FilePath dir_path = path.DirName();
642 item->Remove(true);
643 GetChildrenForPath(dir_path, true);
644#endif
[email protected]dcd23fe2009-11-12 20:21:18645}
[email protected]698601e2009-10-21 22:43:37646
[email protected]88942a22010-08-19 20:34:43647void FilebrowseHandler::OpenNewFullWindow(const ListValue* args) {
648 OpenNewWindow(args, false);
[email protected]dcd23fe2009-11-12 20:21:18649}
650
[email protected]88942a22010-08-19 20:34:43651void FilebrowseHandler::OpenNewPopupWindow(const ListValue* args) {
652 OpenNewWindow(args, true);
653}
[email protected]dcd23fe2009-11-12 20:21:18654
[email protected]88942a22010-08-19 20:34:43655void FilebrowseHandler::OpenNewWindow(const ListValue* args, bool popup) {
656 std::string url = WideToUTF8(ExtractStringValue(args));
657 Browser* browser = popup ?
658 Browser::CreateForType(Browser::TYPE_APP_PANEL, profile_) :
659 BrowserList::GetLastActive();
660 browser->AddTabWithURL(GURL(url), GURL(), PageTransition::LINK, -1,
661 TabStripModel::ADD_SELECTED, NULL, std::string(),
662 &browser);
663 if (popup) {
664 // TODO(dhg): Remove these from being hardcoded. Allow javascript
665 // to specify.
666 browser->window()->SetBounds(gfx::Rect(0, 0, 400, 300));
[email protected]dcd23fe2009-11-12 20:21:18667 }
[email protected]88942a22010-08-19 20:34:43668 browser->window()->Show();
[email protected]dcd23fe2009-11-12 20:21:18669}
670
[email protected]d52bb8a2010-05-10 21:05:35671void FilebrowseHandler::SendPicasawebRequest() {
[email protected]068a71f2010-05-11 22:35:48672#if defined(OS_CHROMEOS)
673 chromeos::UserManager* user_man = chromeos::UserManager::Get();
674 std::string username = user_man->logged_in_user().email();
675
676 if (username.empty()) {
677 LOG(ERROR) << "Unable to get username";
678 return;
679 }
680
[email protected]d52bb8a2010-05-10 21:05:35681 fetch_.reset(URLFetcher::Create(0,
682 GURL(kPicasawebBaseUrl),
683 URLFetcher::GET,
[email protected]068a71f2010-05-11 22:35:48684 this));
[email protected]d52bb8a2010-05-10 21:05:35685 fetch_->set_request_context(profile_->GetRequestContext());
686 fetch_->Start();
[email protected]068a71f2010-05-11 22:35:48687#endif
[email protected]d52bb8a2010-05-10 21:05:35688}
689
[email protected]9d6b9aff2009-12-11 17:39:18690void FilebrowseHandler::ReadInFile() {
691#if defined(OS_CHROMEOS)
692 // Get the users username
693 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35694 chromeos::UserManager* user_man = chromeos::UserManager::Get();
695 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18696
697 if (username.empty()) {
698 LOG(ERROR) << "Unable to get username";
699 return;
700 }
701 int location = username.find_first_of('@',0);
702 if (location <= 0) {
703 LOG(ERROR) << "Username not formatted correctly";
704 return;
705 }
706 username = username.erase(username.find_first_of('@',0));
707 std::string url = kPicasawebUserPrefix;
708 url += username;
709 url += kPicasawebDefault;
710
[email protected]88942a22010-08-19 20:34:43711 FilePath currentpath(current_file_uploaded_);
[email protected]9d6b9aff2009-12-11 17:39:18712 // Get the filename
713 std::string filename;
714 filename = currentpath.BaseName().value();
715 std::string filecontents;
716 if (!file_util::ReadFileToString(currentpath, &filecontents)) {
717 LOG(ERROR) << "Unable to read this file:" << currentpath.value();
718 return;
719 }
[email protected]d52bb8a2010-05-10 21:05:35720 fetch_.reset(URLFetcher::Create(0,
721 GURL(url),
722 URLFetcher::POST,
723 this));
724 fetch_->set_upload_data("image/jpeg", filecontents);
[email protected]9d6b9aff2009-12-11 17:39:18725 // Set the filename on the server
726 std::string slug = "Slug: ";
727 slug += filename;
[email protected]d52bb8a2010-05-10 21:05:35728 fetch_->set_extra_request_headers(slug);
729 fetch_->set_request_context(profile_->GetRequestContext());
730 fetch_->Start();
[email protected]9d6b9aff2009-12-11 17:39:18731#endif
732}
733
734// This is just a prototype for allowing generic uploads to various sites
735// TODO(dhg): Remove this and implement general upload.
[email protected]88942a22010-08-19 20:34:43736void FilebrowseHandler::UploadToPicasaweb(const ListValue* args) {
[email protected]9d6b9aff2009-12-11 17:39:18737#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43738 std::string search_string = WideToUTF8(ExtractStringValue(args));
739 current_file_uploaded_ = search_string;
[email protected]9d6b9aff2009-12-11 17:39:18740 // ReadInFile();
[email protected]88942a22010-08-19 20:34:43741 FilePath current_path(search_string);
[email protected]a67fa08e2010-02-12 20:43:55742 TaskProxy* task = new TaskProxy(AsWeakPtr(), current_path);
[email protected]9d6b9aff2009-12-11 17:39:18743 task->AddRef();
[email protected]d52bb8a2010-05-10 21:05:35744 current_task_ = task;
[email protected]9d6b9aff2009-12-11 17:39:18745 ChromeThread::PostTask(
746 ChromeThread::FILE, FROM_HERE,
747 NewRunnableMethod(
748 task, &TaskProxy::ReadInFileProxy));
749#endif
750}
751
[email protected]a67fa08e2010-02-12 20:43:55752void FilebrowseHandler::GetChildrenForPath(FilePath& path, bool is_refresh) {
753 filelist_value_.reset(new ListValue());
[email protected]88942a22010-08-19 20:34:43754 currentpath_ = path;
[email protected]a67fa08e2010-02-12 20:43:55755
756 if (lister_.get()) {
757 lister_->Cancel();
758 lister_->set_delegate(NULL);
759 lister_ = NULL;
760 }
761
762 is_refresh_ = is_refresh;
[email protected]f872b3e2010-08-13 16:26:10763 FilePath default_download_path;
764 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
765 &default_download_path)) {
766 NOTREACHED();
767 }
768 if (currentpath_ == default_download_path) {
769 lister_ = new net::DirectoryLister(currentpath_,
[email protected]9ee2c8d2010-08-21 23:25:22770 false,
[email protected]f872b3e2010-08-13 16:26:10771 net::DirectoryLister::DATE,
772 this);
773 } else {
774 lister_ = new net::DirectoryLister(currentpath_, this);
775 }
[email protected]a67fa08e2010-02-12 20:43:55776 lister_->Start();
777}
778
[email protected]88942a22010-08-19 20:34:43779void FilebrowseHandler::HandleGetChildren(const ListValue* args) {
780#if defined(OS_CHROMEOS)
781 std::string path = WideToUTF8(ExtractStringValue(args));
782 FilePath currentpath(path);
[email protected]698601e2009-10-21 22:43:37783 filelist_value_.reset(new ListValue());
[email protected]698601e2009-10-21 22:43:37784
[email protected]a67fa08e2010-02-12 20:43:55785 GetChildrenForPath(currentpath, false);
[email protected]88942a22010-08-19 20:34:43786#endif
[email protected]698601e2009-10-21 22:43:37787}
788
[email protected]dcd23fe2009-11-12 20:21:18789void FilebrowseHandler::OnListFile(
[email protected]9ee2c8d2010-08-21 23:25:22790 const net::DirectoryLister::DirectoryListerData& data) {
[email protected]07046ab2010-01-20 21:42:44791#if defined(OS_WIN)
[email protected]9ee2c8d2010-08-21 23:25:22792 if (data.info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
[email protected]07046ab2010-01-20 21:42:44793 return;
794 }
795#elif defined(OS_POSIX)
[email protected]9ee2c8d2010-08-21 23:25:22796 if (data.info.filename[0] == '.') {
[email protected]07046ab2010-01-20 21:42:44797 return;
798 }
799#endif
800
[email protected]698601e2009-10-21 22:43:37801 DictionaryValue* file_value = new DictionaryValue();
802
803#if defined(OS_WIN)
[email protected]9ee2c8d2010-08-21 23:25:22804 int64 size = (static_cast<int64>(data.info.nFileSizeHigh) << 32) |
805 data.info.nFileSizeLow;
806 file_value->SetString(kPropertyTitle, data.info.cFileName);
[email protected]698601e2009-10-21 22:43:37807 file_value->SetString(kPropertyPath,
[email protected]9ee2c8d2010-08-21 23:25:22808 currentpath_.Append(data.info.cFileName).value());
[email protected]698601e2009-10-21 22:43:37809 file_value->SetBoolean(kPropertyDirectory,
[email protected]9ee2c8d2010-08-21 23:25:22810 (data.info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false);
[email protected]698601e2009-10-21 22:43:37811#elif defined(OS_POSIX)
[email protected]9ee2c8d2010-08-21 23:25:22812 file_value->SetString(kPropertyTitle, data.info.filename);
[email protected]698601e2009-10-21 22:43:37813 file_value->SetString(kPropertyPath,
[email protected]9ee2c8d2010-08-21 23:25:22814 currentpath_.Append(data.info.filename).value());
815 file_value->SetBoolean(kPropertyDirectory, S_ISDIR(data.info.stat.st_mode));
[email protected]698601e2009-10-21 22:43:37816#endif
817 filelist_value_->Append(file_value);
818}
819
[email protected]dcd23fe2009-11-12 20:21:18820void FilebrowseHandler::OnListDone(int error) {
[email protected]698601e2009-10-21 22:43:37821 DictionaryValue info_value;
[email protected]a67fa08e2010-02-12 20:43:55822 if (is_refresh_) {
[email protected]e4fbe8e2010-08-07 07:00:46823 info_value.SetString("functionCall", "refresh");
[email protected]a67fa08e2010-02-12 20:43:55824 } else {
[email protected]e4fbe8e2010-08-07 07:00:46825 info_value.SetString("functionCall", "getChildren");
[email protected]a67fa08e2010-02-12 20:43:55826 }
[email protected]698601e2009-10-21 22:43:37827 info_value.SetString(kPropertyPath, currentpath_.value());
[email protected]9d6b9aff2009-12-11 17:39:18828 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37829 info_value, *(filelist_value_.get()));
[email protected]a67fa08e2010-02-12 20:43:55830 SendCurrentDownloads();
[email protected]698601e2009-10-21 22:43:37831}
832
[email protected]88942a22010-08-19 20:34:43833void FilebrowseHandler::HandleGetMetadata(const ListValue* args) {
[email protected]698601e2009-10-21 22:43:37834}
835
[email protected]88942a22010-08-19 20:34:43836void FilebrowseHandler::HandleGetDownloads(const ListValue* args) {
[email protected]274e42b2010-01-29 22:03:57837 ModelChanged();
838}
839
840void FilebrowseHandler::ModelChanged() {
841 ClearDownloadItems();
[email protected]274e42b2010-01-29 22:03:57842
[email protected]0e2b59a2010-07-29 22:27:05843 std::vector<DownloadItem*> downloads;
844 download_manager_->GetAllDownloads(FilePath(), &downloads);
845
[email protected]377b4cc2010-05-18 17:25:19846 std::vector<DownloadItem*> new_downloads;
[email protected]274e42b2010-01-29 22:03:57847 // Scan for any in progress downloads and add ourself to them as an observer.
848 for (DownloadList::iterator it = downloads.begin();
849 it != downloads.end(); ++it) {
850 DownloadItem* download = *it;
851 // We want to know what happens as the download progresses and be notified
852 // when the user validates the dangerous download.
853 if (download->state() == DownloadItem::IN_PROGRESS ||
854 download->safety_state() == DownloadItem::DANGEROUS) {
855 download->AddObserver(this);
[email protected]377b4cc2010-05-18 17:25:19856 active_download_items_.push_back(download);
[email protected]274e42b2010-01-29 22:03:57857 }
[email protected]377b4cc2010-05-18 17:25:19858 DownloadList::iterator item = find(download_items_.begin(),
859 download_items_.end(),
860 download);
861 if (item == download_items_.end() && got_first_download_list_) {
862 SendNewDownload(download);
863 }
864 new_downloads.push_back(download);
[email protected]274e42b2010-01-29 22:03:57865 }
[email protected]377b4cc2010-05-18 17:25:19866 download_items_.swap(new_downloads);
867 got_first_download_list_ = true;
[email protected]274e42b2010-01-29 22:03:57868 SendCurrentDownloads();
869}
870
[email protected]377b4cc2010-05-18 17:25:19871void FilebrowseHandler::SendNewDownload(DownloadItem* download) {
872 ListValue results_value;
873 results_value.Append(download_util::CreateDownloadItemValue(download, -1));
874 dom_ui_->CallJavascriptFunction(L"newDownload", results_value);
875}
876
[email protected]a67fa08e2010-02-12 20:43:55877void FilebrowseHandler::DeleteFile(const FilePath& path) {
878 if (!file_util::Delete(path, true)) {
879 LOG(ERROR) << "unable to delete directory";
880 }
881 ChromeThread::PostTask(
882 ChromeThread::UI, FROM_HERE,
[email protected]d52bb8a2010-05-10 21:05:35883 NewRunnableMethod(current_task_, &TaskProxy::FireDeleteCompleteProxy));
[email protected]a67fa08e2010-02-12 20:43:55884}
885
[email protected]88942a22010-08-19 20:34:43886void FilebrowseHandler::HandleDeleteFile(const ListValue* args) {
887#if defined(OS_CHROMEOS)
888 std::string path = WideToUTF8(ExtractStringValue(args));
889 FilePath currentpath(path);
890 for (unsigned int x = 0; x < active_download_items_.size(); x++) {
891 FilePath item = active_download_items_[x]->full_path();
892 if (item == currentpath) {
893 active_download_items_[x]->Cancel(true);
894 active_download_items_[x]->Remove(true);
895 FilePath dir_path = item.DirName();
896 GetChildrenForPath(dir_path, true);
[email protected]a67fa08e2010-02-12 20:43:55897 return;
898 }
899 }
[email protected]88942a22010-08-19 20:34:43900 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath);
901 task->AddRef();
902 current_task_ = task;
903 ChromeThread::PostTask(
904 ChromeThread::FILE, FROM_HERE,
905 NewRunnableMethod(
906 task, &TaskProxy::DeleteFileProxy));
[email protected]a67fa08e2010-02-12 20:43:55907#endif
908}
909
[email protected]274e42b2010-01-29 22:03:57910void FilebrowseHandler::OnDownloadUpdated(DownloadItem* download) {
[email protected]377b4cc2010-05-18 17:25:19911 DownloadList::iterator it = find(active_download_items_.begin(),
912 active_download_items_.end(),
[email protected]274e42b2010-01-29 22:03:57913 download);
[email protected]377b4cc2010-05-18 17:25:19914 if (it == active_download_items_.end())
[email protected]274e42b2010-01-29 22:03:57915 return;
[email protected]377b4cc2010-05-18 17:25:19916 const int id = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:57917
918 ListValue results_value;
919 results_value.Append(download_util::CreateDownloadItemValue(download, id));
920 dom_ui_->CallJavascriptFunction(L"downloadUpdated", results_value);
921}
922
923void FilebrowseHandler::ClearDownloadItems() {
[email protected]377b4cc2010-05-18 17:25:19924 for (DownloadList::iterator it = active_download_items_.begin();
925 it != active_download_items_.end(); ++it) {
[email protected]274e42b2010-01-29 22:03:57926 (*it)->RemoveObserver(this);
927 }
[email protected]377b4cc2010-05-18 17:25:19928 active_download_items_.clear();
[email protected]274e42b2010-01-29 22:03:57929}
930
931void FilebrowseHandler::SendCurrentDownloads() {
932 ListValue results_value;
[email protected]377b4cc2010-05-18 17:25:19933 for (DownloadList::iterator it = active_download_items_.begin();
934 it != active_download_items_.end(); ++it) {
935 int index = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:57936 results_value.Append(download_util::CreateDownloadItemValue(*it, index));
937 }
938
939 dom_ui_->CallJavascriptFunction(L"downloadsList", results_value);
940}
941
[email protected]698601e2009-10-21 22:43:37942////////////////////////////////////////////////////////////////////////////////
943//
[email protected]f5bf8ccf2010-02-05 18:19:25944// FileBrowseUI
[email protected]698601e2009-10-21 22:43:37945//
946////////////////////////////////////////////////////////////////////////////////
947
[email protected]274e42b2010-01-29 22:03:57948FileBrowseUI::FileBrowseUI(TabContents* contents) : HtmlDialogUI(contents) {
[email protected]9d6b9aff2009-12-11 17:39:18949 FilebrowseHandler* handler = new FilebrowseHandler();
950 AddMessageHandler((handler)->Attach(this));
[email protected]274e42b2010-01-29 22:03:57951 handler->Init();
[email protected]698601e2009-10-21 22:43:37952 FileBrowseUIHTMLSource* html_source = new FileBrowseUIHTMLSource();
953
954 // Set up the chrome://filebrowse/ source.
[email protected]fae20792009-10-28 20:31:58955 ChromeThread::PostTask(
956 ChromeThread::IO, FROM_HERE,
957 NewRunnableMethod(
[email protected]576a4ca2009-11-05 01:41:09958 Singleton<ChromeURLDataManager>::get(),
[email protected]698601e2009-10-21 22:43:37959 &ChromeURLDataManager::AddDataSource,
[email protected]f8f82502009-11-20 23:14:23960 make_scoped_refptr(html_source)));
[email protected]698601e2009-10-21 22:43:37961}
[email protected]f5bf8ccf2010-02-05 18:19:25962
963// static
[email protected]1717246f2010-02-10 17:08:15964Browser* FileBrowseUI::OpenPopup(Profile* profile,
[email protected]467293292010-04-16 19:41:56965 const std::string& hashArgument,
966 int width,
967 int height) {
[email protected]1717246f2010-02-10 17:08:15968 // Get existing pop up for given hashArgument.
969 Browser* browser = GetPopupForPath(hashArgument);
[email protected]f5bf8ccf2010-02-05 18:19:25970
[email protected]1717246f2010-02-10 17:08:15971 // Create new browser if no matching pop up found.
972 if (browser == NULL) {
[email protected]9ee17e8c2010-08-10 19:17:31973 browser = Browser::CreateForType(Browser::TYPE_APP_PANEL, profile);
[email protected]f59f1162010-04-23 21:19:29974 std::string url;
975 if (hashArgument.empty()) {
976 url = chrome::kChromeUIFileBrowseURL;
977 } else {
978 url = kFilebrowseURLHash;
979 url.append(hashArgument);
980 }
[email protected]f5bf8ccf2010-02-05 18:19:25981
[email protected]1717246f2010-02-10 17:08:15982 browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39983 GURL(url), GURL(), PageTransition::LINK, -1,
[email protected]b283a7532010-08-12 21:24:59984 TabStripModel::ADD_SELECTED, NULL, std::string(),
985 &browser);
[email protected]1717246f2010-02-10 17:08:15986 browser->window()->SetBounds(gfx::Rect(kPopupLeft,
987 kPopupTop,
[email protected]467293292010-04-16 19:41:56988 width,
989 height));
[email protected]1717246f2010-02-10 17:08:15990
991 browser->window()->Show();
[email protected]9ee17e8c2010-08-10 19:17:31992 } else {
993 browser->window()->Show();
[email protected]1717246f2010-02-10 17:08:15994 }
[email protected]f5bf8ccf2010-02-05 18:19:25995
996 return browser;
997}
[email protected]1717246f2010-02-10 17:08:15998
999Browser* FileBrowseUI::GetPopupForPath(const std::string& path) {
[email protected]9ee17e8c2010-08-10 19:17:311000 std::string current_path = path;
1001 if (current_path.empty()) {
1002 Profile* profile = BrowserList::GetLastActive()->profile();
1003 PrefService* pref_service = profile->GetPrefs();
1004 bool is_enabled = pref_service->GetBoolean(
1005 prefs::kLabsAdvancedFilesystemEnabled);
1006 if (!is_enabled) {
1007 FilePath default_download_path;
1008 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
1009 &default_download_path)) {
1010 NOTREACHED();
1011 }
1012 current_path = default_download_path.value();
1013 }
1014 }
1015
[email protected]1717246f2010-02-10 17:08:151016 for (BrowserList::const_iterator it = BrowserList::begin();
1017 it != BrowserList::end(); ++it) {
[email protected]9ee17e8c2010-08-10 19:17:311018 if (((*it)->type() == Browser::TYPE_APP_PANEL)) {
[email protected]d519bed2010-05-28 18:42:031019 TabContents* tab_contents = (*it)->GetSelectedTabContents();
1020 DCHECK(tab_contents);
1021 if (!tab_contents)
1022 continue;
1023 const GURL& url = tab_contents->GetURL();
[email protected]1717246f2010-02-10 17:08:151024
1025 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1026 url.host() == chrome::kChromeUIFileBrowseHost &&
[email protected]9ee17e8c2010-08-10 19:17:311027 url.ref() == current_path) {
[email protected]1717246f2010-02-10 17:08:151028 return (*it);
1029 }
1030 }
1031 }
1032
1033 return NULL;
1034}
[email protected]467293292010-04-16 19:41:561035
1036const int FileBrowseUI::kPopupWidth = 250;
1037const int FileBrowseUI::kPopupHeight = 300;
1038const int FileBrowseUI::kSmallPopupWidth = 250;
1039const int FileBrowseUI::kSmallPopupHeight = 50;