Commit dc273049 authored by Min Qin's avatar Min Qin Committed by Commit Bot

Skeleton code for propagating UI to DownloadShelf for OfflineItems

This CL introduces the skeleton UI code for OfflineItems.
see https://docs.google.com/document/d/1xDRCoDYnOb9PvwJ8juSmG75jsE-YKzNomCMaoFtQWtQ/edit#

BUG=881499

Change-Id: Ie31f6bcc77c65a4343f2e5a0be54f3b9b5fdd5bc
Reviewed-on: https://chromium-review.googlesource.com/1211445
Commit-Queue: Min Qin <qinmin@chromium.org>
Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589720}
parent e2c5c0bc
...@@ -439,6 +439,12 @@ jumbo_split_static_library("browser") { ...@@ -439,6 +439,12 @@ jumbo_split_static_library("browser") {
"download/drag_download_item.h", "download/drag_download_item.h",
"download/image_thumbnail_request.cc", "download/image_thumbnail_request.cc",
"download/image_thumbnail_request.h", "download/image_thumbnail_request.h",
"download/offline_item_model.cc",
"download/offline_item_model.h",
"download/offline_item_model_manager.cc",
"download/offline_item_model_manager.h",
"download/offline_item_model_manager_factory.cc",
"download/offline_item_model_manager_factory.h",
"download/offline_item_utils.cc", "download/offline_item_utils.cc",
"download/offline_item_utils.h", "download/offline_item_utils.h",
"download/save_package_file_picker.cc", "download/save_package_file_picker.cc",
...@@ -2569,6 +2575,8 @@ jumbo_split_static_library("browser") { ...@@ -2569,6 +2575,8 @@ jumbo_split_static_library("browser") {
"download/download_shelf.h", "download/download_shelf.h",
"download/download_shelf_context_menu.cc", "download/download_shelf_context_menu.cc",
"download/download_shelf_context_menu.h", "download/download_shelf_context_menu.h",
"download/download_shelf_controller.cc",
"download/download_shelf_controller.h",
"feedback/feedback_dialog_utils.cc", "feedback/feedback_dialog_utils.cc",
"feedback/feedback_dialog_utils.h", "feedback/feedback_dialog_utils.h",
"feedback/feedback_profile_observer.cc", "feedback/feedback_profile_observer.cc",
......
// Copyright 2018 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 "chrome/browser/download/download_shelf_controller.h"
#include "chrome/browser/download/offline_item_model_manager.h"
#include "chrome/browser/download/offline_item_model_manager_factory.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "components/offline_items_collection/core/offline_item.h"
using offline_items_collection::OfflineItemState;
DownloadShelfController::DownloadShelfController(Profile* profile)
: profile_(profile) {}
DownloadShelfController::~DownloadShelfController() = default;
void DownloadShelfController::OnItemsAdded(
const OfflineContentProvider::OfflineItemList& items) {
for (const auto& item : items)
OnItemUpdated(item);
}
void DownloadShelfController::OnItemRemoved(const ContentId& id) {
OfflineItemModelManagerFactory::GetForBrowserContext(profile_)
->RemoveOfflineItemModel(id);
}
void DownloadShelfController::OnItemUpdated(const OfflineItem& item) {
if (item.state == OfflineItemState::CANCELLED)
return;
OfflineItemModelManager* manager =
OfflineItemModelManagerFactory::GetForBrowserContext(profile_);
OfflineItemModel* model = manager->GetOrCreateOfflineItemModel(item);
if (!model->was_ui_notified()) {
OnNewOfflineItemReady(item);
model->set_was_ui_notified(true);
}
}
void DownloadShelfController::OnNewOfflineItemReady(const OfflineItem& item) {
Browser* browser = browser = chrome::FindLastActiveWithProfile(profile_);
if (browser && browser->window()) {
// Add the offline item to DownloadShelf in the browser window.
}
}
// Copyright 2018 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.
#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SHELF_CONTROLLER_H_
#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SHELF_CONTROLLER_H_
#include <map>
#include "base/macros.h"
#include "chrome/browser/download/offline_item_model.h"
#include "components/offline_items_collection/core/offline_content_provider.h"
class Profile;
using ContentId = offline_items_collection::ContentId;
using OfflineContentProvider = offline_items_collection::OfflineContentProvider;
using OfflineItem = offline_items_collection::OfflineItem;
// Class for notifying UI when an OfflineItem should be displayed.
class DownloadShelfController : public OfflineContentProvider::Observer {
public:
explicit DownloadShelfController(Profile* profile);
~DownloadShelfController() override;
private:
// OfflineContentProvider::Observer implementation.
void OnItemsAdded(
const OfflineContentProvider::OfflineItemList& items) override;
void OnItemRemoved(const ContentId& id) override;
void OnItemUpdated(const OfflineItem& item) override;
// Called when a new OfflineItem is to be displayed on UI.
void OnNewOfflineItemReady(const OfflineItem& item);
Profile* profile_;
DISALLOW_COPY_AND_ASSIGN(DownloadShelfController);
};
#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SHELF_CONTROLLER_H_
// Copyright 2018 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 "chrome/browser/download/offline_item_model.h"
OfflineItemModel::OfflineItemModel(
const offline_items_collection::OfflineItem& offline_item)
: was_ui_notified_(false) {}
OfflineItemModel::~OfflineItemModel() = default;
// Copyright 2018 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.
#ifndef CHROME_BROWSER_DOWNLOAD_OFFLINE_ITEM_MODEL_H_
#define CHROME_BROWSER_DOWNLOAD_OFFLINE_ITEM_MODEL_H_
#include "components/offline_items_collection/core/offline_item.h"
// This class is an abstraction for common UI tasks and properties associated
// with an OfflineItem.
class OfflineItemModel {
public:
// Constructs a OfflineItemModel.
explicit OfflineItemModel(
const offline_items_collection::OfflineItem& offline_item);
~OfflineItemModel();
bool was_ui_notified() const { return was_ui_notified_; }
void set_was_ui_notified(bool was_ui_notified) {
was_ui_notified_ = was_ui_notified;
}
private:
bool was_ui_notified_;
DISALLOW_COPY_AND_ASSIGN(OfflineItemModel);
};
#endif // CHROME_BROWSER_DOWNLOAD_OFFLINE_ITEM_MODEL_H_
// Copyright 2018 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 "chrome/browser/download/offline_item_model_manager.h"
OfflineItemModelManager::OfflineItemModelManager() = default;
OfflineItemModelManager::~OfflineItemModelManager() = default;
OfflineItemModel* OfflineItemModelManager::GetOrCreateOfflineItemModel(
const OfflineItem& offline_item) {
auto it = offline_item_models_.find(offline_item.id);
if (it != offline_item_models_.end())
return it->second.get();
offline_item_models_[offline_item.id] =
std::make_unique<OfflineItemModel>(offline_item);
return offline_item_models_[offline_item.id].get();
}
void OfflineItemModelManager::RemoveOfflineItemModel(const ContentId& id) {
offline_item_models_.erase(id);
}
// Copyright 2018 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.
#ifndef CHROME_BROWSER_DOWNLOAD_OFFLINE_ITEM_MODEL_MANAGER_H_
#define CHROME_BROWSER_DOWNLOAD_OFFLINE_ITEM_MODEL_MANAGER_H_
#include <memory>
#include "chrome/browser/download/offline_item_model.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/offline_items_collection/core/offline_item.h"
using ContentId = offline_items_collection::ContentId;
using OfflineItem = offline_items_collection::OfflineItem;
// Class for managing all the OfflineModels for a profile.
class OfflineItemModelManager : public KeyedService {
public:
// Constructs a OfflineItemModelManager.
OfflineItemModelManager();
~OfflineItemModelManager() override;
// Returns the OfflineItemModel for the ContentId, if not found, an empty
// OfflineItemModel will be created and returned.
OfflineItemModel* GetOrCreateOfflineItemModel(
const OfflineItem& offline_item);
void RemoveOfflineItemModel(const ContentId& id);
private:
std::map<ContentId, std::unique_ptr<OfflineItemModel>> offline_item_models_;
DISALLOW_COPY_AND_ASSIGN(OfflineItemModelManager);
};
#endif // CHROME_BROWSER_DOWNLOAD_OFFLINE_ITEM_MODEL_MANAGER_H_
// Copyright 2018 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 "chrome/browser/download/offline_item_model_manager_factory.h"
#include "base/memory/singleton.h"
#include "chrome/browser/download/offline_item_model_manager.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_context.h"
// static
OfflineItemModelManagerFactory* OfflineItemModelManagerFactory::GetInstance() {
return base::Singleton<OfflineItemModelManagerFactory>::get();
}
// static
OfflineItemModelManager* OfflineItemModelManagerFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<OfflineItemModelManager*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
OfflineItemModelManagerFactory::OfflineItemModelManagerFactory()
: BrowserContextKeyedServiceFactory(
"OfflineItemModelManager",
BrowserContextDependencyManager::GetInstance()) {}
OfflineItemModelManagerFactory::~OfflineItemModelManagerFactory() = default;
KeyedService* OfflineItemModelManagerFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new OfflineItemModelManager();
}
// Copyright 2018 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.
#ifndef CHROME_BROWSER_DOWNLOAD_OFFLINE_ITEM_MODEL_MANAGER_FACTORY_H_
#define CHROME_BROWSER_DOWNLOAD_OFFLINE_ITEM_MODEL_MANAGER_FACTORY_H_
#include "base/macros.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class OfflineItemModelManager;
namespace base {
template <typename T>
struct DefaultSingletonTraits;
} // namespace base
namespace content {
class BrowserContext;
} // namespace content
// This class is the main access point for an OfflineItemModelManager. It is
// responsible for building the OfflineItemModelManager and associating it with
// a particular content::BrowserContext.
class OfflineItemModelManagerFactory
: public BrowserContextKeyedServiceFactory {
public:
// Returns a singleton instance of an OfflineItemModelManagerFactory.
static OfflineItemModelManagerFactory* GetInstance();
// Returns the OfflineItemModelManager associated with |context| or creates
// and associates one if it doesn't exist.
static OfflineItemModelManager* GetForBrowserContext(
content::BrowserContext* context);
private:
friend struct base::DefaultSingletonTraits<OfflineItemModelManagerFactory>;
OfflineItemModelManagerFactory();
~OfflineItemModelManagerFactory() override;
// BrowserContextKeyedServiceFactory implementation.
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
DISALLOW_COPY_AND_ASSIGN(OfflineItemModelManagerFactory);
};
#endif // CHROME_BROWSER_DOWNLOAD_OFFLINE_ITEM_MODEL_MANAGER_FACTORY_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment