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

Unified Diff: components/download/internal/controller_impl.cc

Issue 2895953004: Add initial Controller to DownloadService (Closed)
Patch Set: Rebased Created 3 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: components/download/internal/controller_impl.cc
diff --git a/components/download/internal/controller_impl.cc b/components/download/internal/controller_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ae434285a69d021d0c1fab26562afa80006f04b7
--- /dev/null
+++ b/components/download/internal/controller_impl.cc
@@ -0,0 +1,267 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/download/internal/controller_impl.h"
+
+#include <string>
+#include <vector>
+
+#include "base/bind.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "components/download/internal/client_set.h"
+#include "components/download/internal/config.h"
+#include "components/download/internal/entry.h"
+#include "components/download/internal/entry_utils.h"
+#include "components/download/internal/model.h"
+#include "components/download/internal/stats.h"
+
+namespace download {
+
+ControllerImpl::ControllerImpl(std::unique_ptr<ClientSet> clients,
+ std::unique_ptr<Configuration> config,
+ std::unique_ptr<DownloadDriver> driver,
+ std::unique_ptr<Model> model)
+ : clients_(std::move(clients)),
+ config_(std::move(config)),
+ driver_(std::move(driver)),
+ model_(std::move(model)) {}
+
+ControllerImpl::~ControllerImpl() = default;
+
+void ControllerImpl::Initialize() {
+ DCHECK(!startup_status_.Complete());
+
+ driver_->Initialize(this);
+ model_->Initialize(this);
+}
+
+const StartupStatus& ControllerImpl::GetStartupStatus() {
+ return startup_status_;
+}
+
+void ControllerImpl::StartDownload(const DownloadParams& params) {
+ DCHECK(startup_status_.Ok());
+
+ if (start_callbacks_.find(params.guid) != start_callbacks_.end() ||
+ model_->Get(params.guid) != nullptr) {
+ HandleStartDownloadResponse(params.client, params.guid,
+ DownloadParams::StartResult::UNEXPECTED_GUID,
+ params.callback);
+ return;
+ }
+
+ auto* client = clients_->GetClient(params.client);
+ if (!client) {
+ HandleStartDownloadResponse(params.client, params.guid,
+ DownloadParams::StartResult::UNEXPECTED_CLIENT,
+ params.callback);
+ return;
+ }
+
+ uint32_t client_count =
+ util::GetNumberOfEntriesForClient(params.client, model_->PeekEntries());
+ if (client_count >= config_->max_scheduled_downloads) {
+ HandleStartDownloadResponse(params.client, params.guid,
+ DownloadParams::StartResult::BACKOFF,
+ params.callback);
+ return;
+ }
+
+ start_callbacks_[params.guid] = params.callback;
+ model_->Add(Entry(params));
+}
+
+void ControllerImpl::PauseDownload(const std::string& guid) {
+ DCHECK(startup_status_.Ok());
+
+ auto* entry = model_->Get(guid);
+ DCHECK(entry);
+
+ if (entry->state == Entry::State::PAUSED ||
+ entry->state == Entry::State::COMPLETE ||
+ entry->state == Entry::State::WATCHDOG) {
+ return;
+ }
+
+ // TODO(dtrainor): Pause the download.
+}
+
+void ControllerImpl::ResumeDownload(const std::string& guid) {
+ DCHECK(startup_status_.Ok());
+
+ auto* entry = model_->Get(guid);
+ DCHECK(entry);
+
+ if (entry->state != Entry::State::PAUSED)
+ return;
+
+ // TODO(dtrainor): Resume the download.
+}
+
+void ControllerImpl::CancelDownload(const std::string& guid) {
+ DCHECK(startup_status_.Ok());
+
+ auto* entry = model_->Get(guid);
+ DCHECK(entry);
+
+ if (entry->state == Entry::State::NEW) {
+ // Check if we're currently trying to add the download.
+ DCHECK(start_callbacks_.find(entry->guid) != start_callbacks_.end());
+ HandleStartDownloadResponse(entry->client, entry->guid,
+ DownloadParams::StartResult::CLIENT_CANCELLED);
+ }
+
+ // TODO(dtrainor): Cancel the download.
+}
+
+void ControllerImpl::ChangeDownloadCriteria(const std::string& guid,
+ const SchedulingParams& params) {
+ DCHECK(startup_status_.Ok());
+
+ auto* entry = model_->Get(guid);
+ DCHECK(entry);
+
+ // TODO(dtrainor): Change the criteria of the download.
+}
+
+DownloadClient ControllerImpl::GetOwnerOfDownload(const std::string& guid) {
+ auto* entry = model_->Get(guid);
+ return entry ? entry->client : DownloadClient::INVALID;
+}
+
+void ControllerImpl::OnDriverReady(bool success) {
+ DCHECK(!startup_status_.driver_ok.has_value());
+ startup_status_.driver_ok = success;
+ AttemptToFinalizeSetup();
+}
+
+void ControllerImpl::OnDownloadCreated(const DriverEntry& download) {}
+
+void ControllerImpl::OnDownloadFailed(const DriverEntry& download, int reason) {
+}
+
+void ControllerImpl::OnDownloadSucceeded(const DriverEntry& download,
+ const base::FilePath& path) {}
+
+void ControllerImpl::OnDownloadUpdated(const DriverEntry& download) {}
+
+void ControllerImpl::OnModelReady(bool success) {
+ DCHECK(!startup_status_.model_ok.has_value());
+ startup_status_.model_ok = success;
+ AttemptToFinalizeSetup();
+}
+
+void ControllerImpl::OnItemAdded(bool success,
+ DownloadClient client,
+ const std::string& guid) {
+ // If the StartCallback doesn't exist, we already notified the Client about
+ // this item. That means something went wrong, so stop here.
+ if (start_callbacks_.find(guid) == start_callbacks_.end())
+ return;
+
+ if (!success) {
+ HandleStartDownloadResponse(client, guid,
+ DownloadParams::StartResult::INTERNAL_ERROR);
+ return;
+ }
+
+ HandleStartDownloadResponse(client, guid,
+ DownloadParams::StartResult::ACCEPTED);
+
+ auto* entry = model_->Get(guid);
+ DCHECK(entry);
+ DCHECK_EQ(Entry::State::NEW, entry->state);
+
+ // TODO(dtrainor): Make sure we're running all of the downloads we care about.
+}
+
+void ControllerImpl::OnItemUpdated(bool success,
+ DownloadClient client,
+ const std::string& guid) {
+ // TODO(dtrainor): Fail and clean up the download if necessary.
+}
+
+void ControllerImpl::OnItemRemoved(bool success,
+ DownloadClient client,
+ const std::string& guid) {
+ // TODO(dtrainor): Fail and clean up the download if necessary.
+}
+
+void ControllerImpl::AttemptToFinalizeSetup() {
+ if (!startup_status_.Complete())
+ return;
+
+ stats::LogControllerStartupStatus(startup_status_);
+ if (!startup_status_.Ok()) {
+ // TODO(dtrainor): Recover here. Try to clean up any disk state and, if
+ // possible, any DownloadDriver data and continue with initialization?
+ return;
+ }
+
+ CancelOrphanedRequests();
+ ResolveInitialRequestStates();
+ PullCurrentRequestStatus();
+
+ // TODO(dtrainor): Post this so that the initialization step is finalized
+ // before Clients can take action.
+ NotifyClientsOfStartup();
+}
+
+void ControllerImpl::CancelOrphanedRequests() {
+ auto entries = model_->PeekEntries();
+
+ std::vector<std::string> guids_to_remove;
+ for (auto* entry : entries) {
+ if (!clients_->GetClient(entry->client))
+ guids_to_remove.push_back(entry->guid);
+ }
+
+ for (auto guid : guids_to_remove) {
+ // TODO(dtrainor): Remove the download.
+ }
+}
+
+void ControllerImpl::ResolveInitialRequestStates() {
+ // TODO(dtrainor): Implement.
+}
+
+void ControllerImpl::PullCurrentRequestStatus() {
+ // TODO(dtrainor): Implement.
+}
+
+void ControllerImpl::NotifyClientsOfStartup() {
+ auto categorized = util::MapEntriesToClients(clients_->GetRegisteredClients(),
+ model_->PeekEntries());
+
+ for (auto client_id : clients_->GetRegisteredClients()) {
+ clients_->GetClient(client_id)->OnServiceInitialized(
+ categorized[client_id]);
+ }
+}
+
+void ControllerImpl::HandleStartDownloadResponse(
+ DownloadClient client,
+ const std::string& guid,
+ DownloadParams::StartResult result) {
+ auto callback = start_callbacks_[guid];
+ start_callbacks_.erase(guid);
+ HandleStartDownloadResponse(client, guid, result, callback);
+}
+
+void ControllerImpl::HandleStartDownloadResponse(
+ DownloadClient client,
+ const std::string& guid,
+ DownloadParams::StartResult result,
+ const DownloadParams::StartCallback& callback) {
+ stats::LogStartDownloadResult(client, result);
+
+ if (result != DownloadParams::StartResult::ACCEPTED) {
+ // TODO(dtrainor): Clean up any download state.
+ }
+
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(callback, guid, result));
+}
+
+} // namespace download
« no previous file with comments | « components/download/internal/controller_impl.h ('k') | components/download/internal/controller_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698