1 // Copyright 2019 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 CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONNECTION_COORDINATOR_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONNECTION_COORDINATOR_H_
7 
8 #include <memory>
9 #include <tuple>
10 
11 #include "base/callback.h"
12 #include "base/containers/queue.h"
13 #include "base/memory/scoped_refptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "content/browser/indexed_db/indexed_db_origin_state_handle.h"
16 #include "content/browser/indexed_db/indexed_db_task_helper.h"
17 #include "content/browser/indexed_db/list_set.h"
18 #include "content/common/content_export.h"
19 #include "third_party/leveldatabase/src/include/leveldb/status.h"
20 
21 namespace content {
22 class IndexedDBCallbacks;
23 class IndexedDBConnection;
24 class IndexedDBDatabase;
25 struct IndexedDBPendingConnection;
26 
27 class CONTENT_EXPORT IndexedDBConnectionCoordinator {
28  public:
29   static const int64_t kInvalidDatabaseId = 0;
30   static const int64_t kMinimumIndexId = 30;
31 
32   IndexedDBConnectionCoordinator(
33       IndexedDBDatabase* db,
34       TasksAvailableCallback tasks_available_callback);
35   ~IndexedDBConnectionCoordinator();
36 
37   void ScheduleOpenConnection(
38       IndexedDBOriginStateHandle origin_state_handle,
39       std::unique_ptr<IndexedDBPendingConnection> connection);
40 
41   void ScheduleDeleteDatabase(IndexedDBOriginStateHandle origin_state_handle,
42                               scoped_refptr<IndexedDBCallbacks> callbacks,
43                               base::OnceClosure on_deletion_complete);
44 
45   // Call this method to prune any tasks that don't want to be run during
46   // force close. Returns any error caused by rolling back changes.
47   leveldb::Status PruneTasksForForceClose();
48 
49   void OnConnectionClosed(IndexedDBConnection* connection);
50 
51   void OnNoConnections();
52 
53   // Ack that one of the connections notified with a "versionchange" event did
54   // not promptly close. Therefore a "blocked" event should be fired at the
55   // pending connection.
56   void OnVersionChangeIgnored();
57 
58   void CreateAndBindUpgradeTransaction();
59 
60   void OnUpgradeTransactionStarted(int64_t old_version);
61 
62   void OnUpgradeTransactionFinished(bool committed);
63 
64   enum class ExecuteTaskResult {
65     // There are more tasks to run, so ExecuteTask() should be called again.
66     kMoreTasks,
67     // There are tasks but they are waiting on async work to complete. No more
68     // calls to ExecuteTask() are necessary.
69     kPendingAsyncWork,
70     // There was an error executing a task - see the status. The offending task
71     // was removed, and the caller can choose to continue executing tasks if
72     // they want.
73     kError,
74     // There are no more tasks to run.
75     kDone,
76   };
77   std::tuple<ExecuteTaskResult, leveldb::Status> ExecuteTask(
78       bool has_connections);
79 
HasTasks()80   bool HasTasks() const { return !request_queue_.empty(); }
81 
82   // Number of active open/delete calls (running or blocked on other
83   // connections).
84   size_t ActiveOpenDeleteCount() const;
85 
86   // Number of open/delete calls that are waiting their turn.
87   size_t PendingOpenDeleteCount() const;
88 
AsWeakPtr()89   base::WeakPtr<IndexedDBConnectionCoordinator> AsWeakPtr() {
90     return weak_factory_.GetWeakPtr();
91   }
92 
93  private:
94   friend class IndexedDBDatabase;
95   class ConnectionRequest;
96   class OpenRequest;
97   class DeleteRequest;
98 
99   IndexedDBDatabase* db_;
100 
101   TasksAvailableCallback tasks_available_callback_;
102 
103   base::queue<std::unique_ptr<ConnectionRequest>> request_queue_;
104 
105   // |weak_factory_| is used for all callback uses.
106   base::WeakPtrFactory<IndexedDBConnectionCoordinator> weak_factory_{this};
107 
108   DISALLOW_COPY_AND_ASSIGN(IndexedDBConnectionCoordinator);
109 };
110 
111 }  // namespace content
112 
113 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONNECTION_COORDINATOR_H_
114