| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/plugin_download_helper.h" | 5 #include "chrome/browser/plugin_download_helper.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include "base/bind.h" |
| 8 | |
| 9 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/message_loop_proxy.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/common/url_fetcher.h" | 11 #include "content/public/common/url_fetcher.h" |
| 12 #include "net/base/io_buffer.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/url_request/url_request_status.h" |
| 13 | 14 |
| 14 PluginDownloadUrlHelper::PluginDownloadUrlHelper( | 15 using content::BrowserThread; |
| 15 const std::string& download_url, | 16 |
| 16 gfx::NativeWindow caller_window, | 17 PluginDownloadUrlHelper::PluginDownloadUrlHelper() { |
| 17 PluginDownloadUrlHelper::DownloadDelegate* delegate) | |
| 18 : download_file_fetcher_(NULL), | |
| 19 download_file_caller_window_(caller_window), | |
| 20 download_url_(download_url), | |
| 21 delegate_(delegate) { | |
| 22 } | 18 } |
| 23 | 19 |
| 24 PluginDownloadUrlHelper::~PluginDownloadUrlHelper() { | 20 PluginDownloadUrlHelper::~PluginDownloadUrlHelper() { |
| 25 } | 21 } |
| 26 | 22 |
| 27 void PluginDownloadUrlHelper::InitiateDownload( | 23 void PluginDownloadUrlHelper::InitiateDownload( |
| 24 const GURL& download_url, |
| 28 net::URLRequestContextGetter* request_context, | 25 net::URLRequestContextGetter* request_context, |
| 29 base::MessageLoopProxy* file_thread_proxy) { | 26 const DownloadFinishedCallback& callback) { |
| 27 download_url_ = download_url; |
| 28 callback_ = callback; |
| 30 download_file_fetcher_.reset(content::URLFetcher::Create( | 29 download_file_fetcher_.reset(content::URLFetcher::Create( |
| 31 GURL(download_url_), content::URLFetcher::GET, this)); | 30 download_url_, content::URLFetcher::GET, this)); |
| 32 download_file_fetcher_->SetRequestContext(request_context); | 31 download_file_fetcher_->SetRequestContext(request_context); |
| 33 download_file_fetcher_->SaveResponseToTemporaryFile(file_thread_proxy); | 32 download_file_fetcher_->SaveResponseToTemporaryFile( |
| 33 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
| 34 download_file_fetcher_->Start(); | 34 download_file_fetcher_->Start(); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void PluginDownloadUrlHelper::OnURLFetchComplete( | 37 void PluginDownloadUrlHelper::OnURLFetchComplete( |
| 38 const content::URLFetcher* source) { | 38 const content::URLFetcher* source) { |
| 39 bool success = source->GetStatus().is_success(); | 39 net::URLRequestStatus status = source->GetStatus(); |
| 40 FilePath response_file; | 40 if (status.is_success()) { |
| 41 bool success = source->GetResponseAsFilePath(true, &downloaded_file_); |
| 42 DCHECK(success); |
| 43 BrowserThread::PostTaskAndReply( |
| 44 BrowserThread::FILE, FROM_HERE, |
| 45 base::Bind(&PluginDownloadUrlHelper::RenameDownloadedFile, |
| 46 base::Unretained(this)), |
| 47 base::Bind(&PluginDownloadUrlHelper::RunCallback, |
| 48 base::Unretained(this))); |
| 49 } else { |
| 50 NOTREACHED() << "Failed to download the plugin installer: " |
| 51 << net::ErrorToString(status.error()); |
| 52 RunCallback(); |
| 53 } |
| 54 } |
| 41 | 55 |
| 42 if (success) { | 56 void PluginDownloadUrlHelper::RenameDownloadedFile() { |
| 43 if (source->GetResponseAsFilePath(true, &response_file)) { | 57 FilePath new_download_file_path = |
| 44 FilePath new_download_file_path = | 58 downloaded_file_.DirName().AppendASCII( |
| 45 response_file.DirName().AppendASCII( | 59 download_file_fetcher_->GetURL().ExtractFileName()); |
| 46 download_file_fetcher_->GetURL().ExtractFileName()); | |
| 47 | 60 |
| 48 file_util::Delete(new_download_file_path, false); | 61 file_util::Delete(new_download_file_path, false); |
| 49 | 62 |
| 50 if (!file_util::ReplaceFileW(response_file, | 63 if (file_util::ReplaceFile(downloaded_file_, |
| 51 new_download_file_path)) { | 64 new_download_file_path)) { |
| 52 DLOG(ERROR) << "Failed to rename file:" | 65 downloaded_file_ = new_download_file_path; |
| 53 << response_file.value() | 66 } else { |
| 54 << " to file:" | 67 DPLOG(ERROR) << "Failed to rename file: " |
| 55 << new_download_file_path.value(); | 68 << downloaded_file_.value() |
| 56 } else { | 69 << " to file: " |
| 57 response_file = new_download_file_path; | 70 << new_download_file_path.value(); |
| 58 } | |
| 59 } else { | |
| 60 NOTREACHED() << "Failed to download the plugin installer."; | |
| 61 success = false; | |
| 62 } | |
| 63 } | 71 } |
| 72 } |
| 64 | 73 |
| 65 if (delegate_) { | 74 void PluginDownloadUrlHelper::RunCallback() { |
| 66 delegate_->OnDownloadCompleted(response_file, success); | 75 callback_.Run(downloaded_file_); |
| 67 } else { | |
| 68 std::wstring path = response_file.value(); | |
| 69 COPYDATASTRUCT download_file_data = {0}; | |
| 70 download_file_data.cbData = | |
| 71 static_cast<unsigned long>((path.length() + 1) * sizeof(wchar_t)); | |
| 72 download_file_data.lpData = const_cast<wchar_t *>(path.c_str()); | |
| 73 download_file_data.dwData = success; | |
| 74 | |
| 75 if (::IsWindow(download_file_caller_window_)) { | |
| 76 ::SendMessage(download_file_caller_window_, WM_COPYDATA, NULL, | |
| 77 reinterpret_cast<LPARAM>(&download_file_data)); | |
| 78 } | |
| 79 } | |
| 80 // Don't access any members after this. | |
| 81 delete this; | 76 delete this; |
| 82 } | 77 } |
| 83 | |
| OLD | NEW |