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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 | 61 |
62 // Waits for an item record in the downloads database to match |filter|. See | 62 // Waits for an item record in the downloads database to match |filter|. See |
63 // DownloadStoredProperly() below for an example filter. | 63 // DownloadStoredProperly() below for an example filter. |
64 class DownloadPersistedObserver : public DownloadHistory::Observer { | 64 class DownloadPersistedObserver : public DownloadHistory::Observer { |
65 public: | 65 public: |
66 typedef base::Callback<bool( | 66 typedef base::Callback<bool( |
67 DownloadItem* item, | 67 DownloadItem* item, |
68 const history::DownloadRow&)> PersistedFilter; | 68 const history::DownloadRow&)> PersistedFilter; |
69 | 69 |
70 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) | 70 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) |
71 : profile_(profile), | 71 : profile_(profile), |
72 filter_(filter), | 72 filter_(filter), |
73 waiting_(false), | 73 quit_waiting_callback_(), |
Randy Smith (Not in Mondays)
2015/10/19 19:37:02
I think member variables normally are default cons
Łukasz Anforowicz
2015/10/19 22:04:39
Done.
In the original patchset/snapshot, I left t
| |
74 persisted_(false) { | 74 persisted_(false) { |
75 DownloadServiceFactory::GetForBrowserContext(profile_)-> | 75 DownloadServiceFactory::GetForBrowserContext(profile_)-> |
76 GetDownloadHistory()->AddObserver(this); | 76 GetDownloadHistory()->AddObserver(this); |
77 } | 77 } |
78 | 78 |
79 ~DownloadPersistedObserver() override { | 79 ~DownloadPersistedObserver() override { |
80 DownloadService* service = DownloadServiceFactory::GetForBrowserContext( | 80 DownloadService* service = DownloadServiceFactory::GetForBrowserContext( |
81 profile_); | 81 profile_); |
82 if (service && service->GetDownloadHistory()) | 82 if (service && service->GetDownloadHistory()) |
83 service->GetDownloadHistory()->RemoveObserver(this); | 83 service->GetDownloadHistory()->RemoveObserver(this); |
84 } | 84 } |
85 | 85 |
86 bool WaitForPersisted() { | 86 bool WaitForPersisted() { |
87 if (persisted_) | 87 if (persisted_) |
88 return true; | 88 return true; |
89 waiting_ = true; | 89 base::RunLoop run_loop; |
90 content::RunMessageLoop(); | 90 quit_waiting_callback_ = run_loop.QuitClosure(); |
91 waiting_ = false; | 91 run_loop.Run(); |
92 quit_waiting_callback_ = base::Closure(); | |
92 return persisted_; | 93 return persisted_; |
93 } | 94 } |
94 | 95 |
95 void OnDownloadStored(DownloadItem* item, | 96 void OnDownloadStored(DownloadItem* item, |
96 const history::DownloadRow& info) override { | 97 const history::DownloadRow& info) override { |
97 persisted_ = persisted_ || filter_.Run(item, info); | 98 persisted_ = persisted_ || filter_.Run(item, info); |
98 if (persisted_ && waiting_) | 99 if (persisted_ && !quit_waiting_callback_.is_null()) |
99 base::MessageLoopForUI::current()->Quit(); | 100 quit_waiting_callback_.Run(); |
100 } | 101 } |
101 | 102 |
102 private: | 103 private: |
103 Profile* profile_; | 104 Profile* profile_; |
104 PersistedFilter filter_; | 105 PersistedFilter filter_; |
105 bool waiting_; | 106 base::Closure quit_waiting_callback_; |
106 bool persisted_; | 107 bool persisted_; |
107 | 108 |
108 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); | 109 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); |
109 }; | 110 }; |
110 | 111 |
111 // 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. |
112 class DownloadRemovedObserver : public DownloadPersistedObserver { | 113 class DownloadRemovedObserver : public DownloadPersistedObserver { |
113 public: | 114 public: |
114 DownloadRemovedObserver(Profile* profile, int32 download_id) | 115 DownloadRemovedObserver(Profile* profile, int32 download_id) |
115 : DownloadPersistedObserver(profile, PersistedFilter()), | 116 : DownloadPersistedObserver(profile, PersistedFilter()), |
116 removed_(false), | 117 removed_(false), |
117 waiting_(false), | 118 quit_waiting_callback_(), |
Randy Smith (Not in Mondays)
2015/10/19 19:37:01
Ditto (can remove line).
Łukasz Anforowicz
2015/10/19 22:04:39
Done.
| |
118 download_id_(download_id) { | 119 download_id_(download_id) {} |
119 } | |
120 ~DownloadRemovedObserver() override {} | 120 ~DownloadRemovedObserver() override {} |
121 | 121 |
122 bool WaitForRemoved() { | 122 bool WaitForRemoved() { |
123 if (removed_) | 123 if (removed_) |
124 return true; | 124 return true; |
125 waiting_ = true; | 125 base::RunLoop run_loop; |
126 content::RunMessageLoop(); | 126 quit_waiting_callback_ = run_loop.QuitClosure(); |
127 waiting_ = false; | 127 run_loop.Run(); |
128 quit_waiting_callback_ = base::Closure(); | |
128 return removed_; | 129 return removed_; |
129 } | 130 } |
130 | 131 |
131 void OnDownloadStored(DownloadItem* item, | 132 void OnDownloadStored(DownloadItem* item, |
132 const history::DownloadRow& info) override {} | 133 const history::DownloadRow& info) override {} |
133 | 134 |
134 void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) override { | 135 void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) override { |
135 removed_ = ids.find(download_id_) != ids.end(); | 136 removed_ = ids.find(download_id_) != ids.end(); |
136 if (removed_ && waiting_) | 137 if (removed_ && !quit_waiting_callback_.is_null()) |
137 base::MessageLoopForUI::current()->Quit(); | 138 quit_waiting_callback_.Run(); |
138 } | 139 } |
139 | 140 |
140 private: | 141 private: |
141 bool removed_; | 142 bool removed_; |
142 bool waiting_; | 143 base::Closure quit_waiting_callback_; |
143 int32 download_id_; | 144 int32 download_id_; |
144 | 145 |
145 DISALLOW_COPY_AND_ASSIGN(DownloadRemovedObserver); | 146 DISALLOW_COPY_AND_ASSIGN(DownloadRemovedObserver); |
146 }; | 147 }; |
147 | 148 |
148 bool DownloadStoredProperly( | 149 bool DownloadStoredProperly( |
149 const GURL& expected_url, | 150 const GURL& expected_url, |
150 const base::FilePath& expected_path, | 151 const base::FilePath& expected_path, |
151 int64 num_files, | 152 int64 num_files, |
152 history::DownloadState expected_state, | 153 history::DownloadState expected_state, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 } | 185 } |
185 | 186 |
186 const base::FilePath::CharType kTestDir[] = FILE_PATH_LITERAL("save_page"); | 187 const base::FilePath::CharType kTestDir[] = FILE_PATH_LITERAL("save_page"); |
187 | 188 |
188 static const char kAppendedExtension[] = ".html"; | 189 static const char kAppendedExtension[] = ".html"; |
189 | 190 |
190 // Loosely based on logic in DownloadTestObserver. | 191 // Loosely based on logic in DownloadTestObserver. |
191 class DownloadItemCreatedObserver : public DownloadManager::Observer { | 192 class DownloadItemCreatedObserver : public DownloadManager::Observer { |
192 public: | 193 public: |
193 explicit DownloadItemCreatedObserver(DownloadManager* manager) | 194 explicit DownloadItemCreatedObserver(DownloadManager* manager) |
194 : waiting_(false), manager_(manager) { | 195 : quit_waiting_callback_(), manager_(manager) { |
Randy Smith (Not in Mondays)
2015/10/19 19:37:02
Ditto.
Łukasz Anforowicz
2015/10/19 22:04:39
Done.
| |
195 manager->AddObserver(this); | 196 manager->AddObserver(this); |
196 } | 197 } |
197 | 198 |
198 ~DownloadItemCreatedObserver() override { | 199 ~DownloadItemCreatedObserver() override { |
199 if (manager_) | 200 if (manager_) |
200 manager_->RemoveObserver(this); | 201 manager_->RemoveObserver(this); |
201 } | 202 } |
202 | 203 |
203 // Wait for the first download item created after object creation. | 204 // Wait for the first download item created after object creation. |
204 // Note that this class provides no protection against the download | 205 // Note that this class provides no protection against the download |
205 // being destroyed between creation and return of WaitForNewDownloadItem(); | 206 // being destroyed between creation and return of WaitForNewDownloadItem(); |
206 // the caller must guarantee that in some other fashion. | 207 // the caller must guarantee that in some other fashion. |
207 void WaitForDownloadItem(std::vector<DownloadItem*>* items_seen) { | 208 void WaitForDownloadItem(std::vector<DownloadItem*>* items_seen) { |
208 if (!manager_) { | 209 if (!manager_) { |
209 // The manager went away before we were asked to wait; return | 210 // The manager went away before we were asked to wait; return |
210 // what we have, even if it's null. | 211 // what we have, even if it's null. |
211 *items_seen = items_seen_; | 212 *items_seen = items_seen_; |
212 return; | 213 return; |
213 } | 214 } |
214 | 215 |
215 if (items_seen_.empty()) { | 216 if (items_seen_.empty()) { |
216 waiting_ = true; | 217 base::RunLoop run_loop; |
217 content::RunMessageLoop(); | 218 quit_waiting_callback_ = run_loop.QuitClosure(); |
218 waiting_ = false; | 219 run_loop.Run(); |
220 quit_waiting_callback_ = base::Closure(); | |
219 } | 221 } |
220 | 222 |
221 *items_seen = items_seen_; | 223 *items_seen = items_seen_; |
222 return; | 224 return; |
223 } | 225 } |
224 | 226 |
225 private: | 227 private: |
226 // DownloadManager::Observer | 228 // DownloadManager::Observer |
227 void OnDownloadCreated(DownloadManager* manager, | 229 void OnDownloadCreated(DownloadManager* manager, |
228 DownloadItem* item) override { | 230 DownloadItem* item) override { |
229 DCHECK_EQ(manager, manager_); | 231 DCHECK_EQ(manager, manager_); |
230 items_seen_.push_back(item); | 232 items_seen_.push_back(item); |
231 | 233 |
232 if (waiting_) | 234 if (!quit_waiting_callback_.is_null()) |
233 base::MessageLoopForUI::current()->Quit(); | 235 quit_waiting_callback_.Run(); |
234 } | 236 } |
235 | 237 |
236 void ManagerGoingDown(DownloadManager* manager) override { | 238 void ManagerGoingDown(DownloadManager* manager) override { |
237 manager_->RemoveObserver(this); | 239 manager_->RemoveObserver(this); |
238 manager_ = NULL; | 240 manager_ = NULL; |
239 if (waiting_) | 241 if (!quit_waiting_callback_.is_null()) |
240 base::MessageLoopForUI::current()->Quit(); | 242 quit_waiting_callback_.Run(); |
241 } | 243 } |
242 | 244 |
243 bool waiting_; | 245 base::Closure quit_waiting_callback_; |
244 DownloadManager* manager_; | 246 DownloadManager* manager_; |
245 std::vector<DownloadItem*> items_seen_; | 247 std::vector<DownloadItem*> items_seen_; |
246 | 248 |
247 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); | 249 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); |
248 }; | 250 }; |
249 | 251 |
250 class SavePackageFinishedObserver : public content::DownloadManager::Observer { | 252 class SavePackageFinishedObserver : public content::DownloadManager::Observer { |
251 public: | 253 public: |
252 SavePackageFinishedObserver(content::DownloadManager* manager, | 254 SavePackageFinishedObserver(content::DownloadManager* manager, |
253 const base::Closure& callback) | 255 const base::Closure& callback) |
(...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
875 ASSERT_TRUE(base::ReadFileToString(full_file_name, &main_contents)); | 877 ASSERT_TRUE(base::ReadFileToString(full_file_name, &main_contents)); |
876 EXPECT_THAT(main_contents, | 878 EXPECT_THAT(main_contents, |
877 HasSubstr("<iframe src=\"./iframes_files/a.html\"></iframe>")); | 879 HasSubstr("<iframe src=\"./iframes_files/a.html\"></iframe>")); |
878 EXPECT_THAT(main_contents, | 880 EXPECT_THAT(main_contents, |
879 HasSubstr("<iframe src=\"./iframes_files/b.html\"></iframe>")); | 881 HasSubstr("<iframe src=\"./iframes_files/b.html\"></iframe>")); |
880 EXPECT_THAT(main_contents, | 882 EXPECT_THAT(main_contents, |
881 HasSubstr("<img src=\"./iframes_files/1.png\">")); | 883 HasSubstr("<img src=\"./iframes_files/1.png\">")); |
882 } | 884 } |
883 | 885 |
884 } // namespace | 886 } // namespace |
OLD | NEW |