Shaka Player Embedded
open_db_request.cc
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <glog/logging.h>
18 
19 #include <memory>
20 #include <vector>
21 
24 #include "src/js/idb/database.h"
26 #include "src/js/idb/sqlite.h"
27 #include "src/js/idb/transaction.h"
28 #include "src/js/js_error.h"
29 
30 namespace shaka {
31 namespace js {
32 namespace idb {
33 
35  optional<int64_t> version)
36  : IDBRequest(nullopt, nullptr), name_(name), version_(version) {
37  AddListenerField(EventType::UpgradeNeeded, &on_upgrade_needed);
38 }
39 
40 // \cond Doxygen_Skip
41 IDBOpenDBRequest::~IDBOpenDBRequest() {}
42 // \endcond Doxygen_Skip
43 
44 void IDBOpenDBRequest::DoOperation(const std::string& db_path) {
45  std::shared_ptr<SqliteConnection> connection(new SqliteConnection(db_path));
46  DatabaseStatus status = connection->Init();
47  if (status != DatabaseStatus::Success)
48  return CompleteError(status);
49 
51  status = connection->BeginTransaction(&transaction);
52  if (status != DatabaseStatus::Success)
53  return CompleteError(status);
54 
55  int64_t version = 0;
56  status = transaction.GetDbVersion(name_, &version);
57  const bool is_new = status == DatabaseStatus::NotFound;
58  const int64_t new_version = version_.value_or(is_new ? 1 : version);
59  if (!is_new && status != DatabaseStatus::Success)
60  return CompleteError(status);
61  if (new_version < version)
63 
64  std::vector<std::string> store_names;
65  if (!is_new) {
66  status = transaction.ListObjectStores(name_, &store_names);
67  if (status != DatabaseStatus::Success)
68  return CompleteError(status);
69  }
70 
71  RefPtr<IDBDatabase> idb_connection =
72  new IDBDatabase(connection, name_, new_version, store_names);
73 
74  if (version != new_version) {
75  if (is_new)
76  status = transaction.CreateDb(name_, new_version);
77  else
78  status = transaction.UpdateDbVersion(name_, new_version);
79  if (status != DatabaseStatus::Success)
80  return CompleteError(status);
81 
83  idb_connection, IDBTransactionMode::VERSION_CHANGE, store_names));
84  idb_connection->VersionChangeTransaction(idb_trans);
85  idb_trans->sqlite_transaction = &transaction;
86 
88  result_ = Any(idb_connection);
89  this->transaction = idb_trans;
90  auto* event = new events::IDBVersionChangeEvent(EventType::UpgradeNeeded,
91  version, new_version);
92  bool did_throw;
93  DispatchEventInternal(event, &did_throw);
94  if (did_throw)
95  idb_trans->Abort();
96 
97  idb_trans->DoCommit(&transaction);
98 
99  if (idb_trans->aborted || idb_connection->is_closed()) {
100  if (!idb_connection->is_closed())
101  idb_connection->Close();
103  }
104 
105  idb_connection->VersionChangeTransaction(nullptr);
106  }
107 
108  CompleteSuccess(Any(idb_connection));
109 }
110 
112  LOG(FATAL) << "Not reached";
113 }
114 
115 
117  AddListenerField(EventType::UpgradeNeeded,
119 
120  NotImplemented("onblocked");
121 }
122 
123 } // namespace idb
124 } // namespace js
125 } // namespace shaka
Definition: any.h:31
ExceptionOr< bool > DispatchEventInternal(RefPtr< Event > event, bool *did_listeners_throw)
Definition: event_target.cc:68
DatabaseStatus ListObjectStores(const std::string &db_name, std::vector< std::string > *names)
Definition: sqlite.cc:305
void PerformOperation(SqliteTransaction *transaction) override
IDBOpenDBRequest(const std::string &name, optional< int64_t > version)
const char * name
DatabaseStatus GetDbVersion(const std::string &db_name, int64_t *version)
Definition: sqlite.cc:272
const nullopt_t nullopt
Definition: optional.cc:22
void CompleteError(JsError error)
Definition: request.cc:75
DatabaseStatus CreateDb(const std::string &db_name, int64_t version)
Definition: sqlite.cc:237
IDBRequestReadyState ready_state
Definition: request.h:74
void CompleteSuccess(Any result)
Definition: request.cc:64
T value_or(U &&default_value) const &
Definition: optional.h:165
DatabaseStatus UpdateDbVersion(const std::string &db_name, int64_t version)
Definition: sqlite.cc:248
static JsError DOMException(ExceptionCode code)
Definition: js_error.cc:115
Member< IDBTransaction > transaction
Definition: request.h:73
void DoOperation(const std::string &db_path)
void AddListenerField(EventType type, Listener *on_field)
Definition: event_target.h:138