Skip to content

Commit

Permalink
WebApp: Introduce WebAppDatabase.
Browse files Browse the repository at this point in the history
For now we try to reuse as much USS code as possible.
It has limitations and drawbacks (see the code).

We will rethink our persistence code later when we start to design
WebAppSyncBridge
(an implementation for syncer::ModelTypeSyncBridge interface).

Bug: 896150
Change-Id: Ia898b905f39869a6b1c3f79751358425449a617a
Reviewed-on: https://chromium-review.googlesource.com/c/1295529
Reviewed-by: calamity <calamity@chromium.org>
Reviewed-by: Mikel Astiz <mastiz@chromium.org>
Reviewed-by: Ben Wells <benwells@chromium.org>
Commit-Queue: Alexey Baskakov <loyso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605974}
  • Loading branch information
Alexey Baskakov authored and Commit Bot committed Nov 7, 2018
1 parent 9e150bd commit ac8c4b0
Show file tree
Hide file tree
Showing 19 changed files with 860 additions and 18 deletions.
16 changes: 16 additions & 0 deletions chrome/browser/web_applications/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ group("web_app_group") {

source_set("web_applications") {
sources = [
"abstract_web_app_database.h",
"web_app.cc",
"web_app.h",
"web_app_database.cc",
"web_app_database.h",
"web_app_database_factory.cc",
"web_app_database_factory.h",
"web_app_install_manager.cc",
"web_app_install_manager.h",
"web_app_registrar.cc",
Expand All @@ -22,9 +27,14 @@ source_set("web_applications") {
":web_app_group",
"//chrome/browser/web_applications/components",
"//chrome/common",
"//components/sync",
"//content/public/browser",
"//skia",
]

public_deps = [
"//chrome/browser/web_applications/proto",
]
}

source_set("web_applications_test_support") {
Expand All @@ -33,6 +43,10 @@ source_set("web_applications_test_support") {
sources = [
"test/test_data_retriever.cc",
"test/test_data_retriever.h",
"test/test_web_app_database.cc",
"test/test_web_app_database.h",
"test/test_web_app_database_factory.cc",
"test/test_web_app_database_factory.h",
"test/web_app_test.cc",
"test/web_app_test.h",
]
Expand All @@ -42,6 +56,7 @@ source_set("web_applications_test_support") {
":web_applications",
"//chrome/browser/web_applications/components",
"//chrome/test:test_support",
"//components/sync:test_support_model",
"//testing/gtest",
]
}
Expand All @@ -50,6 +65,7 @@ source_set("web_applications_unit_tests") {
testonly = true

sources = [
"web_app_database_unittest.cc",
"web_app_install_manager_unittest.cc",
"web_app_registrar_unittest.cc",
]
Expand Down
39 changes: 39 additions & 0 deletions chrome/browser/web_applications/abstract_web_app_database.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_WEB_APPLICATIONS_ABSTRACT_WEB_APP_DATABASE_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_ABSTRACT_WEB_APP_DATABASE_H_

#include <map>
#include <vector>

#include "base/callback_forward.h"
#include "chrome/browser/web_applications/components/web_app_helpers.h"

namespace web_app {

class WebApp;

using Registry = std::map<AppId, std::unique_ptr<WebApp>>;

// An abstract database for the registry persistence.
// Exclusively used from the UI thread.
class AbstractWebAppDatabase {
public:
virtual ~AbstractWebAppDatabase() = default;

using OnceRegistryOpenedCallback =
base::OnceCallback<void(Registry registry)>;
// Open existing or create new DB. Read all data and return it via callback.
virtual void OpenDatabase(OnceRegistryOpenedCallback callback) = 0;

// |OpenDatabase| must have been called and completed before using any other
// methods. Otherwise, it fails with DCHECK.
virtual void WriteWebApp(const WebApp& web_app) = 0;
virtual void DeleteWebApps(std::vector<AppId> app_ids) = 0;
};

} // namespace web_app

#endif // CHROME_BROWSER_WEB_APPLICATIONS_ABSTRACT_WEB_APP_DATABASE_H_
14 changes: 14 additions & 0 deletions chrome/browser/web_applications/proto/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//third_party/protobuf/proto_library.gni")

proto_library("proto") {
sources = [
"web_app.proto",
]
deps = [
"//components/sync/protocol",
]
}
20 changes: 20 additions & 0 deletions chrome/browser/web_applications/proto/web_app.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

syntax = "proto2";

option optimize_for = LITE_RUNTIME;

package web_app;

// WebApp class data.
// TODO(loyso): Consider moving this proto to components/sync/protocol/
// crbug.com/902214.
message WebAppProto {
// app_id is the client tag for sync system.
optional string app_id = 1;
optional string name = 2;
optional string description = 3;
optional string launch_url = 4;
}
27 changes: 27 additions & 0 deletions chrome/browser/web_applications/test/test_web_app_database.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/web_applications/test/test_web_app_database.h"

#include "chrome/browser/web_applications/web_app.h"

namespace web_app {

TestWebAppDatabase::TestWebAppDatabase() {}

TestWebAppDatabase::~TestWebAppDatabase() {}

void TestWebAppDatabase::OpenDatabase(OnceRegistryOpenedCallback callback) {
open_database_callback_ = std::move(callback);
}

void TestWebAppDatabase::WriteWebApp(const WebApp& web_app) {
write_web_app_id_ = web_app.app_id();
}

void TestWebAppDatabase::DeleteWebApps(std::vector<AppId> app_ids) {
delete_web_app_ids_ = std::move(app_ids);
}

} // namespace web_app
42 changes: 42 additions & 0 deletions chrome/browser/web_applications/test/test_web_app_database.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_WEB_APPLICATIONS_TEST_TEST_WEB_APP_DATABASE_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_TEST_TEST_WEB_APP_DATABASE_H_

#include "base/callback.h"
#include "base/macros.h"
#include "chrome/browser/web_applications/abstract_web_app_database.h"

namespace web_app {

class TestWebAppDatabase : public AbstractWebAppDatabase {
public:
TestWebAppDatabase();
~TestWebAppDatabase() override;

// AbstractWebAppDatabase:
void OpenDatabase(OnceRegistryOpenedCallback callback) override;
void WriteWebApp(const WebApp& web_app) override;
void DeleteWebApps(std::vector<AppId> app_ids) override;

OnceRegistryOpenedCallback TakeOpenDatabaseCallback() {
return std::move(open_database_callback_);
}
const AppId& write_web_app_id() const { return write_web_app_id_; }
const std::vector<AppId>& delete_web_app_ids() const {
return delete_web_app_ids_;
}

private:
OnceRegistryOpenedCallback open_database_callback_;
AppId write_web_app_id_;
std::vector<AppId> delete_web_app_ids_;

DISALLOW_COPY_AND_ASSIGN(TestWebAppDatabase);
};

} // namespace web_app

#endif // CHROME_BROWSER_WEB_APPLICATIONS_TEST_TEST_WEB_APP_DATABASE_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/web_applications/test/test_web_app_database_factory.h"

#include "components/sync/model/model_type_store_test_util.h"

namespace web_app {

TestWebAppDatabaseFactory::TestWebAppDatabaseFactory() {
// InMemoryStore must be created after message_loop_.
store_ = syncer::ModelTypeStoreTestUtil::CreateInMemoryStoreForTest();
}

TestWebAppDatabaseFactory::~TestWebAppDatabaseFactory() {}

syncer::OnceModelTypeStoreFactory TestWebAppDatabaseFactory::GetStoreFactory() {
return syncer::ModelTypeStoreTestUtil::FactoryForForwardingStore(
store_.get());
}

} // namespace web_app
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_WEB_APPLICATIONS_TEST_TEST_WEB_APP_DATABASE_FACTORY_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_TEST_TEST_WEB_APP_DATABASE_FACTORY_H_

#include "base/macros.h"
#include "chrome/browser/web_applications/web_app_database_factory.h"

namespace syncer {
class ModelTypeStore;
} // namespace syncer

namespace web_app {

// Requires base::MessageLoop message_loop_ in test fixture. Reason:
// InMemoryStore needs a SequencedTaskRunner.
// MessageLoop ctor calls MessageLoop::SetThreadTaskRunnerHandle().
class TestWebAppDatabaseFactory : public AbstractWebAppDatabaseFactory {
public:
TestWebAppDatabaseFactory();
~TestWebAppDatabaseFactory() override;

// AbstractWebAppDatabaseFactory interface implementation.
syncer::OnceModelTypeStoreFactory GetStoreFactory() override;

syncer::ModelTypeStore* store() { return store_.get(); }

private:
std::unique_ptr<syncer::ModelTypeStore> store_;

DISALLOW_COPY_AND_ASSIGN(TestWebAppDatabaseFactory);
};

} // namespace web_app

#endif // CHROME_BROWSER_WEB_APPLICATIONS_TEST_TEST_WEB_APP_DATABASE_FACTORY_H_
Loading

0 comments on commit ac8c4b0

Please sign in to comment.