Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_OFFLINE_CONTENT_CORE_OFFLINE_CONTENT_AGGREGATOR_H_ | |
| 6 #define COMPONENTS_OFFLINE_CONTENT_CORE_OFFLINE_CONTENT_AGGREGATOR_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/callback_forward.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/observer_list.h" | |
| 15 #include "base/supports_user_data.h" | |
| 16 #include "components/offline_content/core/offline_content_provider.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace offline_content { | |
| 20 | |
| 21 struct OfflineItem; | |
| 22 | |
| 23 // An implementation of OfflineContentProvider that aggregates multiple other | |
| 24 // providers into a single set of data. See the OfflineContentProvider header | |
| 25 // for comments on expected behavior of the interface. This implementation has | |
| 26 // a few caveats: | |
| 27 // - Once all currently registered providers are initialized this provider will | |
| 28 // trigger OnItemsAvailable on all observers. Until then the provider will | |
| 29 // not be initialized. | |
| 30 // - If a provider is added after OnItemsAvailable was sent, it's initialization | |
| 31 // will act as a notification for OnItemsAdded. This provider will still be | |
| 32 // in the initialized state. | |
| 33 // - Calling any modification method on this provider (Open, Update, Delete, | |
| 34 // etc.) on an OfflineItem belonging to an uninitialized | |
| 35 // OfflineContentProvider will be queued until that provider is initialized. | |
| 36 // NOTE: Any actions taken will be propagated to the provider *before* the | |
| 37 // observers are notified that the provider is initialized. This is meant to | |
| 38 // try to guarantee that the data set incorporates the results of those | |
| 39 // actions. | |
| 40 // | |
| 41 // Routing to the correct provider: | |
| 42 // - Providers must be registered with a unique namespace. The OfflineItems | |
| 43 // created by the provider must also be tagged with the same namespace so that | |
| 44 // actions taken on the OfflineItem can be routed to the correct internal | |
| 45 // provider. The namespace must also be consistent across startups. | |
| 46 class OfflineContentAggregator : public OfflineContentProvider, | |
| 47 public OfflineContentProvider::Observer, | |
| 48 public base::SupportsUserData, | |
| 49 public base::SupportsUserData::Data { | |
| 50 public: | |
| 51 OfflineContentAggregator(); | |
| 52 ~OfflineContentAggregator() override; | |
| 53 | |
| 54 // Registers a provider and associates it with all OfflineItems with | |
| 55 // |name_space|. UI actions taken on OfflineItems with |name_space| will be | |
| 56 // routed to |provider|. |provider| is expected to only expose OfflineItems | |
| 57 // with |name_space| set. | |
| 58 void RegisterProvider(const std::string& name_space, | |
| 59 OfflineContentProvider* provider); | |
| 60 | |
| 61 // Removes the OfflineContentProvider associated with |name_space| from this | |
| 62 // aggregator. | |
| 63 void UnregisterProvider(const std::string& name_space); | |
| 64 | |
| 65 // OfflineContentProvider implementation. | |
| 66 bool AreItemsAvailable() override; | |
| 67 void OpenItem(const ContentId& id) override; | |
| 68 void RemoveItem(const ContentId& id) override; | |
| 69 void CancelDownload(const ContentId& id) override; | |
| 70 void PauseDownload(const ContentId& id) override; | |
| 71 void ResumeDownload(const ContentId& id) override; | |
| 72 const OfflineItem* GetItemById(const ContentId& id) override; | |
| 73 OfflineItemList GetAllItems() override; | |
| 74 void AddObserver(OfflineContentProvider::Observer* observer) override; | |
| 75 void RemoveObserver(OfflineContentProvider::Observer* observer) override; | |
| 76 | |
| 77 private: | |
| 78 // OfflineContentProvider::Observer implementation. | |
| 79 void OnItemsAvailable(OfflineContentProvider* provider) override; | |
| 80 void OnItemsAdded(const OfflineItemList& items) override; | |
| 81 void OnItemRemoved(const ContentId& id) override; | |
| 82 void OnItemUpdated(const OfflineItem& item) override; | |
| 83 | |
| 84 // Checks if the underlying OfflineContentProviders are available. If so, | |
| 85 // it calls OnItemsAvailable on all observers that haven't yet been notified | |
| 86 // of this. | |
| 87 void CheckAndNotifyItemsAvailable(); | |
| 88 | |
| 89 // Checks to see if |provider| is initialized. If so, this flushes any | |
| 90 // pending actions taken on OfflineItems that belong to |provider|. | |
| 91 void FlushPendingActionsIfReady(OfflineContentProvider* provider); | |
| 92 | |
| 93 // Checks if |provider| is initialized. If so, runs |action|, otherwise | |
| 94 // queues it to run once |provider| triggers that it is ready. | |
| 95 void RunIfReady(OfflineContentProvider* provider, | |
| 96 const base::Closure& action); | |
| 97 | |
| 98 // Stores a map of name_space -> OfflineContentProvider. These | |
| 99 // OfflineContentProviders are all aggregated by this class and exposed to the | |
| 100 // consumer as a single list. | |
| 101 // TODO(me): Add a WeakPtrFactory to this map? | |
|
David Trainor- moved to gerrit
2017/02/22 01:23:13
TODO(me): Remove comment!
| |
| 102 using OfflineProviderMap = std::map<std::string, OfflineContentProvider*>; | |
| 103 OfflineProviderMap providers_; | |
| 104 | |
| 105 // Stores a map of OfflineContentProvider -> list of closures that represent | |
| 106 // all actions that need to be taken on the associated OfflineContentProvider | |
| 107 // when it becomes initialized. | |
| 108 using CallbackList = std::vector<base::Closure>; | |
| 109 using PendingActionMap = std::map<OfflineContentProvider*, CallbackList>; | |
| 110 PendingActionMap pending_actions_; | |
| 111 | |
| 112 // A list of all currently registered observers. | |
| 113 base::ObserverList<OfflineContentProvider::Observer> observers_; | |
| 114 | |
| 115 // A list of observers that have been notified that this class is initialized. | |
| 116 // We do not want to notify them of this initialization more than once, so | |
| 117 // we track them here. | |
| 118 using ObserverVector = std::vector<OfflineContentProvider::Observer*>; | |
| 119 ObserverVector signaled_observers_; | |
| 120 | |
| 121 // Whether or not this class currently identifies itself as available and has | |
| 122 // notified the observers. | |
| 123 bool sent_on_items_available_; | |
| 124 | |
| 125 base::WeakPtrFactory<OfflineContentAggregator> weak_ptr_factory_; | |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(OfflineContentAggregator); | |
| 128 }; | |
| 129 | |
| 130 } // namespace offline_content | |
| 131 | |
| 132 #endif // COMPONENTS_OFFLINE_CONTENT_CORE_OFFLINE_CONTENT_AGGREGATOR_H_ | |
| OLD | NEW |