1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149

content / browser / indexed_db / instance / factory_client.cc [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/indexed_db/instance/factory_client.h"

#include <forward_list>
#include <memory>
#include <ostream>
#include <utility>

#include "base/check.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/sequenced_task_runner.h"
#include "content/browser/indexed_db/indexed_db_data_loss_info.h"
#include "content/browser/indexed_db/indexed_db_database_error.h"
#include "content/browser/indexed_db/instance/connection.h"
#include "mojo/public/cpp/bindings/pending_associated_remote.h"
#include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-shared.h"

using blink::IndexedDBDatabaseMetadata;
using blink::IndexedDBKey;
using std::swap;

namespace content::indexed_db {

FactoryClient::FactoryClient(
    mojo::PendingAssociatedRemote<blink::mojom::IDBFactoryClient>
        pending_client)
    : data_loss_(blink::mojom::IDBDataLoss::None) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (pending_client.is_valid()) {
    remote_.Bind(std::move(pending_client));
    remote_.set_disconnect_handler(base::BindOnce(
        &FactoryClient::OnConnectionError, base::Unretained(this)));
  }
}

FactoryClient::~FactoryClient() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void FactoryClient::OnError(const DatabaseError& error) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!complete_);

  if (!remote_) {
    return;
  }
  remote_->Error(error.code(), error.message());
  complete_ = true;
}

void FactoryClient::OnBlocked(int64_t existing_version) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!complete_);

  if (sent_blocked_) {
    return;
  }

  sent_blocked_ = true;

  if (remote_) {
    remote_->Blocked(existing_version);
  }
}

void FactoryClient::OnUpgradeNeeded(
    int64_t old_version,
    std::unique_ptr<Connection> connection,
    const IndexedDBDatabaseMetadata& metadata,
    const IndexedDBDataLossInfo& data_loss_info) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!complete_);

  DCHECK(!connection_created_);

  data_loss_ = data_loss_info.status;
  connection_created_ = true;

  if (!remote_) {
    // Don't destroy the connection while the current transaction task queue is
    // being processed.
    base::SequencedTaskRunner::GetCurrentDefault()->DeleteSoon(
        FROM_HERE, std::move(connection));
    return;
  }

  mojo::PendingAssociatedRemote<blink::mojom::IDBDatabase> pending =
      Connection::MakeSelfOwnedReceiverAndBindRemote(std::move(connection));
  remote_->UpgradeNeeded(std::move(pending), old_version, data_loss_info.status,
                         data_loss_info.message, metadata);
}

void FactoryClient::OnOpenSuccess(std::unique_ptr<Connection> connection,
                                  const IndexedDBDatabaseMetadata& metadata) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!complete_);

  DCHECK_EQ(connection_created_, !connection);

  // Only create a new connection if one was not previously sent in
  // OnUpgradeNeeded.
  std::unique_ptr<Connection> database_connection;
  if (!connection_created_) {
    database_connection = std::move(connection);
  }

  if (!remote_) {
    if (database_connection) {
      // Don't destroy the connection while the current transaction task queue
      // is being processed.
      base::SequencedTaskRunner::GetCurrentDefault()->DeleteSoon(
          FROM_HERE, std::move(database_connection));
    }
    return;
  }

  mojo::PendingAssociatedRemote<blink::mojom::IDBDatabase> pending_remote;
  if (database_connection) {
    pending_remote = Connection::MakeSelfOwnedReceiverAndBindRemote(
        std::move(database_connection));
  }
  remote_->OpenSuccess(std::move(pending_remote), metadata);
  complete_ = true;
}

void FactoryClient::OnDeleteSuccess(int64_t old_version) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!complete_);

  if (!remote_) {
    return;
  }
  remote_->DeleteSuccess(old_version);
  complete_ = true;
}

void FactoryClient::OnConnectionError() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  // Since the renderer-process `IDBFactory` is a self-owned receiver, a
  // disconnection should only occur if the renderer process is gone.
  remote_.reset();
}

}  // namespace content::indexed_db