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

Side by Side Diff: content/browser/background_fetch/background_fetch_job_controller.cc

Issue 2786783002: Dispatch a bare Service Worker event for a finished Background Fetch (Closed)
Patch Set: Dispatch a bare Service Worker event for a finished Background Fetch Created 3 years, 8 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/background_fetch/background_fetch_job_controller.h" 5 #include "content/browser/background_fetch/background_fetch_job_controller.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 23 matching lines...) Expand all
34 ~Core() final { 34 ~Core() final {
35 for (const auto& pair : downloads_) 35 for (const auto& pair : downloads_)
36 pair.first->RemoveObserver(this); 36 pair.first->RemoveObserver(this);
37 } 37 }
38 38
39 // Returns a weak pointer that can be used to talk to |this|. 39 // Returns a weak pointer that can be used to talk to |this|.
40 base::WeakPtr<Core> GetWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); } 40 base::WeakPtr<Core> GetWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); }
41 41
42 // Starts fetching the |request| with the download manager. 42 // Starts fetching the |request| with the download manager.
43 void StartRequest(const BackgroundFetchRequestInfo& request) { 43 void StartRequest(const BackgroundFetchRequestInfo& request) {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI);
44 DCHECK(request_context_); 45 DCHECK(request_context_);
45 46
46 DownloadManager* download_manager = 47 DownloadManager* download_manager =
47 BrowserContext::GetDownloadManager(browser_context_); 48 BrowserContext::GetDownloadManager(browser_context_);
48 DCHECK(download_manager); 49 DCHECK(download_manager);
49 50
50 std::unique_ptr<DownloadUrlParameters> download_parameters( 51 std::unique_ptr<DownloadUrlParameters> download_parameters(
51 base::MakeUnique<DownloadUrlParameters>(request.GetURL(), 52 base::MakeUnique<DownloadUrlParameters>(request.GetURL(),
52 request_context_.get())); 53 request_context_.get()));
53 54
54 // TODO(peter): The |download_parameters| should be populated with all the 55 // TODO(peter): The |download_parameters| should be populated with all the
55 // properties set in the |request|'s ServiceWorkerFetchRequest member. 56 // properties set in the |request|'s ServiceWorkerFetchRequest member.
56 57
57 download_parameters->set_callback(base::Bind( 58 download_parameters->set_callback(base::Bind(
58 &Core::DidStartRequest, weak_ptr_factory_.GetWeakPtr(), request)); 59 &Core::DidStartRequest, weak_ptr_factory_.GetWeakPtr(), request));
59 60
60 download_manager->DownloadUrl(std::move(download_parameters)); 61 download_manager->DownloadUrl(std::move(download_parameters));
61 } 62 }
62 63
63 // DownloadItem::Observer overrides: 64 // DownloadItem::Observer overrides:
64 void OnDownloadUpdated(DownloadItem* item) override { 65 void OnDownloadUpdated(DownloadItem* item) override {
66 DCHECK_CURRENTLY_ON(BrowserThread::UI);
67
65 auto iter = downloads_.find(item); 68 auto iter = downloads_.find(item);
66 DCHECK(iter != downloads_.end()); 69 DCHECK(iter != downloads_.end());
67 70
68 const BackgroundFetchRequestInfo& request = iter->second; 71 const BackgroundFetchRequestInfo& request = iter->second;
69 72
70 switch (item->GetState()) { 73 switch (item->GetState()) {
71 case DownloadItem::DownloadState::COMPLETE: 74 case DownloadItem::DownloadState::COMPLETE:
72 // TODO(peter): Populate the responses' information in the |request|. 75 // TODO(peter): Populate the responses' information in the |request|.
73 76
74 item->RemoveObserver(this); 77 item->RemoveObserver(this);
(...skipping 17 matching lines...) Expand all
92 // TODO(harkness): If the download was previously paused, this should 95 // TODO(harkness): If the download was previously paused, this should
93 // now unpause the notification. 96 // now unpause the notification.
94 break; 97 break;
95 case DownloadItem::DownloadState::MAX_DOWNLOAD_STATE: 98 case DownloadItem::DownloadState::MAX_DOWNLOAD_STATE:
96 NOTREACHED(); 99 NOTREACHED();
97 break; 100 break;
98 } 101 }
99 } 102 }
100 103
101 void OnDownloadDestroyed(DownloadItem* item) override { 104 void OnDownloadDestroyed(DownloadItem* item) override {
105 DCHECK_CURRENTLY_ON(BrowserThread::UI);
102 DCHECK_EQ(downloads_.count(item), 1u); 106 DCHECK_EQ(downloads_.count(item), 1u);
103 downloads_.erase(item); 107 downloads_.erase(item);
104 108
105 item->RemoveObserver(this); 109 item->RemoveObserver(this);
106 } 110 }
107 111
108 private: 112 private:
109 // Called when the download manager has started the given |request|. The 113 // Called when the download manager has started the given |request|. The
110 // |download_item| continues to be owned by the download system. The 114 // |download_item| continues to be owned by the download system. The
111 // |interrupt_reason| will indicate when a request could not be started. 115 // |interrupt_reason| will indicate when a request could not be started.
112 void DidStartRequest(const BackgroundFetchRequestInfo& request, 116 void DidStartRequest(const BackgroundFetchRequestInfo& request,
113 DownloadItem* download_item, 117 DownloadItem* download_item,
114 DownloadInterruptReason interrupt_reason) { 118 DownloadInterruptReason interrupt_reason) {
119 DCHECK_CURRENTLY_ON(BrowserThread::UI);
115 DCHECK_EQ(interrupt_reason, DOWNLOAD_INTERRUPT_REASON_NONE); 120 DCHECK_EQ(interrupt_reason, DOWNLOAD_INTERRUPT_REASON_NONE);
116 DCHECK(download_item); 121 DCHECK(download_item);
117 122
118 // TODO(peter): The above two DCHECKs are assumptions our implementation 123 // TODO(peter): The above two DCHECKs are assumptions our implementation
119 // currently makes, but are not fit for production. We need to handle such 124 // currently makes, but are not fit for production. We need to handle such
120 // failures gracefully. 125 // failures gracefully.
121 126
122 // Register for updates on the download's progress. 127 // Register for updates on the download's progress.
123 download_item->AddObserver(this); 128 download_item->AddObserver(this);
124 129
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 254
250 // TODO(harkness): Abort all in-progress downloads. 255 // TODO(harkness): Abort all in-progress downloads.
251 256
252 state_ = State::ABORTED; 257 state_ = State::ABORTED;
253 258
254 // Inform the owner of the controller about the job having completed. 259 // Inform the owner of the controller about the job having completed.
255 std::move(completed_callback_).Run(this); 260 std::move(completed_callback_).Run(this);
256 } 261 }
257 262
258 } // namespace content 263 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698