blob: 8bab7cd7c340ae3cab8c04935363a1feb2da8def [file] [log] [blame]
[email protected]698601e2009-10-21 22:43:371// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/dom_ui/filebrowse_ui.h"
6
7#include "app/l10n_util.h"
8#include "app/resource_bundle.h"
[email protected]2041cf342010-02-19 03:15:599#include "base/callback.h"
[email protected]698601e2009-10-21 22:43:3710#include "base/logging.h"
11#include "base/message_loop.h"
[email protected]07046ab2010-01-20 21:42:4412#include "base/path_service.h"
[email protected]5c4c7402009-10-30 19:58:0613#include "base/singleton.h"
[email protected]698601e2009-10-21 22:43:3714#include "base/string_piece.h"
15#include "base/string_util.h"
16#include "base/thread.h"
17#include "base/time.h"
18#include "base/values.h"
[email protected]9d6b9aff2009-12-11 17:39:1819#include "base/weak_ptr.h"
[email protected]698601e2009-10-21 22:43:3720#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]dcd23fe2009-11-12 20:21:1821#include "chrome/browser/browser.h"
[email protected]1717246f2010-02-10 17:08:1522#include "chrome/browser/browser_list.h"
[email protected]dcd23fe2009-11-12 20:21:1823#include "chrome/browser/browser_window.h"
[email protected]fae20792009-10-28 20:31:5824#include "chrome/browser/chrome_thread.h"
[email protected]698601e2009-10-21 22:43:3725#include "chrome/browser/dom_ui/dom_ui_favicon_source.h"
[email protected]75986612010-07-16 22:23:5726#include "chrome/browser/download/download_item.h"
[email protected]274e42b2010-01-29 22:03:5727#include "chrome/browser/download/download_manager.h"
28#include "chrome/browser/download/download_util.h"
[email protected]1717246f2010-02-10 17:08:1529#include "chrome/browser/history/history_types.h"
[email protected]698601e2009-10-21 22:43:3730#include "chrome/browser/metrics/user_metrics.h"
[email protected]6ba198f2010-07-02 20:32:0531#include "chrome/browser/pref_service.h"
[email protected]698601e2009-10-21 22:43:3732#include "chrome/browser/profile.h"
[email protected]1717246f2010-02-10 17:08:1533#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]68d2a05f2010-05-07 21:39:5534#include "chrome/browser/dom_ui/mediaplayer_ui.h"
[email protected]07046ab2010-01-20 21:42:4435#include "chrome/common/chrome_paths.h"
[email protected]698601e2009-10-21 22:43:3736#include "chrome/common/jstemplate_builder.h"
[email protected]68d2a05f2010-05-07 21:39:5537#include "chrome/common/net/url_fetcher.h"
[email protected]6ba198f2010-07-02 20:32:0538#include "chrome/common/pref_names.h"
[email protected]698601e2009-10-21 22:43:3739#include "chrome/common/time_format.h"
40#include "chrome/common/url_constants.h"
41#include "net/base/escape.h"
42
43#include "grit/browser_resources.h"
44#include "grit/chromium_strings.h"
45#include "grit/generated_resources.h"
46#include "grit/locale_settings.h"
47
[email protected]07046ab2010-01-20 21:42:4448#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:2449#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]268b02f2010-02-04 21:07:1550#include "chrome/browser/chromeos/cros/mount_library.h"
[email protected]d52bb8a2010-05-10 21:05:3551#include "chrome/browser/chromeos/login/user_manager.h"
[email protected]07046ab2010-01-20 21:42:4452#endif
53
[email protected]698601e2009-10-21 22:43:3754// Maximum number of search results to return in a given search. We should
55// eventually remove this.
56static const int kMaxSearchResults = 100;
57static const std::wstring kPropertyPath = L"path";
58static const std::wstring kPropertyTitle = L"title";
59static const std::wstring kPropertyDirectory = L"isDirectory";
[email protected]9d6b9aff2009-12-11 17:39:1860static const std::string kPicasawebUserPrefix =
61 "http://picasaweb.google.com/data/feed/api/user/";
62static const std::string kPicasawebDefault = "/albumid/default";
[email protected]88f03da02010-06-10 16:21:1363static const std::string kPicasawebDropBox = "/home";
[email protected]9d6b9aff2009-12-11 17:39:1864static const std::string kPicasawebBaseUrl = "http://picasaweb.google.com/";
[email protected]88f03da02010-06-10 16:21:1365static const std::string kMediaPath = "/media";
[email protected]f5bf8ccf2010-02-05 18:19:2566static const char* kFilebrowseURLHash = "chrome://filebrowse#";
67static const int kPopupLeft = 0;
68static const int kPopupTop = 0;
[email protected]f5bf8ccf2010-02-05 18:19:2569
[email protected]698601e2009-10-21 22:43:3770class FileBrowseUIHTMLSource : public ChromeURLDataManager::DataSource {
71 public:
72 FileBrowseUIHTMLSource();
73
74 // Called when the network layer has requested a resource underneath
75 // the path we registered.
[email protected]f09d93792009-11-17 00:10:3676 virtual void StartDataRequest(const std::string& path,
77 bool is_off_the_record,
78 int request_id);
[email protected]698601e2009-10-21 22:43:3779 virtual std::string GetMimeType(const std::string&) const {
80 return "text/html";
81 }
82
83 private:
[email protected]8de85a62009-11-06 08:32:1784 ~FileBrowseUIHTMLSource() {}
85
[email protected]698601e2009-10-21 22:43:3786 DISALLOW_COPY_AND_ASSIGN(FileBrowseUIHTMLSource);
87};
88
[email protected]9d6b9aff2009-12-11 17:39:1889class TaskProxy;
90
[email protected]698601e2009-10-21 22:43:3791// The handler for Javascript messages related to the "filebrowse" view.
[email protected]dcd23fe2009-11-12 20:21:1892class FilebrowseHandler : public net::DirectoryLister::DirectoryListerDelegate,
[email protected]9d6b9aff2009-12-11 17:39:1893 public DOMMessageHandler,
[email protected]07046ab2010-01-20 21:42:4494#if defined(OS_CHROMEOS)
95 public chromeos::MountLibrary::Observer,
96#endif
[email protected]9d6b9aff2009-12-11 17:39:1897 public base::SupportsWeakPtr<FilebrowseHandler>,
[email protected]274e42b2010-01-29 22:03:5798 public URLFetcher::Delegate,
99 public DownloadManager::Observer,
100 public DownloadItem::Observer {
[email protected]698601e2009-10-21 22:43:37101 public:
[email protected]dcd23fe2009-11-12 20:21:18102 FilebrowseHandler();
103 virtual ~FilebrowseHandler();
[email protected]698601e2009-10-21 22:43:37104
[email protected]274e42b2010-01-29 22:03:57105 // Init work after Attach.
106 void Init();
107
[email protected]698601e2009-10-21 22:43:37108 // DirectoryLister::DirectoryListerDelegate methods:
109 virtual void OnListFile(const file_util::FileEnumerator::FindInfo& data);
110 virtual void OnListDone(int error);
111
112 // DOMMessageHandler implementation.
113 virtual DOMMessageHandler* Attach(DOMUI* dom_ui);
114 virtual void RegisterMessages();
115
[email protected]07046ab2010-01-20 21:42:44116#if defined(OS_CHROMEOS)
117 void MountChanged(chromeos::MountLibrary* obj,
118 chromeos::MountEventType evt,
119 const std::string& path);
120#endif
121
[email protected]274e42b2010-01-29 22:03:57122 // DownloadItem::Observer interface
123 virtual void OnDownloadUpdated(DownloadItem* download);
124 virtual void OnDownloadFileCompleted(DownloadItem* download) { }
125 virtual void OnDownloadOpened(DownloadItem* download) { }
126
127 // DownloadManager::Observer interface
128 virtual void ModelChanged();
[email protected]82f37b02010-07-29 22:04:57129
130 void OnSearchDownloadsComplete(std::vector<DownloadItem*> downloads);
[email protected]274e42b2010-01-29 22:03:57131
[email protected]698601e2009-10-21 22:43:37132 // Callback for the "getRoots" message.
133 void HandleGetRoots(const Value* value);
134
[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.
145 void HandleGetChildren(const Value* value);
[email protected]c4a530b2010-03-08 17:33:03146 // Callback for the "refreshDirectory" message.
147 void HandleRefreshDirectory(const Value* value);
[email protected]6ba198f2010-07-02 20:32:05148 void HandleIsAdvancedEnabled(const Value* value);
[email protected]698601e2009-10-21 22:43:37149
150 // Callback for the "getMetadata" message.
151 void HandleGetMetadata(const Value* value);
152
[email protected]9d6b9aff2009-12-11 17:39:18153 // Callback for the "openNewWindow" message.
[email protected]dcd23fe2009-11-12 20:21:18154 void OpenNewFullWindow(const Value* value);
155 void OpenNewPopupWindow(const Value* value);
156
[email protected]9d6b9aff2009-12-11 17:39:18157 // Callback for the "uploadToPicasaweb" message.
158 void UploadToPicasaweb(const Value* value);
159
[email protected]274e42b2010-01-29 22:03:57160 // Callback for the "getDownloads" message.
161 void HandleGetDownloads(const Value* value);
162
163 void HandleCreateNewFolder(const Value* value);
164
[email protected]17496bb2010-03-10 19:48:23165 void PlayMediaFile(const Value* value);
[email protected]89a7e3812010-06-02 19:38:07166 void EnqueueMediaFile(const Value* value);
[email protected]17496bb2010-03-10 19:48:23167
[email protected]a67fa08e2010-02-12 20:43:55168 void HandleDeleteFile(const Value* value);
169 void DeleteFile(const FilePath& path);
170 void FireDeleteComplete(const FilePath& path);
171
172 void HandlePauseToggleDownload(const Value* value);
173
[email protected]c4a530b2010-03-08 17:33:03174 void HandleCancelDownload(const Value* value);
175 void HandleAllowDownload(const Value* value);
176
[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
183 void OpenNewWindow(const Value* value, bool popup);
184
[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]f95f0752010-03-09 16:47:10273 localized_strings.SetString(L"title",
274 l10n_util::GetString(IDS_FILEBROWSER_TITLE));
275 localized_strings.SetString(L"pause",
276 l10n_util::GetString(IDS_FILEBROWSER_PAUSE));
277 localized_strings.SetString(L"resume",
278 l10n_util::GetString(IDS_FILEBROWSER_RESUME));
279 localized_strings.SetString(L"scanning",
280 l10n_util::GetString(IDS_FILEBROWSER_SCANNING));
281 localized_strings.SetString(L"confirmdelete",
282 l10n_util::GetString(IDS_FILEBROWSER_CONFIRM_DELETE));
283 localized_strings.SetString(L"confirmyes",
284 l10n_util::GetString(IDS_FILEBROWSER_CONFIRM_YES));
285 localized_strings.SetString(L"confirmcancel",
286 l10n_util::GetString(IDS_FILEBROWSER_CONFIRM_CANCEL));
287 localized_strings.SetString(L"allowdownload",
288 l10n_util::GetString(IDS_FILEBROWSER_CONFIRM_DOWNLOAD));
289 localized_strings.SetString(L"filenameprompt",
290 l10n_util::GetString(IDS_FILEBROWSER_PROMPT_FILENAME));
291 localized_strings.SetString(L"save",
292 l10n_util::GetString(IDS_FILEBROWSER_SAVE));
293 localized_strings.SetString(L"newfolder",
294 l10n_util::GetString(IDS_FILEBROWSER_NEW_FOLDER));
295 localized_strings.SetString(L"open",
296 l10n_util::GetString(IDS_FILEBROWSER_OPEN));
297 localized_strings.SetString(L"picasaweb",
298 l10n_util::GetString(IDS_FILEBROWSER_UPLOAD_PICASAWEB));
[email protected]a25802d2010-04-21 16:18:20299 localized_strings.SetString(L"flickr",
300 l10n_util::GetString(IDS_FILEBROWSER_UPLOAD_FLICKR));
[email protected]f95f0752010-03-09 16:47:10301 localized_strings.SetString(L"email",
302 l10n_util::GetString(IDS_FILEBROWSER_UPLOAD_EMAIL));
303 localized_strings.SetString(L"delete",
304 l10n_util::GetString(IDS_FILEBROWSER_DELETE));
[email protected]89a7e3812010-06-02 19:38:07305 localized_strings.SetString(L"enqueue",
306 l10n_util::GetString(IDS_FILEBROWSER_ENQUEUE));
[email protected]88f03da02010-06-10 16:21:13307 localized_strings.SetString(L"mediapath", kMediaPath);
308 FilePath default_download_path;
309 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
310 &default_download_path)) {
311 NOTREACHED();
312 }
313 localized_strings.SetString(L"downloadpath", default_download_path.value());
[email protected]183e2f02010-06-11 17:13:14314 localized_strings.SetString(L"error_unknown_file_type",
315 l10n_util::GetString(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;
451 info_value.SetString(L"path", current_file_uploaded_);
452
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));
467 std::string picture_url;
468 picture_url = kPicasawebBaseUrl;
469 picture_url += username;
470 picture_url += kPicasawebDropBox;
471 info_value.SetString(L"url", picture_url);
472 info_value.SetInteger(L"status_code", upload_response_code_);
473 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]dcd23fe2009-11-12 20:21:18505void FilebrowseHandler::HandleGetRoots(const Value* value) {
[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);
518 FilePath currentpath;
519 currentpath = FilePath(disks[i].mount_path);
520 std::string filename;
521 filename = currentpath.BaseName().value();
522 page_value->SetString(kPropertyTitle, filename);
523 page_value->SetBoolean(kPropertyDirectory, true);
524 results_value.Append(page_value);
525 }
526 }
527#else
528 DictionaryValue* page_value = new DictionaryValue();
529 page_value->SetString(kPropertyPath, "/media");
530 page_value->SetString(kPropertyTitle, "Removeable");
[email protected]698601e2009-10-21 22:43:37531 page_value->SetBoolean(kPropertyDirectory, true);
532
533 results_value.Append(page_value);
[email protected]07046ab2010-01-20 21:42:44534#endif
535 FilePath default_download_path;
536 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
537 &default_download_path)) {
538 NOTREACHED();
539 }
540
541 DictionaryValue* download_value = new DictionaryValue();
542 download_value->SetString(kPropertyPath, default_download_path.value());
543 download_value->SetString(kPropertyTitle, "File Shelf");
544 download_value->SetBoolean(kPropertyDirectory, true);
545
546 results_value.Append(download_value);
[email protected]698601e2009-10-21 22:43:37547
[email protected]9d6b9aff2009-12-11 17:39:18548 info_value.SetString(L"functionCall", "getRoots");
[email protected]07046ab2010-01-20 21:42:44549 info_value.SetString(kPropertyPath, "");
[email protected]9d6b9aff2009-12-11 17:39:18550 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37551 info_value, results_value);
552}
553
[email protected]274e42b2010-01-29 22:03:57554void FilebrowseHandler::HandleCreateNewFolder(const Value* value) {
555#if defined(OS_CHROMEOS)
556 if (value && value->GetType() == Value::TYPE_LIST) {
557 const ListValue* list_value = static_cast<const ListValue*>(value);
558 std::string path;
559
560 // Get path string.
561 if (list_value->GetString(0, &path)) {
562
563 FilePath currentpath;
564 currentpath = FilePath(path);
565
566 if (!file_util::CreateDirectory(currentpath)) {
567 LOG(ERROR) << "unable to create directory";
568 }
569 } else {
570 LOG(ERROR) << "Unable to get string";
571 return;
572 }
573 }
574#endif
575}
576
[email protected]17496bb2010-03-10 19:48:23577void FilebrowseHandler::PlayMediaFile(const Value* value) {
578#if defined(OS_CHROMEOS)
579 if (value && value->GetType() == Value::TYPE_LIST) {
580 const ListValue* list_value = static_cast<const ListValue*>(value);
581 std::string path;
582
583 // Get path string.
584 if (list_value->GetString(0, &path)) {
585 FilePath currentpath;
586 currentpath = FilePath(path);
587
588 MediaPlayer* mediaplayer = MediaPlayer::Get();
589 std::string url = currentpath.value();
590
591 GURL gurl(url);
[email protected]93a58bf72010-06-04 23:01:20592 Browser* browser = Browser::GetBrowserForController(
593 &tab_contents_->controller(), NULL);
594 mediaplayer->ForcePlayMediaURL(gurl, browser);
[email protected]89a7e3812010-06-02 19:38:07595 } else {
596 LOG(ERROR) << "Unable to get string";
597 return;
598 }
599 }
600#endif
601}
602
603void FilebrowseHandler::EnqueueMediaFile(const Value* value) {
604#if defined(OS_CHROMEOS)
605 if (value && value->GetType() == Value::TYPE_LIST) {
606 const ListValue* list_value = static_cast<const ListValue*>(value);
607 std::string path;
608
609 // Get path string.
610 if (list_value->GetString(0, &path)) {
611 FilePath currentpath;
612 currentpath = FilePath(path);
613
614 MediaPlayer* mediaplayer = MediaPlayer::Get();
615 std::string url = currentpath.value();
616
617 GURL gurl(url);
[email protected]93a58bf72010-06-04 23:01:20618 Browser* browser = Browser::GetBrowserForController(
619 &tab_contents_->controller(), NULL);
620 mediaplayer->EnqueueMediaURL(gurl, browser);
[email protected]17496bb2010-03-10 19:48:23621 } else {
622 LOG(ERROR) << "Unable to get string";
623 return;
624 }
625 }
626#endif
627}
628
[email protected]6ba198f2010-07-02 20:32:05629void FilebrowseHandler::HandleIsAdvancedEnabled(const Value* value) {
630#if defined(OS_CHROMEOS)
631 Profile* profile = BrowserList::GetLastActive()->profile();
632 PrefService* pref_service = profile->GetPrefs();
633 bool is_enabled = pref_service->GetBoolean(
634 prefs::kLabsAdvancedFilesystemEnabled);
635 bool mp_enabled = pref_service->GetBoolean(prefs::kLabsMediaplayerEnabled);
636 DictionaryValue info_value;
637 info_value.SetBoolean(L"enabled", is_enabled);
638 info_value.SetBoolean(L"mpEnabled", mp_enabled);
639 dom_ui_->CallJavascriptFunction(L"enabledResult",
640 info_value);
641#endif
642}
[email protected]c4a530b2010-03-08 17:33:03643void FilebrowseHandler::HandleRefreshDirectory(const Value* value) {
644 if (value && value->GetType() == Value::TYPE_LIST) {
645 const ListValue* list_value = static_cast<const ListValue*>(value);
646 std::string path;
647
648 // Get path string.
649 if (list_value->GetString(0, &path)) {
650 FilePath currentpath;
651#if defined(OS_WIN)
652 currentpath = FilePath(ASCIIToWide(path));
653#else
654 currentpath = FilePath(path);
655#endif
656 GetChildrenForPath(currentpath, true);
657 } else {
658 LOG(ERROR) << "Unable to get string";
659 return;
660 }
661 }
662}
663
[email protected]a67fa08e2010-02-12 20:43:55664void FilebrowseHandler::HandlePauseToggleDownload(const Value* value) {
665#if defined(OS_CHROMEOS)
666 if (value && value->GetType() == Value::TYPE_LIST) {
667 const ListValue* list_value = static_cast<const ListValue*>(value);
668 int id;
669 std::string str_id;
670
671 if (list_value->GetString(0, &str_id)) {
672 id = atoi(str_id.c_str());
[email protected]377b4cc2010-05-18 17:25:19673 DownloadItem* item = active_download_items_[id];
[email protected]a67fa08e2010-02-12 20:43:55674 item->TogglePause();
675 } else {
676 LOG(ERROR) << "Unable to get id for download to pause";
677 return;
678 }
679 }
680#endif
681}
682
[email protected]c4a530b2010-03-08 17:33:03683void FilebrowseHandler::HandleAllowDownload(const Value* value) {
684#if defined(OS_CHROMEOS)
685 if (value && value->GetType() == Value::TYPE_LIST) {
686 const ListValue* list_value = static_cast<const ListValue*>(value);
687 int id;
688 std::string str_id;
689
690 if (list_value->GetString(0, &str_id)) {
691 id = atoi(str_id.c_str());
[email protected]377b4cc2010-05-18 17:25:19692 DownloadItem* item = active_download_items_[id];
[email protected]c4a530b2010-03-08 17:33:03693 download_manager_->DangerousDownloadValidated(item);
694 } else {
695 LOG(ERROR) << "Unable to get id for download to pause";
696 return;
697 }
698 }
699#endif
700}
701
702void FilebrowseHandler::HandleCancelDownload(const Value* value) {
703#if defined(OS_CHROMEOS)
704 if (value && value->GetType() == Value::TYPE_LIST) {
705 const ListValue* list_value = static_cast<const ListValue*>(value);
706 int id;
707 std::string str_id;
708
709 if (list_value->GetString(0, &str_id)) {
710 id = atoi(str_id.c_str());
[email protected]377b4cc2010-05-18 17:25:19711 DownloadItem* item = active_download_items_[id];
[email protected]c4a530b2010-03-08 17:33:03712 item->Cancel(true);
713 FilePath path = item->full_path();
714 FilePath dir_path = path.DirName();
715 item->Remove(true);
716 GetChildrenForPath(dir_path, true);
717 } else {
718 LOG(ERROR) << "Unable to get id for download to pause";
719 return;
720 }
721 }
722#endif
723}
724
[email protected]dcd23fe2009-11-12 20:21:18725void FilebrowseHandler::OpenNewFullWindow(const Value* value) {
726 OpenNewWindow(value, false);
727}
[email protected]698601e2009-10-21 22:43:37728
[email protected]dcd23fe2009-11-12 20:21:18729void FilebrowseHandler::OpenNewPopupWindow(const Value* value) {
730 OpenNewWindow(value, true);
731}
732
733void FilebrowseHandler::OpenNewWindow(const Value* value, bool popup) {
734 if (value && value->GetType() == Value::TYPE_LIST) {
735 const ListValue* list_value = static_cast<const ListValue*>(value);
736 Value* list_member;
737 std::string path;
738
739 // Get path string.
740 if (list_value->Get(0, &list_member) &&
741 list_member->GetType() == Value::TYPE_STRING) {
742 const StringValue* string_value =
743 static_cast<const StringValue*>(list_member);
744 string_value->GetAsString(&path);
745 } else {
746 LOG(ERROR) << "Unable to get string";
747 return;
748 }
749 Browser* browser;
750 if (popup) {
751 browser = Browser::CreateForPopup(profile_);
752 } else {
[email protected]9419bcf2010-04-12 17:26:10753 browser = BrowserList::GetLastActive();
[email protected]dcd23fe2009-11-12 20:21:18754 }
[email protected]50bd0c7322010-05-12 22:07:24755 TabContents* contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39756 GURL(path), GURL(), PageTransition::LINK, -1,
757 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]50bd0c7322010-05-12 22:07:24758 // AddTabWithURL could have picked another Browser instance to create this
759 // new tab at. So we have to reset the ptr of the browser that we want to
760 // talk to.
761 browser = contents->delegate()->GetBrowser();
[email protected]dcd23fe2009-11-12 20:21:18762 if (popup) {
763 // TODO(dhg): Remove these from being hardcoded. Allow javascript
764 // to specify.
[email protected]e561e202010-03-24 17:57:12765 browser->window()->SetBounds(gfx::Rect(0, 0, 400, 300));
[email protected]dcd23fe2009-11-12 20:21:18766 }
767 browser->window()->Show();
768 } else {
769 LOG(ERROR) << "Wasn't able to get the List if requested files.";
770 return;
771 }
772}
773
[email protected]d52bb8a2010-05-10 21:05:35774void FilebrowseHandler::SendPicasawebRequest() {
[email protected]068a71f2010-05-11 22:35:48775#if defined(OS_CHROMEOS)
776 chromeos::UserManager* user_man = chromeos::UserManager::Get();
777 std::string username = user_man->logged_in_user().email();
778
779 if (username.empty()) {
780 LOG(ERROR) << "Unable to get username";
781 return;
782 }
783
[email protected]d52bb8a2010-05-10 21:05:35784 fetch_.reset(URLFetcher::Create(0,
785 GURL(kPicasawebBaseUrl),
786 URLFetcher::GET,
[email protected]068a71f2010-05-11 22:35:48787 this));
[email protected]d52bb8a2010-05-10 21:05:35788 fetch_->set_request_context(profile_->GetRequestContext());
789 fetch_->Start();
[email protected]068a71f2010-05-11 22:35:48790#endif
[email protected]d52bb8a2010-05-10 21:05:35791}
792
[email protected]9d6b9aff2009-12-11 17:39:18793void FilebrowseHandler::ReadInFile() {
794#if defined(OS_CHROMEOS)
795 // Get the users username
796 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35797 chromeos::UserManager* user_man = chromeos::UserManager::Get();
798 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18799
800 if (username.empty()) {
801 LOG(ERROR) << "Unable to get username";
802 return;
803 }
804 int location = username.find_first_of('@',0);
805 if (location <= 0) {
806 LOG(ERROR) << "Username not formatted correctly";
807 return;
808 }
809 username = username.erase(username.find_first_of('@',0));
810 std::string url = kPicasawebUserPrefix;
811 url += username;
812 url += kPicasawebDefault;
813
814 FilePath currentpath;
815 currentpath = FilePath(current_file_uploaded_);
816 // Get the filename
817 std::string filename;
818 filename = currentpath.BaseName().value();
819 std::string filecontents;
820 if (!file_util::ReadFileToString(currentpath, &filecontents)) {
821 LOG(ERROR) << "Unable to read this file:" << currentpath.value();
822 return;
823 }
[email protected]d52bb8a2010-05-10 21:05:35824 fetch_.reset(URLFetcher::Create(0,
825 GURL(url),
826 URLFetcher::POST,
827 this));
828 fetch_->set_upload_data("image/jpeg", filecontents);
[email protected]9d6b9aff2009-12-11 17:39:18829 // Set the filename on the server
830 std::string slug = "Slug: ";
831 slug += filename;
[email protected]d52bb8a2010-05-10 21:05:35832 fetch_->set_extra_request_headers(slug);
833 fetch_->set_request_context(profile_->GetRequestContext());
834 fetch_->Start();
[email protected]9d6b9aff2009-12-11 17:39:18835#endif
836}
837
838// This is just a prototype for allowing generic uploads to various sites
839// TODO(dhg): Remove this and implement general upload.
840void FilebrowseHandler::UploadToPicasaweb(const Value* value) {
841 std::string path;
842#if defined(OS_CHROMEOS)
843 if (value && value->GetType() == Value::TYPE_LIST) {
844 const ListValue* list_value = static_cast<const ListValue*>(value);
845 Value* list_member;
846
847 // Get search string.
848 if (list_value->Get(0, &list_member) &&
849 list_member->GetType() == Value::TYPE_STRING) {
850 const StringValue* string_value =
851 static_cast<const StringValue*>(list_member);
852 string_value->GetAsString(&path);
853 }
854
855 } else {
856 LOG(ERROR) << "Wasn't able to get the List if requested files.";
857 return;
858 }
859 current_file_uploaded_ = path;
860 // ReadInFile();
[email protected]a67fa08e2010-02-12 20:43:55861 FilePath current_path(path);
862 TaskProxy* task = new TaskProxy(AsWeakPtr(), current_path);
[email protected]9d6b9aff2009-12-11 17:39:18863 task->AddRef();
[email protected]d52bb8a2010-05-10 21:05:35864 current_task_ = task;
[email protected]9d6b9aff2009-12-11 17:39:18865 ChromeThread::PostTask(
866 ChromeThread::FILE, FROM_HERE,
867 NewRunnableMethod(
868 task, &TaskProxy::ReadInFileProxy));
869#endif
870}
871
[email protected]a67fa08e2010-02-12 20:43:55872void FilebrowseHandler::GetChildrenForPath(FilePath& path, bool is_refresh) {
873 filelist_value_.reset(new ListValue());
874 currentpath_ = FilePath(path);
875
876 if (lister_.get()) {
877 lister_->Cancel();
878 lister_->set_delegate(NULL);
879 lister_ = NULL;
880 }
881
882 is_refresh_ = is_refresh;
883 lister_ = new net::DirectoryLister(currentpath_, this);
884 lister_->Start();
885}
886
[email protected]dcd23fe2009-11-12 20:21:18887void FilebrowseHandler::HandleGetChildren(const Value* value) {
888 std::string path;
[email protected]698601e2009-10-21 22:43:37889 if (value && value->GetType() == Value::TYPE_LIST) {
890 const ListValue* list_value = static_cast<const ListValue*>(value);
891 Value* list_member;
892
893 // Get search string.
894 if (list_value->Get(0, &list_member) &&
895 list_member->GetType() == Value::TYPE_STRING) {
896 const StringValue* string_value =
897 static_cast<const StringValue*>(list_member);
898 string_value->GetAsString(&path);
899 }
900
901 } else {
[email protected]dcd23fe2009-11-12 20:21:18902 LOG(ERROR) << "Wasn't able to get the List if requested files.";
[email protected]698601e2009-10-21 22:43:37903 return;
904 }
905 filelist_value_.reset(new ListValue());
[email protected]a67fa08e2010-02-12 20:43:55906 FilePath currentpath;
[email protected]698601e2009-10-21 22:43:37907#if defined(OS_WIN)
[email protected]a67fa08e2010-02-12 20:43:55908 currentpath = FilePath(ASCIIToWide(path));
[email protected]698601e2009-10-21 22:43:37909#else
[email protected]a67fa08e2010-02-12 20:43:55910 currentpath = FilePath(path);
[email protected]698601e2009-10-21 22:43:37911#endif
912
[email protected]a67fa08e2010-02-12 20:43:55913 GetChildrenForPath(currentpath, false);
[email protected]698601e2009-10-21 22:43:37914}
915
[email protected]dcd23fe2009-11-12 20:21:18916void FilebrowseHandler::OnListFile(
[email protected]698601e2009-10-21 22:43:37917 const file_util::FileEnumerator::FindInfo& data) {
[email protected]07046ab2010-01-20 21:42:44918#if defined(OS_WIN)
919 if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
920 return;
921 }
922#elif defined(OS_POSIX)
923 if (data.filename[0] == '.') {
924 return;
925 }
926#endif
927
[email protected]698601e2009-10-21 22:43:37928 DictionaryValue* file_value = new DictionaryValue();
929
930#if defined(OS_WIN)
931 int64 size = (static_cast<int64>(data.nFileSizeHigh) << 32) |
932 data.nFileSizeLow;
933 file_value->SetString(kPropertyTitle, data.cFileName);
934 file_value->SetString(kPropertyPath,
935 currentpath_.Append(data.cFileName).value());
936 file_value->SetBoolean(kPropertyDirectory,
937 (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false);
938
939#elif defined(OS_POSIX)
940 file_value->SetString(kPropertyTitle, data.filename);
941 file_value->SetString(kPropertyPath,
942 currentpath_.Append(data.filename).value());
943 file_value->SetBoolean(kPropertyDirectory, S_ISDIR(data.stat.st_mode));
944#endif
945 filelist_value_->Append(file_value);
946}
947
[email protected]dcd23fe2009-11-12 20:21:18948void FilebrowseHandler::OnListDone(int error) {
[email protected]698601e2009-10-21 22:43:37949 DictionaryValue info_value;
[email protected]a67fa08e2010-02-12 20:43:55950 if (is_refresh_) {
951 info_value.SetString(L"functionCall", "refresh");
952 } else {
953 info_value.SetString(L"functionCall", "getChildren");
954 }
[email protected]698601e2009-10-21 22:43:37955 info_value.SetString(kPropertyPath, currentpath_.value());
[email protected]9d6b9aff2009-12-11 17:39:18956 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37957 info_value, *(filelist_value_.get()));
[email protected]a67fa08e2010-02-12 20:43:55958 SendCurrentDownloads();
[email protected]698601e2009-10-21 22:43:37959}
960
[email protected]dcd23fe2009-11-12 20:21:18961void FilebrowseHandler::HandleGetMetadata(const Value* value) {
[email protected]698601e2009-10-21 22:43:37962}
963
[email protected]274e42b2010-01-29 22:03:57964void FilebrowseHandler::HandleGetDownloads(const Value* value) {
965 ModelChanged();
966}
967
968void FilebrowseHandler::ModelChanged() {
969 ClearDownloadItems();
[email protected]8ddbd66a2010-05-21 16:38:34970 download_manager_->GetAllDownloads(this, FilePath());
[email protected]274e42b2010-01-29 22:03:57971}
972
[email protected]82f37b02010-07-29 22:04:57973void FilebrowseHandler::OnSearchDownloadsComplete(
974 std::vector<DownloadItem*> downloads) {
[email protected]274e42b2010-01-29 22:03:57975 ClearDownloadItems();
[email protected]377b4cc2010-05-18 17:25:19976 std::vector<DownloadItem*> new_downloads;
[email protected]274e42b2010-01-29 22:03:57977 // Scan for any in progress downloads and add ourself to them as an observer.
978 for (DownloadList::iterator it = downloads.begin();
979 it != downloads.end(); ++it) {
980 DownloadItem* download = *it;
981 // We want to know what happens as the download progresses and be notified
982 // when the user validates the dangerous download.
983 if (download->state() == DownloadItem::IN_PROGRESS ||
984 download->safety_state() == DownloadItem::DANGEROUS) {
985 download->AddObserver(this);
[email protected]377b4cc2010-05-18 17:25:19986 active_download_items_.push_back(download);
[email protected]274e42b2010-01-29 22:03:57987 }
[email protected]377b4cc2010-05-18 17:25:19988 DownloadList::iterator item = find(download_items_.begin(),
989 download_items_.end(),
990 download);
991 if (item == download_items_.end() && got_first_download_list_) {
992 SendNewDownload(download);
993 }
994 new_downloads.push_back(download);
[email protected]274e42b2010-01-29 22:03:57995 }
[email protected]377b4cc2010-05-18 17:25:19996 download_items_.swap(new_downloads);
997 got_first_download_list_ = true;
[email protected]274e42b2010-01-29 22:03:57998 SendCurrentDownloads();
999}
1000
[email protected]377b4cc2010-05-18 17:25:191001void FilebrowseHandler::SendNewDownload(DownloadItem* download) {
1002 ListValue results_value;
1003 results_value.Append(download_util::CreateDownloadItemValue(download, -1));
1004 dom_ui_->CallJavascriptFunction(L"newDownload", results_value);
1005}
1006
[email protected]a67fa08e2010-02-12 20:43:551007void FilebrowseHandler::DeleteFile(const FilePath& path) {
1008 if (!file_util::Delete(path, true)) {
1009 LOG(ERROR) << "unable to delete directory";
1010 }
1011 ChromeThread::PostTask(
1012 ChromeThread::UI, FROM_HERE,
[email protected]d52bb8a2010-05-10 21:05:351013 NewRunnableMethod(current_task_, &TaskProxy::FireDeleteCompleteProxy));
[email protected]a67fa08e2010-02-12 20:43:551014}
1015
1016void FilebrowseHandler::HandleDeleteFile(const Value* value) {
1017 #if defined(OS_CHROMEOS)
1018 if (value && value->GetType() == Value::TYPE_LIST) {
1019 const ListValue* list_value = static_cast<const ListValue*>(value);
1020 std::string path;
1021
1022 // Get path string.
1023 if (list_value->GetString(0, &path)) {
1024
1025 FilePath currentpath;
1026 currentpath = FilePath(path);
[email protected]377b4cc2010-05-18 17:25:191027 for (unsigned int x = 0; x < active_download_items_.size(); x++) {
1028 FilePath item = active_download_items_[x]->full_path();
[email protected]c4a530b2010-03-08 17:33:031029 if (item == currentpath) {
[email protected]377b4cc2010-05-18 17:25:191030 active_download_items_[x]->Cancel(true);
1031 active_download_items_[x]->Remove(true);
[email protected]c4a530b2010-03-08 17:33:031032 FilePath dir_path = item.DirName();
1033 GetChildrenForPath(dir_path, true);
1034 return;
1035 }
1036 }
[email protected]a67fa08e2010-02-12 20:43:551037 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath);
1038 task->AddRef();
[email protected]d52bb8a2010-05-10 21:05:351039 current_task_ = task;
[email protected]a67fa08e2010-02-12 20:43:551040 ChromeThread::PostTask(
1041 ChromeThread::FILE, FROM_HERE,
1042 NewRunnableMethod(
1043 task, &TaskProxy::DeleteFileProxy));
1044 } else {
1045 LOG(ERROR) << "Unable to get string";
1046 return;
1047 }
1048 }
1049#endif
1050}
1051
[email protected]274e42b2010-01-29 22:03:571052void FilebrowseHandler::OnDownloadUpdated(DownloadItem* download) {
[email protected]377b4cc2010-05-18 17:25:191053 DownloadList::iterator it = find(active_download_items_.begin(),
1054 active_download_items_.end(),
[email protected]274e42b2010-01-29 22:03:571055 download);
[email protected]377b4cc2010-05-18 17:25:191056 if (it == active_download_items_.end())
[email protected]274e42b2010-01-29 22:03:571057 return;
[email protected]377b4cc2010-05-18 17:25:191058 const int id = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:571059
1060 ListValue results_value;
1061 results_value.Append(download_util::CreateDownloadItemValue(download, id));
1062 dom_ui_->CallJavascriptFunction(L"downloadUpdated", results_value);
1063}
1064
1065void FilebrowseHandler::ClearDownloadItems() {
[email protected]377b4cc2010-05-18 17:25:191066 for (DownloadList::iterator it = active_download_items_.begin();
1067 it != active_download_items_.end(); ++it) {
[email protected]274e42b2010-01-29 22:03:571068 (*it)->RemoveObserver(this);
1069 }
[email protected]377b4cc2010-05-18 17:25:191070 active_download_items_.clear();
[email protected]274e42b2010-01-29 22:03:571071}
1072
1073void FilebrowseHandler::SendCurrentDownloads() {
1074 ListValue results_value;
[email protected]377b4cc2010-05-18 17:25:191075 for (DownloadList::iterator it = active_download_items_.begin();
1076 it != active_download_items_.end(); ++it) {
1077 int index = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:571078 results_value.Append(download_util::CreateDownloadItemValue(*it, index));
1079 }
1080
1081 dom_ui_->CallJavascriptFunction(L"downloadsList", results_value);
1082}
1083
[email protected]698601e2009-10-21 22:43:371084////////////////////////////////////////////////////////////////////////////////
1085//
[email protected]f5bf8ccf2010-02-05 18:19:251086// FileBrowseUI
[email protected]698601e2009-10-21 22:43:371087//
1088////////////////////////////////////////////////////////////////////////////////
1089
[email protected]274e42b2010-01-29 22:03:571090FileBrowseUI::FileBrowseUI(TabContents* contents) : HtmlDialogUI(contents) {
[email protected]9d6b9aff2009-12-11 17:39:181091 FilebrowseHandler* handler = new FilebrowseHandler();
1092 AddMessageHandler((handler)->Attach(this));
[email protected]274e42b2010-01-29 22:03:571093 handler->Init();
[email protected]698601e2009-10-21 22:43:371094 FileBrowseUIHTMLSource* html_source = new FileBrowseUIHTMLSource();
1095
1096 // Set up the chrome://filebrowse/ source.
[email protected]fae20792009-10-28 20:31:581097 ChromeThread::PostTask(
1098 ChromeThread::IO, FROM_HERE,
1099 NewRunnableMethod(
[email protected]576a4ca2009-11-05 01:41:091100 Singleton<ChromeURLDataManager>::get(),
[email protected]698601e2009-10-21 22:43:371101 &ChromeURLDataManager::AddDataSource,
[email protected]f8f82502009-11-20 23:14:231102 make_scoped_refptr(html_source)));
[email protected]698601e2009-10-21 22:43:371103}
[email protected]f5bf8ccf2010-02-05 18:19:251104
1105// static
[email protected]1717246f2010-02-10 17:08:151106Browser* FileBrowseUI::OpenPopup(Profile* profile,
[email protected]467293292010-04-16 19:41:561107 const std::string& hashArgument,
1108 int width,
1109 int height) {
[email protected]1717246f2010-02-10 17:08:151110 // Get existing pop up for given hashArgument.
1111 Browser* browser = GetPopupForPath(hashArgument);
[email protected]f5bf8ccf2010-02-05 18:19:251112
[email protected]1717246f2010-02-10 17:08:151113 // Create new browser if no matching pop up found.
1114 if (browser == NULL) {
1115 browser = Browser::CreateForPopup(profile);
[email protected]f59f1162010-04-23 21:19:291116 std::string url;
1117 if (hashArgument.empty()) {
1118 url = chrome::kChromeUIFileBrowseURL;
1119 } else {
1120 url = kFilebrowseURLHash;
1121 url.append(hashArgument);
1122 }
[email protected]f5bf8ccf2010-02-05 18:19:251123
[email protected]1717246f2010-02-10 17:08:151124 browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:391125 GURL(url), GURL(), PageTransition::LINK, -1,
1126 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]1717246f2010-02-10 17:08:151127 browser->window()->SetBounds(gfx::Rect(kPopupLeft,
1128 kPopupTop,
[email protected]467293292010-04-16 19:41:561129 width,
1130 height));
[email protected]1717246f2010-02-10 17:08:151131
1132 browser->window()->Show();
1133 }
[email protected]f5bf8ccf2010-02-05 18:19:251134
1135 return browser;
1136}
[email protected]1717246f2010-02-10 17:08:151137
1138Browser* FileBrowseUI::GetPopupForPath(const std::string& path) {
1139 for (BrowserList::const_iterator it = BrowserList::begin();
1140 it != BrowserList::end(); ++it) {
1141 if ((*it)->type() == Browser::TYPE_POPUP) {
[email protected]d519bed2010-05-28 18:42:031142 TabContents* tab_contents = (*it)->GetSelectedTabContents();
1143 DCHECK(tab_contents);
1144 if (!tab_contents)
1145 continue;
1146 const GURL& url = tab_contents->GetURL();
[email protected]1717246f2010-02-10 17:08:151147
1148 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1149 url.host() == chrome::kChromeUIFileBrowseHost &&
1150 url.ref() == path) {
1151 return (*it);
1152 }
1153 }
1154 }
1155
1156 return NULL;
1157}
[email protected]467293292010-04-16 19:41:561158
1159const int FileBrowseUI::kPopupWidth = 250;
1160const int FileBrowseUI::kPopupHeight = 300;
1161const int FileBrowseUI::kSmallPopupWidth = 250;
1162const int FileBrowseUI::kSmallPopupHeight = 50;