OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/offline_pages/background/initialize_store_task.h" | |
6 | |
7 #include "base/bind.h" | |
8 | |
9 namespace offline_pages { | |
10 | |
11 const int kRestartAttemptsMaximum = 3; | |
12 | |
13 InitializeStoreTask::InitializeStoreTask( | |
14 RequestQueueStore* store, | |
15 const RequestQueueStore::InitializeCallback& callback) | |
16 : store_(store), | |
17 reset_attempts_left_(kRestartAttemptsMaximum), | |
18 callback_(callback), | |
19 weak_ptr_factory_(this) {} | |
20 | |
21 InitializeStoreTask::~InitializeStoreTask() {} | |
22 | |
23 void InitializeStoreTask::Run() { | |
24 InitializeStore(); | |
25 } | |
26 | |
27 void InitializeStoreTask::InitializeStore() { | |
28 store_->Initialize(base::Bind(&InitializeStoreTask::CompleteIfSuccessful, | |
29 weak_ptr_factory_.GetWeakPtr())); | |
30 } | |
31 | |
32 void InitializeStoreTask::CompleteIfSuccessful(bool success) { | |
33 if (success) { | |
34 callback_.Run(true); | |
35 TaskComplete(); | |
36 return; | |
37 } | |
38 | |
39 TryToResetStore(); | |
40 } | |
41 | |
42 void InitializeStoreTask::TryToResetStore() { | |
43 if (reset_attempts_left_ == 0) { | |
44 callback_.Run(false); | |
45 TaskComplete(); | |
46 return; | |
47 } | |
48 | |
49 reset_attempts_left_--; | |
50 store_->Reset(base::Bind(&InitializeStoreTask::OnStoreResetDone, | |
51 weak_ptr_factory_.GetWeakPtr())); | |
52 } | |
53 | |
54 void InitializeStoreTask::OnStoreResetDone(bool success) { | |
55 if (success) | |
56 InitializeStore(); | |
57 else | |
58 TryToResetStore(); | |
59 } | |
60 | |
61 } // namespace offline_pages | |
OLD | NEW |