OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/extensions/extension_downloads.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/stl_util-inl.h" |
| 14 #include "base/values.h" |
| 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/download/download_create_info.h" |
| 17 #include "chrome/browser/download/download_file_manager.h" |
| 18 #include "chrome/browser/download/download_item.h" |
| 19 #include "chrome/browser/download/download_query.h" |
| 20 #include "chrome/browser/download/download_manager.h" |
| 21 #include "content/browser/renderer_host/render_view_host.h" |
| 22 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 23 #include "chrome/browser/ui/browser_list.h" |
| 24 #include "chrome/browser/icon_loader.h" |
| 25 #include "chrome/browser/download/download_util.h" |
| 26 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 27 #include "chrome/browser/icon_manager.h" |
| 28 |
| 29 bool DownloadsDownloadFunction::RunImpl() { |
| 30 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options_)); |
| 31 EXTENSION_FUNCTION_VALIDATE(options_->GetString("url", &url_)); |
| 32 if (url_.empty()) { |
| 33 error_ = "'url' cannot be empty."; |
| 34 return false; |
| 35 } |
| 36 rdh_ = g_browser_process->resource_dispatcher_host(); |
| 37 if (rdh_ == NULL) { |
| 38 error_ = "I'm afraid I can't do that."; |
| 39 return false; |
| 40 } |
| 41 VLOG(1) << __FUNCTION__ << " " << url_; |
| 42 products_ = new DictionaryValue(); |
| 43 result_.reset(products_); |
| 44 options_->GetString("filename", &filename_); |
| 45 options_->GetBoolean("save_as", &save_as_); |
| 46 options_->GetString("method", &method_); |
| 47 options_->GetDictionary("headers", &extra_headers_); |
| 48 options_->GetString("body", &post_body_); |
| 49 // TODO sanity check method_, extra_headers_, filename_ |
| 50 dl_man_ = profile()->GetDownloadManager(); |
| 51 tab_contents_ = BrowserList::GetLastActive()->GetSelectedTabContentsWrapper() |
| 52 ->tab_contents(); |
| 53 resource_context_ = &profile()->GetResourceContext(); |
| 54 render_process_host_id_ = tab_contents_->GetRenderProcessHost()->id(); |
| 55 render_view_host_routing_id_ = tab_contents_->render_view_host() |
| 56 ->routing_id(); |
| 57 VLOG(1) << __FUNCTION__ << " " << url_; |
| 58 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod( |
| 59 this, &DownloadsDownloadFunction::BeginDownloadOnIOThread))) { |
| 60 error_ = "I'm afraid I can't do that."; |
| 61 return false; |
| 62 } |
| 63 return true; |
| 64 } |
| 65 |
| 66 void DownloadsDownloadFunction::BeginDownloadOnIOThread() { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 68 VLOG(1) << __FUNCTION__ << " " << url_; |
| 69 DownloadSaveInfo save_info; |
| 70 save_info.file_path = FilePath(filename_); |
| 71 net::URLRequest* request = new net::URLRequest(GURL(url_), rdh_); |
| 72 if (method_.empty()) { |
| 73 method_ = "GET"; |
| 74 } |
| 75 request->set_method(method_); |
| 76 if (extra_headers_ != NULL) { |
| 77 DictionaryValue::key_iterator headers_end = extra_headers_->end_keys(); |
| 78 for (DictionaryValue::key_iterator headers_iter = |
| 79 extra_headers_->begin_keys(); |
| 80 headers_iter != headers_end; ++headers_iter) { |
| 81 std::string value; |
| 82 if (extra_headers_->GetStringWithoutPathExpansion( |
| 83 *headers_iter, &value)) { |
| 84 request->SetExtraRequestHeaderByName( |
| 85 *headers_iter, value, false/*overwrite*/); |
| 86 } |
| 87 } |
| 88 } |
| 89 if (!post_body_.empty()) { |
| 90 request->AppendBytesToUpload(post_body_.data(), post_body_.size()); |
| 91 } |
| 92 rdh_->BeginDownload( |
| 93 request, |
| 94 save_info, |
| 95 save_as_, |
| 96 base::Bind(&DownloadsDownloadFunction::OnStarted, this), |
| 97 render_process_host_id_, |
| 98 render_view_host_routing_id_, |
| 99 *resource_context_); |
| 100 } |
| 101 |
| 102 void DownloadsDownloadFunction::OnStarted(int dl_id, int error) { |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 104 VLOG(1) << __FUNCTION__ << " " << url_ << " " << dl_id << " " << error; |
| 105 dl_id_ = dl_id; |
| 106 dl_error_ = error; |
| 107 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod( |
| 108 this, &DownloadsDownloadFunction::RespondOnUIThread)); |
| 109 } |
| 110 |
| 111 void DownloadsDownloadFunction::RespondOnUIThread() { |
| 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 113 VLOG(1) << __FUNCTION__ << " " << url_ << " " << dl_man_ << " " << filename_ |
| 114 << " " << dl_id_ << " " << dl_error_ << " " << method_; |
| 115 if (dl_id_ >= 0) { |
| 116 products_->Set("id", Value::CreateIntegerValue(dl_id_)); |
| 117 } else { |
| 118 products_->Set("error", Value::CreateIntegerValue(dl_error_)); |
| 119 } |
| 120 SendResponse(true); |
| 121 } |
| 122 |
| 123 bool DownloadsSearchFunction::RunImpl() { |
| 124 DictionaryValue* query_json = NULL; |
| 125 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); |
| 126 CHECK(query_json); |
| 127 download_util::DownloadQuery query(*query_json); |
| 128 DownloadManager* dlman = profile()->GetDownloadManager(); |
| 129 if (dlman == NULL) { |
| 130 error_ = "I'm afraid I can't do that."; |
| 131 return false; |
| 132 } |
| 133 ListValue* results = new ListValue(); |
| 134 if (!dlman->Search( |
| 135 download_util::DownloadQuery(*query_json), |
| 136 NULL/*DownloadItem results*/, |
| 137 &error_, |
| 138 results)) { |
| 139 VLOG(1) << __PRETTY_FUNCTION__ << " " << error_; |
| 140 return false; |
| 141 } |
| 142 result_.reset(results); |
| 143 return true; |
| 144 } |
| 145 |
| 146 bool DownloadsPauseFunction::RunImpl() { |
| 147 int dl_id = 0; |
| 148 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); |
| 149 DownloadManager* dlman = profile()->GetDownloadManager(); |
| 150 if (dlman == NULL) { |
| 151 error_ = "I'm afraid I can't do that."; |
| 152 return false; |
| 153 } |
| 154 DownloadItem* item = dlman->GetDownloadItem(dl_id); |
| 155 if (item == NULL) { |
| 156 error_ = "Non-existent download"; |
| 157 return false; |
| 158 } |
| 159 VLOG(1) << __FUNCTION__ << " " << item; |
| 160 if (!item->is_paused()) { |
| 161 item->TogglePause(); |
| 162 } |
| 163 return true; |
| 164 } |
| 165 |
| 166 bool DownloadsResumeFunction::RunImpl() { |
| 167 int dl_id = 0; |
| 168 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); |
| 169 DownloadManager* dlman = profile()->GetDownloadManager(); |
| 170 if (dlman == NULL) { |
| 171 error_ = "I'm afraid I can't do that."; |
| 172 return false; |
| 173 } |
| 174 DownloadItem* item = dlman->GetDownloadItem(dl_id); |
| 175 if (item == NULL) { |
| 176 error_ = "Non-existent download"; |
| 177 return false; |
| 178 } |
| 179 VLOG(1) << __FUNCTION__ << " " << item; |
| 180 if (item->is_paused()) { |
| 181 item->TogglePause(); |
| 182 } |
| 183 return true; |
| 184 } |
| 185 |
| 186 bool DownloadsCancelFunction::RunImpl() { |
| 187 int dl_id = 0; |
| 188 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); |
| 189 DownloadManager* dlman = profile()->GetDownloadManager(); |
| 190 if (dlman == NULL) { |
| 191 error_ = "I'm afraid I can't do that."; |
| 192 return false; |
| 193 } |
| 194 DownloadItem* item = dlman->GetDownloadItem(dl_id); |
| 195 if (item == NULL) { |
| 196 error_ = "Non-existent download"; |
| 197 return false; |
| 198 } |
| 199 VLOG(1) << __FUNCTION__ << " " << item; |
| 200 item->Cancel(true); |
| 201 return true; |
| 202 } |
| 203 |
| 204 bool DownloadsEraseFunction::RunImpl() { |
| 205 return true; |
| 206 } |
| 207 |
| 208 bool DownloadsSetDestinationFunction::RunImpl() { |
| 209 int dl_id = 0; |
| 210 std::string rel_dest_path; |
| 211 if ((args_.get() == NULL) || |
| 212 (args_->GetSize() < 2) || |
| 213 !args_->GetInteger(0, &dl_id) || |
| 214 (dl_id == 0) || |
| 215 !args_->GetString(1, &rel_dest_path) || |
| 216 rel_dest_path.empty()) return false; |
| 217 DownloadManager* dlman = profile()->GetDownloadManager(); |
| 218 DownloadItem* item = dlman->GetDownloadItem(dl_id); |
| 219 if (item == NULL) return false; |
| 220 VLOG(1) << __FUNCTION__ << " " << dl_id << " " << item << " " |
| 221 << rel_dest_path; |
| 222 return true; |
| 223 } |
| 224 |
| 225 bool DownloadsAcceptDangerFunction::RunImpl() { |
| 226 int dl_id = 0; |
| 227 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); |
| 228 DownloadManager* dlman = profile()->GetDownloadManager(); |
| 229 if (dlman == NULL) { |
| 230 error_ = "I'm afraid I can't do that."; |
| 231 return false; |
| 232 } |
| 233 DownloadItem* item = dlman->GetDownloadItem(dl_id); |
| 234 if (item == NULL) { |
| 235 error_ = "Non-existent download"; |
| 236 return false; |
| 237 } |
| 238 VLOG(1) << __FUNCTION__ << " " << item; |
| 239 item->Cancel(true); |
| 240 return true; |
| 241 } |
| 242 |
| 243 bool DownloadsShowFunction::RunImpl() { |
| 244 int dl_id = 0; |
| 245 if ((args_.get() == NULL) || |
| 246 (args_->GetSize() < 1) || |
| 247 !args_->GetInteger(0, &dl_id) || |
| 248 (dl_id == 0)) return false; |
| 249 DownloadManager* dlman = profile()->GetDownloadManager(); |
| 250 DownloadItem* item = dlman->GetDownloadItem(dl_id); |
| 251 if (item == NULL) return false; |
| 252 VLOG(1) << __FUNCTION__ << " " << item; |
| 253 return true; |
| 254 } |
| 255 |
| 256 bool DownloadsDragFunction::RunImpl() { |
| 257 int dl_id = 0; |
| 258 if ((args_.get() == NULL) || |
| 259 (args_->GetSize() < 1) || |
| 260 !args_->GetInteger(0, &dl_id) || |
| 261 (dl_id == 0)) return false; |
| 262 DownloadManager* dlman = profile()->GetDownloadManager(); |
| 263 DownloadItem* item = dlman->GetDownloadItem(dl_id); |
| 264 if (item == NULL) return false; |
| 265 IconManager* im = g_browser_process->icon_manager(); |
| 266 gfx::Image* icon = im->LookupIcon(item->GetUserVerifiedFilePath(), |
| 267 IconLoader::NORMAL); |
| 268 gfx::NativeView view = BrowserList::GetLastActive() |
| 269 ->GetSelectedTabContentsWrapper()->tab_contents()->GetNativeView(); |
| 270 download_util::DragDownload(item, icon, view); |
| 271 VLOG(1) << __FUNCTION__ << " " << dl_id; |
| 272 return true; |
| 273 } |
OLD | NEW |