| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 6 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
| 7 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" | 7 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" |
| 8 #include "chrome/browser/extensions/api/image_writer_private/write_from_url_oper
ation.h" | 8 #include "chrome/browser/extensions/api/image_writer_private/write_from_url_oper
ation.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "net/url_request/url_fetcher.h" | 10 #include "net/url_request/url_fetcher.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 : Operation(manager, extension_id, device_path), | 24 : Operation(manager, extension_id, device_path), |
| 25 request_context_(request_context), | 25 request_context_(request_context), |
| 26 url_(url), | 26 url_(url), |
| 27 hash_(hash), | 27 hash_(hash), |
| 28 download_continuation_() {} | 28 download_continuation_() {} |
| 29 | 29 |
| 30 WriteFromUrlOperation::~WriteFromUrlOperation() { | 30 WriteFromUrlOperation::~WriteFromUrlOperation() { |
| 31 } | 31 } |
| 32 | 32 |
| 33 void WriteFromUrlOperation::StartImpl() { | 33 void WriteFromUrlOperation::StartImpl() { |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 34 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 35 | 35 |
| 36 GetDownloadTarget(base::Bind( | 36 GetDownloadTarget(base::Bind( |
| 37 &WriteFromUrlOperation::Download, | 37 &WriteFromUrlOperation::Download, |
| 38 this, | 38 this, |
| 39 base::Bind( | 39 base::Bind( |
| 40 &WriteFromUrlOperation::VerifyDownload, | 40 &WriteFromUrlOperation::VerifyDownload, |
| 41 this, | 41 this, |
| 42 base::Bind( | 42 base::Bind( |
| 43 &WriteFromUrlOperation::Unzip, | 43 &WriteFromUrlOperation::Unzip, |
| 44 this, | 44 this, |
| 45 base::Bind(&WriteFromUrlOperation::Write, | 45 base::Bind(&WriteFromUrlOperation::Write, |
| 46 this, | 46 this, |
| 47 base::Bind(&WriteFromUrlOperation::VerifyWrite, | 47 base::Bind(&WriteFromUrlOperation::VerifyWrite, |
| 48 this, | 48 this, |
| 49 base::Bind(&WriteFromUrlOperation::Finish, | 49 base::Bind(&WriteFromUrlOperation::Finish, |
| 50 this))))))); | 50 this))))))); |
| 51 } | 51 } |
| 52 | 52 |
| 53 void WriteFromUrlOperation::GetDownloadTarget( | 53 void WriteFromUrlOperation::GetDownloadTarget( |
| 54 const base::Closure& continuation) { | 54 const base::Closure& continuation) { |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 55 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 56 if (IsCancelled()) { | 56 if (IsCancelled()) { |
| 57 return; | 57 return; |
| 58 } | 58 } |
| 59 | 59 |
| 60 if (url_.ExtractFileName() == "") { | 60 if (url_.ExtractFileName() == "") { |
| 61 if (!base::CreateTemporaryFileInDir(temp_dir_.path(), &image_path_)) { | 61 if (!base::CreateTemporaryFileInDir(temp_dir_.path(), &image_path_)) { |
| 62 Error(error::kTempFileError); | 62 Error(error::kTempFileError); |
| 63 return; | 63 return; |
| 64 } | 64 } |
| 65 } else { | 65 } else { |
| 66 base::FilePath file_name = | 66 base::FilePath file_name = |
| 67 base::FilePath::FromUTF8Unsafe(url_.ExtractFileName()); | 67 base::FilePath::FromUTF8Unsafe(url_.ExtractFileName()); |
| 68 image_path_ = temp_dir_.path().Append(file_name); | 68 image_path_ = temp_dir_.path().Append(file_name); |
| 69 } | 69 } |
| 70 | 70 |
| 71 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); | 71 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void WriteFromUrlOperation::Download(const base::Closure& continuation) { | 74 void WriteFromUrlOperation::Download(const base::Closure& continuation) { |
| 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 75 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 76 | 76 |
| 77 if (IsCancelled()) { | 77 if (IsCancelled()) { |
| 78 return; | 78 return; |
| 79 } | 79 } |
| 80 | 80 |
| 81 download_continuation_ = continuation; | 81 download_continuation_ = continuation; |
| 82 | 82 |
| 83 SetStage(image_writer_api::STAGE_DOWNLOAD); | 83 SetStage(image_writer_api::STAGE_DOWNLOAD); |
| 84 | 84 |
| 85 // Store the URL fetcher on this object so that it is destroyed before this | 85 // Store the URL fetcher on this object so that it is destroyed before this |
| (...skipping 17 matching lines...) Expand all Loading... |
| 103 const net::URLFetcher* source, | 103 const net::URLFetcher* source, |
| 104 int64 current, | 104 int64 current, |
| 105 int64 total) { | 105 int64 total) { |
| 106 // No-op | 106 // No-op |
| 107 } | 107 } |
| 108 | 108 |
| 109 void WriteFromUrlOperation::OnURLFetchDownloadProgress( | 109 void WriteFromUrlOperation::OnURLFetchDownloadProgress( |
| 110 const net::URLFetcher* source, | 110 const net::URLFetcher* source, |
| 111 int64 current, | 111 int64 current, |
| 112 int64 total) { | 112 int64 total) { |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 113 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 114 | 114 |
| 115 if (IsCancelled()) { | 115 if (IsCancelled()) { |
| 116 url_fetcher_.reset(NULL); | 116 url_fetcher_.reset(NULL); |
| 117 } | 117 } |
| 118 | 118 |
| 119 int progress = (kProgressComplete * current) / total; | 119 int progress = (kProgressComplete * current) / total; |
| 120 | 120 |
| 121 SetProgress(progress); | 121 SetProgress(progress); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void WriteFromUrlOperation::OnURLFetchComplete(const net::URLFetcher* source) { | 124 void WriteFromUrlOperation::OnURLFetchComplete(const net::URLFetcher* source) { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 125 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 126 | 126 |
| 127 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { | 127 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { |
| 128 SetProgress(kProgressComplete); | 128 SetProgress(kProgressComplete); |
| 129 | 129 |
| 130 download_continuation_.Run(); | 130 download_continuation_.Run(); |
| 131 | 131 |
| 132 // Remove the reference to ourselves in this closure. | 132 // Remove the reference to ourselves in this closure. |
| 133 download_continuation_ = base::Closure(); | 133 download_continuation_ = base::Closure(); |
| 134 } else { | 134 } else { |
| 135 Error(error::kDownloadInterrupted); | 135 Error(error::kDownloadInterrupted); |
| 136 } | 136 } |
| 137 } | 137 } |
| 138 | 138 |
| 139 void WriteFromUrlOperation::VerifyDownload(const base::Closure& continuation) { | 139 void WriteFromUrlOperation::VerifyDownload(const base::Closure& continuation) { |
| 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 140 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 141 | 141 |
| 142 if (IsCancelled()) { | 142 if (IsCancelled()) { |
| 143 return; | 143 return; |
| 144 } | 144 } |
| 145 | 145 |
| 146 // Skip verify if no hash. | 146 // Skip verify if no hash. |
| 147 if (hash_.empty()) { | 147 if (hash_.empty()) { |
| 148 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); | 148 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); |
| 149 return; | 149 return; |
| 150 } | 150 } |
| 151 | 151 |
| 152 SetStage(image_writer_api::STAGE_VERIFYDOWNLOAD); | 152 SetStage(image_writer_api::STAGE_VERIFYDOWNLOAD); |
| 153 | 153 |
| 154 GetMD5SumOfFile( | 154 GetMD5SumOfFile( |
| 155 image_path_, | 155 image_path_, |
| 156 0, | 156 0, |
| 157 0, | 157 0, |
| 158 kProgressComplete, | 158 kProgressComplete, |
| 159 base::Bind( | 159 base::Bind( |
| 160 &WriteFromUrlOperation::VerifyDownloadCompare, this, continuation)); | 160 &WriteFromUrlOperation::VerifyDownloadCompare, this, continuation)); |
| 161 } | 161 } |
| 162 | 162 |
| 163 void WriteFromUrlOperation::VerifyDownloadCompare( | 163 void WriteFromUrlOperation::VerifyDownloadCompare( |
| 164 const base::Closure& continuation, | 164 const base::Closure& continuation, |
| 165 const std::string& download_hash) { | 165 const std::string& download_hash) { |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 166 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 167 if (download_hash != hash_) { | 167 if (download_hash != hash_) { |
| 168 Error(error::kDownloadHashError); | 168 Error(error::kDownloadHashError); |
| 169 return; | 169 return; |
| 170 } | 170 } |
| 171 | 171 |
| 172 BrowserThread::PostTask( | 172 BrowserThread::PostTask( |
| 173 BrowserThread::FILE, | 173 BrowserThread::FILE, |
| 174 FROM_HERE, | 174 FROM_HERE, |
| 175 base::Bind( | 175 base::Bind( |
| 176 &WriteFromUrlOperation::VerifyDownloadComplete, this, continuation)); | 176 &WriteFromUrlOperation::VerifyDownloadComplete, this, continuation)); |
| 177 } | 177 } |
| 178 | 178 |
| 179 void WriteFromUrlOperation::VerifyDownloadComplete( | 179 void WriteFromUrlOperation::VerifyDownloadComplete( |
| 180 const base::Closure& continuation) { | 180 const base::Closure& continuation) { |
| 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 181 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 182 if (IsCancelled()) { | 182 if (IsCancelled()) { |
| 183 return; | 183 return; |
| 184 } | 184 } |
| 185 | 185 |
| 186 SetProgress(kProgressComplete); | 186 SetProgress(kProgressComplete); |
| 187 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); | 187 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); |
| 188 } | 188 } |
| 189 | 189 |
| 190 } // namespace image_writer | 190 } // namespace image_writer |
| 191 } // namespace extensions | 191 } // namespace extensions |
| OLD | NEW |