blob: a522ac9603cb86058ec2a91fbae34512d2189711 [file] [log] [blame]
[email protected]f90bf0d92011-01-13 02:12:441// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]698601e2009-10-21 22:43:372// 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
[email protected]9fec6ff2011-02-10 23:47:107#include <algorithm>
8#include <vector>
9
[email protected]2041cf342010-02-19 03:15:5910#include "base/callback.h"
[email protected]ba8164242010-11-16 21:31:0011#include "base/command_line.h"
[email protected]23ebd2302010-11-04 16:23:2012#include "base/file_util.h"
[email protected]698601e2009-10-21 22:43:3713#include "base/logging.h"
14#include "base/message_loop.h"
[email protected]07046ab2010-01-20 21:42:4415#include "base/path_service.h"
[email protected]5c4c7402009-10-30 19:58:0616#include "base/singleton.h"
[email protected]698601e2009-10-21 22:43:3717#include "base/string_piece.h"
18#include "base/string_util.h"
[email protected]34b99632011-01-01 01:01:0619#include "base/threading/thread.h"
[email protected]698601e2009-10-21 22:43:3720#include "base/time.h"
[email protected]be1ce6a72010-08-03 14:35:2221#include "base/utf_string_conversions.h"
[email protected]698601e2009-10-21 22:43:3722#include "base/values.h"
[email protected]9d6b9aff2009-12-11 17:39:1823#include "base/weak_ptr.h"
[email protected]698601e2009-10-21 22:43:3724#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]facac1532010-10-11 22:59:5025#include "chrome/browser/browser_thread.h"
[email protected]c848d3d92010-09-16 21:57:4526#include "chrome/browser/dom_ui/mediaplayer_ui.h"
[email protected]e0813a392011-02-01 22:57:2527#include "chrome/browser/dom_ui/web_ui_favicon_source.h"
[email protected]75986612010-07-16 22:23:5728#include "chrome/browser/download/download_item.h"
[email protected]274e42b2010-01-29 22:03:5729#include "chrome/browser/download/download_manager.h"
30#include "chrome/browser/download/download_util.h"
[email protected]1717246f2010-02-10 17:08:1531#include "chrome/browser/history/history_types.h"
[email protected]698601e2009-10-21 22:43:3732#include "chrome/browser/metrics/user_metrics.h"
[email protected]8ecad5e2010-12-02 21:18:3333#include "chrome/browser/profiles/profile.h"
[email protected]1717246f2010-02-10 17:08:1534#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]c848d3d92010-09-16 21:57:4535#include "chrome/browser/tabs/tab_strip_model.h"
[email protected]2ad4a902010-11-17 06:05:1336#include "chrome/browser/ui/browser.h"
37#include "chrome/browser/ui/browser_list.h"
[email protected]339d6dd2010-11-12 00:41:5838#include "chrome/browser/ui/browser_navigator.h"
[email protected]2ad4a902010-11-17 06:05:1339#include "chrome/browser/ui/browser_window.h"
[email protected]07046ab2010-01-20 21:42:4440#include "chrome/common/chrome_paths.h"
[email protected]ba8164242010-11-16 21:31:0041#include "chrome/common/chrome_switches.h"
[email protected]698601e2009-10-21 22:43:3742#include "chrome/common/jstemplate_builder.h"
[email protected]68d2a05f2010-05-07 21:39:5543#include "chrome/common/net/url_fetcher.h"
[email protected]698601e2009-10-21 22:43:3744#include "chrome/common/time_format.h"
45#include "chrome/common/url_constants.h"
[email protected]698601e2009-10-21 22:43:3746#include "grit/browser_resources.h"
47#include "grit/chromium_strings.h"
48#include "grit/generated_resources.h"
49#include "grit/locale_settings.h"
[email protected]c848d3d92010-09-16 21:57:4550#include "net/base/escape.h"
[email protected]df53d2d2010-11-19 08:56:1251#include "net/url_request/url_request_file_job.h"
[email protected]c051a1b2011-01-21 23:30:1752#include "ui/base/l10n/l10n_util.h"
[email protected]42ce29d2011-01-20 23:19:4653#include "ui/base/resource/resource_bundle.h"
[email protected]698601e2009-10-21 22:43:3754
[email protected]07046ab2010-01-20 21:42:4455#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:2456#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]268b02f2010-02-04 21:07:1557#include "chrome/browser/chromeos/cros/mount_library.h"
[email protected]d52bb8a2010-05-10 21:05:3558#include "chrome/browser/chromeos/login/user_manager.h"
[email protected]07046ab2010-01-20 21:42:4459#endif
60
[email protected]698601e2009-10-21 22:43:3761// Maximum number of search results to return in a given search. We should
62// eventually remove this.
63static const int kMaxSearchResults = 100;
[email protected]e4fbe8e2010-08-07 07:00:4664static const char kPropertyPath[] = "path";
65static const char kPropertyTitle[] = "title";
66static const char kPropertyDirectory[] = "isDirectory";
67static const char kPicasawebUserPrefix[] =
[email protected]9d6b9aff2009-12-11 17:39:1868 "http://picasaweb.google.com/data/feed/api/user/";
[email protected]e4fbe8e2010-08-07 07:00:4669static const char kPicasawebDefault[] = "/albumid/default";
70static const char kPicasawebDropBox[] = "/home";
71static const char kPicasawebBaseUrl[] = "http://picasaweb.google.com/";
72static const char kMediaPath[] = "/media";
73static const char kFilebrowseURLHash[] = "chrome://filebrowse#";
[email protected]f5bf8ccf2010-02-05 18:19:2574static const int kPopupLeft = 0;
75static const int kPopupTop = 0;
[email protected]f5bf8ccf2010-02-05 18:19:2576
[email protected]698601e2009-10-21 22:43:3777class FileBrowseUIHTMLSource : public ChromeURLDataManager::DataSource {
78 public:
79 FileBrowseUIHTMLSource();
80
81 // Called when the network layer has requested a resource underneath
82 // the path we registered.
[email protected]f09d93792009-11-17 00:10:3683 virtual void StartDataRequest(const std::string& path,
84 bool is_off_the_record,
85 int request_id);
[email protected]698601e2009-10-21 22:43:3786 virtual std::string GetMimeType(const std::string&) const {
87 return "text/html";
88 }
89
90 private:
[email protected]8de85a62009-11-06 08:32:1791 ~FileBrowseUIHTMLSource() {}
92
[email protected]698601e2009-10-21 22:43:3793 DISALLOW_COPY_AND_ASSIGN(FileBrowseUIHTMLSource);
94};
95
[email protected]9d6b9aff2009-12-11 17:39:1896class TaskProxy;
97
[email protected]698601e2009-10-21 22:43:3798// The handler for Javascript messages related to the "filebrowse" view.
[email protected]dcd23fe2009-11-12 20:21:1899class FilebrowseHandler : public net::DirectoryLister::DirectoryListerDelegate,
[email protected]36e12172011-02-08 23:46:02100 public WebUIMessageHandler,
[email protected]07046ab2010-01-20 21:42:44101#if defined(OS_CHROMEOS)
102 public chromeos::MountLibrary::Observer,
103#endif
[email protected]9d6b9aff2009-12-11 17:39:18104 public base::SupportsWeakPtr<FilebrowseHandler>,
[email protected]274e42b2010-01-29 22:03:57105 public URLFetcher::Delegate,
106 public DownloadManager::Observer,
107 public DownloadItem::Observer {
[email protected]698601e2009-10-21 22:43:37108 public:
[email protected]dcd23fe2009-11-12 20:21:18109 FilebrowseHandler();
110 virtual ~FilebrowseHandler();
[email protected]698601e2009-10-21 22:43:37111
[email protected]274e42b2010-01-29 22:03:57112 // Init work after Attach.
113 void Init();
114
[email protected]698601e2009-10-21 22:43:37115 // DirectoryLister::DirectoryListerDelegate methods:
[email protected]9ee2c8d2010-08-21 23:25:22116 virtual void OnListFile(
117 const net::DirectoryLister::DirectoryListerData& data);
[email protected]698601e2009-10-21 22:43:37118 virtual void OnListDone(int error);
119
[email protected]36e12172011-02-08 23:46:02120 // WebUIMessageHandler implementation.
121 virtual WebUIMessageHandler* Attach(DOMUI* dom_ui);
[email protected]698601e2009-10-21 22:43:37122 virtual void RegisterMessages();
123
[email protected]07046ab2010-01-20 21:42:44124#if defined(OS_CHROMEOS)
125 void MountChanged(chromeos::MountLibrary* obj,
126 chromeos::MountEventType evt,
127 const std::string& path);
128#endif
129
[email protected]274e42b2010-01-29 22:03:57130 // DownloadItem::Observer interface
131 virtual void OnDownloadUpdated(DownloadItem* download);
[email protected]ebdd2012011-01-19 11:06:57132 virtual void OnDownloadFileCompleted(DownloadItem* download);
[email protected]274e42b2010-01-29 22:03:57133 virtual void OnDownloadOpened(DownloadItem* download) { }
134
135 // DownloadManager::Observer interface
136 virtual void ModelChanged();
[email protected]82f37b02010-07-29 22:04:57137
[email protected]698601e2009-10-21 22:43:37138 // Callback for the "getRoots" message.
[email protected]88942a22010-08-19 20:34:43139 void HandleGetRoots(const ListValue* args);
[email protected]698601e2009-10-21 22:43:37140
[email protected]9fec6ff2011-02-10 23:47:10141 void GetChildrenForPath(const FilePath& path, bool is_refresh);
[email protected]a67fa08e2010-02-12 20:43:55142
[email protected]9d6b9aff2009-12-11 17:39:18143 void OnURLFetchComplete(const URLFetcher* source,
144 const GURL& url,
[email protected]f90bf0d92011-01-13 02:12:44145 const net::URLRequestStatus& status,
[email protected]9d6b9aff2009-12-11 17:39:18146 int response_code,
147 const ResponseCookies& cookies,
148 const std::string& data);
149
[email protected]698601e2009-10-21 22:43:37150 // Callback for the "getChildren" message.
[email protected]88942a22010-08-19 20:34:43151 void HandleGetChildren(const ListValue* args);
[email protected]9fec6ff2011-02-10 23:47:10152 // Callback for the "refreshDirectory" message.
[email protected]88942a22010-08-19 20:34:43153 void HandleRefreshDirectory(const ListValue* args);
154 void HandleIsAdvancedEnabled(const ListValue* args);
[email protected]698601e2009-10-21 22:43:37155
156 // Callback for the "getMetadata" message.
[email protected]88942a22010-08-19 20:34:43157 void HandleGetMetadata(const ListValue* args);
[email protected]698601e2009-10-21 22:43:37158
[email protected]9fec6ff2011-02-10 23:47:10159 // Callback for the "openNewWindow" message.
[email protected]88942a22010-08-19 20:34:43160 void OpenNewFullWindow(const ListValue* args);
161 void OpenNewPopupWindow(const ListValue* args);
[email protected]dcd23fe2009-11-12 20:21:18162
[email protected]9d6b9aff2009-12-11 17:39:18163 // Callback for the "uploadToPicasaweb" message.
[email protected]88942a22010-08-19 20:34:43164 void UploadToPicasaweb(const ListValue* args);
[email protected]9d6b9aff2009-12-11 17:39:18165
[email protected]274e42b2010-01-29 22:03:57166 // Callback for the "getDownloads" message.
[email protected]88942a22010-08-19 20:34:43167 void HandleGetDownloads(const ListValue* args);
[email protected]274e42b2010-01-29 22:03:57168
[email protected]88942a22010-08-19 20:34:43169 void HandleCreateNewFolder(const ListValue* args);
[email protected]274e42b2010-01-29 22:03:57170
[email protected]88942a22010-08-19 20:34:43171 void PlayMediaFile(const ListValue* args);
172 void EnqueueMediaFile(const ListValue* args);
[email protected]17496bb2010-03-10 19:48:23173
[email protected]88942a22010-08-19 20:34:43174 void HandleDeleteFile(const ListValue* args);
[email protected]d3d9b4fb2010-09-24 16:24:27175 void HandleCopyFile(const ListValue* value);
[email protected]9fec6ff2011-02-10 23:47:10176 void CopyFile(const FilePath& src, const FilePath& dest, TaskProxy* task);
177 void DeleteFile(const FilePath& path, TaskProxy* task);
[email protected]a67fa08e2010-02-12 20:43:55178 void FireDeleteComplete(const FilePath& path);
[email protected]d3d9b4fb2010-09-24 16:24:27179 void FireCopyComplete(const FilePath& src, const FilePath& dest);
[email protected]a67fa08e2010-02-12 20:43:55180
[email protected]88942a22010-08-19 20:34:43181 void HandlePauseToggleDownload(const ListValue* args);
[email protected]a67fa08e2010-02-12 20:43:55182
[email protected]88942a22010-08-19 20:34:43183 void HandleCancelDownload(const ListValue* args);
184 void HandleAllowDownload(const ListValue* args);
[email protected]c4a530b2010-03-08 17:33:03185
[email protected]9fec6ff2011-02-10 23:47:10186 void CreateNewFolder(const FilePath& path) const;
187
[email protected]9d6b9aff2009-12-11 17:39:18188 void ReadInFile();
189 void FireUploadComplete();
190
[email protected]d52bb8a2010-05-10 21:05:35191 void SendPicasawebRequest();
[email protected]23ebd2302010-11-04 16:23:20192
193 // Callback for the "validateSavePath" message.
194 void HandleValidateSavePath(const ListValue* args);
195
196 // Validate a save path on file thread.
[email protected]9fec6ff2011-02-10 23:47:10197 void ValidateSavePathOnFileThread(const FilePath& save_path, TaskProxy* task);
[email protected]23ebd2302010-11-04 16:23:20198
199 // Fire save path validation result to JS onValidatedSavePath.
200 void FireOnValidatedSavePathOnUIThread(bool valid, const FilePath& save_path);
201
[email protected]698601e2009-10-21 22:43:37202 private:
[email protected]dcd23fe2009-11-12 20:21:18203
[email protected]ebdd2012011-01-19 11:06:57204 // Retrieves downloads from the DownloadManager and updates the page.
205 void UpdateDownloadList();
206
[email protected]88942a22010-08-19 20:34:43207 void OpenNewWindow(const ListValue* args, bool popup);
[email protected]dcd23fe2009-11-12 20:21:18208
[email protected]274e42b2010-01-29 22:03:57209 // Clear all download items and their observers.
210 void ClearDownloadItems();
211
212 // Send the current list of downloads to the page.
213 void SendCurrentDownloads();
214
[email protected]377b4cc2010-05-18 17:25:19215 void SendNewDownload(DownloadItem* download);
216
[email protected]9fec6ff2011-02-10 23:47:10217 bool ValidateSaveDir(const FilePath& save_dir, bool exists) const;
218 bool AccessDisabled(const FilePath& path) const;
219
[email protected]698601e2009-10-21 22:43:37220 scoped_ptr<ListValue> filelist_value_;
221 FilePath currentpath_;
[email protected]dcd23fe2009-11-12 20:21:18222 Profile* profile_;
[email protected]93a58bf72010-06-04 23:01:20223 TabContents* tab_contents_;
[email protected]9d6b9aff2009-12-11 17:39:18224 std::string current_file_contents_;
225 std::string current_file_uploaded_;
226 int upload_response_code_;
[email protected]d52bb8a2010-05-10 21:05:35227 TaskProxy* current_task_;
[email protected]698601e2009-10-21 22:43:37228 scoped_refptr<net::DirectoryLister> lister_;
[email protected]a67fa08e2010-02-12 20:43:55229 bool is_refresh_;
[email protected]d52bb8a2010-05-10 21:05:35230 scoped_ptr<URLFetcher> fetch_;
[email protected]698601e2009-10-21 22:43:37231
[email protected]274e42b2010-01-29 22:03:57232 DownloadManager* download_manager_;
233 typedef std::vector<DownloadItem*> DownloadList;
[email protected]377b4cc2010-05-18 17:25:19234 DownloadList active_download_items_;
[email protected]274e42b2010-01-29 22:03:57235 DownloadList download_items_;
[email protected]377b4cc2010-05-18 17:25:19236 bool got_first_download_list_;
[email protected]dcd23fe2009-11-12 20:21:18237 DISALLOW_COPY_AND_ASSIGN(FilebrowseHandler);
[email protected]698601e2009-10-21 22:43:37238};
239
[email protected]9d6b9aff2009-12-11 17:39:18240class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> {
241 public:
[email protected]d3d9b4fb2010-09-24 16:24:27242 TaskProxy(const base::WeakPtr<FilebrowseHandler>& handler,
243 const FilePath& path, const FilePath& dest)
[email protected]a67fa08e2010-02-12 20:43:55244 : handler_(handler),
[email protected]d3d9b4fb2010-09-24 16:24:27245 src_(path),
246 dest_(dest) {}
247 TaskProxy(const base::WeakPtr<FilebrowseHandler>& handler,
248 const FilePath& path)
249 : handler_(handler),
250 src_(path) {}
[email protected]9fec6ff2011-02-10 23:47:10251
252 // TaskProxy is created on the UI thread, so in some cases,
253 // we need to post back to the UI thread for destruction.
254 void DeleteOnUIThread() {
255 BrowserThread::PostTask(
256 BrowserThread::UI, FROM_HERE,
257 NewRunnableMethod(this, &TaskProxy::DoNothing));
[email protected]9d6b9aff2009-12-11 17:39:18258 }
[email protected]9fec6ff2011-02-10 23:47:10259
260 void DoNothing() {}
261
262 void ReadInFileProxy() {
263 if (handler_)
264 handler_->ReadInFile();
265 DeleteOnUIThread();
266 }
267
[email protected]068a71f2010-05-11 22:35:48268 void DeleteFetcher(URLFetcher* fetch) {
269 delete fetch;
270 }
[email protected]9fec6ff2011-02-10 23:47:10271
[email protected]d52bb8a2010-05-10 21:05:35272 void SendPicasawebRequestProxy() {
[email protected]9fec6ff2011-02-10 23:47:10273 if (handler_)
[email protected]d52bb8a2010-05-10 21:05:35274 handler_->SendPicasawebRequest();
[email protected]9fec6ff2011-02-10 23:47:10275 DeleteOnUIThread();
[email protected]d52bb8a2010-05-10 21:05:35276 }
[email protected]9fec6ff2011-02-10 23:47:10277
[email protected]9d6b9aff2009-12-11 17:39:18278 void FireUploadCompleteProxy() {
[email protected]9fec6ff2011-02-10 23:47:10279 if (handler_)
[email protected]9d6b9aff2009-12-11 17:39:18280 handler_->FireUploadComplete();
[email protected]9d6b9aff2009-12-11 17:39:18281 }
[email protected]a67fa08e2010-02-12 20:43:55282
283 void DeleteFileProxy() {
[email protected]9fec6ff2011-02-10 23:47:10284 if (handler_)
285 handler_->DeleteFile(src_, this);
[email protected]d3d9b4fb2010-09-24 16:24:27286 }
287
288 void CopyFileProxy() {
[email protected]9fec6ff2011-02-10 23:47:10289 if (handler_)
290 handler_->CopyFile(src_, dest_, this);
291 }
292
293 void CreateNewFolderProxy() {
294 if (handler_)
295 handler_->CreateNewFolder(src_);
296 DeleteOnUIThread();
[email protected]a67fa08e2010-02-12 20:43:55297 }
298
299 void FireDeleteCompleteProxy() {
[email protected]9fec6ff2011-02-10 23:47:10300 if (handler_)
[email protected]d3d9b4fb2010-09-24 16:24:27301 handler_->FireDeleteComplete(src_);
[email protected]d3d9b4fb2010-09-24 16:24:27302 }
303 void FireCopyCompleteProxy() {
[email protected]9fec6ff2011-02-10 23:47:10304 if (handler_)
[email protected]d3d9b4fb2010-09-24 16:24:27305 handler_->FireCopyComplete(src_, dest_);
[email protected]a67fa08e2010-02-12 20:43:55306 }
[email protected]23ebd2302010-11-04 16:23:20307
308 void ValidateSavePathOnFileThread() {
309 if (handler_)
[email protected]9fec6ff2011-02-10 23:47:10310 handler_->ValidateSavePathOnFileThread(src_, this);
[email protected]23ebd2302010-11-04 16:23:20311 }
[email protected]9fec6ff2011-02-10 23:47:10312
313 void FireOnValidatedSavePathOnUIThread(bool valid) {
[email protected]23ebd2302010-11-04 16:23:20314 if (handler_)
[email protected]9fec6ff2011-02-10 23:47:10315 handler_->FireOnValidatedSavePathOnUIThread(valid, src_);
[email protected]23ebd2302010-11-04 16:23:20316 }
317
[email protected]9d6b9aff2009-12-11 17:39:18318 private:
319 base::WeakPtr<FilebrowseHandler> handler_;
[email protected]d3d9b4fb2010-09-24 16:24:27320 FilePath src_;
321 FilePath dest_;
[email protected]9d6b9aff2009-12-11 17:39:18322 friend class base::RefCountedThreadSafe<TaskProxy>;
[email protected]068a71f2010-05-11 22:35:48323 DISALLOW_COPY_AND_ASSIGN(TaskProxy);
[email protected]9d6b9aff2009-12-11 17:39:18324};
325
326
[email protected]698601e2009-10-21 22:43:37327////////////////////////////////////////////////////////////////////////////////
328//
329// FileBrowseHTMLSource
330//
331////////////////////////////////////////////////////////////////////////////////
332
333FileBrowseUIHTMLSource::FileBrowseUIHTMLSource()
334 : DataSource(chrome::kChromeUIFileBrowseHost, MessageLoop::current()) {
335}
336
337void FileBrowseUIHTMLSource::StartDataRequest(const std::string& path,
[email protected]e42ae9b2010-03-28 00:55:05338 bool is_off_the_record,
339 int request_id) {
[email protected]698601e2009-10-21 22:43:37340 DictionaryValue localized_strings;
[email protected]11f4857282009-11-13 19:56:17341 // TODO(dhg): Add stirings to localized strings, also add more strings
[email protected]dcd23fe2009-11-12 20:21:18342 // that are currently hardcoded.
[email protected]e4fbe8e2010-08-07 07:00:46343 localized_strings.SetString("title",
344 l10n_util::GetStringUTF16(IDS_FILEBROWSER_TITLE));
345 localized_strings.SetString("pause",
346 l10n_util::GetStringUTF16(IDS_FILEBROWSER_PAUSE));
347 localized_strings.SetString("resume",
348 l10n_util::GetStringUTF16(IDS_FILEBROWSER_RESUME));
349 localized_strings.SetString("scanning",
350 l10n_util::GetStringUTF16(IDS_FILEBROWSER_SCANNING));
351 localized_strings.SetString("confirmdelete",
352 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_DELETE));
353 localized_strings.SetString("confirmyes",
354 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_YES));
355 localized_strings.SetString("confirmcancel",
356 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_CANCEL));
357 localized_strings.SetString("allowdownload",
358 l10n_util::GetStringUTF16(IDS_FILEBROWSER_CONFIRM_DOWNLOAD));
359 localized_strings.SetString("filenameprompt",
360 l10n_util::GetStringUTF16(IDS_FILEBROWSER_PROMPT_FILENAME));
361 localized_strings.SetString("save",
362 l10n_util::GetStringUTF16(IDS_FILEBROWSER_SAVE));
363 localized_strings.SetString("newfolder",
364 l10n_util::GetStringUTF16(IDS_FILEBROWSER_NEW_FOLDER));
365 localized_strings.SetString("open",
366 l10n_util::GetStringUTF16(IDS_FILEBROWSER_OPEN));
367 localized_strings.SetString("picasaweb",
368 l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_PICASAWEB));
369 localized_strings.SetString("flickr",
370 l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_FLICKR));
371 localized_strings.SetString("email",
372 l10n_util::GetStringUTF16(IDS_FILEBROWSER_UPLOAD_EMAIL));
373 localized_strings.SetString("delete",
374 l10n_util::GetStringUTF16(IDS_FILEBROWSER_DELETE));
375 localized_strings.SetString("enqueue",
376 l10n_util::GetStringUTF16(IDS_FILEBROWSER_ENQUEUE));
377 localized_strings.SetString("mediapath", kMediaPath);
[email protected]88f03da02010-06-10 16:21:13378 FilePath default_download_path;
379 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
380 &default_download_path)) {
381 NOTREACHED();
382 }
[email protected]e4fbe8e2010-08-07 07:00:46383 // TODO(viettrungluu): this is wrong -- FilePath's need not be Unicode.
384 localized_strings.SetString("downloadpath", default_download_path.value());
385 localized_strings.SetString("error_unknown_file_type",
386 l10n_util::GetStringUTF16(IDS_FILEBROWSER_ERROR_UNKNOWN_FILE_TYPE));
[email protected]698601e2009-10-21 22:43:37387 SetFontAndTextDirection(&localized_strings);
388
389 static const base::StringPiece filebrowse_html(
390 ResourceBundle::GetSharedInstance().GetRawDataResource(
391 IDR_FILEBROWSE_HTML));
392 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml(
393 filebrowse_html, &localized_strings);
394
395 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
396 html_bytes->data.resize(full_html.size());
397 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
398
399 SendResponse(request_id, html_bytes);
400}
401
402////////////////////////////////////////////////////////////////////////////////
403//
[email protected]dcd23fe2009-11-12 20:21:18404// FilebrowseHandler
[email protected]698601e2009-10-21 22:43:37405//
406////////////////////////////////////////////////////////////////////////////////
[email protected]dcd23fe2009-11-12 20:21:18407FilebrowseHandler::FilebrowseHandler()
[email protected]274e42b2010-01-29 22:03:57408 : profile_(NULL),
[email protected]93a58bf72010-06-04 23:01:20409 tab_contents_(NULL),
[email protected]a67fa08e2010-02-12 20:43:55410 is_refresh_(false),
[email protected]d52bb8a2010-05-10 21:05:35411 fetch_(NULL),
[email protected]377b4cc2010-05-18 17:25:19412 download_manager_(NULL),
413 got_first_download_list_(false) {
[email protected]a67fa08e2010-02-12 20:43:55414 lister_ = NULL;
[email protected]07046ab2010-01-20 21:42:44415#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24416 chromeos::MountLibrary* lib =
417 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44418 lib->AddObserver(this);
419#endif
[email protected]698601e2009-10-21 22:43:37420}
421
[email protected]dcd23fe2009-11-12 20:21:18422FilebrowseHandler::~FilebrowseHandler() {
[email protected]07046ab2010-01-20 21:42:44423#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24424 chromeos::MountLibrary* lib =
425 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44426 lib->RemoveObserver(this);
427#endif
[email protected]698601e2009-10-21 22:43:37428 if (lister_.get()) {
429 lister_->Cancel();
430 lister_->set_delegate(NULL);
431 }
[email protected]274e42b2010-01-29 22:03:57432
433 ClearDownloadItems();
434 download_manager_->RemoveObserver(this);
[email protected]068a71f2010-05-11 22:35:48435 URLFetcher* fetch = fetch_.release();
436 if (fetch) {
[email protected]9fec6ff2011-02-10 23:47:10437 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), currentpath_);
[email protected]2dc383ce2010-10-09 03:54:00438 BrowserThread::PostTask(
439 BrowserThread::FILE, FROM_HERE,
[email protected]068a71f2010-05-11 22:35:48440 NewRunnableMethod(
[email protected]9fec6ff2011-02-10 23:47:10441 task.get(), &TaskProxy::DeleteFetcher, fetch));
[email protected]068a71f2010-05-11 22:35:48442 }
[email protected]698601e2009-10-21 22:43:37443}
444
[email protected]36e12172011-02-08 23:46:02445WebUIMessageHandler* FilebrowseHandler::Attach(DOMUI* dom_ui) {
[email protected]698601e2009-10-21 22:43:37446 // Create our favicon data source.
[email protected]dcd23fe2009-11-12 20:21:18447 profile_ = dom_ui->GetProfile();
[email protected]248ce192011-02-10 15:26:34448 profile_->GetChromeURLDataManager()->AddDataSource(
449 new WebUIFavIconSource(profile_));
[email protected]93a58bf72010-06-04 23:01:20450 tab_contents_ = dom_ui->tab_contents();
[email protected]36e12172011-02-08 23:46:02451 return WebUIMessageHandler::Attach(dom_ui);
[email protected]698601e2009-10-21 22:43:37452}
453
[email protected]274e42b2010-01-29 22:03:57454void FilebrowseHandler::Init() {
[email protected]f7e9fd62010-09-28 15:45:06455 download_manager_ = profile_->GetDownloadManager();
[email protected]274e42b2010-01-29 22:03:57456 download_manager_->AddObserver(this);
[email protected]d52bb8a2010-05-10 21:05:35457 static bool sent_request = false;
458 if (!sent_request) {
459 // If we have not sent a request before, we should do one in order to
460 // ensure that we have the correct cookies. This is for uploads.
[email protected]9fec6ff2011-02-10 23:47:10461 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), currentpath_);
462 current_task_ = task;
[email protected]2dc383ce2010-10-09 03:54:00463 BrowserThread::PostTask(
464 BrowserThread::FILE, FROM_HERE,
[email protected]d52bb8a2010-05-10 21:05:35465 NewRunnableMethod(
[email protected]9fec6ff2011-02-10 23:47:10466 task.get(), &TaskProxy::SendPicasawebRequestProxy));
[email protected]d52bb8a2010-05-10 21:05:35467 sent_request = true;
468 }
[email protected]274e42b2010-01-29 22:03:57469}
470
[email protected]dcd23fe2009-11-12 20:21:18471void FilebrowseHandler::RegisterMessages() {
[email protected]698601e2009-10-21 22:43:37472 dom_ui_->RegisterMessageCallback("getRoots",
[email protected]dcd23fe2009-11-12 20:21:18473 NewCallback(this, &FilebrowseHandler::HandleGetRoots));
[email protected]698601e2009-10-21 22:43:37474 dom_ui_->RegisterMessageCallback("getChildren",
[email protected]dcd23fe2009-11-12 20:21:18475 NewCallback(this, &FilebrowseHandler::HandleGetChildren));
[email protected]698601e2009-10-21 22:43:37476 dom_ui_->RegisterMessageCallback("getMetadata",
[email protected]dcd23fe2009-11-12 20:21:18477 NewCallback(this, &FilebrowseHandler::HandleGetMetadata));
478 dom_ui_->RegisterMessageCallback("openNewPopupWindow",
479 NewCallback(this, &FilebrowseHandler::OpenNewPopupWindow));
480 dom_ui_->RegisterMessageCallback("openNewFullWindow",
481 NewCallback(this, &FilebrowseHandler::OpenNewFullWindow));
[email protected]9d6b9aff2009-12-11 17:39:18482 dom_ui_->RegisterMessageCallback("uploadToPicasaweb",
483 NewCallback(this, &FilebrowseHandler::UploadToPicasaweb));
[email protected]274e42b2010-01-29 22:03:57484 dom_ui_->RegisterMessageCallback("getDownloads",
485 NewCallback(this, &FilebrowseHandler::HandleGetDownloads));
486 dom_ui_->RegisterMessageCallback("createNewFolder",
487 NewCallback(this, &FilebrowseHandler::HandleCreateNewFolder));
[email protected]17496bb2010-03-10 19:48:23488 dom_ui_->RegisterMessageCallback("playMediaFile",
489 NewCallback(this, &FilebrowseHandler::PlayMediaFile));
[email protected]89a7e3812010-06-02 19:38:07490 dom_ui_->RegisterMessageCallback("enqueueMediaFile",
491 NewCallback(this, &FilebrowseHandler::EnqueueMediaFile));
[email protected]a67fa08e2010-02-12 20:43:55492 dom_ui_->RegisterMessageCallback("pauseToggleDownload",
493 NewCallback(this, &FilebrowseHandler::HandlePauseToggleDownload));
494 dom_ui_->RegisterMessageCallback("deleteFile",
495 NewCallback(this, &FilebrowseHandler::HandleDeleteFile));
[email protected]d3d9b4fb2010-09-24 16:24:27496 dom_ui_->RegisterMessageCallback("copyFile",
497 NewCallback(this, &FilebrowseHandler::HandleCopyFile));
[email protected]c4a530b2010-03-08 17:33:03498 dom_ui_->RegisterMessageCallback("cancelDownload",
499 NewCallback(this, &FilebrowseHandler::HandleCancelDownload));
500 dom_ui_->RegisterMessageCallback("allowDownload",
501 NewCallback(this, &FilebrowseHandler::HandleAllowDownload));
502 dom_ui_->RegisterMessageCallback("refreshDirectory",
503 NewCallback(this, &FilebrowseHandler::HandleRefreshDirectory));
[email protected]6ba198f2010-07-02 20:32:05504 dom_ui_->RegisterMessageCallback("isAdvancedEnabled",
505 NewCallback(this, &FilebrowseHandler::HandleIsAdvancedEnabled));
[email protected]23ebd2302010-11-04 16:23:20506 dom_ui_->RegisterMessageCallback("validateSavePath",
507 NewCallback(this, &FilebrowseHandler::HandleValidateSavePath));
[email protected]9d6b9aff2009-12-11 17:39:18508}
509
[email protected]a67fa08e2010-02-12 20:43:55510
511void FilebrowseHandler::FireDeleteComplete(const FilePath& path) {
512 // We notify the UI by telling it to refresh its contents.
513 FilePath dir_path = path.DirName();
514 GetChildrenForPath(dir_path, true);
515};
516
[email protected]d3d9b4fb2010-09-24 16:24:27517void FilebrowseHandler::FireCopyComplete(const FilePath& src,
518 const FilePath& dest) {
519 // Notify the UI somehow.
520 FilePath dir_path = dest.DirName();
521 GetChildrenForPath(dir_path, true);
522};
523
[email protected]9d6b9aff2009-12-11 17:39:18524void FilebrowseHandler::FireUploadComplete() {
[email protected]d52bb8a2010-05-10 21:05:35525#if defined(OS_CHROMEOS)
[email protected]9d6b9aff2009-12-11 17:39:18526 DictionaryValue info_value;
[email protected]e4fbe8e2010-08-07 07:00:46527 info_value.SetString("path", current_file_uploaded_);
[email protected]9d6b9aff2009-12-11 17:39:18528
529 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35530 chromeos::UserManager* user_man = chromeos::UserManager::Get();
531 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18532
533 if (username.empty()) {
534 LOG(ERROR) << "Unable to get username";
535 return;
536 }
[email protected]9fec6ff2011-02-10 23:47:10537 int location = username.find_first_of('@', 0);
[email protected]9d6b9aff2009-12-11 17:39:18538 if (location <= 0) {
539 LOG(ERROR) << "Username not formatted correctly";
540 return;
541 }
[email protected]9fec6ff2011-02-10 23:47:10542 username = username.erase(username.find_first_of('@', 0));
[email protected]e4fbe8e2010-08-07 07:00:46543 std::string picture_url = kPicasawebBaseUrl;
[email protected]9d6b9aff2009-12-11 17:39:18544 picture_url += username;
545 picture_url += kPicasawebDropBox;
[email protected]e4fbe8e2010-08-07 07:00:46546 info_value.SetString("url", picture_url);
547 info_value.SetInteger("status_code", upload_response_code_);
[email protected]9d6b9aff2009-12-11 17:39:18548 dom_ui_->CallJavascriptFunction(L"uploadComplete", info_value);
[email protected]d52bb8a2010-05-10 21:05:35549#endif
[email protected]9d6b9aff2009-12-11 17:39:18550}
551
[email protected]07046ab2010-01-20 21:42:44552#if defined(OS_CHROMEOS)
553void FilebrowseHandler::MountChanged(chromeos::MountLibrary* obj,
554 chromeos::MountEventType evt,
555 const std::string& path) {
556 if (evt == chromeos::DISK_REMOVED ||
557 evt == chromeos::DISK_CHANGED) {
558 dom_ui_->CallJavascriptFunction(L"rootsChanged");
559 }
560}
561#endif
562
[email protected]9d6b9aff2009-12-11 17:39:18563void FilebrowseHandler::OnURLFetchComplete(const URLFetcher* source,
564 const GURL& url,
[email protected]f90bf0d92011-01-13 02:12:44565 const net::URLRequestStatus& status,
[email protected]9d6b9aff2009-12-11 17:39:18566 int response_code,
567 const ResponseCookies& cookies,
568 const std::string& data) {
569 upload_response_code_ = response_code;
[email protected]666205032010-10-21 20:56:58570 VLOG(1) << "Response code: " << response_code;
571 VLOG(1) << "Request url: " << url;
[email protected]068a71f2010-05-11 22:35:48572 if (StartsWithASCII(url.spec(), kPicasawebUserPrefix, true)) {
[email protected]2dc383ce2010-10-09 03:54:00573 BrowserThread::PostTask(
574 BrowserThread::UI, FROM_HERE,
[email protected]068a71f2010-05-11 22:35:48575 NewRunnableMethod(current_task_, &TaskProxy::FireUploadCompleteProxy));
576 }
577 fetch_.reset();
[email protected]698601e2009-10-21 22:43:37578}
579
[email protected]88942a22010-08-19 20:34:43580void FilebrowseHandler::HandleGetRoots(const ListValue* args) {
[email protected]698601e2009-10-21 22:43:37581 ListValue results_value;
582 DictionaryValue info_value;
[email protected]698601e2009-10-21 22:43:37583 // TODO(dhg): add other entries, make this more general
[email protected]07046ab2010-01-20 21:42:44584#if defined(OS_CHROMEOS)
[email protected]62c7ef32010-03-23 23:44:24585 chromeos::MountLibrary* lib =
586 chromeos::CrosLibrary::Get()->GetMountLibrary();
[email protected]07046ab2010-01-20 21:42:44587 const chromeos::MountLibrary::DiskVector& disks = lib->disks();
588
589 for (size_t i = 0; i < disks.size(); ++i) {
590 if (!disks[i].mount_path.empty()) {
591 DictionaryValue* page_value = new DictionaryValue();
592 page_value->SetString(kPropertyPath, disks[i].mount_path);
[email protected]88942a22010-08-19 20:34:43593 FilePath currentpath(disks[i].mount_path);
[email protected]07046ab2010-01-20 21:42:44594 std::string filename;
595 filename = currentpath.BaseName().value();
596 page_value->SetString(kPropertyTitle, filename);
597 page_value->SetBoolean(kPropertyDirectory, true);
598 results_value.Append(page_value);
599 }
600 }
601#else
602 DictionaryValue* page_value = new DictionaryValue();
603 page_value->SetString(kPropertyPath, "/media");
604 page_value->SetString(kPropertyTitle, "Removeable");
[email protected]698601e2009-10-21 22:43:37605 page_value->SetBoolean(kPropertyDirectory, true);
[email protected]698601e2009-10-21 22:43:37606 results_value.Append(page_value);
[email protected]07046ab2010-01-20 21:42:44607#endif
608 FilePath default_download_path;
609 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
610 &default_download_path)) {
611 NOTREACHED();
612 }
613
614 DictionaryValue* download_value = new DictionaryValue();
615 download_value->SetString(kPropertyPath, default_download_path.value());
616 download_value->SetString(kPropertyTitle, "File Shelf");
617 download_value->SetBoolean(kPropertyDirectory, true);
618
619 results_value.Append(download_value);
[email protected]698601e2009-10-21 22:43:37620
[email protected]e4fbe8e2010-08-07 07:00:46621 info_value.SetString("functionCall", "getRoots");
[email protected]07046ab2010-01-20 21:42:44622 info_value.SetString(kPropertyPath, "");
[email protected]9d6b9aff2009-12-11 17:39:18623 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37624 info_value, results_value);
625}
626
[email protected]88942a22010-08-19 20:34:43627void FilebrowseHandler::HandleCreateNewFolder(const ListValue* args) {
[email protected]274e42b2010-01-29 22:03:57628#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43629 std::string path = WideToUTF8(ExtractStringValue(args));
630 FilePath currentpath(path);
[email protected]274e42b2010-01-29 22:03:57631
[email protected]9fec6ff2011-02-10 23:47:10632 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), currentpath);
633 BrowserThread::PostTask(
634 BrowserThread::FILE, FROM_HERE,
635 NewRunnableMethod(
636 task.get(), &TaskProxy::CreateNewFolderProxy));
[email protected]274e42b2010-01-29 22:03:57637#endif
638}
639
[email protected]9fec6ff2011-02-10 23:47:10640void FilebrowseHandler::CreateNewFolder(const FilePath& currentpath) const {
641 if (!ValidateSaveDir(currentpath, false) ||
642 !file_util::CreateDirectory(currentpath))
643 LOG(ERROR) << "Unable to create directory " << currentpath.value();
644}
645
[email protected]88942a22010-08-19 20:34:43646void FilebrowseHandler::PlayMediaFile(const ListValue* args) {
[email protected]17496bb2010-03-10 19:48:23647#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43648 std::string url = WideToUTF8(ExtractStringValue(args));
649 GURL gurl(url);
[email protected]17496bb2010-03-10 19:48:23650
[email protected]88942a22010-08-19 20:34:43651 Browser* browser = Browser::GetBrowserForController(
652 &tab_contents_->controller(), NULL);
[email protected]dc8caba2010-12-13 16:52:35653 MediaPlayer* mediaplayer = MediaPlayer::GetInstance();
[email protected]88942a22010-08-19 20:34:43654 mediaplayer->ForcePlayMediaURL(gurl, browser);
[email protected]89a7e3812010-06-02 19:38:07655#endif
656}
657
[email protected]88942a22010-08-19 20:34:43658void FilebrowseHandler::EnqueueMediaFile(const ListValue* args) {
[email protected]89a7e3812010-06-02 19:38:07659#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43660 std::string url = WideToUTF8(ExtractStringValue(args));
661 GURL gurl(url);
[email protected]89a7e3812010-06-02 19:38:07662
[email protected]88942a22010-08-19 20:34:43663 Browser* browser = Browser::GetBrowserForController(
664 &tab_contents_->controller(), NULL);
[email protected]dc8caba2010-12-13 16:52:35665 MediaPlayer* mediaplayer = MediaPlayer::GetInstance();
[email protected]88942a22010-08-19 20:34:43666 mediaplayer->EnqueueMediaURL(gurl, browser);
[email protected]17496bb2010-03-10 19:48:23667#endif
668}
669
[email protected]88942a22010-08-19 20:34:43670void FilebrowseHandler::HandleIsAdvancedEnabled(const ListValue* args) {
[email protected]6ba198f2010-07-02 20:32:05671#if defined(OS_CHROMEOS)
[email protected]ba8164242010-11-16 21:31:00672 bool is_enabled = CommandLine::ForCurrentProcess()->HasSwitch(
673 switches::kEnableAdvancedFileSystem);
674 bool mp_enabled = CommandLine::ForCurrentProcess()->HasSwitch(
675 switches::kEnableMediaPlayer);
[email protected]6ba198f2010-07-02 20:32:05676 DictionaryValue info_value;
[email protected]e4fbe8e2010-08-07 07:00:46677 info_value.SetBoolean("enabled", is_enabled);
678 info_value.SetBoolean("mpEnabled", mp_enabled);
[email protected]6ba198f2010-07-02 20:32:05679 dom_ui_->CallJavascriptFunction(L"enabledResult",
680 info_value);
[email protected]f872b3e2010-08-13 16:26:10681
[email protected]6ba198f2010-07-02 20:32:05682#endif
683}
[email protected]c4a530b2010-03-08 17:33:03684
[email protected]88942a22010-08-19 20:34:43685void FilebrowseHandler::HandleRefreshDirectory(const ListValue* args) {
[email protected]a67fa08e2010-02-12 20:43:55686#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43687 std::string path = WideToUTF8(ExtractStringValue(args));
688 FilePath currentpath(path);
689 GetChildrenForPath(currentpath, true);
[email protected]a67fa08e2010-02-12 20:43:55690#endif
691}
692
[email protected]88942a22010-08-19 20:34:43693void FilebrowseHandler::HandlePauseToggleDownload(const ListValue* args) {
[email protected]c4a530b2010-03-08 17:33:03694#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43695 int id;
696 ExtractIntegerValue(args, &id);
[email protected]9fec6ff2011-02-10 23:47:10697 if ((id - 1) >= static_cast<int>(active_download_items_.size())) {
[email protected]d3d9b4fb2010-09-24 16:24:27698 return;
699 }
[email protected]88942a22010-08-19 20:34:43700 DownloadItem* item = active_download_items_[id];
701 item->TogglePause();
[email protected]c4a530b2010-03-08 17:33:03702#endif
703}
704
[email protected]88942a22010-08-19 20:34:43705void FilebrowseHandler::HandleAllowDownload(const ListValue* args) {
[email protected]c4a530b2010-03-08 17:33:03706#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43707 int id;
708 ExtractIntegerValue(args, &id);
[email protected]9fec6ff2011-02-10 23:47:10709 if ((id - 1) >= static_cast<int>(active_download_items_.size())) {
[email protected]d3d9b4fb2010-09-24 16:24:27710 return;
711 }
712
[email protected]88942a22010-08-19 20:34:43713 DownloadItem* item = active_download_items_[id];
714 download_manager_->DangerousDownloadValidated(item);
[email protected]c4a530b2010-03-08 17:33:03715#endif
716}
717
[email protected]88942a22010-08-19 20:34:43718void FilebrowseHandler::HandleCancelDownload(const ListValue* args) {
719#if defined(OS_CHROMEOS)
720 int id;
721 ExtractIntegerValue(args, &id);
[email protected]9fec6ff2011-02-10 23:47:10722 if ((id - 1) >= static_cast<int>(active_download_items_.size())) {
[email protected]d3d9b4fb2010-09-24 16:24:27723 return;
724 }
[email protected]88942a22010-08-19 20:34:43725 DownloadItem* item = active_download_items_[id];
[email protected]88942a22010-08-19 20:34:43726 FilePath path = item->full_path();
[email protected]d3d9b4fb2010-09-24 16:24:27727 item->Cancel(true);
[email protected]88942a22010-08-19 20:34:43728 FilePath dir_path = path.DirName();
729 item->Remove(true);
730 GetChildrenForPath(dir_path, true);
731#endif
[email protected]dcd23fe2009-11-12 20:21:18732}
[email protected]698601e2009-10-21 22:43:37733
[email protected]88942a22010-08-19 20:34:43734void FilebrowseHandler::OpenNewFullWindow(const ListValue* args) {
735 OpenNewWindow(args, false);
[email protected]dcd23fe2009-11-12 20:21:18736}
737
[email protected]88942a22010-08-19 20:34:43738void FilebrowseHandler::OpenNewPopupWindow(const ListValue* args) {
739 OpenNewWindow(args, true);
740}
[email protected]dcd23fe2009-11-12 20:21:18741
[email protected]88942a22010-08-19 20:34:43742void FilebrowseHandler::OpenNewWindow(const ListValue* args, bool popup) {
743 std::string url = WideToUTF8(ExtractStringValue(args));
744 Browser* browser = popup ?
745 Browser::CreateForType(Browser::TYPE_APP_PANEL, profile_) :
746 BrowserList::GetLastActive();
[email protected]616381f02010-11-02 15:15:33747 browser::NavigateParams params(browser, GURL(url), PageTransition::LINK);
748 params.disposition = NEW_FOREGROUND_TAB;
749 browser::Navigate(&params);
750 // TODO(beng): The following two calls should be automatic by Navigate().
[email protected]88942a22010-08-19 20:34:43751 if (popup) {
752 // TODO(dhg): Remove these from being hardcoded. Allow javascript
753 // to specify.
[email protected]616381f02010-11-02 15:15:33754 params.browser->window()->SetBounds(gfx::Rect(0, 0, 400, 300));
[email protected]dcd23fe2009-11-12 20:21:18755 }
[email protected]616381f02010-11-02 15:15:33756 params.browser->window()->Show();
[email protected]dcd23fe2009-11-12 20:21:18757}
758
[email protected]d52bb8a2010-05-10 21:05:35759void FilebrowseHandler::SendPicasawebRequest() {
[email protected]068a71f2010-05-11 22:35:48760#if defined(OS_CHROMEOS)
761 chromeos::UserManager* user_man = chromeos::UserManager::Get();
762 std::string username = user_man->logged_in_user().email();
763
764 if (username.empty()) {
765 LOG(ERROR) << "Unable to get username";
766 return;
767 }
768
[email protected]d52bb8a2010-05-10 21:05:35769 fetch_.reset(URLFetcher::Create(0,
770 GURL(kPicasawebBaseUrl),
771 URLFetcher::GET,
[email protected]068a71f2010-05-11 22:35:48772 this));
[email protected]d52bb8a2010-05-10 21:05:35773 fetch_->set_request_context(profile_->GetRequestContext());
774 fetch_->Start();
[email protected]068a71f2010-05-11 22:35:48775#endif
[email protected]d52bb8a2010-05-10 21:05:35776}
777
[email protected]9d6b9aff2009-12-11 17:39:18778void FilebrowseHandler::ReadInFile() {
779#if defined(OS_CHROMEOS)
780 // Get the users username
781 std::string username;
[email protected]d52bb8a2010-05-10 21:05:35782 chromeos::UserManager* user_man = chromeos::UserManager::Get();
783 username = user_man->logged_in_user().email();
[email protected]9d6b9aff2009-12-11 17:39:18784
785 if (username.empty()) {
786 LOG(ERROR) << "Unable to get username";
787 return;
788 }
[email protected]9fec6ff2011-02-10 23:47:10789 int location = username.find_first_of('@', 0);
[email protected]9d6b9aff2009-12-11 17:39:18790 if (location <= 0) {
791 LOG(ERROR) << "Username not formatted correctly";
792 return;
793 }
[email protected]9fec6ff2011-02-10 23:47:10794 username = username.erase(username.find_first_of('@', 0));
[email protected]9d6b9aff2009-12-11 17:39:18795 std::string url = kPicasawebUserPrefix;
796 url += username;
797 url += kPicasawebDefault;
798
[email protected]88942a22010-08-19 20:34:43799 FilePath currentpath(current_file_uploaded_);
[email protected]9d6b9aff2009-12-11 17:39:18800 // Get the filename
801 std::string filename;
802 filename = currentpath.BaseName().value();
803 std::string filecontents;
804 if (!file_util::ReadFileToString(currentpath, &filecontents)) {
805 LOG(ERROR) << "Unable to read this file:" << currentpath.value();
806 return;
807 }
[email protected]d52bb8a2010-05-10 21:05:35808 fetch_.reset(URLFetcher::Create(0,
809 GURL(url),
810 URLFetcher::POST,
811 this));
812 fetch_->set_upload_data("image/jpeg", filecontents);
[email protected]9d6b9aff2009-12-11 17:39:18813 // Set the filename on the server
814 std::string slug = "Slug: ";
815 slug += filename;
[email protected]d52bb8a2010-05-10 21:05:35816 fetch_->set_extra_request_headers(slug);
817 fetch_->set_request_context(profile_->GetRequestContext());
818 fetch_->Start();
[email protected]9d6b9aff2009-12-11 17:39:18819#endif
820}
821
822// This is just a prototype for allowing generic uploads to various sites
823// TODO(dhg): Remove this and implement general upload.
[email protected]88942a22010-08-19 20:34:43824void FilebrowseHandler::UploadToPicasaweb(const ListValue* args) {
[email protected]9d6b9aff2009-12-11 17:39:18825#if defined(OS_CHROMEOS)
[email protected]88942a22010-08-19 20:34:43826 std::string search_string = WideToUTF8(ExtractStringValue(args));
827 current_file_uploaded_ = search_string;
[email protected]9d6b9aff2009-12-11 17:39:18828 // ReadInFile();
[email protected]88942a22010-08-19 20:34:43829 FilePath current_path(search_string);
[email protected]9fec6ff2011-02-10 23:47:10830 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), current_path);
[email protected]d52bb8a2010-05-10 21:05:35831 current_task_ = task;
[email protected]2dc383ce2010-10-09 03:54:00832 BrowserThread::PostTask(
833 BrowserThread::FILE, FROM_HERE,
[email protected]9d6b9aff2009-12-11 17:39:18834 NewRunnableMethod(
[email protected]9fec6ff2011-02-10 23:47:10835 task.get(), &TaskProxy::ReadInFileProxy));
[email protected]9d6b9aff2009-12-11 17:39:18836#endif
837}
838
[email protected]9fec6ff2011-02-10 23:47:10839void FilebrowseHandler::GetChildrenForPath(const FilePath& path,
840 bool is_refresh) {
[email protected]a67fa08e2010-02-12 20:43:55841 filelist_value_.reset(new ListValue());
[email protected]88942a22010-08-19 20:34:43842 currentpath_ = path;
[email protected]a67fa08e2010-02-12 20:43:55843
844 if (lister_.get()) {
845 lister_->Cancel();
846 lister_->set_delegate(NULL);
847 lister_ = NULL;
848 }
849
850 is_refresh_ = is_refresh;
[email protected]df53d2d2010-11-19 08:56:12851
852#if defined(OS_CHROMEOS)
853 // Don't allow listing files in inaccessible dirs.
[email protected]9fec6ff2011-02-10 23:47:10854 if (AccessDisabled(path))
[email protected]df53d2d2010-11-19 08:56:12855 return;
[email protected]9fec6ff2011-02-10 23:47:10856#endif
[email protected]df53d2d2010-11-19 08:56:12857
[email protected]f872b3e2010-08-13 16:26:10858 FilePath default_download_path;
859 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
860 &default_download_path)) {
861 NOTREACHED();
862 }
[email protected]9fec6ff2011-02-10 23:47:10863
[email protected]f872b3e2010-08-13 16:26:10864 if (currentpath_ == default_download_path) {
865 lister_ = new net::DirectoryLister(currentpath_,
[email protected]9ee2c8d2010-08-21 23:25:22866 false,
[email protected]f872b3e2010-08-13 16:26:10867 net::DirectoryLister::DATE,
868 this);
869 } else {
870 lister_ = new net::DirectoryLister(currentpath_, this);
871 }
[email protected]a67fa08e2010-02-12 20:43:55872 lister_->Start();
873}
874
[email protected]88942a22010-08-19 20:34:43875void FilebrowseHandler::HandleGetChildren(const ListValue* args) {
876#if defined(OS_CHROMEOS)
877 std::string path = WideToUTF8(ExtractStringValue(args));
878 FilePath currentpath(path);
[email protected]698601e2009-10-21 22:43:37879 filelist_value_.reset(new ListValue());
[email protected]698601e2009-10-21 22:43:37880
[email protected]a67fa08e2010-02-12 20:43:55881 GetChildrenForPath(currentpath, false);
[email protected]88942a22010-08-19 20:34:43882#endif
[email protected]698601e2009-10-21 22:43:37883}
884
[email protected]dcd23fe2009-11-12 20:21:18885void FilebrowseHandler::OnListFile(
[email protected]9ee2c8d2010-08-21 23:25:22886 const net::DirectoryLister::DirectoryListerData& data) {
[email protected]07046ab2010-01-20 21:42:44887#if defined(OS_WIN)
[email protected]9ee2c8d2010-08-21 23:25:22888 if (data.info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
[email protected]07046ab2010-01-20 21:42:44889 return;
890 }
891#elif defined(OS_POSIX)
[email protected]9ee2c8d2010-08-21 23:25:22892 if (data.info.filename[0] == '.') {
[email protected]07046ab2010-01-20 21:42:44893 return;
894 }
[email protected]9fec6ff2011-02-10 23:47:10895
896 // Suppress .crdownload files.
897 static const char crdownload[] = (".crdownload");
898 static const size_t crdownload_size = arraysize(crdownload);
899 const std::string& filename = data.info.filename;
900 if ((filename.size() > crdownload_size) &&
901 (filename.rfind(crdownload) == (filename.size() - crdownload_size)))
902 return;
[email protected]07046ab2010-01-20 21:42:44903#endif
904
[email protected]698601e2009-10-21 22:43:37905 DictionaryValue* file_value = new DictionaryValue();
906
907#if defined(OS_WIN)
[email protected]9ee2c8d2010-08-21 23:25:22908 int64 size = (static_cast<int64>(data.info.nFileSizeHigh) << 32) |
909 data.info.nFileSizeLow;
910 file_value->SetString(kPropertyTitle, data.info.cFileName);
[email protected]698601e2009-10-21 22:43:37911 file_value->SetString(kPropertyPath,
[email protected]9ee2c8d2010-08-21 23:25:22912 currentpath_.Append(data.info.cFileName).value());
[email protected]698601e2009-10-21 22:43:37913 file_value->SetBoolean(kPropertyDirectory,
[email protected]9ee2c8d2010-08-21 23:25:22914 (data.info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false);
[email protected]698601e2009-10-21 22:43:37915#elif defined(OS_POSIX)
[email protected]9ee2c8d2010-08-21 23:25:22916 file_value->SetString(kPropertyTitle, data.info.filename);
[email protected]698601e2009-10-21 22:43:37917 file_value->SetString(kPropertyPath,
[email protected]9ee2c8d2010-08-21 23:25:22918 currentpath_.Append(data.info.filename).value());
919 file_value->SetBoolean(kPropertyDirectory, S_ISDIR(data.info.stat.st_mode));
[email protected]698601e2009-10-21 22:43:37920#endif
921 filelist_value_->Append(file_value);
922}
923
[email protected]dcd23fe2009-11-12 20:21:18924void FilebrowseHandler::OnListDone(int error) {
[email protected]698601e2009-10-21 22:43:37925 DictionaryValue info_value;
[email protected]a67fa08e2010-02-12 20:43:55926 if (is_refresh_) {
[email protected]e4fbe8e2010-08-07 07:00:46927 info_value.SetString("functionCall", "refresh");
[email protected]a67fa08e2010-02-12 20:43:55928 } else {
[email protected]e4fbe8e2010-08-07 07:00:46929 info_value.SetString("functionCall", "getChildren");
[email protected]a67fa08e2010-02-12 20:43:55930 }
[email protected]698601e2009-10-21 22:43:37931 info_value.SetString(kPropertyPath, currentpath_.value());
[email protected]9d6b9aff2009-12-11 17:39:18932 dom_ui_->CallJavascriptFunction(L"browseFileResult",
[email protected]698601e2009-10-21 22:43:37933 info_value, *(filelist_value_.get()));
[email protected]698601e2009-10-21 22:43:37934}
935
[email protected]88942a22010-08-19 20:34:43936void FilebrowseHandler::HandleGetMetadata(const ListValue* args) {
[email protected]698601e2009-10-21 22:43:37937}
938
[email protected]88942a22010-08-19 20:34:43939void FilebrowseHandler::HandleGetDownloads(const ListValue* args) {
[email protected]ebdd2012011-01-19 11:06:57940 UpdateDownloadList();
[email protected]274e42b2010-01-29 22:03:57941}
942
943void FilebrowseHandler::ModelChanged() {
[email protected]ebdd2012011-01-19 11:06:57944 if (!currentpath_.empty())
945 GetChildrenForPath(currentpath_, true);
946 else
947 UpdateDownloadList();
948}
949
950void FilebrowseHandler::UpdateDownloadList() {
[email protected]274e42b2010-01-29 22:03:57951 ClearDownloadItems();
[email protected]274e42b2010-01-29 22:03:57952
[email protected]0e2b59a2010-07-29 22:27:05953 std::vector<DownloadItem*> downloads;
954 download_manager_->GetAllDownloads(FilePath(), &downloads);
955
[email protected]377b4cc2010-05-18 17:25:19956 std::vector<DownloadItem*> new_downloads;
[email protected]274e42b2010-01-29 22:03:57957 // Scan for any in progress downloads and add ourself to them as an observer.
958 for (DownloadList::iterator it = downloads.begin();
959 it != downloads.end(); ++it) {
960 DownloadItem* download = *it;
961 // We want to know what happens as the download progresses and be notified
962 // when the user validates the dangerous download.
963 if (download->state() == DownloadItem::IN_PROGRESS ||
964 download->safety_state() == DownloadItem::DANGEROUS) {
965 download->AddObserver(this);
[email protected]377b4cc2010-05-18 17:25:19966 active_download_items_.push_back(download);
[email protected]274e42b2010-01-29 22:03:57967 }
[email protected]377b4cc2010-05-18 17:25:19968 DownloadList::iterator item = find(download_items_.begin(),
969 download_items_.end(),
970 download);
971 if (item == download_items_.end() && got_first_download_list_) {
972 SendNewDownload(download);
973 }
974 new_downloads.push_back(download);
[email protected]274e42b2010-01-29 22:03:57975 }
[email protected]377b4cc2010-05-18 17:25:19976 download_items_.swap(new_downloads);
977 got_first_download_list_ = true;
[email protected]274e42b2010-01-29 22:03:57978 SendCurrentDownloads();
979}
980
[email protected]377b4cc2010-05-18 17:25:19981void FilebrowseHandler::SendNewDownload(DownloadItem* download) {
982 ListValue results_value;
983 results_value.Append(download_util::CreateDownloadItemValue(download, -1));
984 dom_ui_->CallJavascriptFunction(L"newDownload", results_value);
985}
986
[email protected]9fec6ff2011-02-10 23:47:10987void FilebrowseHandler::DeleteFile(const FilePath& path, TaskProxy* task) {
[email protected]a67fa08e2010-02-12 20:43:55988 if (!file_util::Delete(path, true)) {
989 LOG(ERROR) << "unable to delete directory";
990 }
[email protected]2dc383ce2010-10-09 03:54:00991 BrowserThread::PostTask(
992 BrowserThread::UI, FROM_HERE,
[email protected]9fec6ff2011-02-10 23:47:10993 NewRunnableMethod(task, &TaskProxy::FireDeleteCompleteProxy));
[email protected]a67fa08e2010-02-12 20:43:55994}
995
[email protected]9fec6ff2011-02-10 23:47:10996void FilebrowseHandler::CopyFile(const FilePath& src,
997 const FilePath& dest,
998 TaskProxy* task) {
[email protected]d3d9b4fb2010-09-24 16:24:27999 if (file_util::DirectoryExists(src)) {
1000 if (!file_util::CopyDirectory(src, dest, true)) {
1001 LOG(ERROR) << "unable to copy directory:" << src.value();
1002 }
1003 } else {
1004 if (!file_util::CopyFile(src, dest)) {
1005 LOG(ERROR) << "unable to copy file" << src.value();
1006 }
1007 }
[email protected]2dc383ce2010-10-09 03:54:001008 BrowserThread::PostTask(
1009 BrowserThread::UI, FROM_HERE,
[email protected]9fec6ff2011-02-10 23:47:101010 NewRunnableMethod(task, &TaskProxy::FireCopyCompleteProxy));
[email protected]d3d9b4fb2010-09-24 16:24:271011}
1012
[email protected]88942a22010-08-19 20:34:431013void FilebrowseHandler::HandleDeleteFile(const ListValue* args) {
1014#if defined(OS_CHROMEOS)
1015 std::string path = WideToUTF8(ExtractStringValue(args));
1016 FilePath currentpath(path);
[email protected]df53d2d2010-11-19 08:56:121017
1018 // Don't allow file deletion in inaccessible dirs.
[email protected]9fec6ff2011-02-10 23:47:101019 if (AccessDisabled(currentpath))
[email protected]df53d2d2010-11-19 08:56:121020 return;
1021
[email protected]88942a22010-08-19 20:34:431022 for (unsigned int x = 0; x < active_download_items_.size(); x++) {
1023 FilePath item = active_download_items_[x]->full_path();
1024 if (item == currentpath) {
1025 active_download_items_[x]->Cancel(true);
1026 active_download_items_[x]->Remove(true);
1027 FilePath dir_path = item.DirName();
1028 GetChildrenForPath(dir_path, true);
[email protected]a67fa08e2010-02-12 20:43:551029 return;
1030 }
1031 }
[email protected]9fec6ff2011-02-10 23:47:101032 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), currentpath);
[email protected]2dc383ce2010-10-09 03:54:001033 BrowserThread::PostTask(
1034 BrowserThread::FILE, FROM_HERE,
[email protected]88942a22010-08-19 20:34:431035 NewRunnableMethod(
[email protected]9fec6ff2011-02-10 23:47:101036 task.get(), &TaskProxy::DeleteFileProxy));
[email protected]a67fa08e2010-02-12 20:43:551037#endif
1038}
1039
[email protected]d3d9b4fb2010-09-24 16:24:271040void FilebrowseHandler::HandleCopyFile(const ListValue* value) {
1041#if defined(OS_CHROMEOS)
1042 if (value && value->GetType() == Value::TYPE_LIST) {
1043 const ListValue* list_value = static_cast<const ListValue*>(value);
1044 std::string src;
1045 std::string dest;
1046
1047 // Get path string.
1048 if (list_value->GetString(0, &src) &&
1049 list_value->GetString(1, &dest)) {
1050 FilePath SrcPath = FilePath(src);
1051 FilePath DestPath = FilePath(dest);
1052
[email protected]df53d2d2010-11-19 08:56:121053 // Don't allow file copy to inaccessible dirs.
[email protected]9fec6ff2011-02-10 23:47:101054 if (AccessDisabled(DestPath))
[email protected]df53d2d2010-11-19 08:56:121055 return;
1056
[email protected]9fec6ff2011-02-10 23:47:101057 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(),
1058 SrcPath, DestPath);
[email protected]2dc383ce2010-10-09 03:54:001059 BrowserThread::PostTask(
1060 BrowserThread::FILE, FROM_HERE,
[email protected]d3d9b4fb2010-09-24 16:24:271061 NewRunnableMethod(
[email protected]9fec6ff2011-02-10 23:47:101062 task.get(), &TaskProxy::CopyFileProxy));
[email protected]d3d9b4fb2010-09-24 16:24:271063 } else {
1064 LOG(ERROR) << "Unable to get string";
1065 return;
1066 }
1067 }
1068#endif
1069}
1070
[email protected]23ebd2302010-11-04 16:23:201071void FilebrowseHandler::HandleValidateSavePath(const ListValue* args) {
1072 std::string string_path;
1073 if (!args || !args->GetString(0, &string_path)) {
1074 FireOnValidatedSavePathOnUIThread(false, FilePath()); // Invalid save path.
1075 return;
1076 }
1077
1078 FilePath save_path(string_path);
1079
1080#if defined(OS_CHROMEOS)
1081 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr(), save_path);
1082 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
1083 NewRunnableMethod(task.get(), &TaskProxy::ValidateSavePathOnFileThread));
1084#else
1085 // No save path checking for non-ChromeOS platforms.
1086 FireOnValidatedSavePathOnUIThread(true, save_path);
1087#endif
1088}
1089
1090void FilebrowseHandler::ValidateSavePathOnFileThread(
[email protected]9fec6ff2011-02-10 23:47:101091 const FilePath& save_path, TaskProxy* task) {
[email protected]23ebd2302010-11-04 16:23:201092#if defined(OS_CHROMEOS)
1093 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
1094
[email protected]9fec6ff2011-02-10 23:47:101095 const bool valid = ValidateSaveDir(save_path.DirName(), true);
1096
1097 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
1098 NewRunnableMethod(task,
1099 &TaskProxy::FireOnValidatedSavePathOnUIThread,
1100 valid));
1101#endif
1102}
1103
1104bool FilebrowseHandler::ValidateSaveDir(const FilePath& save_dir,
1105 bool exists) const {
1106#if defined(OS_CHROMEOS)
[email protected]23ebd2302010-11-04 16:23:201107 FilePath default_download_path;
1108 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
1109 &default_download_path)) {
1110 NOTREACHED();
1111 }
1112
[email protected]9fec6ff2011-02-10 23:47:101113 // Valid save dir must be inside default download dir.
1114 if (default_download_path == save_dir)
1115 return true;
1116 if (exists) {
1117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
1118 return file_util::ContainsPath(default_download_path, save_dir);
1119 } else {
1120 return default_download_path.IsParent(save_dir);
1121 }
[email protected]23ebd2302010-11-04 16:23:201122#endif
[email protected]9fec6ff2011-02-10 23:47:101123 return false;
[email protected]23ebd2302010-11-04 16:23:201124}
1125
1126void FilebrowseHandler::FireOnValidatedSavePathOnUIThread(bool valid,
1127 const FilePath& save_path) {
1128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1129
1130 FundamentalValue valid_value(valid);
1131 StringValue path_value(save_path.value());
1132 dom_ui_->CallJavascriptFunction(L"onValidatedSavePath",
1133 valid_value, path_value);
1134}
[email protected]d3d9b4fb2010-09-24 16:24:271135
[email protected]274e42b2010-01-29 22:03:571136void FilebrowseHandler::OnDownloadUpdated(DownloadItem* download) {
[email protected]377b4cc2010-05-18 17:25:191137 DownloadList::iterator it = find(active_download_items_.begin(),
1138 active_download_items_.end(),
[email protected]274e42b2010-01-29 22:03:571139 download);
[email protected]377b4cc2010-05-18 17:25:191140 if (it == active_download_items_.end())
[email protected]274e42b2010-01-29 22:03:571141 return;
[email protected]377b4cc2010-05-18 17:25:191142 const int id = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:571143
[email protected]499d97c2010-10-25 23:12:151144 scoped_ptr<DictionaryValue> download_item(
1145 download_util::CreateDownloadItemValue(download, id));
1146 dom_ui_->CallJavascriptFunction(L"downloadUpdated", *download_item.get());
[email protected]274e42b2010-01-29 22:03:571147}
1148
1149void FilebrowseHandler::ClearDownloadItems() {
[email protected]377b4cc2010-05-18 17:25:191150 for (DownloadList::iterator it = active_download_items_.begin();
1151 it != active_download_items_.end(); ++it) {
[email protected]274e42b2010-01-29 22:03:571152 (*it)->RemoveObserver(this);
1153 }
[email protected]377b4cc2010-05-18 17:25:191154 active_download_items_.clear();
[email protected]274e42b2010-01-29 22:03:571155}
1156
1157void FilebrowseHandler::SendCurrentDownloads() {
1158 ListValue results_value;
[email protected]377b4cc2010-05-18 17:25:191159 for (DownloadList::iterator it = active_download_items_.begin();
1160 it != active_download_items_.end(); ++it) {
1161 int index = static_cast<int>(it - active_download_items_.begin());
[email protected]274e42b2010-01-29 22:03:571162 results_value.Append(download_util::CreateDownloadItemValue(*it, index));
1163 }
1164
1165 dom_ui_->CallJavascriptFunction(L"downloadsList", results_value);
1166}
1167
[email protected]ebdd2012011-01-19 11:06:571168void FilebrowseHandler::OnDownloadFileCompleted(DownloadItem* download) {
1169 GetChildrenForPath(currentpath_, true);
1170}
1171
[email protected]9fec6ff2011-02-10 23:47:101172bool FilebrowseHandler::AccessDisabled(const FilePath& path) const {
1173 return !ValidateSaveDir(path, false) &&
1174 net::URLRequestFileJob::AccessDisabled(path);
1175}
1176
[email protected]698601e2009-10-21 22:43:371177////////////////////////////////////////////////////////////////////////////////
1178//
[email protected]f5bf8ccf2010-02-05 18:19:251179// FileBrowseUI
[email protected]698601e2009-10-21 22:43:371180//
1181////////////////////////////////////////////////////////////////////////////////
1182
[email protected]274e42b2010-01-29 22:03:571183FileBrowseUI::FileBrowseUI(TabContents* contents) : HtmlDialogUI(contents) {
[email protected]9d6b9aff2009-12-11 17:39:181184 FilebrowseHandler* handler = new FilebrowseHandler();
1185 AddMessageHandler((handler)->Attach(this));
[email protected]274e42b2010-01-29 22:03:571186 handler->Init();
[email protected]698601e2009-10-21 22:43:371187 FileBrowseUIHTMLSource* html_source = new FileBrowseUIHTMLSource();
1188
1189 // Set up the chrome://filebrowse/ source.
[email protected]248ce192011-02-10 15:26:341190 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source);
[email protected]698601e2009-10-21 22:43:371191}
[email protected]f5bf8ccf2010-02-05 18:19:251192
1193// static
[email protected]1717246f2010-02-10 17:08:151194Browser* FileBrowseUI::OpenPopup(Profile* profile,
[email protected]467293292010-04-16 19:41:561195 const std::string& hashArgument,
1196 int width,
1197 int height) {
[email protected]1717246f2010-02-10 17:08:151198 // Get existing pop up for given hashArgument.
[email protected]f7e9fd62010-09-28 15:45:061199 Browser* browser = GetPopupForPath(hashArgument, profile);
[email protected]f5bf8ccf2010-02-05 18:19:251200
[email protected]1717246f2010-02-10 17:08:151201 // Create new browser if no matching pop up found.
1202 if (browser == NULL) {
[email protected]9ee17e8c2010-08-10 19:17:311203 browser = Browser::CreateForType(Browser::TYPE_APP_PANEL, profile);
[email protected]f59f1162010-04-23 21:19:291204 std::string url;
1205 if (hashArgument.empty()) {
1206 url = chrome::kChromeUIFileBrowseURL;
1207 } else {
1208 url = kFilebrowseURLHash;
1209 url.append(hashArgument);
1210 }
[email protected]f5bf8ccf2010-02-05 18:19:251211
[email protected]616381f02010-11-02 15:15:331212 browser::NavigateParams params(browser, GURL(url), PageTransition::LINK);
1213 params.disposition = NEW_FOREGROUND_TAB;
1214 browser::Navigate(&params);
1215 // TODO(beng): The following two calls should be automatic by Navigate().
1216 params.browser->window()->SetBounds(gfx::Rect(kPopupLeft,
1217 kPopupTop,
1218 width,
1219 height));
[email protected]1717246f2010-02-10 17:08:151220
[email protected]616381f02010-11-02 15:15:331221 params.browser->window()->Show();
[email protected]9ee17e8c2010-08-10 19:17:311222 } else {
1223 browser->window()->Show();
[email protected]1717246f2010-02-10 17:08:151224 }
[email protected]f5bf8ccf2010-02-05 18:19:251225
1226 return browser;
1227}
[email protected]1717246f2010-02-10 17:08:151228
[email protected]f7e9fd62010-09-28 15:45:061229Browser* FileBrowseUI::GetPopupForPath(const std::string& path,
1230 Profile* profile) {
[email protected]9ee17e8c2010-08-10 19:17:311231 std::string current_path = path;
1232 if (current_path.empty()) {
[email protected]ba8164242010-11-16 21:31:001233 bool is_enabled = CommandLine::ForCurrentProcess()->HasSwitch(
1234 switches::kEnableAdvancedFileSystem);
[email protected]9ee17e8c2010-08-10 19:17:311235 if (!is_enabled) {
1236 FilePath default_download_path;
1237 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
1238 &default_download_path)) {
1239 NOTREACHED();
1240 }
1241 current_path = default_download_path.value();
1242 }
1243 }
1244
[email protected]1717246f2010-02-10 17:08:151245 for (BrowserList::const_iterator it = BrowserList::begin();
1246 it != BrowserList::end(); ++it) {
[email protected]9ee17e8c2010-08-10 19:17:311247 if (((*it)->type() == Browser::TYPE_APP_PANEL)) {
[email protected]d519bed2010-05-28 18:42:031248 TabContents* tab_contents = (*it)->GetSelectedTabContents();
1249 DCHECK(tab_contents);
1250 if (!tab_contents)
1251 continue;
1252 const GURL& url = tab_contents->GetURL();
[email protected]1717246f2010-02-10 17:08:151253
1254 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1255 url.host() == chrome::kChromeUIFileBrowseHost &&
[email protected]f7e9fd62010-09-28 15:45:061256 url.ref() == current_path &&
1257 (*it)->profile() == profile) {
[email protected]1717246f2010-02-10 17:08:151258 return (*it);
1259 }
1260 }
1261 }
1262
1263 return NULL;
1264}
[email protected]467293292010-04-16 19:41:561265
1266const int FileBrowseUI::kPopupWidth = 250;
1267const int FileBrowseUI::kPopupHeight = 300;
1268const int FileBrowseUI::kSmallPopupWidth = 250;
1269const int FileBrowseUI::kSmallPopupHeight = 50;