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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 62 |
63 // Waits for an item record in the downloads database to match |filter|. See | 63 // Waits for an item record in the downloads database to match |filter|. See |
64 // DownloadStoredProperly() below for an example filter. | 64 // DownloadStoredProperly() below for an example filter. |
65 class DownloadPersistedObserver : public DownloadHistory::Observer { | 65 class DownloadPersistedObserver : public DownloadHistory::Observer { |
66 public: | 66 public: |
67 typedef base::Callback<bool( | 67 typedef base::Callback<bool( |
68 DownloadItem* item, | 68 DownloadItem* item, |
69 const history::DownloadRow&)> PersistedFilter; | 69 const history::DownloadRow&)> PersistedFilter; |
70 | 70 |
71 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) | 71 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) |
72 : profile_(profile), | 72 : profile_(profile), |
73 filter_(filter), | 73 filter_(filter), |
74 waiting_(false), | 74 persisted_(false) { |
75 persisted_(false) { | |
76 DownloadServiceFactory::GetForBrowserContext(profile_)-> | 75 DownloadServiceFactory::GetForBrowserContext(profile_)-> |
77 GetDownloadHistory()->AddObserver(this); | 76 GetDownloadHistory()->AddObserver(this); |
78 } | 77 } |
79 | 78 |
80 ~DownloadPersistedObserver() override { | 79 ~DownloadPersistedObserver() override { |
81 DownloadService* service = DownloadServiceFactory::GetForBrowserContext( | 80 DownloadService* service = DownloadServiceFactory::GetForBrowserContext( |
82 profile_); | 81 profile_); |
83 if (service && service->GetDownloadHistory()) | 82 if (service && service->GetDownloadHistory()) |
84 service->GetDownloadHistory()->RemoveObserver(this); | 83 service->GetDownloadHistory()->RemoveObserver(this); |
85 } | 84 } |
86 | 85 |
87 bool WaitForPersisted() { | 86 bool WaitForPersisted() { |
88 if (persisted_) | 87 if (persisted_) |
89 return true; | 88 return true; |
90 waiting_ = true; | 89 base::RunLoop run_loop; |
91 content::RunMessageLoop(); | 90 quit_waiting_callback_ = run_loop.QuitClosure(); |
92 waiting_ = false; | 91 run_loop.Run(); |
| 92 quit_waiting_callback_ = base::Closure(); |
93 return persisted_; | 93 return persisted_; |
94 } | 94 } |
95 | 95 |
96 void OnDownloadStored(DownloadItem* item, | 96 void OnDownloadStored(DownloadItem* item, |
97 const history::DownloadRow& info) override { | 97 const history::DownloadRow& info) override { |
98 persisted_ = persisted_ || filter_.Run(item, info); | 98 persisted_ = persisted_ || filter_.Run(item, info); |
99 if (persisted_ && waiting_) | 99 if (persisted_ && !quit_waiting_callback_.is_null()) |
100 base::MessageLoopForUI::current()->QuitWhenIdle(); | 100 quit_waiting_callback_.Run(); |
101 } | 101 } |
102 | 102 |
103 private: | 103 private: |
104 Profile* profile_; | 104 Profile* profile_; |
105 PersistedFilter filter_; | 105 PersistedFilter filter_; |
106 bool waiting_; | 106 base::Closure quit_waiting_callback_; |
107 bool persisted_; | 107 bool persisted_; |
108 | 108 |
109 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); | 109 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); |
110 }; | 110 }; |
111 | 111 |
112 // Waits for an item record to be removed from the downloads database. | 112 // Waits for an item record to be removed from the downloads database. |
113 class DownloadRemovedObserver : public DownloadPersistedObserver { | 113 class DownloadRemovedObserver : public DownloadPersistedObserver { |
114 public: | 114 public: |
115 DownloadRemovedObserver(Profile* profile, int32 download_id) | 115 DownloadRemovedObserver(Profile* profile, int32 download_id) |
116 : DownloadPersistedObserver(profile, PersistedFilter()), | 116 : DownloadPersistedObserver(profile, PersistedFilter()), |
117 removed_(false), | 117 removed_(false), |
118 waiting_(false), | 118 download_id_(download_id) {} |
119 download_id_(download_id) { | |
120 } | |
121 ~DownloadRemovedObserver() override {} | 119 ~DownloadRemovedObserver() override {} |
122 | 120 |
123 bool WaitForRemoved() { | 121 bool WaitForRemoved() { |
124 if (removed_) | 122 if (removed_) |
125 return true; | 123 return true; |
126 waiting_ = true; | 124 base::RunLoop run_loop; |
127 content::RunMessageLoop(); | 125 quit_waiting_callback_ = run_loop.QuitClosure(); |
128 waiting_ = false; | 126 run_loop.Run(); |
| 127 quit_waiting_callback_ = base::Closure(); |
129 return removed_; | 128 return removed_; |
130 } | 129 } |
131 | 130 |
132 void OnDownloadStored(DownloadItem* item, | 131 void OnDownloadStored(DownloadItem* item, |
133 const history::DownloadRow& info) override {} | 132 const history::DownloadRow& info) override {} |
134 | 133 |
135 void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) override { | 134 void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) override { |
136 removed_ = ids.find(download_id_) != ids.end(); | 135 removed_ = ids.find(download_id_) != ids.end(); |
137 if (removed_ && waiting_) | 136 if (removed_ && !quit_waiting_callback_.is_null()) |
138 base::MessageLoopForUI::current()->QuitWhenIdle(); | 137 quit_waiting_callback_.Run(); |
139 } | 138 } |
140 | 139 |
141 private: | 140 private: |
142 bool removed_; | 141 bool removed_; |
143 bool waiting_; | 142 base::Closure quit_waiting_callback_; |
144 int32 download_id_; | 143 int32 download_id_; |
145 | 144 |
146 DISALLOW_COPY_AND_ASSIGN(DownloadRemovedObserver); | 145 DISALLOW_COPY_AND_ASSIGN(DownloadRemovedObserver); |
147 }; | 146 }; |
148 | 147 |
149 bool DownloadStoredProperly( | 148 bool DownloadStoredProperly( |
150 const GURL& expected_url, | 149 const GURL& expected_url, |
151 const base::FilePath& expected_path, | 150 const base::FilePath& expected_path, |
152 int64 num_files, | 151 int64 num_files, |
153 history::DownloadState expected_state, | 152 history::DownloadState expected_state, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 } | 184 } |
186 | 185 |
187 const base::FilePath::CharType kTestDir[] = FILE_PATH_LITERAL("save_page"); | 186 const base::FilePath::CharType kTestDir[] = FILE_PATH_LITERAL("save_page"); |
188 | 187 |
189 static const char kAppendedExtension[] = ".html"; | 188 static const char kAppendedExtension[] = ".html"; |
190 | 189 |
191 // Loosely based on logic in DownloadTestObserver. | 190 // Loosely based on logic in DownloadTestObserver. |
192 class DownloadItemCreatedObserver : public DownloadManager::Observer { | 191 class DownloadItemCreatedObserver : public DownloadManager::Observer { |
193 public: | 192 public: |
194 explicit DownloadItemCreatedObserver(DownloadManager* manager) | 193 explicit DownloadItemCreatedObserver(DownloadManager* manager) |
195 : waiting_(false), manager_(manager) { | 194 : manager_(manager) { |
196 manager->AddObserver(this); | 195 manager->AddObserver(this); |
197 } | 196 } |
198 | 197 |
199 ~DownloadItemCreatedObserver() override { | 198 ~DownloadItemCreatedObserver() override { |
200 if (manager_) | 199 if (manager_) |
201 manager_->RemoveObserver(this); | 200 manager_->RemoveObserver(this); |
202 } | 201 } |
203 | 202 |
204 // Wait for the first download item created after object creation. | 203 // Wait for the first download item created after object creation. |
205 // Note that this class provides no protection against the download | 204 // Note that this class provides no protection against the download |
206 // being destroyed between creation and return of WaitForNewDownloadItem(); | 205 // being destroyed between creation and return of WaitForNewDownloadItem(); |
207 // the caller must guarantee that in some other fashion. | 206 // the caller must guarantee that in some other fashion. |
208 void WaitForDownloadItem(std::vector<DownloadItem*>* items_seen) { | 207 void WaitForDownloadItem(std::vector<DownloadItem*>* items_seen) { |
209 if (!manager_) { | 208 if (!manager_) { |
210 // The manager went away before we were asked to wait; return | 209 // The manager went away before we were asked to wait; return |
211 // what we have, even if it's null. | 210 // what we have, even if it's null. |
212 *items_seen = items_seen_; | 211 *items_seen = items_seen_; |
213 return; | 212 return; |
214 } | 213 } |
215 | 214 |
216 if (items_seen_.empty()) { | 215 if (items_seen_.empty()) { |
217 waiting_ = true; | 216 base::RunLoop run_loop; |
218 content::RunMessageLoop(); | 217 quit_waiting_callback_ = run_loop.QuitClosure(); |
219 waiting_ = false; | 218 run_loop.Run(); |
| 219 quit_waiting_callback_ = base::Closure(); |
220 } | 220 } |
221 | 221 |
222 *items_seen = items_seen_; | 222 *items_seen = items_seen_; |
223 return; | 223 return; |
224 } | 224 } |
225 | 225 |
226 private: | 226 private: |
227 // DownloadManager::Observer | 227 // DownloadManager::Observer |
228 void OnDownloadCreated(DownloadManager* manager, | 228 void OnDownloadCreated(DownloadManager* manager, |
229 DownloadItem* item) override { | 229 DownloadItem* item) override { |
230 DCHECK_EQ(manager, manager_); | 230 DCHECK_EQ(manager, manager_); |
231 items_seen_.push_back(item); | 231 items_seen_.push_back(item); |
232 | 232 |
233 if (waiting_) | 233 if (!quit_waiting_callback_.is_null()) |
234 base::MessageLoopForUI::current()->QuitWhenIdle(); | 234 quit_waiting_callback_.Run(); |
235 } | 235 } |
236 | 236 |
237 void ManagerGoingDown(DownloadManager* manager) override { | 237 void ManagerGoingDown(DownloadManager* manager) override { |
238 manager_->RemoveObserver(this); | 238 manager_->RemoveObserver(this); |
239 manager_ = NULL; | 239 manager_ = NULL; |
240 if (waiting_) | 240 if (!quit_waiting_callback_.is_null()) |
241 base::MessageLoopForUI::current()->QuitWhenIdle(); | 241 quit_waiting_callback_.Run(); |
242 } | 242 } |
243 | 243 |
244 bool waiting_; | 244 base::Closure quit_waiting_callback_; |
245 DownloadManager* manager_; | 245 DownloadManager* manager_; |
246 std::vector<DownloadItem*> items_seen_; | 246 std::vector<DownloadItem*> items_seen_; |
247 | 247 |
248 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); | 248 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); |
249 }; | 249 }; |
250 | 250 |
251 class SavePackageFinishedObserver : public content::DownloadManager::Observer { | 251 class SavePackageFinishedObserver : public content::DownloadManager::Observer { |
252 public: | 252 public: |
253 SavePackageFinishedObserver(content::DownloadManager* manager, | 253 SavePackageFinishedObserver(content::DownloadManager* manager, |
254 const base::Closure& callback) | 254 const base::Closure& callback) |
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
929 pos = mhtml.find("Content-Type: image/png", pos); | 929 pos = mhtml.find("Content-Type: image/png", pos); |
930 if (pos == std::string::npos) | 930 if (pos == std::string::npos) |
931 break; | 931 break; |
932 count++; | 932 count++; |
933 pos++; | 933 pos++; |
934 } | 934 } |
935 EXPECT_EQ(1, count) << "Verify number of image/png parts in the mhtml output"; | 935 EXPECT_EQ(1, count) << "Verify number of image/png parts in the mhtml output"; |
936 } | 936 } |
937 | 937 |
938 } // namespace | 938 } // namespace |
OLD | NEW |