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

Side by Side Diff: components/offline_pages/core/downloads/download_ui_adapter.cc

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: update Created 4 years, 1 month 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/offline_pages/downloads/download_ui_adapter.h" 5 #include "components/offline_pages/core/downloads/download_ui_adapter.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/guid.h" 8 #include "base/guid.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/threading/thread_task_runner_handle.h" 11 #include "base/threading/thread_task_runner_handle.h"
12 #include "components/offline_pages/client_namespace_constants.h" 12 #include "components/offline_pages/core/client_namespace_constants.h"
13 #include "components/offline_pages/client_policy_controller.h" 13 #include "components/offline_pages/core/client_policy_controller.h"
14 #include "components/offline_pages/downloads/download_ui_item.h" 14 #include "components/offline_pages/core/downloads/download_ui_item.h"
15 #include "components/offline_pages/offline_page_model.h" 15 #include "components/offline_pages/core/offline_page_model.h"
16 16
17 namespace offline_pages { 17 namespace offline_pages {
18 18
19 namespace { 19 namespace {
20 const char kDownloadUIAdapterKey[] = "download-ui-adapter"; 20 const char kDownloadUIAdapterKey[] = "download-ui-adapter";
21 } 21 }
22 22
23 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page) 23 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page)
24 : ui_item(base::MakeUnique<DownloadUIItem>(page)), 24 : ui_item(base::MakeUnique<DownloadUIItem>(page)),
25 offline_id(page.offline_id) {} 25 offline_id(page.offline_id) {}
26 26
27 DownloadUIAdapter::ItemInfo::~ItemInfo() {} 27 DownloadUIAdapter::ItemInfo::~ItemInfo() {}
28 28
29 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model) 29 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model)
30 : model_(model), 30 : model_(model),
31 state_(State::NOT_LOADED), 31 state_(State::NOT_LOADED),
32 observers_count_(0), 32 observers_count_(0),
33 weak_ptr_factory_(this) { 33 weak_ptr_factory_(this) {}
34 }
35 34
36 DownloadUIAdapter::~DownloadUIAdapter() { } 35 DownloadUIAdapter::~DownloadUIAdapter() {}
37 36
38 // static 37 // static
39 DownloadUIAdapter* DownloadUIAdapter::FromOfflinePageModel( 38 DownloadUIAdapter* DownloadUIAdapter::FromOfflinePageModel(
40 OfflinePageModel* offline_page_model) { 39 OfflinePageModel* offline_page_model) {
41 DownloadUIAdapter* adapter = static_cast<DownloadUIAdapter*>( 40 DownloadUIAdapter* adapter = static_cast<DownloadUIAdapter*>(
42 offline_page_model->GetUserData(kDownloadUIAdapterKey)); 41 offline_page_model->GetUserData(kDownloadUIAdapterKey));
43 if (!adapter) { 42 if (!adapter) {
44 adapter = new DownloadUIAdapter(offline_page_model); 43 adapter = new DownloadUIAdapter(offline_page_model);
45 offline_page_model->SetUserData(kDownloadUIAdapterKey, adapter); 44 offline_page_model->SetUserData(kDownloadUIAdapterKey, adapter);
46 } 45 }
47 return adapter; 46 return adapter;
48 } 47 }
49 48
50 void DownloadUIAdapter::AddObserver(Observer* observer) { 49 void DownloadUIAdapter::AddObserver(Observer* observer) {
51 DCHECK(observer); 50 DCHECK(observer);
52 if (observers_.HasObserver(observer)) 51 if (observers_.HasObserver(observer))
53 return; 52 return;
54 if (observers_count_ == 0) 53 if (observers_count_ == 0)
55 LoadCache(); 54 LoadCache();
56 observers_.AddObserver(observer); 55 observers_.AddObserver(observer);
57 ++observers_count_; 56 ++observers_count_;
58 // If the items are already loaded, post the notification right away. 57 // If the items are already loaded, post the notification right away.
59 // Don't just invoke it from here to avoid reentrancy in the client. 58 // Don't just invoke it from here to avoid reentrancy in the client.
60 if (state_ == State::LOADED) { 59 if (state_ == State::LOADED) {
61 base::ThreadTaskRunnerHandle::Get()->PostTask( 60 base::ThreadTaskRunnerHandle::Get()->PostTask(
62 FROM_HERE, base::Bind(&DownloadUIAdapter::NotifyItemsLoaded, 61 FROM_HERE,
63 weak_ptr_factory_.GetWeakPtr(), 62 base::Bind(&DownloadUIAdapter::NotifyItemsLoaded,
64 base::Unretained(observer))); 63 weak_ptr_factory_.GetWeakPtr(), base::Unretained(observer)));
65 } 64 }
66 } 65 }
67 66
68 void DownloadUIAdapter::RemoveObserver(Observer* observer) { 67 void DownloadUIAdapter::RemoveObserver(Observer* observer) {
69 DCHECK(observer); 68 DCHECK(observer);
70 if (!observers_.HasObserver(observer)) 69 if (!observers_.HasObserver(observer))
71 return; 70 return;
72 observers_.RemoveObserver(observer); 71 observers_.RemoveObserver(observer);
73 --observers_count_; 72 --observers_count_;
74 // Once the last observer is gone, clear cached data. 73 // Once the last observer is gone, clear cached data.
75 if (observers_count_ == 0) 74 if (observers_count_ == 0)
76 ClearCache(); 75 ClearCache();
77 } 76 }
78 77
79 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { 78 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) {
80 // This signal is not used here. 79 // This signal is not used here.
81 } 80 }
82 81
83 void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) { 82 void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) {
84 DCHECK(model == model_); 83 DCHECK(model == model_);
85 model_->GetAllPages( 84 model_->GetAllPages(base::Bind(&DownloadUIAdapter::OnOfflinePagesChanged,
86 base::Bind(&DownloadUIAdapter::OnOfflinePagesChanged, 85 weak_ptr_factory_.GetWeakPtr()));
87 weak_ptr_factory_.GetWeakPtr()));
88 } 86 }
89 87
90 void DownloadUIAdapter::OfflinePageDeleted( 88 void DownloadUIAdapter::OfflinePageDeleted(int64_t offline_id,
91 int64_t offline_id, const ClientId& client_id) { 89 const ClientId& client_id) {
92 if (!IsVisibleInUI(client_id)) 90 if (!IsVisibleInUI(client_id))
93 return; 91 return;
94 std::string guid = client_id.id; 92 std::string guid = client_id.id;
95 DownloadUIItems::const_iterator it = items_.find(guid); 93 DownloadUIItems::const_iterator it = items_.find(guid);
96 if (it == items_.end()) 94 if (it == items_.end())
97 return; 95 return;
98 items_.erase(it); 96 items_.erase(it);
99 for (Observer& observer : observers_) 97 for (Observer& observer : observers_)
100 observer.ItemDeleted(guid); 98 observer.ItemDeleted(guid);
101 } 99 }
102 100
103 std::vector<const DownloadUIItem*> DownloadUIAdapter::GetAllItems() const { 101 std::vector<const DownloadUIItem*> DownloadUIAdapter::GetAllItems() const {
104 std::vector<const DownloadUIItem*> result; 102 std::vector<const DownloadUIItem*> result;
105 for (const auto& item : items_) 103 for (const auto& item : items_)
106 result.push_back(item.second->ui_item.get()); 104 result.push_back(item.second->ui_item.get());
107 return result; 105 return result;
108 } 106 }
109 107
110 const DownloadUIItem* 108 const DownloadUIItem* DownloadUIAdapter::GetItem(
111 DownloadUIAdapter::GetItem(const std::string& guid) const { 109 const std::string& guid) const {
112 DownloadUIItems::const_iterator it = items_.find(guid); 110 DownloadUIItems::const_iterator it = items_.find(guid);
113 if (it == items_.end()) 111 if (it == items_.end())
114 return nullptr; 112 return nullptr;
115 return it->second->ui_item.get(); 113 return it->second->ui_item.get();
116 } 114 }
117 115
118 void DownloadUIAdapter::DeleteItem(const std::string& guid) { 116 void DownloadUIAdapter::DeleteItem(const std::string& guid) {
119 // TODO(dimich): Also remove pending request from RequestQueue. 117 // TODO(dimich): Also remove pending request from RequestQueue.
120 DownloadUIItems::const_iterator it = items_.find(guid); 118 DownloadUIItems::const_iterator it = items_.find(guid);
121 if (it == items_.end()) 119 if (it == items_.end())
122 return; 120 return;
123 121
124 std::vector<int64_t> page_ids; 122 std::vector<int64_t> page_ids;
125 page_ids.push_back(it->second->offline_id); 123 page_ids.push_back(it->second->offline_id);
126 // TODO(dimich): This should be ExpirePages(...Now()..) when Expire is 124 // TODO(dimich): This should be ExpirePages(...Now()..) when Expire is
127 // firing Observer method. The resulting Observer notification will update 125 // firing Observer method. The resulting Observer notification will update
128 // local cache. 126 // local cache.
129 model_->DeletePagesByOfflineId( 127 model_->DeletePagesByOfflineId(
130 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, 128 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone,
131 weak_ptr_factory_.GetWeakPtr())); 129 weak_ptr_factory_.GetWeakPtr()));
132 } 130 }
133 131
134 int64_t DownloadUIAdapter::GetOfflineIdByGuid( 132 int64_t DownloadUIAdapter::GetOfflineIdByGuid(const std::string& guid) const {
135 const std::string& guid) const {
136 // TODO(dimich): when requests are also in the cache, filter them out. 133 // TODO(dimich): when requests are also in the cache, filter them out.
137 // Requests do not yet have offline ID. 134 // Requests do not yet have offline ID.
138 DownloadUIItems::const_iterator it = items_.find(guid); 135 DownloadUIItems::const_iterator it = items_.find(guid);
139 if (it != items_.end()) 136 if (it != items_.end())
140 return it->second->offline_id; 137 return it->second->offline_id;
141 return 0; 138 return 0;
142 } 139 }
143 140
144 // Note that several LoadCache calls may be issued before the async GetAllPages 141 // Note that several LoadCache calls may be issued before the async GetAllPages
145 // comes back. 142 // comes back.
146 void DownloadUIAdapter::LoadCache() { 143 void DownloadUIAdapter::LoadCache() {
147 // TODO(dimich): Add fetching from RequestQueue as well. 144 // TODO(dimich): Add fetching from RequestQueue as well.
148 state_ = State::LOADING; 145 state_ = State::LOADING;
149 model_->GetAllPages( 146 model_->GetAllPages(base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded,
150 base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded, 147 weak_ptr_factory_.GetWeakPtr()));
151 weak_ptr_factory_.GetWeakPtr()));
152 } 148 }
153 149
154 void DownloadUIAdapter::ClearCache() { 150 void DownloadUIAdapter::ClearCache() {
155 // Once loaded, this class starts to observe the model. Only remove observer 151 // Once loaded, this class starts to observe the model. Only remove observer
156 // if it was added. 152 // if it was added.
157 if (state_ == State::LOADED) 153 if (state_ == State::LOADED)
158 model_->RemoveObserver(this); 154 model_->RemoveObserver(this);
159 items_.clear(); 155 items_.clear();
160 state_ = State::NOT_LOADED; 156 state_ = State::NOT_LOADED;
161 } 157 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 // TODO(dimich): Consider adding UMA to record user actions. 207 // TODO(dimich): Consider adding UMA to record user actions.
212 } 208 }
213 209
214 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { 210 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) {
215 const std::string& name_space = client_id.name_space; 211 const std::string& name_space = client_id.name_space;
216 return model_->GetPolicyController()->IsSupportedByDownload(name_space) && 212 return model_->GetPolicyController()->IsSupportedByDownload(name_space) &&
217 base::IsValidGUID(client_id.id); 213 base::IsValidGUID(client_id.id);
218 } 214 }
219 215
220 } // namespace offline_pages 216 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698