Chromium Code Reviews| Index: chrome/browser/background_fetch/background_fetch_client_impl.cc |
| diff --git a/chrome/browser/background_fetch/background_fetch_client_impl.cc b/chrome/browser/background_fetch/background_fetch_client_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1f5ba83f7b01f8e8637da7a551bb19a2d6218708 |
| --- /dev/null |
| +++ b/chrome/browser/background_fetch/background_fetch_client_impl.cc |
| @@ -0,0 +1,131 @@ |
| +// 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 <sstream> |
| +#include <string> |
| + |
| +#include "chrome/browser/background_fetch/background_fetch_client_impl.h" |
| + |
| +#include "chrome/browser/offline_items_collection/offline_content_aggregator_factory.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "components/offline_items_collection/core/offline_content_aggregator.h" |
| +#include "components/offline_items_collection/core/offline_item.h" |
| + |
| +namespace { |
| + |
| +const char kBackgroundFetchNamespace[] = "BackgroundFetchNamespace"; |
| + |
| +} // namespace |
| + |
| +using offline_items_collection::ContentId; |
| +using offline_items_collection::OfflineContentAggregator; |
| +using offline_items_collection::OfflineContentAggregatorFactory; |
| +using offline_items_collection::OfflineContentProvider; |
| +using offline_items_collection::OfflineItem; |
| + |
| +BackgroundFetchClientImpl::BackgroundFetchClientImpl(Profile* profile) |
| + : profile_(profile), namespace_(kBackgroundFetchNamespace) { |
| + DCHECK(profile_); |
| + if (profile->IsOffTheRecord()) { |
| + // There can be multiple incognito namespaces, so append the memory address |
| + // of this client to provide a guaranteed unique namespace. |
| + std::ostringstream suffix; |
| + suffix << "Incognito" << this; |
| + namespace_ += suffix.str(); |
| + } |
| + |
| + OfflineContentAggregator* aggregator = |
| + OfflineContentAggregatorFactory::GetForBrowserContext(profile_); |
| + if (!aggregator) |
| + return; |
| + aggregator->RegisterProvider(namespace_, this); |
| +} |
| + |
| +BackgroundFetchClientImpl::~BackgroundFetchClientImpl() = default; |
| + |
| +void BackgroundFetchClientImpl::Shutdown() { |
| + OfflineContentAggregator* aggregator = |
| + OfflineContentAggregatorFactory::GetForBrowserContext(profile_); |
| + if (!aggregator) |
| + return; |
| + |
| + aggregator->UnregisterProvider(namespace_); |
| + |
| + // If the profile is an incognito profile, clean up all data. |
| + if (delegate_ && namespace_ != kBackgroundFetchNamespace) |
| + delegate_->CleanupAllTasks(); |
| +} |
| + |
| +void BackgroundFetchClientImpl::SetDelegate( |
| + content::BackgroundFetchClient::Delegate* delegate) { |
| + delegate_ = delegate; |
| +} |
| + |
| +void BackgroundFetchClientImpl::CancelDownload(const ContentId& content_id) { |
| + DCHECK_EQ(content_id.name_space, namespace_); |
| + |
| + if (!delegate_) |
| + return; |
| + |
| + delegate_->CancelDownload(content_id.id); |
| +} |
| + |
| +void BackgroundFetchClientImpl::PauseDownload(const ContentId& content_id) { |
| + DCHECK_EQ(content_id.name_space, namespace_); |
| + |
| + if (!delegate_) |
| + return; |
| + |
| + delegate_->PauseDownload(content_id.id); |
| +} |
| + |
| +void BackgroundFetchClientImpl::ResumeDownload(const ContentId& content_id) { |
| + DCHECK_EQ(content_id.name_space, namespace_); |
| + |
| + if (!delegate_) |
| + return; |
| + |
| + delegate_->ResumeDownload(content_id.id); |
| +} |
| + |
| +void BackgroundFetchClientImpl::OpenItem(const ContentId& content_id) { |
| + // TODO(harkness): Add another call to BackgroundFetchClient::Delegate and |
| + // plumb this to 'onclickevent' to the ServiceWorker. |
| +} |
| + |
| +void BackgroundFetchClientImpl::GetVisualsForItem( |
| + const ContentId& id, |
| + const VisualsCallback& callback) { |
| + // TODO(harkness): Update with icons. |
| + callback.Run(id, nullptr); |
|
David Trainor- moved to gerrit
2017/04/19 22:45:12
Can you post this for consistent behavior? Just p
|
| +} |
| + |
| +void BackgroundFetchClientImpl::AddObserver(Observer* observer) { |
| + // TODO(harkness): Add a path for the OfflineContentProvider to observe |
| + // changes in available BackgroundFetch items. |
| +} |
| + |
| +void BackgroundFetchClientImpl::RemoveObserver(Observer* observer) {} |
| + |
| +bool BackgroundFetchClientImpl::AreItemsAvailable() { |
| + // TODO(harkness): Follow up with dtrainor about what action to take for this. |
| + return false; |
| +} |
| + |
| +void BackgroundFetchClientImpl::RemoveItem(const ContentId& content_id) { |
| + // Not applicable for Background Fetch, since offline items don't exist in the |
| + // offline content system beyond the duration of the download. |
| +} |
| + |
| +const OfflineItem* BackgroundFetchClientImpl::GetItemById( |
| + const ContentId& content_id) { |
| + // TODO(harkness): Follow up with dtrainor about what action to take for this. |
| + return nullptr; |
| +} |
| + |
| +OfflineContentProvider::OfflineItemList |
| +BackgroundFetchClientImpl::GetAllItems() { |
| + // TODO(harkness): Follow up with dtrainor about what action to take for this. |
| + return OfflineContentProvider::OfflineItemList(); |
| +} |