Index: chrome/browser/extensions/extension_downloads.cc |
diff --git a/chrome/browser/extensions/extension_downloads.cc b/chrome/browser/extensions/extension_downloads.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a56629e5ffefd1e73124565f96957ebd7cbff7a4 |
--- /dev/null |
+++ b/chrome/browser/extensions/extension_downloads.cc |
@@ -0,0 +1,241 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/extension_downloads.h" |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "base/bind.h" |
+#include "base/logging.h" |
+#include "base/values.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/download/download_create_info.h" |
+#include "chrome/browser/download/download_file_manager.h" |
+#include "chrome/browser/download/download_item.h" |
+#include "chrome/browser/download/download_manager.h" |
+#include "content/browser/renderer_host/render_view_host.h" |
+#include "content/browser/renderer_host/resource_dispatcher_host.h" |
+#include "chrome/browser/ui/browser_list.h" |
+#include "chrome/browser/download/download_util.h" |
+#include "chrome/browser/icon_loader.h" |
+#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
+#include "chrome/browser/icon_manager.h" |
+ |
+namespace { |
+DictionaryValue* DownloadItemToJSON(DownloadItem* item) { |
+ return NULL; |
+} |
+ |
+void SearchDownloads(DictionaryValue* query, |
+ std::vector<DownloadItem*>* items) { |
+ CHECK(query); |
+ CHECK(items); |
+} |
+} // anonymous namespace |
+ |
+bool DownloadsDownloadFunction::RunImpl() { |
+ products_ = new DictionaryValue(); |
+ result_.reset(products_); |
+ if ((args_.get() == NULL) || |
+ (args_->GetSize() < 1) || |
+ !args_->GetDictionary(0, &options_) || |
+ (options_ == NULL) || |
+ !options_->GetString("url", &url_) || |
+ url_.empty()) return false; |
+ options_->GetString("filename", &filename_); |
+ options_->GetBoolean("save_as", &save_as_); |
+ options_->GetString("method", &method_); |
+ options_->GetDictionary("headers", &extra_headers_); |
+ options_->GetString("body", &post_body_); |
+ // TODO sanity check method_, extra_headers_, filename_ |
+ dl_man_ = profile()->GetDownloadManager(); |
+ rdh_ = g_browser_process->resource_dispatcher_host(); |
+ tab_contents_ = BrowserList::GetLastActive()->GetSelectedTabContentsWrapper() |
+ ->tab_contents(); |
+ resource_context_ = &profile()->GetResourceContext(); |
+ render_process_host_id_ = tab_contents_->GetRenderProcessHost()->id(); |
+ render_view_host_routing_id_ = tab_contents_->render_view_host() |
+ ->routing_id(); |
+ if (rdh_ == NULL) { |
+ products_->Set("error", Value::CreateIntegerValue(-1)); // TODO |
+ return false; |
+ } |
+ dlf_man_ = rdh_->download_file_manager(); |
+ if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod( |
+ this, &DownloadsDownloadFunction::BeginDownloadOnIOThread))) { |
+ products_->Set("error", Value::CreateIntegerValue(-1)); // TODO |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+void DownloadsDownloadFunction::OnUnstartable(int error) { |
+ dl_error_ = error; |
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod( |
+ this, &DownloadsDownloadFunction::RespondOnUIThread)); |
+} |
+ |
+void DownloadsDownloadFunction::BeginDownloadOnIOThread() { |
+ DownloadSaveInfo save_info; |
+ save_info.file_path = FilePath(filename_); |
+ rdh_->BeginDownload( |
+ GURL(url_), |
+ GURL()/*referrer*/, |
+ save_info, |
+ save_as_, |
+ method_, |
+ extra_headers_, |
+ post_body_, |
+ base::Bind(&DownloadsDownloadFunction::OnResponseStarted, this), |
+ base::Bind(&DownloadsDownloadFunction::OnUnstartable, this), |
+ render_process_host_id_, |
+ render_view_host_routing_id_, |
+ *resource_context_); |
+} |
+ |
+void DownloadsDownloadFunction::OnResponseStarted(int dl_id) { |
+ // I wonder what thread this method is called on. |
Sam Kerner (Chrome)
2011/06/21 03:37:40
A DCHECK() that you are on the right thread would
bSh
2011/06/24 19:46:25
Done.
|
+ dl_id_ = dl_id; |
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod( |
+ this, &DownloadsDownloadFunction::RespondOnUIThread)); |
+} |
+ |
+void DownloadsDownloadFunction::RespondOnUIThread() { |
+ VLOG(1) << __FUNCTION__ << " " << url_ << " " << dl_man_ << " " << filename_ |
+ << " " << dl_id_ << " " << dl_error_ << " " << method_; |
+ if (dl_id_ >= 0) { |
+ products_->Set("id", Value::CreateIntegerValue(dl_id_)); |
+ } else { |
+ products_->Set("error", Value::CreateIntegerValue(dl_error_)); |
+ } |
+ SendResponse(true); |
+} |
+ |
+bool DownloadsSearchFunction::RunImpl() { |
+ DictionaryValue* query = NULL; |
+ if ((args_.get() == NULL) || |
+ (args_->GetSize() < 1) || |
+ !args_->GetDictionary(0, &query)) return false; |
+ DownloadManager* dlman = profile()->GetDownloadManager(); |
+ VLOG(1) << __FUNCTION__ << " " << dlman; |
+ std::vector<DownloadItem*> items; |
+ SearchDownloads(query, &items); |
+ ListValue* result = new ListValue(); |
+ result_.reset(result); |
+ for (size_t i = 0; i < items.size(); ++i) { |
+ result->Set(i, DownloadItemToJSON(items[i])); |
+ } |
+ return true; |
+} |
+ |
+bool DownloadsPauseFunction::RunImpl() { |
+ int dl_id = 0; |
+ if ((args_.get() == NULL) || |
+ (args_->GetSize() < 1) || |
+ !args_->GetInteger(0, &dl_id) || |
+ (dl_id == 0)) return false; |
+ DownloadManager* dlman = profile()->GetDownloadManager(); |
+ DownloadItem* item = dlman->GetDownloadItem(dl_id); |
+ if (item == NULL) return false; |
+ VLOG(1) << __FUNCTION__ << " " << item; |
+ return true; |
+} |
+ |
+bool DownloadsResumeFunction::RunImpl() { |
+ int dl_id = 0; |
+ if ((args_.get() == NULL) || |
+ (args_->GetSize() < 1) || |
+ !args_->GetInteger(0, &dl_id) || |
+ (dl_id == 0)) return false; |
+ DownloadManager* dlman = profile()->GetDownloadManager(); |
+ DownloadItem* item = dlman->GetDownloadItem(dl_id); |
+ if (item == NULL) return false; |
+ VLOG(1) << __FUNCTION__ << " " << item; |
+ return true; |
+} |
+ |
+bool DownloadsCancelFunction::RunImpl() { |
+ int dl_id = 0; |
+ if ((args_.get() == NULL) || |
+ (args_->GetSize() < 1) || |
+ !args_->GetInteger(0, &dl_id) || |
+ (dl_id == 0)) return false; |
+ DownloadManager* dlman = profile()->GetDownloadManager(); |
+ DownloadItem* item = dlman->GetDownloadItem(dl_id); |
+ if (item == NULL) return false; |
+ VLOG(1) << __FUNCTION__ << " " << item; |
+ return true; |
+} |
+ |
+bool DownloadsEraseFunction::RunImpl() { |
+ DictionaryValue* query = NULL; |
+ if ((args_.get() == NULL) || |
+ (args_->GetSize() < 1) || |
+ !args_->GetDictionary(0, &query)) return false; |
+ VLOG(1) << __FUNCTION__; |
+ return true; |
+} |
+ |
+bool DownloadsSetDestinationFunction::RunImpl() { |
+ int dl_id = 0; |
+ std::string rel_dest_path; |
+ if ((args_.get() == NULL) || |
Sam Kerner (Chrome)
2011/06/21 03:37:40
The complex guarding if() makes me a bit nervous..
bSh
2011/06/24 19:46:25
Done.
|
+ (args_->GetSize() < 2) || |
+ !args_->GetInteger(0, &dl_id) || |
+ (dl_id == 0) || |
+ !args_->GetString(1, &rel_dest_path) || |
+ rel_dest_path.empty()) return false; |
+ DownloadManager* dlman = profile()->GetDownloadManager(); |
+ DownloadItem* item = dlman->GetDownloadItem(dl_id); |
+ if (item == NULL) return false; |
+ VLOG(1) << __FUNCTION__ << " " << dl_id << " " << item << " " |
+ << rel_dest_path; |
+ return true; |
+} |
+ |
+bool DownloadsAcceptDangerFunction::RunImpl() { |
+ int dl_id = 0; |
+ if ((args_.get() == NULL) || |
+ (args_->GetSize() < 1) || |
+ !args_->GetInteger(0, &dl_id) || |
+ (dl_id == 0)) return false; |
+ DownloadManager* dlman = profile()->GetDownloadManager(); |
+ DownloadItem* item = dlman->GetDownloadItem(dl_id); |
+ if (item == NULL) return false; |
+ VLOG(1) << __FUNCTION__ << " " << item; |
+ return true; |
+} |
+ |
+bool DownloadsShowFunction::RunImpl() { |
+ int dl_id = 0; |
+ if ((args_.get() == NULL) || |
+ (args_->GetSize() < 1) || |
+ !args_->GetInteger(0, &dl_id) || |
+ (dl_id == 0)) return false; |
+ DownloadManager* dlman = profile()->GetDownloadManager(); |
+ DownloadItem* item = dlman->GetDownloadItem(dl_id); |
+ if (item == NULL) return false; |
+ VLOG(1) << __FUNCTION__ << " " << item; |
+ return true; |
+} |
+ |
+bool DownloadsDragFunction::RunImpl() { |
+ int dl_id = 0; |
+ if ((args_.get() == NULL) || |
+ (args_->GetSize() < 1) || |
+ !args_->GetInteger(0, &dl_id) || |
+ (dl_id == 0)) return false; |
+ DownloadManager* dlman = profile()->GetDownloadManager(); |
+ DownloadItem* item = dlman->GetDownloadItem(dl_id); |
+ if (item == NULL) return false; |
+ IconManager* im = g_browser_process->icon_manager(); |
+ gfx::Image* icon = im->LookupIcon(item->GetUserVerifiedFilePath(), |
+ IconLoader::NORMAL); |
+ gfx::NativeView view = BrowserList::GetLastActive() |
+ ->GetSelectedTabContentsWrapper()->tab_contents()->GetNativeView(); |
+ download_util::DragDownload(item, icon, view); |
+ VLOG(1) << __FUNCTION__ << " " << dl_id; |
+ return true; |
+} |