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 #ifndef COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_STORE_TYPES_H_
6 #define COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_STORE_TYPES_H_
7 
8 #include <stdint.h>
9 
10 #include <utility>
11 #include <vector>
12 
13 // This file contains common types and callbacks used by storage of various
14 // offline page related components.
15 namespace offline_pages {
16 
17 // Current store state. When LOADED, the store is operational. When
18 // loading or reset fails, it is reflected appropriately.
19 enum class StoreState {
20   NOT_LOADED,      // Store is not loaded yet.
21   LOADED,          // Store is properly loaded and operational.
22   FAILED_LOADING,  // Store initialization failed.
23   FAILED_RESET,    // Resetting the store failed.
24   INITIALIZING,    // Store is in the process of initializing.
25 };
26 
27 // Statuses referring to actions taken on items in the stores.
28 // GENERATED_JAVA_ENUM_PACKAGE:org.chromium.components.offlinepages
29 enum class ItemActionStatus {
30   SUCCESS,
31   ALREADY_EXISTS,
32   NOT_FOUND,
33   STORE_ERROR,
34 };
35 
36 // Result for synchronous operations (like database and file operations) that
37 // are part of the tasks used by Offline Pages.
38 // Keep it in sync with OfflinePagesSyncOperationResult in enums.xml for
39 // histograms usages.
40 enum class SyncOperationResult {
41   SUCCESS,                   // Successful operation
42   INVALID_DB_CONNECTION,     // Invalid database connection
43   TRANSACTION_BEGIN_ERROR,   // Failed when start a DB transaction
44   TRANSACTION_COMMIT_ERROR,  // Failed when commiting a DB transaction
45   DB_OPERATION_ERROR,        // Failed when executing a DB statement
46   FILE_OPERATION_ERROR,      // Failed while doing file operations
47   kMaxValue = FILE_OPERATION_ERROR,
48 };
49 
50 // List of item action statuses mapped to item ID.
51 typedef std::vector<std::pair<int64_t, ItemActionStatus>> MultipleItemStatuses;
52 
53 // Collective result for store update.
54 template <typename T>
55 class StoreUpdateResult {
56  public:
StoreUpdateResult(StoreState state)57   explicit StoreUpdateResult(StoreState state) : store_state(state) {}
~StoreUpdateResult()58   ~StoreUpdateResult() {}
59 
60   // Move-only to avoid accidental copies.
61   StoreUpdateResult(const StoreUpdateResult& other) = delete;
62   StoreUpdateResult(StoreUpdateResult&& other) = default;
63 
64   StoreUpdateResult& operator=(const StoreUpdateResult&) = delete;
65   StoreUpdateResult& operator=(StoreUpdateResult&&) = default;
66 
67   // List of Offline ID to item action status mappings.
68   // It is meant to be consumed by the original caller of the operation.
69   MultipleItemStatuses item_statuses;
70 
71   // List of successfully updated offline page items as seen after operation
72   // concludes. It is meant to be used when passing to the observers.
73   std::vector<T> updated_items;
74 
75   // State of the store after the operation is done.
76   StoreState store_state;
77 };
78 
79 // This enum is backed by a UMA histogram therefore its entries should not be
80 // deleted or re-ordered and new ones should only be appended.
81 // See enum definition with the same name in tools/metrics/histograms/enum.xml.
82 enum class OfflinePagesStoreEvent {
83   kOpenedFirstTime = 0,
84   kReopened = 1,
85   kClosed = 2,
86   kCloseSkipped = 3,
87   kMaxValue = kCloseSkipped,
88 };
89 
90 }  // namespace offline_pages
91 
92 #endif  // COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_STORE_TYPES_H_
93