blob: 3637f41246ff5dbed6dc5f54906d86252381f23d [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:
110 virtual void OnListFile(const file_util::FileEnumerator::FindInfo& data);
111 virtual void OnListDone(int error);
112
113 // DOMMessageHandler implementation.
114 virtual DOMMessageHandler* Attach(DOMUI* dom_ui);
115 virtual void RegisterMessages();
116
[email protected]07046ab2010-01-20 21:42:44117#if defined(OS_CHROMEOS)
118 void MountChanged(chromeos::MountLibrary* obj,
119 chromeos::MountEventType evt,
120 const std::string& path);
121#endif
122
[email protected]274e42b2010-01-29 22:03:57123 // DownloadItem::Observer interface
124 virtual void OnDownloadUpdated(DownloadItem* download);
125 virtual void OnDownloadFileCompleted(DownloadItem* download) { }
126 virtual void OnDownloadOpened(DownloadItem* download) { }
127
128 // DownloadManager::Observer interface
129 virtual void ModelChanged();
[email protected]82f37b02010-07-29 22:04:57130
[email protected]698601e2009-10-21 22:43:37131 // Callback for the "getRoots" message.
132 void HandleGetRoots(const Value* value);
133
[email protected]a67fa08e2010-02-12 20:43:55134 void GetChildrenForPath(FilePath& path, bool is_refresh);
135
[email protected]9d6b9aff2009-12-11 17:39:18136 void OnURLFetchComplete(const URLFetcher* source,
137 const GURL& url,
138 const URLRequestStatus& status,
139 int response_code,
140 const ResponseCookies& cookies,
141 const std::string& data);
142
[email protected]698601e2009-10-21 22:43:37143 // Callback for the "getChildren" message.
144 void HandleGetChildren(const Value* value);
[email protected]c4a530b2010-03-08 17:33:03145 // Callback for the "refreshDirectory" message.
146 void HandleRefreshDirectory(const Value* value);
[email protected]6ba198f2010-07-02 20:32:05147 void HandleIsAdvancedEnabled(const Value* value);
[email protected]698601e2009-10-21 22:43:37148
149 // Callback for the "getMetadata" message.
150 void HandleGetMetadata(const Value* value);
151
[email protected]9d6b9aff2009-12-11 17:39:18152 // Callback for the "openNewWindow" message.
[email protected]dcd23fe2009-11-12 20:21:18153 void OpenNewFullWindow(const Value* value);
154 void OpenNewPopupWindow(const Value* value);
155
[email protected]9d6b9aff2009-12-11 17:39:18156 // Callback for the "uploadToPicasaweb" message.
157 void UploadToPicasaweb(const Value* value);
158
[email protected]274e42b2010-01-29 22:03:57159 // Callback for the "getDownloads" message.
160 void HandleGetDownloads(const Value* value);
161
162 void HandleCreateNewFolder(const Value* value);
163
[email protected]17496bb2010-03-10 19:48:23164 void PlayMediaFile(const Value* value);
[email protected]89a7e3812010-06-02 19:38:07165 void EnqueueMediaFile(const Value* value);
[email protected]17496bb2010-03-10 19:48:23166
[email protected]a67fa08e2010-02-12 20:43:55167 void HandleDeleteFile(const Value* value);
168 void DeleteFile(const FilePath& path);
169 void FireDeleteComplete(const FilePath& path);
170
171 void HandlePauseToggleDownload(const Value* value);
172
[email protected]c4a530b2010-03-08 17:33:03173 void HandleCancelDownload(const Value* value);
174 void HandleAllowDownload(const Value* value);
175
[email protected]9d6b9aff2009-12-11 17:39:18176 void ReadInFile();
177 void FireUploadComplete();
178
[email protected]d52bb8a2010-05-10 21:05:35179 void SendPicasawebRequest();
[email protected]698601e2009-10-21 22:43:37180 private:
[email protected]dcd23fe2009-11-12 20:21:18181
182 void OpenNewWindow(const Value* value, bool popup);
183
[email protected]274e42b2010-01-29 22:03:57184 // Clear all download items and their observers.
185 void ClearDownloadItems();
186
187 // Send the current list of downloads to the page.
188 void SendCurrentDownloads();
189
[email protected]377b4cc2010-05-18 17:25:19190 void SendNewDownload(DownloadItem* download);
191
[email protected]698601e2009-10-21 22:43:37192 scoped_ptr<ListValue> filelist_value_;
193 FilePath currentpath_;
[email protected]dcd23fe2009-11-12 20:21:18194 Profile* profile_;
[email protected]93a58bf72010-06-04 23:01:20195 TabContents* tab_contents_;
[email protected]9d6b9aff2009-12-11 17:39:18196 std::string current_file_contents_;
197 std::string current_file_uploaded_;
198 int upload_response_code_;
[email protected]d52bb8a2010-05-10 21:05:35199 TaskProxy* current_task_;
[email protected]698601e2009-10-21 22:43:37200 scoped_refptr<net::DirectoryLister> lister_;
[email protected]a67fa08e2010-02-12 20:43:55201 bool is_refresh_;
[email protected]d52bb8a2010-05-10 21:05:35202 scoped_ptr<URLFetcher> fetch_;
[email protected]698601e2009-10-21 22:43:37203
[email protected]274e42b2010-01-29 22:03:57204 DownloadManager* download_manager_;
205 typedef std::vector<DownloadItem*> DownloadList;
[email protected]377b4cc2010-05-18 17:25:19206 DownloadList active_download_items_;
[email protected]274e42b2010-01-29 22:03:57207 DownloadList download_items_;
[email protected]377b4cc2010-05-18 17:25:19208 bool got_first_download_list_;
[email protected]dcd23fe2009-11-12 20:21:18209 DISALLOW_COPY_AND_ASSIGN(FilebrowseHandler);
[email protected]698601e2009-10-21 22:43:37210};
211
[email protected]9d6b9aff2009-12-11 17:39:18212class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> {
213 public:
[email protected]d52bb8a2010-05-10 21:05:35214 explicit TaskProxy(const base::WeakPtr<FilebrowseHandler>& handler,
215 FilePath& path)
[email protected]a67fa08e2010-02-12 20:43:55216 : handler_(handler),
217 path_(path) {}
[email protected]9d6b9aff2009-12-11 17:39:18218 void ReadInFileProxy() {
219 if (handler_) {
220 handler_->ReadInFile();
221 }
222 }
[email protected]068a71f2010-05-11 22:35:48223 void DeleteFetcher(URLFetcher* fetch) {
224 delete fetch;
225 }
[email protected]d52bb8a2010-05-10 21:05:35226 void SendPicasawebRequestProxy() {
227 if (handler_) {
228 handler_->SendPicasawebRequest();
229 }
230 }
[email protected]9d6b9aff2009-12-11 17:39:18231 void FireUploadCompleteProxy() {
232 if (handler_) {
233 handler_->FireUploadComplete();
234 }
235 }
[email protected]a67fa08e2010-02-12 20:43:55236
237 void DeleteFileProxy() {
238 if (handler_) {
239 handler_->DeleteFile(path_);
240 }
241 }
242
243 void FireDeleteCompleteProxy() {
244 if (handler_) {
245 handler_->FireDeleteComplete(path_);
246 }
247 }
[email protected]9d6b9aff2009-12-11 17:39:18248 private:
249 base::WeakPtr<FilebrowseHandler> handler_;
[email protected]a67fa08e2010-02-12 20:43:55250 FilePath path_;
[email protected]9d6b9aff2009-12-11 17:39:18251 friend class base::RefCountedThreadSafe<TaskProxy>;
[email protected]068a71f2010-05-11 22:35:48252 DISALLOW_COPY_AND_ASSIGN(TaskProxy);
[email protected]9d6b9aff2009-12-11 17:39:18253};
254
255
[email protected]698601e2009-10-21 22:43:37256////////////////////////////////////////////////////////////////////////////////
257//
258// FileBrowseHTMLSource
259//
260////////////////////////////////////////////////////////////////////////////////
261
262FileBrowseUIHTMLSource::FileBrowseUIHTMLSource()
263 : DataSource(chrome::kChromeUIFileBrowseHost, MessageLoop::current()) {
264}
265
266void FileBrowseUIHTMLSource::StartDataRequest(const std::string& path,
[email protected]e42ae9b2010-03-28 00:55:05267 bool is_off_the_record,
268 int request_id) {
[email protected]698601e2009-10-21 22:43:37269 DictionaryValue localized_strings;
[email protected]11f4857282009-11-13 19:56:17270 // TODO(dhg): Add stirings to localized strings, also add more strings
[email protected]dcd23fe2009-11-12 20:21:18271 // that are currently hardcoded.
[email protected]e4fbe8e2010-08-07 07:00:46272 localized_strings.SetString("title",
273 l10n_util::GetStringUTF16(IDS_FILEBROWSER_TITLE));
274 localized_strings.SetString("pause",
275 l10n_util::GetStringUTF16(IDS_FILEBROWSER_PAUSE));
276 localized_strings.SetString("resume",
277 l10n_util::GetStringUTF16(IDS_FILEBROWSER_RESUME));
278 localized_strings.SetString("scanning",
279 l10n_util::GetStringUTF16(IDS_FILEBROWSER_SCANNING));
280 localized_strings.SetString("confirmdelete",
281 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_DELETE));
282 localized_strings.SetString("confirmyes",
283 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_YES));
284 localized_strings.SetString("confirmcancel",
285 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_CANCEL));
286 localized_strings.SetString("allowdownload",
287 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_DOWNLOAD));
288 localized_strings.SetString("filenameprompt",
289 l10n_util::GetStringUTF16(IDS_FILEBROWSER_PROMPT_FILENAME));
290 localized_strings.SetString("save",
291 l10n_util::GetStringUTF16(IDS_FILEBROWSER_SAVE));
292 localized_strings.SetString("newfolder",
293 l10n_util::GetStringUTF16(IDS_FILEBROWSER_NEW_FOLDER));
294 localized_strings.SetString("open",
295 l10n_util::GetStringUTF16(IDS_FILEBROWSER_OPEN));
296 localized_strings.SetString("picasaweb",
297 l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_PICASAWEB));
298 localized_strings.SetString("flickr",
299 l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_FLICKR));
300 localized_strings.SetString("email",
301 l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_EMAIL));
302 localized_strings.SetString("delete",
303 l10n_util::GetStringUTF16(IDS_FILEBROWSER_DELETE));
304 localized_strings.SetString("enqueue",
305 l10n_util::GetStringUTF16(IDS_FILEBROWSER_ENQUEUE));
306 localized_strings.SetString("mediapath", kMediaPath);
[email protected]88f03da02010-06-10 16:21:13307 FilePath default_download_path;
308 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
309 &default_download_path)) {
310 NOTREACHED();
311 }
[email protected]e4fbe8e2010-08-07 07:00:46312 // TODO(viettrungluu): this is wrong -- FilePath's need not be Unicode.
313 localized_strings.SetString("downloadpath", default_download_path.value());
314 localized_strings.SetString("error_unknown_file_type",
315 l10n_util::GetStringUTF16(IDS_FILEBROWSER_ERROR_UNKNOWN_FILE_TYPE));
[email protected]698601e2009-10-21 22:43:37316 SetFontAndTextDirection(&localized_strings);
317
318 static const base::StringPiece filebrowse_html(
319 ResourceBundle::GetSharedInstance().GetRawDataResource(
320 IDR_FILEBROWSE_HTML));
321 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml(
322 filebrowse_html, &localized_strings);
323
324 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
325 html_bytes->data.resize(full_html.size());
326 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
327
328 SendResponse(request_id, html_bytes);
329}
330
331////////////////////////////////////////////////////////////////////////////////
332//
[email protected]dcd23fe2009-11-12 20:21:18333// FilebrowseHandler
[email protected]698601e2009-10-21 22:43:37334//
335////////////////////////////////////////////////////////////////////////////////
[email protected]dcd23fe2009-11-12 20:21:18336FilebrowseHandler::FilebrowseHandler()
[email protected]274e42b2010-01-29 22:03:57337 : profile_(NULL),
[email protected]93a58bf72010-06-04 23:01:20338 tab_contents_(NULL),
[email protected]a67fa08e2010-02-12 20:43:55339 is_refresh_(false),
[email protected]d52bb8a2010-05-10 21:05:35340 fetch_(NULL),
[email protected]377b4cc2010-05-18 17:25:19341 download_manager_(NULL),
342 got_first_download_list_(false) {
[email protected]a67fa08e2010-02-12 20:43:55343 lister_ = NULL;
[email protected]07046ab2010-01-20 21:42:44344#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24345 chromeos::MountLibrary* lib =
346 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44347 lib->AddObserver(this);
348#endif
[email protected]698601e2009-10-21 22:43:37349}
350
[email protected]dcd23fe2009-11-12 20:21:18351FilebrowseHandler::~FilebrowseHandler() {
[email protected]07046ab2010-01-20 21:42:44352#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24353 chromeos::MountLibrary* lib =
354 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44355 lib->RemoveObserver(this);
356#endif
[email protected]698601e2009-10-21 22:43:37357 if (lister_.get()) {
358 lister_->Cancel();
359 lister_->set_delegate(NULL);
360 }
[email protected]274e42b2010-01-29 22:03:57361
362 ClearDownloadItems();
363 download_manager_->RemoveObserver(this);
[email protected]068a71f2010-05-11 22:35:48364 URLFetcher* fetch = fetch_.release();
365 if (fetch) {
366 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath_);
367 task->AddRef();
368 ChromeThread::PostTask(
369 ChromeThread::FILE, FROM_HERE,
370 NewRunnableMethod(
371 task, &TaskProxy::DeleteFetcher, fetch));
372 }
[email protected]698601e2009-10-21 22:43:37373}
374
[email protected]dcd23fe2009-11-12 20:21:18375DOMMessageHandler* FilebrowseHandler::Attach(DOMUI* dom_ui) {
[email protected]698601e2009-10-21 22:43:37376 // Create our favicon data source.
[email protected]fae20792009-10-28 20:31:58377 ChromeThread::PostTask(
378 ChromeThread::IO, FROM_HERE,
379 NewRunnableMethod(
[email protected]576a4ca2009-11-05 01:41:09380 Singleton<ChromeURLDataManager>::get(),
[email protected]fae20792009-10-28 20:31:58381 &ChromeURLDataManager::AddDataSource,
[email protected]f8f82502009-11-20 23:14:23382 make_scoped_refptr(new DOMUIFavIconSource(dom_ui->GetProfile()))));
[email protected]dcd23fe2009-11-12 20:21:18383 profile_ = dom_ui->GetProfile();
[email protected]93a58bf72010-06-04 23:01:20384 tab_contents_ = dom_ui->tab_contents();
[email protected]698601e2009-10-21 22:43:37385 return DOMMessageHandler::Attach(dom_ui);
386}
387
[email protected]274e42b2010-01-29 22:03:57388void FilebrowseHandler::Init() {
389 download_manager_ = profile_->GetOriginalProfile()->GetDownloadManager();
390 download_manager_->AddObserver(this);
[email protected]d52bb8a2010-05-10 21:05:35391 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath_);
392 task->AddRef();
393 current_task_ = task;
394 static bool sent_request = false;
395 if (!sent_request) {
396 // If we have not sent a request before, we should do one in order to
397 // ensure that we have the correct cookies. This is for uploads.
398 ChromeThread::PostTask(
399 ChromeThread::FILE, FROM_HERE,
400 NewRunnableMethod(
401 task, &TaskProxy::SendPicasawebRequestProxy));
402 sent_request = true;
403 }
[email protected]274e42b2010-01-29 22:03:57404}
405
[email protected]dcd23fe2009-11-12 20:21:18406void FilebrowseHandler::RegisterMessages() {
[email protected]698601e2009-10-21 22:43:37407 dom_ui_->RegisterMessageCallback("getRoots",
[email protected]dcd23fe2009-11-12 20:21:18408 NewCallback(this, &FilebrowseHandler::HandleGetRoots));
[email protected]698601e2009-10-21 22:43:37409 dom_ui_->RegisterMessageCallback("getChildren",
[email protected]dcd23fe2009-11-12 20:21:18410 NewCallback(this, &FilebrowseHandler::HandleGetChildren));
[email protected]698601e2009-10-21 22:43:37411 dom_ui_->RegisterMessageCallback("getMetadata",
[email protected]dcd23fe2009-11-12 20:21:18412 NewCallback(this, &FilebrowseHandler::HandleGetMetadata));
413 dom_ui_->RegisterMessageCallback("openNewPopupWindow",
414 NewCallback(this, &FilebrowseHandler::OpenNewPopupWindow));
415 dom_ui_->RegisterMessageCallback("openNewFullWindow",
416 NewCallback(this, &FilebrowseHandler::OpenNewFullWindow));
[email protected]9d6b9aff2009-12-11 17:39:18417 dom_ui_->RegisterMessageCallback("uploadToPicasaweb",
418 NewCallback(this, &FilebrowseHandler::UploadToPicasaweb));
[email protected]274e42b2010-01-29 22:03:57419 dom_ui_->RegisterMessageCallback("getDownloads",
420 NewCallback(this, &FilebrowseHandler::HandleGetDownloads));
421 dom_ui_->RegisterMessageCallback("createNewFolder",
422 NewCallback(this, &FilebrowseHandler::HandleCreateNewFolder));
[email protected]17496bb2010-03-10 19:48:23423 dom_ui_->RegisterMessageCallback("playMediaFile",
424 NewCallback(this, &FilebrowseHandler::PlayMediaFile));
[email protected]89a7e3812010-06-02 19:38:07425 dom_ui_->RegisterMessageCallback("enqueueMediaFile",
426 NewCallback(this, &FilebrowseHandler::EnqueueMediaFile));
[email protected]a67fa08e2010-02-12 20:43:55427 dom_ui_->RegisterMessageCallback("pauseToggleDownload",
428 NewCallback(this, &FilebrowseHandler::HandlePauseToggleDownload));
429 dom_ui_->RegisterMessageCallback("deleteFile",
430 NewCallback(this, &FilebrowseHandler::HandleDeleteFile));
[email protected]c4a530b2010-03-08 17:33:03431 dom_ui_->RegisterMessageCallback("cancelDownload",
432 NewCallback(this, &FilebrowseHandler::HandleCancelDownload));
433 dom_ui_->RegisterMessageCallback("allowDownload",
434 NewCallback(this, &FilebrowseHandler::HandleAllowDownload));
435 dom_ui_->RegisterMessageCallback("refreshDirectory",
436 NewCallback(this, &FilebrowseHandler::HandleRefreshDirectory));
[email protected]6ba198f2010-07-02 20:32:05437 dom_ui_->RegisterMessageCallback("isAdvancedEnabled",
438 NewCallback(this, &FilebrowseHandler::HandleIsAdvancedEnabled));
[email protected]9d6b9aff2009-12-11 17:39:18439}
440
[email protected]a67fa08e2010-02-12 20:43:55441
442void FilebrowseHandler::FireDeleteComplete(const FilePath& path) {
443 // We notify the UI by telling it to refresh its contents.
444 FilePath dir_path = path.DirName();
445 GetChildrenForPath(dir_path, true);
446};
447
[email protected]9d6b9aff2009-12-11 17:39:18448void FilebrowseHandler::FireUploadComplete() {
[email protected]d52bb8a2010-05-10 21:05:35449#if defined(OS_CHROMEOS)
[email protected]9d6b9aff2009-12-11 17:39:18450 DictionaryValue info_value;
[email protected]e4fbe8e2010-08-07 07:00:46451 info_value.SetString("path", current_file_uploaded_);
[email protected]9d6b9aff2009-12-11 17:39:18452
453 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35454 chromeos::UserManager* user_man = chromeos::UserManager::Get();
455 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18456
457 if (username.empty()) {
458 LOG(ERROR) << "Unable to get username";
459 return;
460 }
461 int location = username.find_first_of('@',0);
462 if (location <= 0) {
463 LOG(ERROR) << "Username not formatted correctly";
464 return;
465 }
466 username = username.erase(username.find_first_of('@',0));
[email protected]e4fbe8e2010-08-07 07:00:46467 std::string picture_url = kPicasawebBaseUrl;
[email protected]9d6b9aff2009-12-11 17:39:18468 picture_url += username;
469 picture_url += kPicasawebDropBox;
[email protected]e4fbe8e2010-08-07 07:00:46470 info_value.SetString("url", picture_url);
471 info_value.SetInteger("status_code", upload_response_code_);
[email protected]9d6b9aff2009-12-11 17:39:18472 dom_ui_->CallJavascriptFunction(L"uploadComplete", info_value);
[email protected]d52bb8a2010-05-10 21:05:35473#endif
[email protected]9d6b9aff2009-12-11 17:39:18474}
475
[email protected]07046ab2010-01-20 21:42:44476#if defined(OS_CHROMEOS)
477void FilebrowseHandler::MountChanged(chromeos::MountLibrary* obj,
478 chromeos::MountEventType evt,
479 const std::string& path) {
480 if (evt == chromeos::DISK_REMOVED ||
481 evt == chromeos::DISK_CHANGED) {
482 dom_ui_->CallJavascriptFunction(L"rootsChanged");
483 }
484}
485#endif
486
[email protected]9d6b9aff2009-12-11 17:39:18487void FilebrowseHandler::OnURLFetchComplete(const URLFetcher* source,
488 const GURL& url,
489 const URLRequestStatus& status,
490 int response_code,
491 const ResponseCookies& cookies,
492 const std::string& data) {
493 upload_response_code_ = response_code;
[email protected]d52bb8a2010-05-10 21:05:35494 LOG(INFO) << "Response code:" << response_code;
495 LOG(INFO) << "request url" << url;
[email protected]068a71f2010-05-11 22:35:48496 if (StartsWithASCII(url.spec(), kPicasawebUserPrefix, true)) {
497 ChromeThread::PostTask(
498 ChromeThread::UI, FROM_HERE,
499 NewRunnableMethod(current_task_, &TaskProxy::FireUploadCompleteProxy));
500 }
501 fetch_.reset();
[email protected]698601e2009-10-21 22:43:37502}
503
[email protected]dcd23fe2009-11-12 20:21:18504void FilebrowseHandler::HandleGetRoots(const Value* value) {
[email protected]698601e2009-10-21 22:43:37505 ListValue results_value;
506 DictionaryValue info_value;
[email protected]698601e2009-10-21 22:43:37507 // TODO(dhg): add other entries, make this more general
[email protected]07046ab2010-01-20 21:42:44508#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24509 chromeos::MountLibrary* lib =
510 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44511 const chromeos::MountLibrary::DiskVector& disks = lib->disks();
512
513 for (size_t i = 0; i < disks.size(); ++i) {
514 if (!disks[i].mount_path.empty()) {
515 DictionaryValue* page_value = new DictionaryValue();
516 page_value->SetString(kPropertyPath, disks[i].mount_path);
517 FilePath currentpath;
518 currentpath = FilePath(disks[i].mount_path);
519 std::string filename;
520 filename = currentpath.BaseName().value();
521 page_value->SetString(kPropertyTitle, filename);
522 page_value->SetBoolean(kPropertyDirectory, true);
523 results_value.Append(page_value);
524 }
525 }
526#else
527 DictionaryValue* page_value = new DictionaryValue();
528 page_value->SetString(kPropertyPath, "/media");
529 page_value->SetString(kPropertyTitle, "Removeable");
[email protected]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]274e42b2010-01-29 22:03:57553void FilebrowseHandler::HandleCreateNewFolder(const Value* value) {
554#if defined(OS_CHROMEOS)
555 if (value && value->GetType() == Value::TYPE_LIST) {
556 const ListValue* list_value = static_cast<const ListValue*>(value);
557 std::string path;
558
559 // Get path string.
560 if (list_value->GetString(0, &path)) {
561
562 FilePath currentpath;
563 currentpath = FilePath(path);
564
565 if (!file_util::CreateDirectory(currentpath)) {
566 LOG(ERROR) << "unable to create directory";
567 }
568 } else {
569 LOG(ERROR) << "Unable to get string";
570 return;
571 }
572 }
573#endif
574}
575
[email protected]17496bb2010-03-10 19:48:23576void FilebrowseHandler::PlayMediaFile(const Value* value) {
577#if defined(OS_CHROMEOS)
578 if (value && value->GetType() == Value::TYPE_LIST) {
579 const ListValue* list_value = static_cast<const ListValue*>(value);
580 std::string path;
581
582 // Get path string.
583 if (list_value->GetString(0, &path)) {
584 FilePath currentpath;
585 currentpath = FilePath(path);
586
587 MediaPlayer* mediaplayer = MediaPlayer::Get();
588 std::string url = currentpath.value();
589
590 GURL gurl(url);
[email protected]93a58bf72010-06-04 23:01:20591 Browser* browser = Browser::GetBrowserForController(
592 &tab_contents_->controller(), NULL);
593 mediaplayer->ForcePlayMediaURL(gurl, browser);
[email protected]89a7e3812010-06-02 19:38:07594 } else {
595 LOG(ERROR) << "Unable to get string";
596 return;
597 }
598 }
599#endif
600}
601
602void FilebrowseHandler::EnqueueMediaFile(const Value* value) {
603#if defined(OS_CHROMEOS)
604 if (value && value->GetType() == Value::TYPE_LIST) {
605 const ListValue* list_value = static_cast<const ListValue*>(value);
606 std::string path;
607
608 // Get path string.
609 if (list_value->GetString(0, &path)) {
610 FilePath currentpath;
611 currentpath = FilePath(path);
612
613 MediaPlayer* mediaplayer = MediaPlayer::Get();
614 std::string url = currentpath.value();
615
616 GURL gurl(url);
[email protected]93a58bf72010-06-04 23:01:20617 Browser* browser = Browser::GetBrowserForController(
618 &tab_contents_->controller(), NULL);
619 mediaplayer->EnqueueMediaURL(gurl, browser);
[email protected]17496bb2010-03-10 19:48:23620 } else {
621 LOG(ERROR) << "Unable to get string";
622 return;
623 }
624 }
625#endif
626}
627
[email protected]6ba198f2010-07-02 20:32:05628void FilebrowseHandler::HandleIsAdvancedEnabled(const Value* value) {
629#if defined(OS_CHROMEOS)
630 Profile* profile = BrowserList::GetLastActive()->profile();
631 PrefService* pref_service = profile->GetPrefs();
632 bool is_enabled = pref_service->GetBoolean(
633 prefs::kLabsAdvancedFilesystemEnabled);
634 bool mp_enabled = pref_service->GetBoolean(prefs::kLabsMediaplayerEnabled);
635 DictionaryValue info_value;
[email protected]e4fbe8e2010-08-07 07:00:46636 info_value.SetBoolean("enabled", is_enabled);
637 info_value.SetBoolean("mpEnabled", mp_enabled);
[email protected]6ba198f2010-07-02 20:32:05638 dom_ui_->CallJavascriptFunction(L"enabledResult",
639 info_value);
640#endif
641}
[email protected]c4a530b2010-03-08 17:33:03642void FilebrowseHandler::HandleRefreshDirectory(const Value* value) {
643 if (value && value->GetType() == Value::TYPE_LIST) {
644 const ListValue* list_value = static_cast<const ListValue*>(value);
645 std::string path;
646
647 // Get path string.
648 if (list_value->GetString(0, &path)) {
649 FilePath currentpath;
650#if defined(OS_WIN)
651 currentpath = FilePath(ASCIIToWide(path));
652#else
653 currentpath = FilePath(path);
654#endif
655 GetChildrenForPath(currentpath, true);
656 } else {
657 LOG(ERROR) << "Unable to get string";
658 return;
659 }
660 }
661}
662
[email protected]a67fa08e2010-02-12 20:43:55663void FilebrowseHandler::HandlePauseToggleDownload(const Value* value) {
664#if defined(OS_CHROMEOS)
665 if (value && value->GetType() == Value::TYPE_LIST) {
666 const ListValue* list_value = static_cast<const ListValue*>(value);
667 int id;
668 std::string str_id;
669
670 if (list_value->GetString(0, &str_id)) {
671 id = atoi(str_id.c_str());
[email protected]377b4cc2010-05-18 17:25:19672 DownloadItem* item = active_download_items_[id];
[email protected]a67fa08e2010-02-12 20:43:55673 item->TogglePause();
674 } else {
675 LOG(ERROR) << "Unable to get id for download to pause";
676 return;
677 }
678 }
679#endif
680}
681
[email protected]c4a530b2010-03-08 17:33:03682void FilebrowseHandler::HandleAllowDownload(const Value* value) {
683#if defined(OS_CHROMEOS)
684 if (value && value->GetType() == Value::TYPE_LIST) {
685 const ListValue* list_value = static_cast<const ListValue*>(value);
686 int id;
687 std::string str_id;
688
689 if (list_value->GetString(0, &str_id)) {
690 id = atoi(str_id.c_str());
[email protected]377b4cc2010-05-18 17:25:19691 DownloadItem* item = active_download_items_[id];
[email protected]c4a530b2010-03-08 17:33:03692 download_manager_->DangerousDownloadValidated(item);
693 } else {
694 LOG(ERROR) << "Unable to get id for download to pause";
695 return;
696 }
697 }
698#endif
699}
700
701void FilebrowseHandler::HandleCancelDownload(const Value* value) {
702#if defined(OS_CHROMEOS)
703 if (value && value->GetType() == Value::TYPE_LIST) {
704 const ListValue* list_value = static_cast<const ListValue*>(value);
705 int id;
706 std::string str_id;
707
708 if (list_value->GetString(0, &str_id)) {
709 id = atoi(str_id.c_str());
[email protected]377b4cc2010-05-18 17:25:19710 DownloadItem* item = active_download_items_[id];
[email protected]c4a530b2010-03-08 17:33:03711 item->Cancel(true);
712 FilePath path = item->full_path();
713 FilePath dir_path = path.DirName();
714 item->Remove(true);
715 GetChildrenForPath(dir_path, true);
716 } else {
717 LOG(ERROR) << "Unable to get id for download to pause";
718 return;
719 }
720 }
721#endif
722}
723
[email protected]dcd23fe2009-11-12 20:21:18724void FilebrowseHandler::OpenNewFullWindow(const Value* value) {
725 OpenNewWindow(value, false);
726}
[email protected]698601e2009-10-21 22:43:37727
[email protected]dcd23fe2009-11-12 20:21:18728void FilebrowseHandler::OpenNewPopupWindow(const Value* value) {
729 OpenNewWindow(value, true);
730}
731
732void FilebrowseHandler::OpenNewWindow(const Value* value, bool popup) {
733 if (value && value->GetType() == Value::TYPE_LIST) {
734 const ListValue* list_value = static_cast<const ListValue*>(value);
735 Value* list_member;
736 std::string path;
737
738 // Get path string.
739 if (list_value->Get(0, &list_member) &&
740 list_member->GetType() == Value::TYPE_STRING) {
741 const StringValue* string_value =
742 static_cast<const StringValue*>(list_member);
743 string_value->GetAsString(&path);
744 } else {
745 LOG(ERROR) << "Unable to get string";
746 return;
747 }
748 Browser* browser;
749 if (popup) {
750 browser = Browser::CreateForPopup(profile_);
751 } else {
[email protected]9419bcf2010-04-12 17:26:10752 browser = BrowserList::GetLastActive();
[email protected]dcd23fe2009-11-12 20:21:18753 }
[email protected]50bd0c7322010-05-12 22:07:24754 TabContents* contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39755 GURL(path), GURL(), PageTransition::LINK, -1,
756 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]50bd0c7322010-05-12 22:07:24757 // AddTabWithURL could have picked another Browser instance to create this
758 // new tab at. So we have to reset the ptr of the browser that we want to
759 // talk to.
760 browser = contents->delegate()->GetBrowser();
[email protected]dcd23fe2009-11-12 20:21:18761 if (popup) {
762 // TODO(dhg): Remove these from being hardcoded. Allow javascript
763 // to specify.
[email protected]e561e202010-03-24 17:57:12764 browser->window()->SetBounds(gfx::Rect(0, 0, 400, 300));
[email protected]dcd23fe2009-11-12 20:21:18765 }
766 browser->window()->Show();
767 } else {
768 LOG(ERROR) << "Wasn't able to get the List if requested files.";
769 return;
770 }
771}
772
[email protected]d52bb8a2010-05-10 21:05:35773void FilebrowseHandler::SendPicasawebRequest() {
[email protected]068a71f2010-05-11 22:35:48774#if defined(OS_CHROMEOS)
775 chromeos::UserManager* user_man = chromeos::UserManager::Get();
776 std::string username = user_man->logged_in_user().email();
777
778 if (username.empty()) {
779 LOG(ERROR) << "Unable to get username";
780 return;
781 }
782
[email protected]d52bb8a2010-05-10 21:05:35783 fetch_.reset(URLFetcher::Create(0,
784 GURL(kPicasawebBaseUrl),
785 URLFetcher::GET,
[email protected]068a71f2010-05-11 22:35:48786 this));
[email protected]d52bb8a2010-05-10 21:05:35787 fetch_->set_request_context(profile_->GetRequestContext());
788 fetch_->Start();
[email protected]068a71f2010-05-11 22:35:48789#endif
[email protected]d52bb8a2010-05-10 21:05:35790}
791
[email protected]9d6b9aff2009-12-11 17:39:18792void FilebrowseHandler::ReadInFile() {
793#if defined(OS_CHROMEOS)
794 // Get the users username
795 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35796 chromeos::UserManager* user_man = chromeos::UserManager::Get();
797 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18798
799 if (username.empty()) {
800 LOG(ERROR) << "Unable to get username";
801 return;
802 }
803 int location = username.find_first_of('@',0);
804 if (location <= 0) {
805 LOG(ERROR) << "Username not formatted correctly";
806 return;
807 }
808 username = username.erase(username.find_first_of('@',0));
809 std::string url = kPicasawebUserPrefix;
810 url += username;
811 url += kPicasawebDefault;
812
813 FilePath currentpath;
814 currentpath = FilePath(current_file_uploaded_);
815 // Get the filename
816 std::string filename;
817 filename = currentpath.BaseName().value();
818 std::string filecontents;
819 if (!file_util::ReadFileToString(currentpath, &filecontents)) {
820 LOG(ERROR) << "Unable to read this file:" << currentpath.value();
821 return;
822 }
[email protected]d52bb8a2010-05-10 21:05:35823 fetch_.reset(URLFetcher::Create(0,
824 GURL(url),
825 URLFetcher::POST,
826 this));
827 fetch_->set_upload_data("image/jpeg", filecontents);
[email protected]9d6b9aff2009-12-11 17:39:18828 // Set the filename on the server
829 std::string slug = "Slug: ";
830 slug += filename;
[email protected]d52bb8a2010-05-10 21:05:35831 fetch_->set_extra_request_headers(slug);
832 fetch_->set_request_context(profile_->GetRequestContext());
833 fetch_->Start();
[email protected]9d6b9aff2009-12-11 17:39:18834#endif
835}
836
837// This is just a prototype for allowing generic uploads to various sites
838// TODO(dhg): Remove this and implement general upload.
839void FilebrowseHandler::UploadToPicasaweb(const Value* value) {
840 std::string path;
841#if defined(OS_CHROMEOS)
842 if (value && value->GetType() == Value::TYPE_LIST) {
843 const ListValue* list_value = static_cast<const ListValue*>(value);
844 Value* list_member;
845
846 // Get search string.
847 if (list_value->Get(0, &list_member) &&
848 list_member->GetType() == Value::TYPE_STRING) {
849 const StringValue* string_value =
850 static_cast<const StringValue*>(list_member);
851 string_value->GetAsString(&path);
852 }
853
854 } else {
855 LOG(ERROR) << "Wasn't able to get the List if requested files.";
856 return;
857 }
858 current_file_uploaded_ = path;
859 // ReadInFile();
[email protected]a67fa08e2010-02-12 20:43:55860 FilePath current_path(path);
861 TaskProxy* task = new TaskProxy(AsWeakPtr(), current_path);
[email protected]9d6b9aff2009-12-11 17:39:18862 task->AddRef();
[email protected]d52bb8a2010-05-10 21:05:35863 current_task_ = task;
[email protected]9d6b9aff2009-12-11 17:39:18864 ChromeThread::PostTask(
865 ChromeThread::FILE, FROM_HERE,
866 NewRunnableMethod(
867 task, &TaskProxy::ReadInFileProxy));
868#endif
869}
870
[email protected]a67fa08e2010-02-12 20:43:55871void FilebrowseHandler::GetChildrenForPath(FilePath& path, bool is_refresh) {
872 filelist_value_.reset(new ListValue());
873 currentpath_ = FilePath(path);
874
875 if (lister_.get()) {
876 lister_->Cancel();
877 lister_->set_delegate(NULL);
878 lister_ = NULL;
879 }
880
881 is_refresh_ = is_refresh;
882 lister_ = new net::DirectoryLister(currentpath_, this);
883 lister_->Start();
884}
885
[email protected]dcd23fe2009-11-12 20:21:18886void FilebrowseHandler::HandleGetChildren(const Value* value) {
887 std::string path;
[email protected]698601e2009-10-21 22:43:37888 if (value && value->GetType() == Value::TYPE_LIST) {
889 const ListValue* list_value = static_cast<const ListValue*>(value);
890 Value* list_member;
891
892 // Get search string.
893 if (list_value->Get(0, &list_member) &&
894 list_member->GetType() == Value::TYPE_STRING) {
895 const StringValue* string_value =
896 static_cast<const StringValue*>(list_member);
897 string_value->GetAsString(&path);
898 }
899
900 } else {
[email protected]dcd23fe2009-11-12 20:21:18901 LOG(ERROR) << "Wasn't able to get the List if requested files.";
[email protected]698601e2009-10-21 22:43:37902 return;
903 }
904 filelist_value_.reset(new ListValue());
[email protected]a67fa08e2010-02-12 20:43:55905 FilePath currentpath;
[email protected]698601e2009-10-21 22:43:37906#if defined(OS_WIN)
[email protected]a67fa08e2010-02-12 20:43:55907 currentpath = FilePath(ASCIIToWide(path));
[email protected]698601e2009-10-21 22:43:37908#else
[email protected]a67fa08e2010-02-12 20:43:55909 currentpath = FilePath(path);
[email protected]698601e2009-10-21 22:43:37910#endif
911
[email protected]a67fa08e2010-02-12 20:43:55912 GetChildrenForPath(currentpath, false);
[email protected]698601e2009-10-21 22:43:37913}
914
[email protected]dcd23fe2009-11-12 20:21:18915void FilebrowseHandler::OnListFile(
[email protected]698601e2009-10-21 22:43:37916 const file_util::FileEnumerator::FindInfo& data) {
[email protected]07046ab2010-01-20 21:42:44917#if defined(OS_WIN)
918 if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
919 return;
920 }
921#elif defined(OS_POSIX)
922 if (data.filename[0] == '.') {
923 return;
924 }
925#endif
926
[email protected]698601e2009-10-21 22:43:37927 DictionaryValue* file_value = new DictionaryValue();
928
929#if defined(OS_WIN)
930 int64 size = (static_cast<int64>(data.nFileSizeHigh) << 32) |
931 data.nFileSizeLow;
932 file_value->SetString(kPropertyTitle, data.cFileName);
933 file_value->SetString(kPropertyPath,
934 currentpath_.Append(data.cFileName).value());
935 file_value->SetBoolean(kPropertyDirectory,
936 (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false);
937
938#elif defined(OS_POSIX)
939 file_value->SetString(kPropertyTitle, data.filename);
940 file_value->SetString(kPropertyPath,
941 currentpath_.Append(data.filename).value());
942 file_value->SetBoolean(kPropertyDirectory, S_ISDIR(data.stat.st_mode));
943#endif
944 filelist_value_->Append(file_value);
945}
946
[email protected]dcd23fe2009-11-12 20:21:18947void FilebrowseHandler::OnListDone(int error) {
[email protected]698601e2009-10-21 22:43:37948 DictionaryValue info_value;
[email protected]a67fa08e2010-02-12 20:43:55949 if (is_refresh_) {
[email protected]e4fbe8e2010-08-07 07:00:46950 info_value.SetString("functionCall", "refresh");
[email protected]a67fa08e2010-02-12 20:43:55951 } else {
[email protected]e4fbe8e2010-08-07 07:00:46952 info_value.SetString("functionCall", "getChildren");
[email protected]a67fa08e2010-02-12 20:43:55953 }
[email protected]698601e2009-10-21 22:43:37954 info_value.SetString(kPropertyPath, currentpath_.value());
[email protected]9d6b9aff2009-12-11 17:39:18955 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37956 info_value, *(filelist_value_.get()));
[email protected]a67fa08e2010-02-12 20:43:55957 SendCurrentDownloads();
[email protected]698601e2009-10-21 22:43:37958}
959
[email protected]dcd23fe2009-11-12 20:21:18960void FilebrowseHandler::HandleGetMetadata(const Value* value) {
[email protected]698601e2009-10-21 22:43:37961}
962
[email protected]274e42b2010-01-29 22:03:57963void FilebrowseHandler::HandleGetDownloads(const Value* value) {
964 ModelChanged();
965}
966
967void FilebrowseHandler::ModelChanged() {
968 ClearDownloadItems();
[email protected]274e42b2010-01-29 22:03:57969
[email protected]0e2b59a2010-07-29 22:27:05970 std::vector<DownloadItem*> downloads;
971 download_manager_->GetAllDownloads(FilePath(), &downloads);
972
[email protected]377b4cc2010-05-18 17:25:19973 std::vector<DownloadItem*> new_downloads;
[email protected]274e42b2010-01-29 22:03:57974 // Scan for any in progress downloads and add ourself to them as an observer.
975 for (DownloadList::iterator it = downloads.begin();
976 it != downloads.end(); ++it) {
977 DownloadItem* download = *it;
978 // We want to know what happens as the download progresses and be notified
979 // when the user validates the dangerous download.
980 if (download->state() == DownloadItem::IN_PROGRESS ||
981 download->safety_state() == DownloadItem::DANGEROUS) {
982 download->AddObserver(this);
[email protected]377b4cc2010-05-18 17:25:19983 active_download_items_.push_back(download);
[email protected]274e42b2010-01-29 22:03:57984 }
[email protected]377b4cc2010-05-18 17:25:19985 DownloadList::iterator item = find(download_items_.begin(),
986 download_items_.end(),
987 download);
988 if (item == download_items_.end() && got_first_download_list_) {
989 SendNewDownload(download);
990 }
991 new_downloads.push_back(download);
[email protected]274e42b2010-01-29 22:03:57992 }
[email protected]377b4cc2010-05-18 17:25:19993 download_items_.swap(new_downloads);
994 got_first_download_list_ = true;
[email protected]274e42b2010-01-29 22:03:57995 SendCurrentDownloads();
996}
997
[email protected]377b4cc2010-05-18 17:25:19998void FilebrowseHandler::SendNewDownload(DownloadItem* download) {
999 ListValue results_value;
1000 results_value.Append(download_util::CreateDownloadItemValue(download, -1));
1001 dom_ui_->CallJavascriptFunction(L"newDownload", results_value);
1002}
1003
[email protected]a67fa08e2010-02-12 20:43:551004void FilebrowseHandler::DeleteFile(const FilePath& path) {
1005 if (!file_util::Delete(path, true)) {
1006 LOG(ERROR) << "unable to delete directory";
1007 }
1008 ChromeThread::PostTask(
1009 ChromeThread::UI, FROM_HERE,
[email protected]d52bb8a2010-05-10 21:05:351010 NewRunnableMethod(current_task_, &TaskProxy::FireDeleteCompleteProxy));
[email protected]a67fa08e2010-02-12 20:43:551011}
1012
1013void FilebrowseHandler::HandleDeleteFile(const Value* value) {
1014 #if defined(OS_CHROMEOS)
1015 if (value && value->GetType() == Value::TYPE_LIST) {
1016 const ListValue* list_value = static_cast<const ListValue*>(value);
1017 std::string path;
1018
1019 // Get path string.
1020 if (list_value->GetString(0, &path)) {
1021
1022 FilePath currentpath;
1023 currentpath = FilePath(path);
[email protected]377b4cc2010-05-18 17:25:191024 for (unsigned int x = 0; x < active_download_items_.size(); x++) {
1025 FilePath item = active_download_items_[x]->full_path();
[email protected]c4a530b2010-03-08 17:33:031026 if (item == currentpath) {
[email protected]377b4cc2010-05-18 17:25:191027 active_download_items_[x]->Cancel(true);
1028 active_download_items_[x]->Remove(true);
[email protected]c4a530b2010-03-08 17:33:031029 FilePath dir_path = item.DirName();
1030 GetChildrenForPath(dir_path, true);
1031 return;
1032 }
1033 }
[email protected]a67fa08e2010-02-12 20:43:551034 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath);
1035 task->AddRef();
[email protected]d52bb8a2010-05-10 21:05:351036 current_task_ = task;
[email protected]a67fa08e2010-02-12 20:43:551037 ChromeThread::PostTask(
1038 ChromeThread::FILE, FROM_HERE,
1039 NewRunnableMethod(
1040 task, &TaskProxy::DeleteFileProxy));
1041 } else {
1042 LOG(ERROR) << "Unable to get string";
1043 return;
1044 }
1045 }
1046#endif
1047}
1048
[email protected]274e42b2010-01-29 22:03:571049void FilebrowseHandler::OnDownloadUpdated(DownloadItem* download) {
[email protected]377b4cc2010-05-18 17:25:191050 DownloadList::iterator it = find(active_download_items_.begin(),
1051 active_download_items_.end(),
[email protected]274e42b2010-01-29 22:03:571052 download);
[email protected]377b4cc2010-05-18 17:25:191053 if (it == active_download_items_.end())
[email protected]274e42b2010-01-29 22:03:571054 return;
[email protected]377b4cc2010-05-18 17:25:191055 const int id = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:571056
1057 ListValue results_value;
1058 results_value.Append(download_util::CreateDownloadItemValue(download, id));
1059 dom_ui_->CallJavascriptFunction(L"downloadUpdated", results_value);
1060}
1061
1062void FilebrowseHandler::ClearDownloadItems() {
[email protected]377b4cc2010-05-18 17:25:191063 for (DownloadList::iterator it = active_download_items_.begin();
1064 it != active_download_items_.end(); ++it) {
[email protected]274e42b2010-01-29 22:03:571065 (*it)->RemoveObserver(this);
1066 }
[email protected]377b4cc2010-05-18 17:25:191067 active_download_items_.clear();
[email protected]274e42b2010-01-29 22:03:571068}
1069
1070void FilebrowseHandler::SendCurrentDownloads() {
1071 ListValue results_value;
[email protected]377b4cc2010-05-18 17:25:191072 for (DownloadList::iterator it = active_download_items_.begin();
1073 it != active_download_items_.end(); ++it) {
1074 int index = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:571075 results_value.Append(download_util::CreateDownloadItemValue(*it, index));
1076 }
1077
1078 dom_ui_->CallJavascriptFunction(L"downloadsList", results_value);
1079}
1080
[email protected]698601e2009-10-21 22:43:371081////////////////////////////////////////////////////////////////////////////////
1082//
[email protected]f5bf8ccf2010-02-05 18:19:251083// FileBrowseUI
[email protected]698601e2009-10-21 22:43:371084//
1085////////////////////////////////////////////////////////////////////////////////
1086
[email protected]274e42b2010-01-29 22:03:571087FileBrowseUI::FileBrowseUI(TabContents* contents) : HtmlDialogUI(contents) {
[email protected]9d6b9aff2009-12-11 17:39:181088 FilebrowseHandler* handler = new FilebrowseHandler();
1089 AddMessageHandler((handler)->Attach(this));
[email protected]274e42b2010-01-29 22:03:571090 handler->Init();
[email protected]698601e2009-10-21 22:43:371091 FileBrowseUIHTMLSource* html_source = new FileBrowseUIHTMLSource();
1092
1093 // Set up the chrome://filebrowse/ source.
[email protected]fae20792009-10-28 20:31:581094 ChromeThread::PostTask(
1095 ChromeThread::IO, FROM_HERE,
1096 NewRunnableMethod(
[email protected]576a4ca2009-11-05 01:41:091097 Singleton<ChromeURLDataManager>::get(),
[email protected]698601e2009-10-21 22:43:371098 &ChromeURLDataManager::AddDataSource,
[email protected]f8f82502009-11-20 23:14:231099 make_scoped_refptr(html_source)));
[email protected]698601e2009-10-21 22:43:371100}
[email protected]f5bf8ccf2010-02-05 18:19:251101
1102// static
[email protected]1717246f2010-02-10 17:08:151103Browser* FileBrowseUI::OpenPopup(Profile* profile,
[email protected]467293292010-04-16 19:41:561104 const std::string& hashArgument,
1105 int width,
1106 int height) {
[email protected]1717246f2010-02-10 17:08:151107 // Get existing pop up for given hashArgument.
1108 Browser* browser = GetPopupForPath(hashArgument);
[email protected]f5bf8ccf2010-02-05 18:19:251109
[email protected]1717246f2010-02-10 17:08:151110 // Create new browser if no matching pop up found.
1111 if (browser == NULL) {
1112 browser = Browser::CreateForPopup(profile);
[email protected]f59f1162010-04-23 21:19:291113 std::string url;
1114 if (hashArgument.empty()) {
1115 url = chrome::kChromeUIFileBrowseURL;
1116 } else {
1117 url = kFilebrowseURLHash;
1118 url.append(hashArgument);
1119 }
[email protected]f5bf8ccf2010-02-05 18:19:251120
[email protected]1717246f2010-02-10 17:08:151121 browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:391122 GURL(url), GURL(), PageTransition::LINK, -1,
1123 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]1717246f2010-02-10 17:08:151124 browser->window()->SetBounds(gfx::Rect(kPopupLeft,
1125 kPopupTop,
[email protected]467293292010-04-16 19:41:561126 width,
1127 height));
[email protected]1717246f2010-02-10 17:08:151128
1129 browser->window()->Show();
1130 }
[email protected]f5bf8ccf2010-02-05 18:19:251131
1132 return browser;
1133}
[email protected]1717246f2010-02-10 17:08:151134
1135Browser* FileBrowseUI::GetPopupForPath(const std::string& path) {
1136 for (BrowserList::const_iterator it = BrowserList::begin();
1137 it != BrowserList::end(); ++it) {
1138 if ((*it)->type() == Browser::TYPE_POPUP) {
[email protected]d519bed2010-05-28 18:42:031139 TabContents* tab_contents = (*it)->GetSelectedTabContents();
1140 DCHECK(tab_contents);
1141 if (!tab_contents)
1142 continue;
1143 const GURL& url = tab_contents->GetURL();
[email protected]1717246f2010-02-10 17:08:151144
1145 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1146 url.host() == chrome::kChromeUIFileBrowseHost &&
1147 url.ref() == path) {
1148 return (*it);
1149 }
1150 }
1151 }
1152
1153 return NULL;
1154}
[email protected]467293292010-04-16 19:41:561155
1156const int FileBrowseUI::kPopupWidth = 250;
1157const int FileBrowseUI::kPopupHeight = 300;
1158const int FileBrowseUI::kSmallPopupWidth = 250;
1159const int FileBrowseUI::kSmallPopupHeight = 50;