blob: e277c16a238938d7d3aa7db93c8c1070fce28015 [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
[email protected]698601e2009-10-21 22:43:37130 // Callback for the "getRoots" message.
131 void HandleGetRoots(const Value* value);
132
[email protected]a67fa08e2010-02-12 20:43:55133 void GetChildrenForPath(FilePath& path, bool is_refresh);
134
[email protected]9d6b9aff2009-12-11 17:39:18135 void OnURLFetchComplete(const URLFetcher* source,
136 const GURL& url,
137 const URLRequestStatus& status,
138 int response_code,
139 const ResponseCookies& cookies,
140 const std::string& data);
141
[email protected]698601e2009-10-21 22:43:37142 // Callback for the "getChildren" message.
143 void HandleGetChildren(const Value* value);
[email protected]c4a530b2010-03-08 17:33:03144 // Callback for the "refreshDirectory" message.
145 void HandleRefreshDirectory(const Value* value);
[email protected]6ba198f2010-07-02 20:32:05146 void HandleIsAdvancedEnabled(const Value* value);
[email protected]698601e2009-10-21 22:43:37147
148 // Callback for the "getMetadata" message.
149 void HandleGetMetadata(const Value* value);
150
[email protected]9d6b9aff2009-12-11 17:39:18151 // Callback for the "openNewWindow" message.
[email protected]dcd23fe2009-11-12 20:21:18152 void OpenNewFullWindow(const Value* value);
153 void OpenNewPopupWindow(const Value* value);
154
[email protected]9d6b9aff2009-12-11 17:39:18155 // Callback for the "uploadToPicasaweb" message.
156 void UploadToPicasaweb(const Value* value);
157
[email protected]274e42b2010-01-29 22:03:57158 // Callback for the "getDownloads" message.
159 void HandleGetDownloads(const Value* value);
160
161 void HandleCreateNewFolder(const Value* value);
162
[email protected]17496bb2010-03-10 19:48:23163 void PlayMediaFile(const Value* value);
[email protected]89a7e3812010-06-02 19:38:07164 void EnqueueMediaFile(const Value* value);
[email protected]17496bb2010-03-10 19:48:23165
[email protected]a67fa08e2010-02-12 20:43:55166 void HandleDeleteFile(const Value* value);
167 void DeleteFile(const FilePath& path);
168 void FireDeleteComplete(const FilePath& path);
169
170 void HandlePauseToggleDownload(const Value* value);
171
[email protected]c4a530b2010-03-08 17:33:03172 void HandleCancelDownload(const Value* value);
173 void HandleAllowDownload(const Value* value);
174
[email protected]9d6b9aff2009-12-11 17:39:18175 void ReadInFile();
176 void FireUploadComplete();
177
[email protected]d52bb8a2010-05-10 21:05:35178 void SendPicasawebRequest();
[email protected]698601e2009-10-21 22:43:37179 private:
[email protected]dcd23fe2009-11-12 20:21:18180
181 void OpenNewWindow(const Value* value, bool popup);
182
[email protected]274e42b2010-01-29 22:03:57183 // Clear all download items and their observers.
184 void ClearDownloadItems();
185
186 // Send the current list of downloads to the page.
187 void SendCurrentDownloads();
188
[email protected]377b4cc2010-05-18 17:25:19189 void SendNewDownload(DownloadItem* download);
190
[email protected]698601e2009-10-21 22:43:37191 scoped_ptr<ListValue> filelist_value_;
192 FilePath currentpath_;
[email protected]dcd23fe2009-11-12 20:21:18193 Profile* profile_;
[email protected]93a58bf72010-06-04 23:01:20194 TabContents* tab_contents_;
[email protected]9d6b9aff2009-12-11 17:39:18195 std::string current_file_contents_;
196 std::string current_file_uploaded_;
197 int upload_response_code_;
[email protected]d52bb8a2010-05-10 21:05:35198 TaskProxy* current_task_;
[email protected]698601e2009-10-21 22:43:37199 scoped_refptr<net::DirectoryLister> lister_;
[email protected]a67fa08e2010-02-12 20:43:55200 bool is_refresh_;
[email protected]d52bb8a2010-05-10 21:05:35201 scoped_ptr<URLFetcher> fetch_;
[email protected]698601e2009-10-21 22:43:37202
[email protected]274e42b2010-01-29 22:03:57203 DownloadManager* download_manager_;
204 typedef std::vector<DownloadItem*> DownloadList;
[email protected]377b4cc2010-05-18 17:25:19205 DownloadList active_download_items_;
[email protected]274e42b2010-01-29 22:03:57206 DownloadList download_items_;
[email protected]377b4cc2010-05-18 17:25:19207 bool got_first_download_list_;
[email protected]dcd23fe2009-11-12 20:21:18208 DISALLOW_COPY_AND_ASSIGN(FilebrowseHandler);
[email protected]698601e2009-10-21 22:43:37209};
210
[email protected]9d6b9aff2009-12-11 17:39:18211class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> {
212 public:
[email protected]d52bb8a2010-05-10 21:05:35213 explicit TaskProxy(const base::WeakPtr<FilebrowseHandler>& handler,
214 FilePath& path)
[email protected]a67fa08e2010-02-12 20:43:55215 : handler_(handler),
216 path_(path) {}
[email protected]9d6b9aff2009-12-11 17:39:18217 void ReadInFileProxy() {
218 if (handler_) {
219 handler_->ReadInFile();
220 }
221 }
[email protected]068a71f2010-05-11 22:35:48222 void DeleteFetcher(URLFetcher* fetch) {
223 delete fetch;
224 }
[email protected]d52bb8a2010-05-10 21:05:35225 void SendPicasawebRequestProxy() {
226 if (handler_) {
227 handler_->SendPicasawebRequest();
228 }
229 }
[email protected]9d6b9aff2009-12-11 17:39:18230 void FireUploadCompleteProxy() {
231 if (handler_) {
232 handler_->FireUploadComplete();
233 }
234 }
[email protected]a67fa08e2010-02-12 20:43:55235
236 void DeleteFileProxy() {
237 if (handler_) {
238 handler_->DeleteFile(path_);
239 }
240 }
241
242 void FireDeleteCompleteProxy() {
243 if (handler_) {
244 handler_->FireDeleteComplete(path_);
245 }
246 }
[email protected]9d6b9aff2009-12-11 17:39:18247 private:
248 base::WeakPtr<FilebrowseHandler> handler_;
[email protected]a67fa08e2010-02-12 20:43:55249 FilePath path_;
[email protected]9d6b9aff2009-12-11 17:39:18250 friend class base::RefCountedThreadSafe<TaskProxy>;
[email protected]068a71f2010-05-11 22:35:48251 DISALLOW_COPY_AND_ASSIGN(TaskProxy);
[email protected]9d6b9aff2009-12-11 17:39:18252};
253
254
[email protected]698601e2009-10-21 22:43:37255////////////////////////////////////////////////////////////////////////////////
256//
257// FileBrowseHTMLSource
258//
259////////////////////////////////////////////////////////////////////////////////
260
261FileBrowseUIHTMLSource::FileBrowseUIHTMLSource()
262 : DataSource(chrome::kChromeUIFileBrowseHost, MessageLoop::current()) {
263}
264
265void FileBrowseUIHTMLSource::StartDataRequest(const std::string& path,
[email protected]e42ae9b2010-03-28 00:55:05266 bool is_off_the_record,
267 int request_id) {
[email protected]698601e2009-10-21 22:43:37268 DictionaryValue localized_strings;
[email protected]11f4857282009-11-13 19:56:17269 // TODO(dhg): Add stirings to localized strings, also add more strings
[email protected]dcd23fe2009-11-12 20:21:18270 // that are currently hardcoded.
[email protected]f95f0752010-03-09 16:47:10271 localized_strings.SetString(L"title",
272 l10n_util::GetString(IDS_FILEBROWSER_TITLE));
273 localized_strings.SetString(L"pause",
274 l10n_util::GetString(IDS_FILEBROWSER_PAUSE));
275 localized_strings.SetString(L"resume",
276 l10n_util::GetString(IDS_FILEBROWSER_RESUME));
277 localized_strings.SetString(L"scanning",
278 l10n_util::GetString(IDS_FILEBROWSER_SCANNING));
279 localized_strings.SetString(L"confirmdelete",
280 l10n_util::GetString(IDS_FILEBROWSER_CONFIRM_DELETE));
281 localized_strings.SetString(L"confirmyes",
282 l10n_util::GetString(IDS_FILEBROWSER_CONFIRM_YES));
283 localized_strings.SetString(L"confirmcancel",
284 l10n_util::GetString(IDS_FILEBROWSER_CONFIRM_CANCEL));
285 localized_strings.SetString(L"allowdownload",
286 l10n_util::GetString(IDS_FILEBROWSER_CONFIRM_DOWNLOAD));
287 localized_strings.SetString(L"filenameprompt",
288 l10n_util::GetString(IDS_FILEBROWSER_PROMPT_FILENAME));
289 localized_strings.SetString(L"save",
290 l10n_util::GetString(IDS_FILEBROWSER_SAVE));
291 localized_strings.SetString(L"newfolder",
292 l10n_util::GetString(IDS_FILEBROWSER_NEW_FOLDER));
293 localized_strings.SetString(L"open",
294 l10n_util::GetString(IDS_FILEBROWSER_OPEN));
295 localized_strings.SetString(L"picasaweb",
296 l10n_util::GetString(IDS_FILEBROWSER_UPLOAD_PICASAWEB));
[email protected]a25802d2010-04-21 16:18:20297 localized_strings.SetString(L"flickr",
298 l10n_util::GetString(IDS_FILEBROWSER_UPLOAD_FLICKR));
[email protected]f95f0752010-03-09 16:47:10299 localized_strings.SetString(L"email",
300 l10n_util::GetString(IDS_FILEBROWSER_UPLOAD_EMAIL));
301 localized_strings.SetString(L"delete",
302 l10n_util::GetString(IDS_FILEBROWSER_DELETE));
[email protected]89a7e3812010-06-02 19:38:07303 localized_strings.SetString(L"enqueue",
304 l10n_util::GetString(IDS_FILEBROWSER_ENQUEUE));
[email protected]88f03da02010-06-10 16:21:13305 localized_strings.SetString(L"mediapath", kMediaPath);
306 FilePath default_download_path;
307 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
308 &default_download_path)) {
309 NOTREACHED();
310 }
311 localized_strings.SetString(L"downloadpath", default_download_path.value());
[email protected]183e2f02010-06-11 17:13:14312 localized_strings.SetString(L"error_unknown_file_type",
313 l10n_util::GetString(IDS_FILEBROWSER_ERROR_UNKNOWN_FILE_TYPE));
[email protected]698601e2009-10-21 22:43:37314 SetFontAndTextDirection(&localized_strings);
315
316 static const base::StringPiece filebrowse_html(
317 ResourceBundle::GetSharedInstance().GetRawDataResource(
318 IDR_FILEBROWSE_HTML));
319 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml(
320 filebrowse_html, &localized_strings);
321
322 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
323 html_bytes->data.resize(full_html.size());
324 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
325
326 SendResponse(request_id, html_bytes);
327}
328
329////////////////////////////////////////////////////////////////////////////////
330//
[email protected]dcd23fe2009-11-12 20:21:18331// FilebrowseHandler
[email protected]698601e2009-10-21 22:43:37332//
333////////////////////////////////////////////////////////////////////////////////
[email protected]dcd23fe2009-11-12 20:21:18334FilebrowseHandler::FilebrowseHandler()
[email protected]274e42b2010-01-29 22:03:57335 : profile_(NULL),
[email protected]93a58bf72010-06-04 23:01:20336 tab_contents_(NULL),
[email protected]a67fa08e2010-02-12 20:43:55337 is_refresh_(false),
[email protected]d52bb8a2010-05-10 21:05:35338 fetch_(NULL),
[email protected]377b4cc2010-05-18 17:25:19339 download_manager_(NULL),
340 got_first_download_list_(false) {
[email protected]a67fa08e2010-02-12 20:43:55341 lister_ = NULL;
[email protected]07046ab2010-01-20 21:42:44342#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24343 chromeos::MountLibrary* lib =
344 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44345 lib->AddObserver(this);
346#endif
[email protected]698601e2009-10-21 22:43:37347}
348
[email protected]dcd23fe2009-11-12 20:21:18349FilebrowseHandler::~FilebrowseHandler() {
[email protected]07046ab2010-01-20 21:42:44350#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24351 chromeos::MountLibrary* lib =
352 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44353 lib->RemoveObserver(this);
354#endif
[email protected]698601e2009-10-21 22:43:37355 if (lister_.get()) {
356 lister_->Cancel();
357 lister_->set_delegate(NULL);
358 }
[email protected]274e42b2010-01-29 22:03:57359
360 ClearDownloadItems();
361 download_manager_->RemoveObserver(this);
[email protected]068a71f2010-05-11 22:35:48362 URLFetcher* fetch = fetch_.release();
363 if (fetch) {
364 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath_);
365 task->AddRef();
366 ChromeThread::PostTask(
367 ChromeThread::FILE, FROM_HERE,
368 NewRunnableMethod(
369 task, &TaskProxy::DeleteFetcher, fetch));
370 }
[email protected]698601e2009-10-21 22:43:37371}
372
[email protected]dcd23fe2009-11-12 20:21:18373DOMMessageHandler* FilebrowseHandler::Attach(DOMUI* dom_ui) {
[email protected]698601e2009-10-21 22:43:37374 // Create our favicon data source.
[email protected]fae20792009-10-28 20:31:58375 ChromeThread::PostTask(
376 ChromeThread::IO, FROM_HERE,
377 NewRunnableMethod(
[email protected]576a4ca2009-11-05 01:41:09378 Singleton<ChromeURLDataManager>::get(),
[email protected]fae20792009-10-28 20:31:58379 &ChromeURLDataManager::AddDataSource,
[email protected]f8f82502009-11-20 23:14:23380 make_scoped_refptr(new DOMUIFavIconSource(dom_ui->GetProfile()))));
[email protected]dcd23fe2009-11-12 20:21:18381 profile_ = dom_ui->GetProfile();
[email protected]93a58bf72010-06-04 23:01:20382 tab_contents_ = dom_ui->tab_contents();
[email protected]698601e2009-10-21 22:43:37383 return DOMMessageHandler::Attach(dom_ui);
384}
385
[email protected]274e42b2010-01-29 22:03:57386void FilebrowseHandler::Init() {
387 download_manager_ = profile_->GetOriginalProfile()->GetDownloadManager();
388 download_manager_->AddObserver(this);
[email protected]d52bb8a2010-05-10 21:05:35389 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath_);
390 task->AddRef();
391 current_task_ = task;
392 static bool sent_request = false;
393 if (!sent_request) {
394 // If we have not sent a request before, we should do one in order to
395 // ensure that we have the correct cookies. This is for uploads.
396 ChromeThread::PostTask(
397 ChromeThread::FILE, FROM_HERE,
398 NewRunnableMethod(
399 task, &TaskProxy::SendPicasawebRequestProxy));
400 sent_request = true;
401 }
[email protected]274e42b2010-01-29 22:03:57402}
403
[email protected]dcd23fe2009-11-12 20:21:18404void FilebrowseHandler::RegisterMessages() {
[email protected]698601e2009-10-21 22:43:37405 dom_ui_->RegisterMessageCallback("getRoots",
[email protected]dcd23fe2009-11-12 20:21:18406 NewCallback(this, &FilebrowseHandler::HandleGetRoots));
[email protected]698601e2009-10-21 22:43:37407 dom_ui_->RegisterMessageCallback("getChildren",
[email protected]dcd23fe2009-11-12 20:21:18408 NewCallback(this, &FilebrowseHandler::HandleGetChildren));
[email protected]698601e2009-10-21 22:43:37409 dom_ui_->RegisterMessageCallback("getMetadata",
[email protected]dcd23fe2009-11-12 20:21:18410 NewCallback(this, &FilebrowseHandler::HandleGetMetadata));
411 dom_ui_->RegisterMessageCallback("openNewPopupWindow",
412 NewCallback(this, &FilebrowseHandler::OpenNewPopupWindow));
413 dom_ui_->RegisterMessageCallback("openNewFullWindow",
414 NewCallback(this, &FilebrowseHandler::OpenNewFullWindow));
[email protected]9d6b9aff2009-12-11 17:39:18415 dom_ui_->RegisterMessageCallback("uploadToPicasaweb",
416 NewCallback(this, &FilebrowseHandler::UploadToPicasaweb));
[email protected]274e42b2010-01-29 22:03:57417 dom_ui_->RegisterMessageCallback("getDownloads",
418 NewCallback(this, &FilebrowseHandler::HandleGetDownloads));
419 dom_ui_->RegisterMessageCallback("createNewFolder",
420 NewCallback(this, &FilebrowseHandler::HandleCreateNewFolder));
[email protected]17496bb2010-03-10 19:48:23421 dom_ui_->RegisterMessageCallback("playMediaFile",
422 NewCallback(this, &FilebrowseHandler::PlayMediaFile));
[email protected]89a7e3812010-06-02 19:38:07423 dom_ui_->RegisterMessageCallback("enqueueMediaFile",
424 NewCallback(this, &FilebrowseHandler::EnqueueMediaFile));
[email protected]a67fa08e2010-02-12 20:43:55425 dom_ui_->RegisterMessageCallback("pauseToggleDownload",
426 NewCallback(this, &FilebrowseHandler::HandlePauseToggleDownload));
427 dom_ui_->RegisterMessageCallback("deleteFile",
428 NewCallback(this, &FilebrowseHandler::HandleDeleteFile));
[email protected]c4a530b2010-03-08 17:33:03429 dom_ui_->RegisterMessageCallback("cancelDownload",
430 NewCallback(this, &FilebrowseHandler::HandleCancelDownload));
431 dom_ui_->RegisterMessageCallback("allowDownload",
432 NewCallback(this, &FilebrowseHandler::HandleAllowDownload));
433 dom_ui_->RegisterMessageCallback("refreshDirectory",
434 NewCallback(this, &FilebrowseHandler::HandleRefreshDirectory));
[email protected]6ba198f2010-07-02 20:32:05435 dom_ui_->RegisterMessageCallback("isAdvancedEnabled",
436 NewCallback(this, &FilebrowseHandler::HandleIsAdvancedEnabled));
[email protected]9d6b9aff2009-12-11 17:39:18437}
438
[email protected]a67fa08e2010-02-12 20:43:55439
440void FilebrowseHandler::FireDeleteComplete(const FilePath& path) {
441 // We notify the UI by telling it to refresh its contents.
442 FilePath dir_path = path.DirName();
443 GetChildrenForPath(dir_path, true);
444};
445
[email protected]9d6b9aff2009-12-11 17:39:18446void FilebrowseHandler::FireUploadComplete() {
[email protected]d52bb8a2010-05-10 21:05:35447#if defined(OS_CHROMEOS)
[email protected]9d6b9aff2009-12-11 17:39:18448 DictionaryValue info_value;
449 info_value.SetString(L"path", current_file_uploaded_);
450
451 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35452 chromeos::UserManager* user_man = chromeos::UserManager::Get();
453 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18454
455 if (username.empty()) {
456 LOG(ERROR) << "Unable to get username";
457 return;
458 }
459 int location = username.find_first_of('@',0);
460 if (location <= 0) {
461 LOG(ERROR) << "Username not formatted correctly";
462 return;
463 }
464 username = username.erase(username.find_first_of('@',0));
465 std::string picture_url;
466 picture_url = kPicasawebBaseUrl;
467 picture_url += username;
468 picture_url += kPicasawebDropBox;
469 info_value.SetString(L"url", picture_url);
470 info_value.SetInteger(L"status_code", upload_response_code_);
471 dom_ui_->CallJavascriptFunction(L"uploadComplete", info_value);
[email protected]d52bb8a2010-05-10 21:05:35472#endif
[email protected]9d6b9aff2009-12-11 17:39:18473}
474
[email protected]07046ab2010-01-20 21:42:44475#if defined(OS_CHROMEOS)
476void FilebrowseHandler::MountChanged(chromeos::MountLibrary* obj,
477 chromeos::MountEventType evt,
478 const std::string& path) {
479 if (evt == chromeos::DISK_REMOVED ||
480 evt == chromeos::DISK_CHANGED) {
481 dom_ui_->CallJavascriptFunction(L"rootsChanged");
482 }
483}
484#endif
485
[email protected]9d6b9aff2009-12-11 17:39:18486void FilebrowseHandler::OnURLFetchComplete(const URLFetcher* source,
487 const GURL& url,
488 const URLRequestStatus& status,
489 int response_code,
490 const ResponseCookies& cookies,
491 const std::string& data) {
492 upload_response_code_ = response_code;
[email protected]d52bb8a2010-05-10 21:05:35493 LOG(INFO) << "Response code:" << response_code;
494 LOG(INFO) << "request url" << url;
[email protected]068a71f2010-05-11 22:35:48495 if (StartsWithASCII(url.spec(), kPicasawebUserPrefix, true)) {
496 ChromeThread::PostTask(
497 ChromeThread::UI, FROM_HERE,
498 NewRunnableMethod(current_task_, &TaskProxy::FireUploadCompleteProxy));
499 }
500 fetch_.reset();
[email protected]698601e2009-10-21 22:43:37501}
502
[email protected]dcd23fe2009-11-12 20:21:18503void FilebrowseHandler::HandleGetRoots(const Value* value) {
[email protected]698601e2009-10-21 22:43:37504 ListValue results_value;
505 DictionaryValue info_value;
[email protected]698601e2009-10-21 22:43:37506 // TODO(dhg): add other entries, make this more general
[email protected]07046ab2010-01-20 21:42:44507#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24508 chromeos::MountLibrary* lib =
509 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44510 const chromeos::MountLibrary::DiskVector& disks = lib->disks();
511
512 for (size_t i = 0; i < disks.size(); ++i) {
513 if (!disks[i].mount_path.empty()) {
514 DictionaryValue* page_value = new DictionaryValue();
515 page_value->SetString(kPropertyPath, disks[i].mount_path);
516 FilePath currentpath;
517 currentpath = FilePath(disks[i].mount_path);
518 std::string filename;
519 filename = currentpath.BaseName().value();
520 page_value->SetString(kPropertyTitle, filename);
521 page_value->SetBoolean(kPropertyDirectory, true);
522 results_value.Append(page_value);
523 }
524 }
525#else
526 DictionaryValue* page_value = new DictionaryValue();
527 page_value->SetString(kPropertyPath, "/media");
528 page_value->SetString(kPropertyTitle, "Removeable");
[email protected]698601e2009-10-21 22:43:37529 page_value->SetBoolean(kPropertyDirectory, true);
530
531 results_value.Append(page_value);
[email protected]07046ab2010-01-20 21:42:44532#endif
533 FilePath default_download_path;
534 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
535 &default_download_path)) {
536 NOTREACHED();
537 }
538
539 DictionaryValue* download_value = new DictionaryValue();
540 download_value->SetString(kPropertyPath, default_download_path.value());
541 download_value->SetString(kPropertyTitle, "File Shelf");
542 download_value->SetBoolean(kPropertyDirectory, true);
543
544 results_value.Append(download_value);
[email protected]698601e2009-10-21 22:43:37545
[email protected]9d6b9aff2009-12-11 17:39:18546 info_value.SetString(L"functionCall", "getRoots");
[email protected]07046ab2010-01-20 21:42:44547 info_value.SetString(kPropertyPath, "");
[email protected]9d6b9aff2009-12-11 17:39:18548 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37549 info_value, results_value);
550}
551
[email protected]274e42b2010-01-29 22:03:57552void FilebrowseHandler::HandleCreateNewFolder(const Value* value) {
553#if defined(OS_CHROMEOS)
554 if (value && value->GetType() == Value::TYPE_LIST) {
555 const ListValue* list_value = static_cast<const ListValue*>(value);
556 std::string path;
557
558 // Get path string.
559 if (list_value->GetString(0, &path)) {
560
561 FilePath currentpath;
562 currentpath = FilePath(path);
563
564 if (!file_util::CreateDirectory(currentpath)) {
565 LOG(ERROR) << "unable to create directory";
566 }
567 } else {
568 LOG(ERROR) << "Unable to get string";
569 return;
570 }
571 }
572#endif
573}
574
[email protected]17496bb2010-03-10 19:48:23575void FilebrowseHandler::PlayMediaFile(const Value* value) {
576#if defined(OS_CHROMEOS)
577 if (value && value->GetType() == Value::TYPE_LIST) {
578 const ListValue* list_value = static_cast<const ListValue*>(value);
579 std::string path;
580
581 // Get path string.
582 if (list_value->GetString(0, &path)) {
583 FilePath currentpath;
584 currentpath = FilePath(path);
585
586 MediaPlayer* mediaplayer = MediaPlayer::Get();
587 std::string url = currentpath.value();
588
589 GURL gurl(url);
[email protected]93a58bf72010-06-04 23:01:20590 Browser* browser = Browser::GetBrowserForController(
591 &tab_contents_->controller(), NULL);
592 mediaplayer->ForcePlayMediaURL(gurl, browser);
[email protected]89a7e3812010-06-02 19:38:07593 } else {
594 LOG(ERROR) << "Unable to get string";
595 return;
596 }
597 }
598#endif
599}
600
601void FilebrowseHandler::EnqueueMediaFile(const Value* value) {
602#if defined(OS_CHROMEOS)
603 if (value && value->GetType() == Value::TYPE_LIST) {
604 const ListValue* list_value = static_cast<const ListValue*>(value);
605 std::string path;
606
607 // Get path string.
608 if (list_value->GetString(0, &path)) {
609 FilePath currentpath;
610 currentpath = FilePath(path);
611
612 MediaPlayer* mediaplayer = MediaPlayer::Get();
613 std::string url = currentpath.value();
614
615 GURL gurl(url);
[email protected]93a58bf72010-06-04 23:01:20616 Browser* browser = Browser::GetBrowserForController(
617 &tab_contents_->controller(), NULL);
618 mediaplayer->EnqueueMediaURL(gurl, browser);
[email protected]17496bb2010-03-10 19:48:23619 } else {
620 LOG(ERROR) << "Unable to get string";
621 return;
622 }
623 }
624#endif
625}
626
[email protected]6ba198f2010-07-02 20:32:05627void FilebrowseHandler::HandleIsAdvancedEnabled(const Value* value) {
628#if defined(OS_CHROMEOS)
629 Profile* profile = BrowserList::GetLastActive()->profile();
630 PrefService* pref_service = profile->GetPrefs();
631 bool is_enabled = pref_service->GetBoolean(
632 prefs::kLabsAdvancedFilesystemEnabled);
633 bool mp_enabled = pref_service->GetBoolean(prefs::kLabsMediaplayerEnabled);
634 DictionaryValue info_value;
635 info_value.SetBoolean(L"enabled", is_enabled);
636 info_value.SetBoolean(L"mpEnabled", mp_enabled);
637 dom_ui_->CallJavascriptFunction(L"enabledResult",
638 info_value);
639#endif
640}
[email protected]c4a530b2010-03-08 17:33:03641void FilebrowseHandler::HandleRefreshDirectory(const Value* value) {
642 if (value && value->GetType() == Value::TYPE_LIST) {
643 const ListValue* list_value = static_cast<const ListValue*>(value);
644 std::string path;
645
646 // Get path string.
647 if (list_value->GetString(0, &path)) {
648 FilePath currentpath;
649#if defined(OS_WIN)
650 currentpath = FilePath(ASCIIToWide(path));
651#else
652 currentpath = FilePath(path);
653#endif
654 GetChildrenForPath(currentpath, true);
655 } else {
656 LOG(ERROR) << "Unable to get string";
657 return;
658 }
659 }
660}
661
[email protected]a67fa08e2010-02-12 20:43:55662void FilebrowseHandler::HandlePauseToggleDownload(const Value* value) {
663#if defined(OS_CHROMEOS)
664 if (value && value->GetType() == Value::TYPE_LIST) {
665 const ListValue* list_value = static_cast<const ListValue*>(value);
666 int id;
667 std::string str_id;
668
669 if (list_value->GetString(0, &str_id)) {
670 id = atoi(str_id.c_str());
[email protected]377b4cc2010-05-18 17:25:19671 DownloadItem* item = active_download_items_[id];
[email protected]a67fa08e2010-02-12 20:43:55672 item->TogglePause();
673 } else {
674 LOG(ERROR) << "Unable to get id for download to pause";
675 return;
676 }
677 }
678#endif
679}
680
[email protected]c4a530b2010-03-08 17:33:03681void FilebrowseHandler::HandleAllowDownload(const Value* value) {
682#if defined(OS_CHROMEOS)
683 if (value && value->GetType() == Value::TYPE_LIST) {
684 const ListValue* list_value = static_cast<const ListValue*>(value);
685 int id;
686 std::string str_id;
687
688 if (list_value->GetString(0, &str_id)) {
689 id = atoi(str_id.c_str());
[email protected]377b4cc2010-05-18 17:25:19690 DownloadItem* item = active_download_items_[id];
[email protected]c4a530b2010-03-08 17:33:03691 download_manager_->DangerousDownloadValidated(item);
692 } else {
693 LOG(ERROR) << "Unable to get id for download to pause";
694 return;
695 }
696 }
697#endif
698}
699
700void FilebrowseHandler::HandleCancelDownload(const Value* value) {
701#if defined(OS_CHROMEOS)
702 if (value && value->GetType() == Value::TYPE_LIST) {
703 const ListValue* list_value = static_cast<const ListValue*>(value);
704 int id;
705 std::string str_id;
706
707 if (list_value->GetString(0, &str_id)) {
708 id = atoi(str_id.c_str());
[email protected]377b4cc2010-05-18 17:25:19709 DownloadItem* item = active_download_items_[id];
[email protected]c4a530b2010-03-08 17:33:03710 item->Cancel(true);
711 FilePath path = item->full_path();
712 FilePath dir_path = path.DirName();
713 item->Remove(true);
714 GetChildrenForPath(dir_path, true);
715 } else {
716 LOG(ERROR) << "Unable to get id for download to pause";
717 return;
718 }
719 }
720#endif
721}
722
[email protected]dcd23fe2009-11-12 20:21:18723void FilebrowseHandler::OpenNewFullWindow(const Value* value) {
724 OpenNewWindow(value, false);
725}
[email protected]698601e2009-10-21 22:43:37726
[email protected]dcd23fe2009-11-12 20:21:18727void FilebrowseHandler::OpenNewPopupWindow(const Value* value) {
728 OpenNewWindow(value, true);
729}
730
731void FilebrowseHandler::OpenNewWindow(const Value* value, bool popup) {
732 if (value && value->GetType() == Value::TYPE_LIST) {
733 const ListValue* list_value = static_cast<const ListValue*>(value);
734 Value* list_member;
735 std::string path;
736
737 // Get path string.
738 if (list_value->Get(0, &list_member) &&
739 list_member->GetType() == Value::TYPE_STRING) {
740 const StringValue* string_value =
741 static_cast<const StringValue*>(list_member);
742 string_value->GetAsString(&path);
743 } else {
744 LOG(ERROR) << "Unable to get string";
745 return;
746 }
747 Browser* browser;
748 if (popup) {
749 browser = Browser::CreateForPopup(profile_);
750 } else {
[email protected]9419bcf2010-04-12 17:26:10751 browser = BrowserList::GetLastActive();
[email protected]dcd23fe2009-11-12 20:21:18752 }
[email protected]50bd0c7322010-05-12 22:07:24753 TabContents* contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39754 GURL(path), GURL(), PageTransition::LINK, -1,
755 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]50bd0c7322010-05-12 22:07:24756 // AddTabWithURL could have picked another Browser instance to create this
757 // new tab at. So we have to reset the ptr of the browser that we want to
758 // talk to.
759 browser = contents->delegate()->GetBrowser();
[email protected]dcd23fe2009-11-12 20:21:18760 if (popup) {
761 // TODO(dhg): Remove these from being hardcoded. Allow javascript
762 // to specify.
[email protected]e561e202010-03-24 17:57:12763 browser->window()->SetBounds(gfx::Rect(0, 0, 400, 300));
[email protected]dcd23fe2009-11-12 20:21:18764 }
765 browser->window()->Show();
766 } else {
767 LOG(ERROR) << "Wasn't able to get the List if requested files.";
768 return;
769 }
770}
771
[email protected]d52bb8a2010-05-10 21:05:35772void FilebrowseHandler::SendPicasawebRequest() {
[email protected]068a71f2010-05-11 22:35:48773#if defined(OS_CHROMEOS)
774 chromeos::UserManager* user_man = chromeos::UserManager::Get();
775 std::string username = user_man->logged_in_user().email();
776
777 if (username.empty()) {
778 LOG(ERROR) << "Unable to get username";
779 return;
780 }
781
[email protected]d52bb8a2010-05-10 21:05:35782 fetch_.reset(URLFetcher::Create(0,
783 GURL(kPicasawebBaseUrl),
784 URLFetcher::GET,
[email protected]068a71f2010-05-11 22:35:48785 this));
[email protected]d52bb8a2010-05-10 21:05:35786 fetch_->set_request_context(profile_->GetRequestContext());
787 fetch_->Start();
[email protected]068a71f2010-05-11 22:35:48788#endif
[email protected]d52bb8a2010-05-10 21:05:35789}
790
[email protected]9d6b9aff2009-12-11 17:39:18791void FilebrowseHandler::ReadInFile() {
792#if defined(OS_CHROMEOS)
793 // Get the users username
794 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35795 chromeos::UserManager* user_man = chromeos::UserManager::Get();
796 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18797
798 if (username.empty()) {
799 LOG(ERROR) << "Unable to get username";
800 return;
801 }
802 int location = username.find_first_of('@',0);
803 if (location <= 0) {
804 LOG(ERROR) << "Username not formatted correctly";
805 return;
806 }
807 username = username.erase(username.find_first_of('@',0));
808 std::string url = kPicasawebUserPrefix;
809 url += username;
810 url += kPicasawebDefault;
811
812 FilePath currentpath;
813 currentpath = FilePath(current_file_uploaded_);
814 // Get the filename
815 std::string filename;
816 filename = currentpath.BaseName().value();
817 std::string filecontents;
818 if (!file_util::ReadFileToString(currentpath, &filecontents)) {
819 LOG(ERROR) << "Unable to read this file:" << currentpath.value();
820 return;
821 }
[email protected]d52bb8a2010-05-10 21:05:35822 fetch_.reset(URLFetcher::Create(0,
823 GURL(url),
824 URLFetcher::POST,
825 this));
826 fetch_->set_upload_data("image/jpeg", filecontents);
[email protected]9d6b9aff2009-12-11 17:39:18827 // Set the filename on the server
828 std::string slug = "Slug: ";
829 slug += filename;
[email protected]d52bb8a2010-05-10 21:05:35830 fetch_->set_extra_request_headers(slug);
831 fetch_->set_request_context(profile_->GetRequestContext());
832 fetch_->Start();
[email protected]9d6b9aff2009-12-11 17:39:18833#endif
834}
835
836// This is just a prototype for allowing generic uploads to various sites
837// TODO(dhg): Remove this and implement general upload.
838void FilebrowseHandler::UploadToPicasaweb(const Value* value) {
839 std::string path;
840#if defined(OS_CHROMEOS)
841 if (value && value->GetType() == Value::TYPE_LIST) {
842 const ListValue* list_value = static_cast<const ListValue*>(value);
843 Value* list_member;
844
845 // Get search string.
846 if (list_value->Get(0, &list_member) &&
847 list_member->GetType() == Value::TYPE_STRING) {
848 const StringValue* string_value =
849 static_cast<const StringValue*>(list_member);
850 string_value->GetAsString(&path);
851 }
852
853 } else {
854 LOG(ERROR) << "Wasn't able to get the List if requested files.";
855 return;
856 }
857 current_file_uploaded_ = path;
858 // ReadInFile();
[email protected]a67fa08e2010-02-12 20:43:55859 FilePath current_path(path);
860 TaskProxy* task = new TaskProxy(AsWeakPtr(), current_path);
[email protected]9d6b9aff2009-12-11 17:39:18861 task->AddRef();
[email protected]d52bb8a2010-05-10 21:05:35862 current_task_ = task;
[email protected]9d6b9aff2009-12-11 17:39:18863 ChromeThread::PostTask(
864 ChromeThread::FILE, FROM_HERE,
865 NewRunnableMethod(
866 task, &TaskProxy::ReadInFileProxy));
867#endif
868}
869
[email protected]a67fa08e2010-02-12 20:43:55870void FilebrowseHandler::GetChildrenForPath(FilePath& path, bool is_refresh) {
871 filelist_value_.reset(new ListValue());
872 currentpath_ = FilePath(path);
873
874 if (lister_.get()) {
875 lister_->Cancel();
876 lister_->set_delegate(NULL);
877 lister_ = NULL;
878 }
879
880 is_refresh_ = is_refresh;
881 lister_ = new net::DirectoryLister(currentpath_, this);
882 lister_->Start();
883}
884
[email protected]dcd23fe2009-11-12 20:21:18885void FilebrowseHandler::HandleGetChildren(const Value* value) {
886 std::string path;
[email protected]698601e2009-10-21 22:43:37887 if (value && value->GetType() == Value::TYPE_LIST) {
888 const ListValue* list_value = static_cast<const ListValue*>(value);
889 Value* list_member;
890
891 // Get search string.
892 if (list_value->Get(0, &list_member) &&
893 list_member->GetType() == Value::TYPE_STRING) {
894 const StringValue* string_value =
895 static_cast<const StringValue*>(list_member);
896 string_value->GetAsString(&path);
897 }
898
899 } else {
[email protected]dcd23fe2009-11-12 20:21:18900 LOG(ERROR) << "Wasn't able to get the List if requested files.";
[email protected]698601e2009-10-21 22:43:37901 return;
902 }
903 filelist_value_.reset(new ListValue());
[email protected]a67fa08e2010-02-12 20:43:55904 FilePath currentpath;
[email protected]698601e2009-10-21 22:43:37905#if defined(OS_WIN)
[email protected]a67fa08e2010-02-12 20:43:55906 currentpath = FilePath(ASCIIToWide(path));
[email protected]698601e2009-10-21 22:43:37907#else
[email protected]a67fa08e2010-02-12 20:43:55908 currentpath = FilePath(path);
[email protected]698601e2009-10-21 22:43:37909#endif
910
[email protected]a67fa08e2010-02-12 20:43:55911 GetChildrenForPath(currentpath, false);
[email protected]698601e2009-10-21 22:43:37912}
913
[email protected]dcd23fe2009-11-12 20:21:18914void FilebrowseHandler::OnListFile(
[email protected]698601e2009-10-21 22:43:37915 const file_util::FileEnumerator::FindInfo& data) {
[email protected]07046ab2010-01-20 21:42:44916#if defined(OS_WIN)
917 if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
918 return;
919 }
920#elif defined(OS_POSIX)
921 if (data.filename[0] == '.') {
922 return;
923 }
924#endif
925
[email protected]698601e2009-10-21 22:43:37926 DictionaryValue* file_value = new DictionaryValue();
927
928#if defined(OS_WIN)
929 int64 size = (static_cast<int64>(data.nFileSizeHigh) << 32) |
930 data.nFileSizeLow;
931 file_value->SetString(kPropertyTitle, data.cFileName);
932 file_value->SetString(kPropertyPath,
933 currentpath_.Append(data.cFileName).value());
934 file_value->SetBoolean(kPropertyDirectory,
935 (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false);
936
937#elif defined(OS_POSIX)
938 file_value->SetString(kPropertyTitle, data.filename);
939 file_value->SetString(kPropertyPath,
940 currentpath_.Append(data.filename).value());
941 file_value->SetBoolean(kPropertyDirectory, S_ISDIR(data.stat.st_mode));
942#endif
943 filelist_value_->Append(file_value);
944}
945
[email protected]dcd23fe2009-11-12 20:21:18946void FilebrowseHandler::OnListDone(int error) {
[email protected]698601e2009-10-21 22:43:37947 DictionaryValue info_value;
[email protected]a67fa08e2010-02-12 20:43:55948 if (is_refresh_) {
949 info_value.SetString(L"functionCall", "refresh");
950 } else {
951 info_value.SetString(L"functionCall", "getChildren");
952 }
[email protected]698601e2009-10-21 22:43:37953 info_value.SetString(kPropertyPath, currentpath_.value());
[email protected]9d6b9aff2009-12-11 17:39:18954 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37955 info_value, *(filelist_value_.get()));
[email protected]a67fa08e2010-02-12 20:43:55956 SendCurrentDownloads();
[email protected]698601e2009-10-21 22:43:37957}
958
[email protected]dcd23fe2009-11-12 20:21:18959void FilebrowseHandler::HandleGetMetadata(const Value* value) {
[email protected]698601e2009-10-21 22:43:37960}
961
[email protected]274e42b2010-01-29 22:03:57962void FilebrowseHandler::HandleGetDownloads(const Value* value) {
963 ModelChanged();
964}
965
966void FilebrowseHandler::ModelChanged() {
967 ClearDownloadItems();
[email protected]274e42b2010-01-29 22:03:57968
[email protected]0e2b59a2010-07-29 22:27:05969 std::vector<DownloadItem*> downloads;
970 download_manager_->GetAllDownloads(FilePath(), &downloads);
971
[email protected]377b4cc2010-05-18 17:25:19972 std::vector<DownloadItem*> new_downloads;
[email protected]274e42b2010-01-29 22:03:57973 // Scan for any in progress downloads and add ourself to them as an observer.
974 for (DownloadList::iterator it = downloads.begin();
975 it != downloads.end(); ++it) {
976 DownloadItem* download = *it;
977 // We want to know what happens as the download progresses and be notified
978 // when the user validates the dangerous download.
979 if (download->state() == DownloadItem::IN_PROGRESS ||
980 download->safety_state() == DownloadItem::DANGEROUS) {
981 download->AddObserver(this);
[email protected]377b4cc2010-05-18 17:25:19982 active_download_items_.push_back(download);
[email protected]274e42b2010-01-29 22:03:57983 }
[email protected]377b4cc2010-05-18 17:25:19984 DownloadList::iterator item = find(download_items_.begin(),
985 download_items_.end(),
986 download);
987 if (item == download_items_.end() && got_first_download_list_) {
988 SendNewDownload(download);
989 }
990 new_downloads.push_back(download);
[email protected]274e42b2010-01-29 22:03:57991 }
[email protected]377b4cc2010-05-18 17:25:19992 download_items_.swap(new_downloads);
993 got_first_download_list_ = true;
[email protected]274e42b2010-01-29 22:03:57994 SendCurrentDownloads();
995}
996
[email protected]377b4cc2010-05-18 17:25:19997void FilebrowseHandler::SendNewDownload(DownloadItem* download) {
998 ListValue results_value;
999 results_value.Append(download_util::CreateDownloadItemValue(download, -1));
1000 dom_ui_->CallJavascriptFunction(L"newDownload", results_value);
1001}
1002
[email protected]a67fa08e2010-02-12 20:43:551003void FilebrowseHandler::DeleteFile(const FilePath& path) {
1004 if (!file_util::Delete(path, true)) {
1005 LOG(ERROR) << "unable to delete directory";
1006 }
1007 ChromeThread::PostTask(
1008 ChromeThread::UI, FROM_HERE,
[email protected]d52bb8a2010-05-10 21:05:351009 NewRunnableMethod(current_task_, &TaskProxy::FireDeleteCompleteProxy));
[email protected]a67fa08e2010-02-12 20:43:551010}
1011
1012void FilebrowseHandler::HandleDeleteFile(const Value* value) {
1013 #if defined(OS_CHROMEOS)
1014 if (value && value->GetType() == Value::TYPE_LIST) {
1015 const ListValue* list_value = static_cast<const ListValue*>(value);
1016 std::string path;
1017
1018 // Get path string.
1019 if (list_value->GetString(0, &path)) {
1020
1021 FilePath currentpath;
1022 currentpath = FilePath(path);
[email protected]377b4cc2010-05-18 17:25:191023 for (unsigned int x = 0; x < active_download_items_.size(); x++) {
1024 FilePath item = active_download_items_[x]->full_path();
[email protected]c4a530b2010-03-08 17:33:031025 if (item == currentpath) {
[email protected]377b4cc2010-05-18 17:25:191026 active_download_items_[x]->Cancel(true);
1027 active_download_items_[x]->Remove(true);
[email protected]c4a530b2010-03-08 17:33:031028 FilePath dir_path = item.DirName();
1029 GetChildrenForPath(dir_path, true);
1030 return;
1031 }
1032 }
[email protected]a67fa08e2010-02-12 20:43:551033 TaskProxy* task = new TaskProxy(AsWeakPtr(), currentpath);
1034 task->AddRef();
[email protected]d52bb8a2010-05-10 21:05:351035 current_task_ = task;
[email protected]a67fa08e2010-02-12 20:43:551036 ChromeThread::PostTask(
1037 ChromeThread::FILE, FROM_HERE,
1038 NewRunnableMethod(
1039 task, &TaskProxy::DeleteFileProxy));
1040 } else {
1041 LOG(ERROR) << "Unable to get string";
1042 return;
1043 }
1044 }
1045#endif
1046}
1047
[email protected]274e42b2010-01-29 22:03:571048void FilebrowseHandler::OnDownloadUpdated(DownloadItem* download) {
[email protected]377b4cc2010-05-18 17:25:191049 DownloadList::iterator it = find(active_download_items_.begin(),
1050 active_download_items_.end(),
[email protected]274e42b2010-01-29 22:03:571051 download);
[email protected]377b4cc2010-05-18 17:25:191052 if (it == active_download_items_.end())
[email protected]274e42b2010-01-29 22:03:571053 return;
[email protected]377b4cc2010-05-18 17:25:191054 const int id = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:571055
1056 ListValue results_value;
1057 results_value.Append(download_util::CreateDownloadItemValue(download, id));
1058 dom_ui_->CallJavascriptFunction(L"downloadUpdated", results_value);
1059}
1060
1061void FilebrowseHandler::ClearDownloadItems() {
[email protected]377b4cc2010-05-18 17:25:191062 for (DownloadList::iterator it = active_download_items_.begin();
1063 it != active_download_items_.end(); ++it) {
[email protected]274e42b2010-01-29 22:03:571064 (*it)->RemoveObserver(this);
1065 }
[email protected]377b4cc2010-05-18 17:25:191066 active_download_items_.clear();
[email protected]274e42b2010-01-29 22:03:571067}
1068
1069void FilebrowseHandler::SendCurrentDownloads() {
1070 ListValue results_value;
[email protected]377b4cc2010-05-18 17:25:191071 for (DownloadList::iterator it = active_download_items_.begin();
1072 it != active_download_items_.end(); ++it) {
1073 int index = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:571074 results_value.Append(download_util::CreateDownloadItemValue(*it, index));
1075 }
1076
1077 dom_ui_->CallJavascriptFunction(L"downloadsList", results_value);
1078}
1079
[email protected]698601e2009-10-21 22:43:371080////////////////////////////////////////////////////////////////////////////////
1081//
[email protected]f5bf8ccf2010-02-05 18:19:251082// FileBrowseUI
[email protected]698601e2009-10-21 22:43:371083//
1084////////////////////////////////////////////////////////////////////////////////
1085
[email protected]274e42b2010-01-29 22:03:571086FileBrowseUI::FileBrowseUI(TabContents* contents) : HtmlDialogUI(contents) {
[email protected]9d6b9aff2009-12-11 17:39:181087 FilebrowseHandler* handler = new FilebrowseHandler();
1088 AddMessageHandler((handler)->Attach(this));
[email protected]274e42b2010-01-29 22:03:571089 handler->Init();
[email protected]698601e2009-10-21 22:43:371090 FileBrowseUIHTMLSource* html_source = new FileBrowseUIHTMLSource();
1091
1092 // Set up the chrome://filebrowse/ source.
[email protected]fae20792009-10-28 20:31:581093 ChromeThread::PostTask(
1094 ChromeThread::IO, FROM_HERE,
1095 NewRunnableMethod(
[email protected]576a4ca2009-11-05 01:41:091096 Singleton<ChromeURLDataManager>::get(),
[email protected]698601e2009-10-21 22:43:371097 &ChromeURLDataManager::AddDataSource,
[email protected]f8f82502009-11-20 23:14:231098 make_scoped_refptr(html_source)));
[email protected]698601e2009-10-21 22:43:371099}
[email protected]f5bf8ccf2010-02-05 18:19:251100
1101// static
[email protected]1717246f2010-02-10 17:08:151102Browser* FileBrowseUI::OpenPopup(Profile* profile,
[email protected]467293292010-04-16 19:41:561103 const std::string& hashArgument,
1104 int width,
1105 int height) {
[email protected]1717246f2010-02-10 17:08:151106 // Get existing pop up for given hashArgument.
1107 Browser* browser = GetPopupForPath(hashArgument);
[email protected]f5bf8ccf2010-02-05 18:19:251108
[email protected]1717246f2010-02-10 17:08:151109 // Create new browser if no matching pop up found.
1110 if (browser == NULL) {
1111 browser = Browser::CreateForPopup(profile);
[email protected]f59f1162010-04-23 21:19:291112 std::string url;
1113 if (hashArgument.empty()) {
1114 url = chrome::kChromeUIFileBrowseURL;
1115 } else {
1116 url = kFilebrowseURLHash;
1117 url.append(hashArgument);
1118 }
[email protected]f5bf8ccf2010-02-05 18:19:251119
[email protected]1717246f2010-02-10 17:08:151120 browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:391121 GURL(url), GURL(), PageTransition::LINK, -1,
1122 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]1717246f2010-02-10 17:08:151123 browser->window()->SetBounds(gfx::Rect(kPopupLeft,
1124 kPopupTop,
[email protected]467293292010-04-16 19:41:561125 width,
1126 height));
[email protected]1717246f2010-02-10 17:08:151127
1128 browser->window()->Show();
1129 }
[email protected]f5bf8ccf2010-02-05 18:19:251130
1131 return browser;
1132}
[email protected]1717246f2010-02-10 17:08:151133
1134Browser* FileBrowseUI::GetPopupForPath(const std::string& path) {
1135 for (BrowserList::const_iterator it = BrowserList::begin();
1136 it != BrowserList::end(); ++it) {
1137 if ((*it)->type() == Browser::TYPE_POPUP) {
[email protected]d519bed2010-05-28 18:42:031138 TabContents* tab_contents = (*it)->GetSelectedTabContents();
1139 DCHECK(tab_contents);
1140 if (!tab_contents)
1141 continue;
1142 const GURL& url = tab_contents->GetURL();
[email protected]1717246f2010-02-10 17:08:151143
1144 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1145 url.host() == chrome::kChromeUIFileBrowseHost &&
1146 url.ref() == path) {
1147 return (*it);
1148 }
1149 }
1150 }
1151
1152 return NULL;
1153}
[email protected]467293292010-04-16 19:41:561154
1155const int FileBrowseUI::kPopupWidth = 250;
1156const int FileBrowseUI::kPopupHeight = 300;
1157const int FileBrowseUI::kSmallPopupWidth = 250;
1158const int FileBrowseUI::kSmallPopupHeight = 50;