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

Side by Side Diff: components/offline_pages/offline_page_test_store.cc

Issue 2497703002: [Offline pages] Resetting offline page metadata store to handle LOAD/INIT failures (Closed)
Patch Set: Addressing STORE_INIT_FAILED 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/offline_page_test_store.h" 5 #include "components/offline_pages/offline_page_test_store.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 namespace offline_pages { 13 namespace offline_pages {
14 14
15 OfflinePageTestStore::OfflinePageTestStore( 15 OfflinePageTestStore::OfflinePageTestStore(
16 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) 16 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
17 : task_runner_(task_runner), scenario_(TestScenario::SUCCESSFUL) {} 17 : task_runner_(task_runner),
18 scenario_(TestScenario::SUCCESSFUL),
19 store_state_(StoreState::NOT_LOADED) {}
18 20
19 OfflinePageTestStore::OfflinePageTestStore( 21 OfflinePageTestStore::OfflinePageTestStore(
20 const OfflinePageTestStore& other_store) 22 const OfflinePageTestStore& other_store)
21 : task_runner_(other_store.task_runner_), 23 : task_runner_(other_store.task_runner_),
22 scenario_(other_store.scenario_), 24 scenario_(other_store.scenario_),
23 offline_pages_(other_store.offline_pages_) {} 25 offline_pages_(other_store.offline_pages_) {}
24 26
25 OfflinePageTestStore::~OfflinePageTestStore() {} 27 OfflinePageTestStore::~OfflinePageTestStore() {}
26 28
27 void OfflinePageTestStore::GetOfflinePages(const LoadCallback& callback) { 29 void OfflinePageTestStore::Initialize(const InitializeCallback& callback) {
28 OfflinePageMetadataStore::LoadStatus load_status; 30 if (scenario_ == TestScenario::LOAD_FAILED_RESET_FAILED ||
29 if (scenario_ == TestScenario::LOAD_FAILED) { 31 scenario_ == TestScenario::LOAD_FAILED_RESET_SUCCESS) {
30 load_status = OfflinePageMetadataStore::STORE_LOAD_FAILED; 32 store_state_ = StoreState::FAILED_LOADING;
31 offline_pages_.clear(); 33 offline_pages_.clear();
32 } else { 34 } else {
33 load_status = OfflinePageMetadataStore::LOAD_SUCCEEDED; 35 store_state_ = StoreState::LOADED;
34 } 36 }
35 task_runner_->PostTask(FROM_HERE, 37 task_runner_->PostTask(FROM_HERE, base::Bind(callback, store_state_));
36 base::Bind(callback, load_status, GetAllPages())); 38 }
39
40 void OfflinePageTestStore::GetOfflinePages(const LoadCallback& callback) {
41 task_runner_->PostTask(FROM_HERE, base::Bind(callback, GetAllPages()));
37 } 42 }
38 43
39 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page, 44 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page,
40 const AddCallback& callback) { 45 const AddCallback& callback) {
41 // TODO(fgorski): Add and cover scenario with existing item. 46 // TODO(fgorski): Add and cover scenario with existing item.
42 ItemActionStatus result; 47 ItemActionStatus result;
43 if (scenario_ == TestScenario::SUCCESSFUL) { 48 if (scenario_ == TestScenario::SUCCESSFUL) {
44 offline_pages_[offline_page.offline_id] = offline_page; 49 offline_pages_[offline_page.offline_id] = offline_page;
45 last_saved_page_ = offline_page; 50 last_saved_page_ = offline_page;
46 result = ItemActionStatus::SUCCESS; 51 result = ItemActionStatus::SUCCESS;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 status = ItemActionStatus::NOT_FOUND; 109 status = ItemActionStatus::NOT_FOUND;
105 } 110 }
106 result->item_statuses.push_back(std::make_pair(offline_id, status)); 111 result->item_statuses.push_back(std::make_pair(offline_id, status));
107 } 112 }
108 } 113 }
109 114
110 task_runner_->PostTask(FROM_HERE, 115 task_runner_->PostTask(FROM_HERE,
111 base::Bind(callback, base::Passed(&result))); 116 base::Bind(callback, base::Passed(&result)));
112 } 117 }
113 118
114 void OfflinePageTestStore::Reset(const ResetCallback& callback) { 119 void OfflinePageTestStore::Reset(const InitializeCallback& callback) {
120 if (scenario_ == TestScenario::LOAD_FAILED_RESET_FAILED) {
121 store_state_ = StoreState::FAILED_RESET;
122 } else {
123 store_state_ = StoreState::LOADED;
124 // Scenario is flipped to successful here, as the reset succeeds.
125 if (scenario_ == TestScenario::LOAD_FAILED_RESET_SUCCESS)
126 scenario_ = TestScenario::SUCCESSFUL;
127 }
128
115 offline_pages_.clear(); 129 offline_pages_.clear();
116 task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); 130 task_runner_->PostTask(FROM_HERE, base::Bind(callback, store_state_));
117 } 131 }
118 132
119 StoreState OfflinePageTestStore::state() const { 133 StoreState OfflinePageTestStore::state() const {
120 return StoreState::LOADED; 134 return store_state_;
121 } 135 }
122 136
123 void OfflinePageTestStore::UpdateLastAccessTime( 137 void OfflinePageTestStore::UpdateLastAccessTime(
124 int64_t offline_id, 138 int64_t offline_id,
125 const base::Time& last_access_time) { 139 const base::Time& last_access_time) {
126 auto iter = offline_pages_.find(offline_id); 140 auto iter = offline_pages_.find(offline_id);
127 if (iter == offline_pages_.end()) 141 if (iter == offline_pages_.end())
128 return; 142 return;
129 iter->second.last_access_time = last_access_time; 143 iter->second.last_access_time = last_access_time;
130 } 144 }
131 145
132 std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const { 146 std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const {
133 std::vector<OfflinePageItem> offline_pages; 147 std::vector<OfflinePageItem> offline_pages;
134 for (const auto& id_page_pair : offline_pages_) 148 for (const auto& id_page_pair : offline_pages_)
135 offline_pages.push_back(id_page_pair.second); 149 offline_pages.push_back(id_page_pair.second);
136 return offline_pages; 150 return offline_pages;
137 } 151 }
138 152
139 void OfflinePageTestStore::ClearAllPages() { 153 void OfflinePageTestStore::ClearAllPages() {
140 offline_pages_.clear(); 154 offline_pages_.clear();
141 } 155 }
142 156
143 } // namespace offline_pages 157 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698