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/public/test/download_test_observer.h" | 5 #include "content/public/test/download_test_observer.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 26 matching lines...) Expand all Loading... | |
37 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, | 37 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, |
38 int32 download_id) { | 38 int32 download_id) { |
39 DownloadItem* download = download_manager->GetDownloadItem(download_id); | 39 DownloadItem* download = download_manager->GetDownloadItem(download_id); |
40 ASSERT_TRUE(download->IsPartialDownload()); | 40 ASSERT_TRUE(download->IsPartialDownload()); |
41 download->Cancel(true); | 41 download->Cancel(true); |
42 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | 42 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
43 } | 43 } |
44 | 44 |
45 } // namespace | 45 } // namespace |
46 | 46 |
47 DownloadUpdatedObserver::DownloadUpdatedObserver( | |
48 DownloadItem* item, DownloadUpdatedObserver::EventFilter filter) | |
49 : item_(item), filter_(filter), waiting_(false), event_seen_(false) { | |
50 item->AddObserver(this); | |
51 } | |
52 | |
53 DownloadUpdatedObserver::~DownloadUpdatedObserver() { | |
54 if (item_) | |
55 item_->RemoveObserver(this); | |
56 } | |
57 | |
58 bool DownloadUpdatedObserver::WaitForEvent() { | |
59 if (item_ && filter_.Run(item_)) | |
60 return true; | |
61 | |
62 waiting_ = true; | |
63 RunMessageLoop(); | |
64 waiting_ = false; | |
65 return event_seen_; | |
benjhayden
2012/09/10 17:37:29
Nice!
What if WaitForEvent() is called on the sam
Randy Smith (Not in Mondays)
2012/09/10 18:32:34
I think of this type of class as tracking whether
| |
66 } | |
67 | |
68 void DownloadUpdatedObserver::OnDownloadUpdated(DownloadItem* item) { | |
69 DCHECK_EQ(item_, item); | |
70 if (filter_.Run(item_)) | |
71 event_seen_ = true; | |
72 if (waiting_ && event_seen_) | |
73 MessageLoopForUI::current()->Quit(); | |
74 } | |
75 | |
76 void DownloadUpdatedObserver::OnDownloadDestroyed(DownloadItem* item) { | |
77 DCHECK_EQ(item_, item); | |
78 item_->RemoveObserver(this); | |
79 item_ = NULL; | |
80 if (waiting_) | |
81 MessageLoopForUI::current()->Quit(); | |
82 } | |
83 | |
47 DownloadTestObserver::DownloadTestObserver( | 84 DownloadTestObserver::DownloadTestObserver( |
48 DownloadManager* download_manager, | 85 DownloadManager* download_manager, |
49 size_t wait_count, | 86 size_t wait_count, |
50 DangerousDownloadAction dangerous_download_action) | 87 DangerousDownloadAction dangerous_download_action) |
51 : download_manager_(download_manager), | 88 : download_manager_(download_manager), |
52 wait_count_(wait_count), | 89 wait_count_(wait_count), |
53 finished_downloads_at_construction_(0), | 90 finished_downloads_at_construction_(0), |
54 waiting_(false), | 91 waiting_(false), |
55 dangerous_download_action_(dangerous_download_action) { | 92 dangerous_download_action_(dangerous_download_action) { |
56 } | 93 } |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 MessageLoopForUI::current()->Quit(); | 413 MessageLoopForUI::current()->Quit(); |
377 } | 414 } |
378 | 415 |
379 const DownloadUrlParameters::OnStartedCallback | 416 const DownloadUrlParameters::OnStartedCallback |
380 DownloadTestItemCreationObserver::callback() { | 417 DownloadTestItemCreationObserver::callback() { |
381 return base::Bind( | 418 return base::Bind( |
382 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); | 419 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); |
383 } | 420 } |
384 | 421 |
385 } // namespace content | 422 } // namespace content |
OLD | NEW |