| 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 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) | 62 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) |
| 63 : profile_(profile), | 63 : profile_(profile), |
| 64 filter_(filter), | 64 filter_(filter), |
| 65 waiting_(false), | 65 waiting_(false), |
| 66 persisted_(false) { | 66 persisted_(false) { |
| 67 DownloadServiceFactory::GetForBrowserContext(profile_)-> | 67 DownloadServiceFactory::GetForBrowserContext(profile_)-> |
| 68 GetDownloadHistory()->AddObserver(this); | 68 GetDownloadHistory()->AddObserver(this); |
| 69 } | 69 } |
| 70 | 70 |
| 71 virtual ~DownloadPersistedObserver() { | 71 ~DownloadPersistedObserver() override { |
| 72 DownloadService* service = DownloadServiceFactory::GetForBrowserContext( | 72 DownloadService* service = DownloadServiceFactory::GetForBrowserContext( |
| 73 profile_); | 73 profile_); |
| 74 if (service && service->GetDownloadHistory()) | 74 if (service && service->GetDownloadHistory()) |
| 75 service->GetDownloadHistory()->RemoveObserver(this); | 75 service->GetDownloadHistory()->RemoveObserver(this); |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool WaitForPersisted() { | 78 bool WaitForPersisted() { |
| 79 if (persisted_) | 79 if (persisted_) |
| 80 return true; | 80 return true; |
| 81 waiting_ = true; | 81 waiting_ = true; |
| 82 content::RunMessageLoop(); | 82 content::RunMessageLoop(); |
| 83 waiting_ = false; | 83 waiting_ = false; |
| 84 return persisted_; | 84 return persisted_; |
| 85 } | 85 } |
| 86 | 86 |
| 87 virtual void OnDownloadStored(DownloadItem* item, | 87 void OnDownloadStored(DownloadItem* item, |
| 88 const history::DownloadRow& info) override { | 88 const history::DownloadRow& info) override { |
| 89 persisted_ = persisted_ || filter_.Run(item, info); | 89 persisted_ = persisted_ || filter_.Run(item, info); |
| 90 if (persisted_ && waiting_) | 90 if (persisted_ && waiting_) |
| 91 base::MessageLoopForUI::current()->Quit(); | 91 base::MessageLoopForUI::current()->Quit(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 private: | 94 private: |
| 95 Profile* profile_; | 95 Profile* profile_; |
| 96 PersistedFilter filter_; | 96 PersistedFilter filter_; |
| 97 bool waiting_; | 97 bool waiting_; |
| 98 bool persisted_; | 98 bool persisted_; |
| 99 | 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); | 100 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); |
| 101 }; | 101 }; |
| 102 | 102 |
| 103 // Waits for an item record to be removed from the downloads database. | 103 // Waits for an item record to be removed from the downloads database. |
| 104 class DownloadRemovedObserver : public DownloadPersistedObserver { | 104 class DownloadRemovedObserver : public DownloadPersistedObserver { |
| 105 public: | 105 public: |
| 106 DownloadRemovedObserver(Profile* profile, int32 download_id) | 106 DownloadRemovedObserver(Profile* profile, int32 download_id) |
| 107 : DownloadPersistedObserver(profile, PersistedFilter()), | 107 : DownloadPersistedObserver(profile, PersistedFilter()), |
| 108 removed_(false), | 108 removed_(false), |
| 109 waiting_(false), | 109 waiting_(false), |
| 110 download_id_(download_id) { | 110 download_id_(download_id) { |
| 111 } | 111 } |
| 112 virtual ~DownloadRemovedObserver() {} | 112 ~DownloadRemovedObserver() override {} |
| 113 | 113 |
| 114 bool WaitForRemoved() { | 114 bool WaitForRemoved() { |
| 115 if (removed_) | 115 if (removed_) |
| 116 return true; | 116 return true; |
| 117 waiting_ = true; | 117 waiting_ = true; |
| 118 content::RunMessageLoop(); | 118 content::RunMessageLoop(); |
| 119 waiting_ = false; | 119 waiting_ = false; |
| 120 return removed_; | 120 return removed_; |
| 121 } | 121 } |
| 122 | 122 |
| 123 virtual void OnDownloadStored(DownloadItem* item, | 123 void OnDownloadStored(DownloadItem* item, |
| 124 const history::DownloadRow& info) override { | 124 const history::DownloadRow& info) override {} |
| 125 } | |
| 126 | 125 |
| 127 virtual void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) override { | 126 void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) override { |
| 128 removed_ = ids.find(download_id_) != ids.end(); | 127 removed_ = ids.find(download_id_) != ids.end(); |
| 129 if (removed_ && waiting_) | 128 if (removed_ && waiting_) |
| 130 base::MessageLoopForUI::current()->Quit(); | 129 base::MessageLoopForUI::current()->Quit(); |
| 131 } | 130 } |
| 132 | 131 |
| 133 private: | 132 private: |
| 134 bool removed_; | 133 bool removed_; |
| 135 bool waiting_; | 134 bool waiting_; |
| 136 int32 download_id_; | 135 int32 download_id_; |
| 137 | 136 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 static const char kAppendedExtension[] = ".html"; | 180 static const char kAppendedExtension[] = ".html"; |
| 182 | 181 |
| 183 // Loosely based on logic in DownloadTestObserver. | 182 // Loosely based on logic in DownloadTestObserver. |
| 184 class DownloadItemCreatedObserver : public DownloadManager::Observer { | 183 class DownloadItemCreatedObserver : public DownloadManager::Observer { |
| 185 public: | 184 public: |
| 186 explicit DownloadItemCreatedObserver(DownloadManager* manager) | 185 explicit DownloadItemCreatedObserver(DownloadManager* manager) |
| 187 : waiting_(false), manager_(manager) { | 186 : waiting_(false), manager_(manager) { |
| 188 manager->AddObserver(this); | 187 manager->AddObserver(this); |
| 189 } | 188 } |
| 190 | 189 |
| 191 virtual ~DownloadItemCreatedObserver() { | 190 ~DownloadItemCreatedObserver() override { |
| 192 if (manager_) | 191 if (manager_) |
| 193 manager_->RemoveObserver(this); | 192 manager_->RemoveObserver(this); |
| 194 } | 193 } |
| 195 | 194 |
| 196 // Wait for the first download item created after object creation. | 195 // Wait for the first download item created after object creation. |
| 197 // Note that this class provides no protection against the download | 196 // Note that this class provides no protection against the download |
| 198 // being destroyed between creation and return of WaitForNewDownloadItem(); | 197 // being destroyed between creation and return of WaitForNewDownloadItem(); |
| 199 // the caller must guarantee that in some other fashion. | 198 // the caller must guarantee that in some other fashion. |
| 200 void WaitForDownloadItem(std::vector<DownloadItem*>* items_seen) { | 199 void WaitForDownloadItem(std::vector<DownloadItem*>* items_seen) { |
| 201 if (!manager_) { | 200 if (!manager_) { |
| 202 // The manager went away before we were asked to wait; return | 201 // The manager went away before we were asked to wait; return |
| 203 // what we have, even if it's null. | 202 // what we have, even if it's null. |
| 204 *items_seen = items_seen_; | 203 *items_seen = items_seen_; |
| 205 return; | 204 return; |
| 206 } | 205 } |
| 207 | 206 |
| 208 if (items_seen_.empty()) { | 207 if (items_seen_.empty()) { |
| 209 waiting_ = true; | 208 waiting_ = true; |
| 210 content::RunMessageLoop(); | 209 content::RunMessageLoop(); |
| 211 waiting_ = false; | 210 waiting_ = false; |
| 212 } | 211 } |
| 213 | 212 |
| 214 *items_seen = items_seen_; | 213 *items_seen = items_seen_; |
| 215 return; | 214 return; |
| 216 } | 215 } |
| 217 | 216 |
| 218 private: | 217 private: |
| 219 // DownloadManager::Observer | 218 // DownloadManager::Observer |
| 220 virtual void OnDownloadCreated( | 219 void OnDownloadCreated(DownloadManager* manager, |
| 221 DownloadManager* manager, DownloadItem* item) override { | 220 DownloadItem* item) override { |
| 222 DCHECK_EQ(manager, manager_); | 221 DCHECK_EQ(manager, manager_); |
| 223 items_seen_.push_back(item); | 222 items_seen_.push_back(item); |
| 224 | 223 |
| 225 if (waiting_) | 224 if (waiting_) |
| 226 base::MessageLoopForUI::current()->Quit(); | 225 base::MessageLoopForUI::current()->Quit(); |
| 227 } | 226 } |
| 228 | 227 |
| 229 virtual void ManagerGoingDown(DownloadManager* manager) override { | 228 void ManagerGoingDown(DownloadManager* manager) override { |
| 230 manager_->RemoveObserver(this); | 229 manager_->RemoveObserver(this); |
| 231 manager_ = NULL; | 230 manager_ = NULL; |
| 232 if (waiting_) | 231 if (waiting_) |
| 233 base::MessageLoopForUI::current()->Quit(); | 232 base::MessageLoopForUI::current()->Quit(); |
| 234 } | 233 } |
| 235 | 234 |
| 236 bool waiting_; | 235 bool waiting_; |
| 237 DownloadManager* manager_; | 236 DownloadManager* manager_; |
| 238 std::vector<DownloadItem*> items_seen_; | 237 std::vector<DownloadItem*> items_seen_; |
| 239 | 238 |
| 240 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); | 239 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); |
| 241 }; | 240 }; |
| 242 | 241 |
| 243 class SavePackageFinishedObserver : public content::DownloadManager::Observer { | 242 class SavePackageFinishedObserver : public content::DownloadManager::Observer { |
| 244 public: | 243 public: |
| 245 SavePackageFinishedObserver(content::DownloadManager* manager, | 244 SavePackageFinishedObserver(content::DownloadManager* manager, |
| 246 const base::Closure& callback) | 245 const base::Closure& callback) |
| 247 : download_manager_(manager), | 246 : download_manager_(manager), |
| 248 callback_(callback) { | 247 callback_(callback) { |
| 249 download_manager_->AddObserver(this); | 248 download_manager_->AddObserver(this); |
| 250 } | 249 } |
| 251 | 250 |
| 252 virtual ~SavePackageFinishedObserver() { | 251 ~SavePackageFinishedObserver() override { |
| 253 if (download_manager_) | 252 if (download_manager_) |
| 254 download_manager_->RemoveObserver(this); | 253 download_manager_->RemoveObserver(this); |
| 255 } | 254 } |
| 256 | 255 |
| 257 // DownloadManager::Observer: | 256 // DownloadManager::Observer: |
| 258 virtual void OnSavePackageSuccessfullyFinished( | 257 void OnSavePackageSuccessfullyFinished(content::DownloadManager* manager, |
| 259 content::DownloadManager* manager, content::DownloadItem* item) override { | 258 content::DownloadItem* item) override { |
| 260 callback_.Run(); | 259 callback_.Run(); |
| 261 } | 260 } |
| 262 virtual void ManagerGoingDown(content::DownloadManager* manager) override { | 261 void ManagerGoingDown(content::DownloadManager* manager) override { |
| 263 download_manager_->RemoveObserver(this); | 262 download_manager_->RemoveObserver(this); |
| 264 download_manager_ = NULL; | 263 download_manager_ = NULL; |
| 265 } | 264 } |
| 266 | 265 |
| 267 private: | 266 private: |
| 268 content::DownloadManager* download_manager_; | 267 content::DownloadManager* download_manager_; |
| 269 base::Closure callback_; | 268 base::Closure callback_; |
| 270 | 269 |
| 271 DISALLOW_COPY_AND_ASSIGN(SavePackageFinishedObserver); | 270 DISALLOW_COPY_AND_ASSIGN(SavePackageFinishedObserver); |
| 272 }; | 271 }; |
| 273 | 272 |
| 274 class SavePageBrowserTest : public InProcessBrowserTest { | 273 class SavePageBrowserTest : public InProcessBrowserTest { |
| 275 public: | 274 public: |
| 276 SavePageBrowserTest() {} | 275 SavePageBrowserTest() {} |
| 277 virtual ~SavePageBrowserTest(); | 276 virtual ~SavePageBrowserTest(); |
| 278 | 277 |
| 279 protected: | 278 protected: |
| 280 virtual void SetUp() override { | 279 virtual void SetUp() override { |
| 281 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 280 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
| 282 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 281 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
| 283 InProcessBrowserTest::SetUp(); | 282 InProcessBrowserTest::SetUp(); |
| 284 } | 283 } |
| 285 | 284 |
| 286 virtual void SetUpOnMainThread() override { | 285 void SetUpOnMainThread() override { |
| 287 browser()->profile()->GetPrefs()->SetFilePath( | 286 browser()->profile()->GetPrefs()->SetFilePath( |
| 288 prefs::kDownloadDefaultDirectory, save_dir_.path()); | 287 prefs::kDownloadDefaultDirectory, save_dir_.path()); |
| 289 browser()->profile()->GetPrefs()->SetFilePath( | 288 browser()->profile()->GetPrefs()->SetFilePath( |
| 290 prefs::kSaveFileDefaultDirectory, save_dir_.path()); | 289 prefs::kSaveFileDefaultDirectory, save_dir_.path()); |
| 291 BrowserThread::PostTask( | 290 BrowserThread::PostTask( |
| 292 BrowserThread::IO, FROM_HERE, | 291 BrowserThread::IO, FROM_HERE, |
| 293 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 292 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 294 } | 293 } |
| 295 | 294 |
| 296 GURL NavigateToMockURL(const std::string& prefix) { | 295 GURL NavigateToMockURL(const std::string& prefix) { |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 | 435 |
| 437 // TODO(benjhayden): Figure out how to safely wait for SavePackage's finished | 436 // TODO(benjhayden): Figure out how to safely wait for SavePackage's finished |
| 438 // notification, then expect the contents of the downloaded file. | 437 // notification, then expect the contents of the downloaded file. |
| 439 } | 438 } |
| 440 | 439 |
| 441 class DelayingDownloadManagerDelegate : public ChromeDownloadManagerDelegate { | 440 class DelayingDownloadManagerDelegate : public ChromeDownloadManagerDelegate { |
| 442 public: | 441 public: |
| 443 explicit DelayingDownloadManagerDelegate(Profile* profile) | 442 explicit DelayingDownloadManagerDelegate(Profile* profile) |
| 444 : ChromeDownloadManagerDelegate(profile) { | 443 : ChromeDownloadManagerDelegate(profile) { |
| 445 } | 444 } |
| 446 virtual ~DelayingDownloadManagerDelegate() {} | 445 ~DelayingDownloadManagerDelegate() override {} |
| 447 | 446 |
| 448 virtual bool ShouldCompleteDownload( | 447 bool ShouldCompleteDownload( |
| 449 content::DownloadItem* item, | 448 content::DownloadItem* item, |
| 450 const base::Closure& user_complete_callback) override { | 449 const base::Closure& user_complete_callback) override { |
| 451 return false; | 450 return false; |
| 452 } | 451 } |
| 453 | 452 |
| 454 private: | 453 private: |
| 455 DISALLOW_COPY_AND_ASSIGN(DelayingDownloadManagerDelegate); | 454 DISALLOW_COPY_AND_ASSIGN(DelayingDownloadManagerDelegate); |
| 456 }; | 455 }; |
| 457 | 456 |
| 458 // Disabled on Windows due to flakiness. http://crbug.com/162323 | 457 // Disabled on Windows due to flakiness. http://crbug.com/162323 |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 | 750 |
| 752 EXPECT_TRUE(base::DieFileDie(full_file_name, false)); | 751 EXPECT_TRUE(base::DieFileDie(full_file_name, false)); |
| 753 EXPECT_TRUE(base::DieFileDie(dir, true)); | 752 EXPECT_TRUE(base::DieFileDie(dir, true)); |
| 754 } | 753 } |
| 755 #endif | 754 #endif |
| 756 | 755 |
| 757 class SavePageAsMHTMLBrowserTest : public SavePageBrowserTest { | 756 class SavePageAsMHTMLBrowserTest : public SavePageBrowserTest { |
| 758 public: | 757 public: |
| 759 SavePageAsMHTMLBrowserTest() {} | 758 SavePageAsMHTMLBrowserTest() {} |
| 760 virtual ~SavePageAsMHTMLBrowserTest(); | 759 virtual ~SavePageAsMHTMLBrowserTest(); |
| 761 virtual void SetUpCommandLine(CommandLine* command_line) override { | 760 void SetUpCommandLine(CommandLine* command_line) override { |
| 762 command_line->AppendSwitch(switches::kSavePageAsMHTML); | 761 command_line->AppendSwitch(switches::kSavePageAsMHTML); |
| 763 } | 762 } |
| 764 | 763 |
| 765 private: | 764 private: |
| 766 DISALLOW_COPY_AND_ASSIGN(SavePageAsMHTMLBrowserTest); | 765 DISALLOW_COPY_AND_ASSIGN(SavePageAsMHTMLBrowserTest); |
| 767 }; | 766 }; |
| 768 | 767 |
| 769 SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() { | 768 SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() { |
| 770 } | 769 } |
| 771 | 770 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 base::FilePath download_dir = DownloadPrefs::FromDownloadManager( | 809 base::FilePath download_dir = DownloadPrefs::FromDownloadManager( |
| 811 GetDownloadManager())->DownloadPath(); | 810 GetDownloadManager())->DownloadPath(); |
| 812 base::FilePath filename = download_dir.AppendASCII("dataurl.txt"); | 811 base::FilePath filename = download_dir.AppendASCII("dataurl.txt"); |
| 813 ASSERT_TRUE(base::PathExists(filename)); | 812 ASSERT_TRUE(base::PathExists(filename)); |
| 814 std::string contents; | 813 std::string contents; |
| 815 EXPECT_TRUE(base::ReadFileToString(filename, &contents)); | 814 EXPECT_TRUE(base::ReadFileToString(filename, &contents)); |
| 816 EXPECT_EQ("foo", contents); | 815 EXPECT_EQ("foo", contents); |
| 817 } | 816 } |
| 818 | 817 |
| 819 } // namespace | 818 } // namespace |
| OLD | NEW |