Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(306)

Side by Side Diff: content/browser/download/drag_download_file.cc

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

Powered by Google App Engine
This is Rietveld 408576698