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

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

Issue 2727253002: Added DownloadItem::Observer to JobController. (Closed)
Patch Set: Added TODO Created 3 years, 9 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 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "content/browser/background_fetch/background_fetch_job_data.h" 11 #include "content/browser/background_fetch/background_fetch_job_data.h"
12 #include "content/browser/background_fetch/background_fetch_request_info.h" 12 #include "content/browser/background_fetch/background_fetch_request_info.h"
13 #include "content/public/browser/browser_context.h" 13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/download_manager.h" 15 #include "content/public/browser/download_manager.h"
16 #include "content/public/browser/storage_partition.h" 16 #include "content/public/browser/storage_partition.h"
17 17
18 namespace content { 18 namespace content {
19 19
20 BackgroundFetchJobController::BackgroundFetchJobController( 20 BackgroundFetchJobController::BackgroundFetchJobController(
21 const std::string& job_guid, 21 const std::string& job_guid,
22 BrowserContext* browser_context, 22 BrowserContext* browser_context,
23 StoragePartition* storage_partition, 23 StoragePartition* storage_partition,
24 std::unique_ptr<BackgroundFetchJobData> job_data) 24 std::unique_ptr<BackgroundFetchJobData> job_data)
25 : browser_context_(browser_context), 25 : browser_context_(browser_context),
26 storage_partition_(storage_partition), 26 storage_partition_(storage_partition),
27 job_data_(std::move(job_data)) {} 27 job_data_(std::move(job_data)),
28 weak_ptr_factory_(this) {}
28 29
29 BackgroundFetchJobController::~BackgroundFetchJobController() = default; 30 BackgroundFetchJobController::~BackgroundFetchJobController() = default;
30 31
31 void BackgroundFetchJobController::Shutdown() { 32 void BackgroundFetchJobController::Shutdown() {
32 // TODO(harkness): Remove any observers. 33 DownloadManager* manager =
34 BrowserContext::GetDownloadManager(browser_context_);
35 // For any downloads in progress, remove the observer.
36 for (auto& guid_map_entry : download_guid_map_) {
37 DownloadItem* item = manager->GetDownloadByGuid(guid_map_entry.first);
38 // TODO(harkness): Follow up with DownloadManager team about whether this
39 // should be a DCHECK.
40 if (item)
41 item->RemoveObserver(this);
42 }
43 download_guid_map_.clear();
44
33 // TODO(harkness): Write final status to the DataManager. After this call, the 45 // TODO(harkness): Write final status to the DataManager. After this call, the
34 // |job_data_| is no longer valid. 46 // |job_data_| is no longer valid.
35 job_data_.reset(nullptr); 47 job_data_.reset();
36 } 48 }
37 49
38 void BackgroundFetchJobController::StartProcessing() { 50 void BackgroundFetchJobController::StartProcessing() {
39 DCHECK(job_data_); 51 DCHECK(job_data_);
40 52
41 const BackgroundFetchRequestInfo& fetch_request = 53 const BackgroundFetchRequestInfo& fetch_request =
42 job_data_->GetNextBackgroundFetchRequestInfo(); 54 job_data_->GetNextBackgroundFetchRequestInfo();
43 ProcessRequest(fetch_request); 55 ProcessRequest(fetch_request);
44 56
45 // Currently this only supports jobs of size 1. 57 // Currently, this processes a single request at a time.
46 // TODO(crbug.com/692544) 58 }
47 DCHECK(!job_data_->HasRequestsRemaining()); 59
60 void BackgroundFetchJobController::DownloadStarted(
61 const std::string& request_guid,
62 DownloadItem* item,
63 DownloadInterruptReason reason) {
64 // Start observing the DownloadItem. No need to store the pointer because it
65 // can be retrieved from the DownloadManager.
66 item->AddObserver(this);
67 download_guid_map_[item->GetGuid()] = request_guid;
68 job_data_->SetRequestDownloadGuid(request_guid, item->GetGuid());
69 }
70
71 void BackgroundFetchJobController::OnDownloadUpdated(DownloadItem* item) {
72 DCHECK(download_guid_map_.find(item->GetGuid()) != download_guid_map_.end());
73
74 bool requests_remaining = false;
75 switch (item->GetState()) {
76 case DownloadItem::DownloadState::CANCELLED:
77 // TODO(harkness): Expand the "complete" flag on the RequestInfo to
78 // include error states.
79 case DownloadItem::DownloadState::COMPLETE:
80 requests_remaining = job_data_->BackgroundFetchRequestInfoComplete(
81 download_guid_map_[item->GetGuid()]);
82 // TODO(harkness): Tell the notification service to update the download
83 // notification.
84
85 if (requests_remaining) {
86 ProcessRequest(job_data_->GetNextBackgroundFetchRequestInfo());
87 } else if (job_data_->IsComplete()) {
88 // TODO(harkness): Return the data to the context.
89 }
90 break;
91 case DownloadItem::DownloadState::INTERRUPTED:
92 // TODO(harkness): Just update the notification that it is paused.
93 break;
94 case DownloadItem::DownloadState::IN_PROGRESS:
95 // TODO(harkness): If the download was previously paused, this should now
96 // unpause the notification.
97 break;
98 case DownloadItem::DownloadState::MAX_DOWNLOAD_STATE:
99 // TODO(harkness): Consider how we want to handle errors here.
100 break;
101 }
102 }
103
104 void BackgroundFetchJobController::OnDownloadDestroyed(DownloadItem* item) {
105 // This can be called as part of Shutdown. Make sure the observers are
106 // removed.
107 item->RemoveObserver(this);
48 } 108 }
49 109
50 void BackgroundFetchJobController::ProcessRequest( 110 void BackgroundFetchJobController::ProcessRequest(
51 const BackgroundFetchRequestInfo& fetch_request) { 111 const BackgroundFetchRequestInfo& fetch_request) {
52 // TODO(harkness): Start the observer watching for this download.
53 // TODO(harkness): Check if the download is already in progress or completed. 112 // TODO(harkness): Check if the download is already in progress or completed.
54 113
55 // Send the fetch request to the DownloadManager. 114 // Set up the download parameters and the OnStartedCallback.
56 std::unique_ptr<DownloadUrlParameters> params( 115 std::unique_ptr<DownloadUrlParameters> params(
57 base::MakeUnique<DownloadUrlParameters>( 116 base::MakeUnique<DownloadUrlParameters>(
58 fetch_request.url(), storage_partition_->GetURLRequestContext())); 117 fetch_request.url(), storage_partition_->GetURLRequestContext()));
59 // TODO(harkness): Currently this is the only place the browser_context is 118 params->set_callback(
60 // used. Evaluate whether we can just pass in the download manager explicitly. 119 base::Bind(&BackgroundFetchJobController::DownloadStarted,
120 weak_ptr_factory_.GetWeakPtr(), fetch_request.guid()));
121
61 BrowserContext::GetDownloadManager(browser_context_) 122 BrowserContext::GetDownloadManager(browser_context_)
62 ->DownloadUrl(std::move(params)); 123 ->DownloadUrl(std::move(params));
63 } 124 }
64 125
65 } // namespace content 126 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698