| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/download/drag_download_file.h" | 5 #include "content/browser/download/drag_download_file.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | |
| 15 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 16 #include "content/browser/download/download_stats.h" | 15 #include "content/browser/download/download_stats.h" |
| 17 #include "content/browser/web_contents/web_contents_impl.h" | 16 #include "content/browser/web_contents/web_contents_impl.h" |
| 18 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/download_item.h" | 19 #include "content/public/browser/download_item.h" |
| 21 #include "content/public/browser/download_save_info.h" | 20 #include "content/public/browser/download_save_info.h" |
| 22 #include "content/public/browser/download_url_parameters.h" | 21 #include "content/public/browser/download_url_parameters.h" |
| 23 | 22 |
| 24 namespace content { | 23 namespace content { |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| 28 typedef base::Callback<void(bool)> OnCompleted; | 27 typedef base::Callback<void(bool)> OnCompleted; |
| 29 | 28 |
| 30 } // namespace | 29 } // namespace |
| 31 | 30 |
| 32 // On windows, DragDownloadFile runs on a thread other than the UI thread. | 31 // On windows, DragDownloadFile runs on a thread other than the UI thread. |
| 33 // DownloadItem and DownloadManager may not be accessed on any thread other than | 32 // DownloadItem and DownloadManager may not be accessed on any thread other than |
| 34 // the UI thread. DragDownloadFile may run on either the "drag" thread or the UI | 33 // the UI thread. DragDownloadFile may run on either the "drag" thread or the UI |
| 35 // thread depending on the platform, but DragDownloadFileUI strictly always runs | 34 // thread depending on the platform, but DragDownloadFileUI strictly always runs |
| 36 // on the UI thread. On platforms where DragDownloadFile runs on the UI thread, | 35 // on the UI thread. On platforms where DragDownloadFile runs on the UI thread, |
| 37 // none of the PostTasks are necessary, but it simplifies the code to do them | 36 // none of the PostTasks are necessary, but it simplifies the code to do them |
| 38 // anyway. | 37 // anyway. |
| 39 class DragDownloadFile::DragDownloadFileUI : public DownloadItem::Observer { | 38 class DragDownloadFile::DragDownloadFileUI : public DownloadItem::Observer { |
| 40 public: | 39 public: |
| 41 DragDownloadFileUI( | 40 DragDownloadFileUI(const GURL& url, |
| 42 const GURL& url, | 41 const Referrer& referrer, |
| 43 const Referrer& referrer, | 42 const std::string& referrer_encoding, |
| 44 const std::string& referrer_encoding, | 43 WebContents* web_contents, |
| 45 WebContents* web_contents, | 44 base::MessageLoop* on_completed_loop, |
| 46 scoped_refptr<base::SingleThreadTaskRunner> on_completed_task_runner, | 45 const OnCompleted& on_completed) |
| 47 const OnCompleted& on_completed) | 46 : on_completed_loop_(on_completed_loop), |
| 48 : on_completed_task_runner_(on_completed_task_runner), | |
| 49 on_completed_(on_completed), | 47 on_completed_(on_completed), |
| 50 url_(url), | 48 url_(url), |
| 51 referrer_(referrer), | 49 referrer_(referrer), |
| 52 referrer_encoding_(referrer_encoding), | 50 referrer_encoding_(referrer_encoding), |
| 53 web_contents_(web_contents), | 51 web_contents_(web_contents), |
| 54 download_item_(NULL), | 52 download_item_(NULL), |
| 55 weak_ptr_factory_(this) { | 53 weak_ptr_factory_(this) { |
| 56 DCHECK(on_completed_task_runner_); | 54 DCHECK(on_completed_loop_); |
| 57 DCHECK(!on_completed_.is_null()); | 55 DCHECK(!on_completed_.is_null()); |
| 58 DCHECK(web_contents_); | 56 DCHECK(web_contents_); |
| 59 // May be called on any thread. | 57 // May be called on any thread. |
| 60 // Do not call weak_ptr_factory_.GetWeakPtr() outside the UI thread. | 58 // Do not call weak_ptr_factory_.GetWeakPtr() outside the UI thread. |
| 61 } | 59 } |
| 62 | 60 |
| 63 void InitiateDownload(base::File file, | 61 void InitiateDownload(base::File file, |
| 64 const base::FilePath& file_path) { | 62 const base::FilePath& file_path) { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 66 DownloadManager* download_manager = | 64 DownloadManager* download_manager = |
| (...skipping 30 matching lines...) Expand all Loading... |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 95 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 98 if (download_item_) | 96 if (download_item_) |
| 99 download_item_->RemoveObserver(this); | 97 download_item_->RemoveObserver(this); |
| 100 } | 98 } |
| 101 | 99 |
| 102 void OnDownloadStarted(DownloadItem* item, | 100 void OnDownloadStarted(DownloadItem* item, |
| 103 DownloadInterruptReason interrupt_reason) { | 101 DownloadInterruptReason interrupt_reason) { |
| 104 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 102 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 105 if (!item || item->GetState() != DownloadItem::IN_PROGRESS) { | 103 if (!item || item->GetState() != DownloadItem::IN_PROGRESS) { |
| 106 DCHECK(!item || item->GetLastReason() != DOWNLOAD_INTERRUPT_REASON_NONE); | 104 DCHECK(!item || item->GetLastReason() != DOWNLOAD_INTERRUPT_REASON_NONE); |
| 107 on_completed_task_runner_->PostTask(FROM_HERE, | 105 on_completed_loop_->task_runner()->PostTask( |
| 108 base::Bind(on_completed_, false)); | 106 FROM_HERE, base::Bind(on_completed_, false)); |
| 109 return; | 107 return; |
| 110 } | 108 } |
| 111 DCHECK_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); | 109 DCHECK_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); |
| 112 download_item_ = item; | 110 download_item_ = item; |
| 113 download_item_->AddObserver(this); | 111 download_item_->AddObserver(this); |
| 114 } | 112 } |
| 115 | 113 |
| 116 // DownloadItem::Observer: | 114 // DownloadItem::Observer: |
| 117 void OnDownloadUpdated(DownloadItem* item) override { | 115 void OnDownloadUpdated(DownloadItem* item) override { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 116 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 119 DCHECK_EQ(download_item_, item); | 117 DCHECK_EQ(download_item_, item); |
| 120 DownloadItem::DownloadState state = download_item_->GetState(); | 118 DownloadItem::DownloadState state = download_item_->GetState(); |
| 121 if (state == DownloadItem::COMPLETE || | 119 if (state == DownloadItem::COMPLETE || |
| 122 state == DownloadItem::CANCELLED || | 120 state == DownloadItem::CANCELLED || |
| 123 state == DownloadItem::INTERRUPTED) { | 121 state == DownloadItem::INTERRUPTED) { |
| 124 if (!on_completed_.is_null()) { | 122 if (!on_completed_.is_null()) { |
| 125 on_completed_task_runner_->PostTask( | 123 on_completed_loop_->task_runner()->PostTask( |
| 126 FROM_HERE, | 124 FROM_HERE, |
| 127 base::Bind(on_completed_, state == DownloadItem::COMPLETE)); | 125 base::Bind(on_completed_, state == DownloadItem::COMPLETE)); |
| 128 on_completed_.Reset(); | 126 on_completed_.Reset(); |
| 129 } | 127 } |
| 130 download_item_->RemoveObserver(this); | 128 download_item_->RemoveObserver(this); |
| 131 download_item_ = NULL; | 129 download_item_ = NULL; |
| 132 } | 130 } |
| 133 // Ignore other states. | 131 // Ignore other states. |
| 134 } | 132 } |
| 135 | 133 |
| 136 void OnDownloadDestroyed(DownloadItem* item) override { | 134 void OnDownloadDestroyed(DownloadItem* item) override { |
| 137 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 135 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 138 DCHECK_EQ(download_item_, item); | 136 DCHECK_EQ(download_item_, item); |
| 139 if (!on_completed_.is_null()) { | 137 if (!on_completed_.is_null()) { |
| 140 const bool is_complete = | 138 const bool is_complete = |
| 141 download_item_->GetState() == DownloadItem::COMPLETE; | 139 download_item_->GetState() == DownloadItem::COMPLETE; |
| 142 on_completed_task_runner_->PostTask( | 140 on_completed_loop_->task_runner()->PostTask( |
| 143 FROM_HERE, base::Bind(on_completed_, is_complete)); | 141 FROM_HERE, base::Bind(on_completed_, is_complete)); |
| 144 on_completed_.Reset(); | 142 on_completed_.Reset(); |
| 145 } | 143 } |
| 146 download_item_->RemoveObserver(this); | 144 download_item_->RemoveObserver(this); |
| 147 download_item_ = NULL; | 145 download_item_ = NULL; |
| 148 } | 146 } |
| 149 | 147 |
| 150 scoped_refptr<base::SingleThreadTaskRunner> const on_completed_task_runner_; | 148 base::MessageLoop* on_completed_loop_; |
| 151 OnCompleted on_completed_; | 149 OnCompleted on_completed_; |
| 152 GURL url_; | 150 GURL url_; |
| 153 Referrer referrer_; | 151 Referrer referrer_; |
| 154 std::string referrer_encoding_; | 152 std::string referrer_encoding_; |
| 155 WebContents* web_contents_; | 153 WebContents* web_contents_; |
| 156 DownloadItem* download_item_; | 154 DownloadItem* download_item_; |
| 157 | 155 |
| 158 // Only used in the callback from DownloadManager::DownloadUrl(). | 156 // Only used in the callback from DownloadManager::DownloadUrl(). |
| 159 base::WeakPtrFactory<DragDownloadFileUI> weak_ptr_factory_; | 157 base::WeakPtrFactory<DragDownloadFileUI> weak_ptr_factory_; |
| 160 | 158 |
| 161 DISALLOW_COPY_AND_ASSIGN(DragDownloadFileUI); | 159 DISALLOW_COPY_AND_ASSIGN(DragDownloadFileUI); |
| 162 }; | 160 }; |
| 163 | 161 |
| 164 DragDownloadFile::DragDownloadFile(const base::FilePath& file_path, | 162 DragDownloadFile::DragDownloadFile(const base::FilePath& file_path, |
| 165 base::File file, | 163 base::File file, |
| 166 const GURL& url, | 164 const GURL& url, |
| 167 const Referrer& referrer, | 165 const Referrer& referrer, |
| 168 const std::string& referrer_encoding, | 166 const std::string& referrer_encoding, |
| 169 WebContents* web_contents) | 167 WebContents* web_contents) |
| 170 : file_path_(file_path), | 168 : file_path_(file_path), |
| 171 file_(std::move(file)), | 169 file_(std::move(file)), |
| 172 drag_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 170 drag_message_loop_(base::MessageLoop::current()), |
| 173 state_(INITIALIZED), | 171 state_(INITIALIZED), |
| 174 drag_ui_(NULL), | 172 drag_ui_(NULL), |
| 175 weak_ptr_factory_(this) { | 173 weak_ptr_factory_(this) { |
| 176 drag_ui_ = new DragDownloadFileUI( | 174 drag_ui_ = new DragDownloadFileUI( |
| 177 url, referrer, referrer_encoding, web_contents, drag_task_runner_, | 175 url, |
| 176 referrer, |
| 177 referrer_encoding, |
| 178 web_contents, |
| 179 drag_message_loop_, |
| 178 base::Bind(&DragDownloadFile::DownloadCompleted, | 180 base::Bind(&DragDownloadFile::DownloadCompleted, |
| 179 weak_ptr_factory_.GetWeakPtr())); | 181 weak_ptr_factory_.GetWeakPtr())); |
| 180 DCHECK(!file_path_.empty()); | 182 DCHECK(!file_path_.empty()); |
| 181 } | 183 } |
| 182 | 184 |
| 183 DragDownloadFile::~DragDownloadFile() { | 185 DragDownloadFile::~DragDownloadFile() { |
| 184 CheckThread(); | 186 CheckThread(); |
| 185 | 187 |
| 186 // This is the only place that drag_ui_ can be deleted from. Post a message to | 188 // This is the only place that drag_ui_ can be deleted from. Post a message to |
| 187 // the UI thread so that it calls RemoveObserver on the right thread, and so | 189 // the UI thread so that it calls RemoveObserver on the right thread, and so |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 | 237 |
| 236 // Release the observer since we do not need it any more. | 238 // Release the observer since we do not need it any more. |
| 237 observer_ = NULL; | 239 observer_ = NULL; |
| 238 | 240 |
| 239 if (nested_loop_.running()) | 241 if (nested_loop_.running()) |
| 240 nested_loop_.Quit(); | 242 nested_loop_.Quit(); |
| 241 } | 243 } |
| 242 | 244 |
| 243 void DragDownloadFile::CheckThread() { | 245 void DragDownloadFile::CheckThread() { |
| 244 #if defined(OS_WIN) | 246 #if defined(OS_WIN) |
| 245 DCHECK(drag_task_runner_->BelongsToCurrentThread()); | 247 DCHECK(drag_message_loop_ == base::MessageLoop::current()); |
| 246 #else | 248 #else |
| 247 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 249 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 248 #endif | 250 #endif |
| 249 } | 251 } |
| 250 | 252 |
| 251 } // namespace content | 253 } // namespace content |
| OLD | NEW |