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

Side by Side Diff: components/offline_content/core/offline_content_aggregator.h

Issue 2690333002: Initial checkin of OfflineContentProvider. (Closed)
Patch Set: ItemsAvailable -> AreItemsAvailable Created 3 years, 10 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
(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 //
37 // Routing to the correct provider:
38 // - Providers must be registered with a unique namespace. The OfflineItems
39 // created by the provider must also be tagged with the same namespace so that
40 // actions taken on the OfflineItem can be routed to the correct internal
41 // provider. The namespace must also be consistent across startups.
42 class OfflineContentAggregator : public OfflineContentProvider,
43 public OfflineContentProvider::Observer,
44 public base::SupportsUserData,
45 public base::SupportsUserData::Data {
46 public:
47 OfflineContentAggregator();
48 ~OfflineContentAggregator() override;
49
50 // Registers a provider and associates it with all OfflineItems with
51 // |name_space|. UI actions taken on OfflineItems with |name_space| will be
52 // routed to |provider|. |provider| is expected to only expose OfflineItems
53 // with |name_space| set.
54 void RegisterProvider(const std::string& name_space,
55 OfflineContentProvider* provider);
56
57 // Removes the OfflineContentProvider associated with |name_space| from this
58 // aggregator.
59 void UnregisterProvider(const std::string& name_space);
60
61 // OfflineContentProvider implementation.
62 bool AreItemsAvailable() override;
63 void OpenItem(const ContentId& id) override;
64 void RemoveItem(const ContentId& id) override;
65 void CancelDownload(const ContentId& id) override;
66 void PauseDownload(const ContentId& id) override;
67 void ResumeDownload(const ContentId& id) override;
68 OfflineItem* GetItemById(const ContentId& id) override;
69 OfflineItemList GetAllItems() override;
70 void AddObserver(OfflineContentProvider::Observer* observer) override;
71 void RemoveObserver(OfflineContentProvider::Observer* observer) override;
72
73 private:
74 // OfflineContentProvider::Observer implementation.
75 void OnItemsAvailable(OfflineContentProvider* provider) override;
76 void OnItemsAdded(const OfflineItemList& items) override;
77 void OnItemRemoved(const ContentId& id) override;
78 void OnItemUpdated(const OfflineItem& item) override;
79
80 // Checks if the underlying OfflineContentProviders are available. If so,
81 // it calls OnItemsAvailable on all observers that haven't yet been notified
82 // of this.
83 void CheckAndNotifyItemsAvailable();
84
85 // Checks to see if |provider| is initialized. If so, this flushes any
86 // pending actions taken on OfflineItems that belong to |provider|.
87 void FlushPendingActionsIfReady(OfflineContentProvider* provider);
88
89 // Stores a map of name_space -> OfflineContentProvider. These
90 // OfflineContentProviders are all aggregated by this class and exposed to the
91 // consumer as a single list.
92 using OfflineProviderMap = std::map<std::string, OfflineContentProvider*>;
93 OfflineProviderMap providers_;
94
95 // Stores a map of name_space -> list of closures that represent all actions
96 // that need to be taken on the associated OfflineContentProvider when it
97 // becomes initialized.
98 using CallbackList = std::vector<base::Closure>;
99 using PendingActionMap = std::map<std::string, CallbackList>;
100 PendingActionMap pending_actions_;
101
102 // A list of all currently registered observers.
103 base::ObserverList<OfflineContentProvider::Observer> observers_;
104
105 // A list of observers that have been notified that this class is initialized.
106 // We do not want to notify them of this initialization more than once, so
107 // we track them here.
108 using ObserverVector = std::vector<OfflineContentProvider::Observer*>;
109 ObserverVector signaled_observers_;
110
111 // Whether or not this class currently identifies itself as available and has
112 // notified the observers.
113 bool sent_on_items_available_;
114
115 base::WeakPtrFactory<OfflineContentAggregator> weak_ptr_factory_;
116
117 DISALLOW_COPY_AND_ASSIGN(OfflineContentAggregator);
118 };
119
120 } // namespace offline_content
121
122 #endif // COMPONENTS_OFFLINE_CONTENT_CORE_OFFLINE_CONTENT_AGGREGATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698