Shaka Player Embedded
delete_db_request.cc
Go to the documentation of this file.
1 // Copyright 2019 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 
22 #include "src/js/idb/database.h"
24 #include "src/js/idb/sqlite.h"
25 #include "src/js/idb/transaction.h"
26 #include "src/js/js_error.h"
27 
28 namespace shaka {
29 namespace js {
30 namespace idb {
31 
33  : IDBRequest(nullopt, nullptr), name_(name) {}
34 
35 void IDBDeleteDBRequest::DoOperation(const std::string& db_path) {
36  std::shared_ptr<SqliteConnection> connection(new SqliteConnection(db_path));
37  DatabaseStatus status = connection->Init();
38  if (status != DatabaseStatus::Success)
39  return CompleteError(status);
40 
42  status = connection->BeginTransaction(&transaction);
43  if (status != DatabaseStatus::Success)
44  return CompleteError(status);
45 
46  int64_t version = 0;
47  status = transaction.GetDbVersion(name_, &version);
48  if (status != DatabaseStatus::Success && status != DatabaseStatus::NotFound)
49  return CompleteError(status);
50 
51  if (status != DatabaseStatus::NotFound) {
52  status = transaction.DeleteDb(name_);
53  if (status != DatabaseStatus::Success)
54  return CompleteError(status);
55 
56  status = transaction.Commit();
57  if (status != DatabaseStatus::Success)
58  return CompleteError(status);
59  }
60 
61  // Don't use CompleteSuccess so we can fire a special event instead.
63  RaiseEvent<events::IDBVersionChangeEvent>(EventType::Success, version,
64  nullopt);
65 }
66 
68  SqliteTransaction* /* transaction */) {
69  LOG(FATAL) << "Not reached";
70 }
71 
72 } // namespace idb
73 } // namespace js
74 } // namespace shaka
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
IDBDeleteDBRequest(const std::string &name)
void PerformOperation(SqliteTransaction *transaction) override
void CompleteError(JsError error)
Definition: request.cc:75
IDBRequestReadyState ready_state
Definition: request.h:74
Member< IDBTransaction > transaction
Definition: request.h:73
DatabaseStatus DeleteDb(const std::string &db_name)
Definition: sqlite.cc:260
void DoOperation(const std::string &db_path)