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