diff --git a/components/feed/core/BUILD.gn b/components/feed/core/BUILD.gn index 9f4d11cc637c29..8c2f3b1ea5f0f7 100644 --- a/components/feed/core/BUILD.gn +++ b/components/feed/core/BUILD.gn @@ -20,6 +20,10 @@ source_set("feed_core") { "feed_image_database.h", "feed_image_manager.cc", "feed_image_manager.h", + "feed_journal_mutation.cc", + "feed_journal_mutation.h", + "feed_journal_operation.cc", + "feed_journal_operation.h", "feed_networking_host.cc", "feed_networking_host.h", "feed_scheduler_host.cc", @@ -73,8 +77,10 @@ source_set("core_unit_tests") { testonly = true sources = [ "feed_content_database_unittest.cc", + "feed_content_mutation_unittest.cc", "feed_image_database_unittest.cc", "feed_image_manager_unittest.cc", + "feed_journal_mutation_unittest.cc", "feed_networking_host_unittest.cc", "feed_scheduler_host_unittest.cc", "feed_storage_database_unittest.cc", diff --git a/components/feed/core/feed_content_database.cc b/components/feed/core/feed_content_database.cc index 455dc9d523ee3e..6126a56a2667fa 100644 --- a/components/feed/core/feed_content_database.cc +++ b/components/feed/core/feed_content_database.cc @@ -117,36 +117,35 @@ void FeedContentDatabase::CommitContentMutation( DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK(content_mutation); - PerformNextOperation(content_mutation->TakeOperations(), std::move(callback)); + PerformNextOperation(std::move(content_mutation), std::move(callback)); } void FeedContentDatabase::PerformNextOperation( - ContentOperationList operations_list, + std::unique_ptr content_mutation, ConfirmationCallback callback) { - DCHECK(!operations_list.empty()); + DCHECK(!content_mutation->Empty()); - ContentOperation operation(operations_list.front()); - operations_list.pop_front(); + ContentOperation operation = content_mutation->TakeFristOperation(); switch (operation.type()) { case ContentOperation::CONTENT_DELETE: // TODO(gangwu): If deletes are continuous, we should combine them into // one commit. - DeleteContent(std::move(operation), std::move(operations_list), + DeleteContent(std::move(operation), std::move(content_mutation), std::move(callback)); break; case ContentOperation::CONTENT_DELETE_BY_PREFIX: - DeleteContentByPrefix(std::move(operation), std::move(operations_list), + DeleteContentByPrefix(std::move(operation), std::move(content_mutation), std::move(callback)); break; case ContentOperation::CONTENT_UPSERT: // TODO(gangwu): If upserts are continuous, we should combine them into // one commit. - UpsertContent(std::move(operation), std::move(operations_list), + UpsertContent(std::move(operation), std::move(content_mutation), std::move(callback)); break; case ContentOperation::CONTENT_DELETE_ALL: - DeleteAllContent(std::move(operation), std::move(operations_list), + DeleteAllContent(std::move(operation), std::move(content_mutation), std::move(callback)); break; default: @@ -156,9 +155,10 @@ void FeedContentDatabase::PerformNextOperation( } } -void FeedContentDatabase::UpsertContent(ContentOperation operation, - ContentOperationList operations_list, - ConfirmationCallback callback) { +void FeedContentDatabase::UpsertContent( + ContentOperation operation, + std::unique_ptr content_mutation, + ConfirmationCallback callback) { DCHECK_EQ(operation.type(), ContentOperation::CONTENT_UPSERT); auto contents_to_save = std::make_unique(); @@ -170,13 +170,14 @@ void FeedContentDatabase::UpsertContent(ContentOperation operation, storage_database_->UpdateEntries( std::move(contents_to_save), std::make_unique>(), base::BindOnce(&FeedContentDatabase::OnOperationCommitted, - weak_ptr_factory_.GetWeakPtr(), std::move(operations_list), - std::move(callback))); + weak_ptr_factory_.GetWeakPtr(), + std::move(content_mutation), std::move(callback))); } -void FeedContentDatabase::DeleteContent(ContentOperation operation, - ContentOperationList operations_list, - ConfirmationCallback callback) { +void FeedContentDatabase::DeleteContent( + ContentOperation operation, + std::unique_ptr content_mutation, + ConfirmationCallback callback) { DCHECK_EQ(operation.type(), ContentOperation::CONTENT_DELETE); auto content_to_delete = std::make_unique>( @@ -185,13 +186,13 @@ void FeedContentDatabase::DeleteContent(ContentOperation operation, storage_database_->UpdateEntries( std::make_unique(), std::move(content_to_delete), base::BindOnce(&FeedContentDatabase::OnOperationCommitted, - weak_ptr_factory_.GetWeakPtr(), std::move(operations_list), - std::move(callback))); + weak_ptr_factory_.GetWeakPtr(), + std::move(content_mutation), std::move(callback))); } void FeedContentDatabase::DeleteContentByPrefix( ContentOperation operation, - ContentOperationList operations_list, + std::unique_ptr content_mutation, ConfirmationCallback callback) { DCHECK_EQ(operation.type(), ContentOperation::CONTENT_DELETE_BY_PREFIX); @@ -199,13 +200,14 @@ void FeedContentDatabase::DeleteContentByPrefix( std::make_unique(), base::BindRepeating(&DatabasePrefixFilter, operation.prefix()), base::BindOnce(&FeedContentDatabase::OnOperationCommitted, - weak_ptr_factory_.GetWeakPtr(), std::move(operations_list), - std::move(callback))); + weak_ptr_factory_.GetWeakPtr(), + std::move(content_mutation), std::move(callback))); } -void FeedContentDatabase::DeleteAllContent(ContentOperation operation, - ContentOperationList operations_list, - ConfirmationCallback callback) { +void FeedContentDatabase::DeleteAllContent( + ContentOperation operation, + std::unique_ptr content_mutation, + ConfirmationCallback callback) { DCHECK_EQ(operation.type(), ContentOperation::CONTENT_DELETE_ALL); std::string key_prefix = ""; @@ -213,8 +215,8 @@ void FeedContentDatabase::DeleteAllContent(ContentOperation operation, std::make_unique(), base::BindRepeating(&DatabasePrefixFilter, std::move(key_prefix)), base::BindOnce(&FeedContentDatabase::OnOperationCommitted, - weak_ptr_factory_.GetWeakPtr(), std::move(operations_list), - std::move(callback))); + weak_ptr_factory_.GetWeakPtr(), + std::move(content_mutation), std::move(callback))); } void FeedContentDatabase::OnDatabaseInitialized(bool success) { @@ -268,7 +270,7 @@ void FeedContentDatabase::OnLoadKeysForLoadAllContentKeys( } void FeedContentDatabase::OnOperationCommitted( - ContentOperationList operations_list, + std::unique_ptr content_mutation, ConfirmationCallback callback, bool success) { // Commit is unsuccessful, skip processing the other operations since @@ -281,12 +283,12 @@ void FeedContentDatabase::OnOperationCommitted( } // All operations were committed successfully, call |callback|. - if (operations_list.empty()) { + if (content_mutation->Empty()) { std::move(callback).Run(success); return; } - PerformNextOperation(std::move(operations_list), std::move(callback)); + PerformNextOperation(std::move(content_mutation), std::move(callback)); } } // namespace feed diff --git a/components/feed/core/feed_content_database.h b/components/feed/core/feed_content_database.h index f7faa1931e656c..cc5bc6697f38b2 100644 --- a/components/feed/core/feed_content_database.h +++ b/components/feed/core/feed_content_database.h @@ -85,19 +85,19 @@ class FeedContentDatabase { // These methods work with |CommitContentMutation|. They process // |ContentOperation| in |ContentMutation| which is passed to // |PerformNextOperation| by |CommitContentMutation|. - void PerformNextOperation(ContentOperationList operations_list, + void PerformNextOperation(std::unique_ptr content_mutation, ConfirmationCallback callback); void UpsertContent(ContentOperation operation, - ContentOperationList operations_list, + std::unique_ptr content_mutation, ConfirmationCallback callback); void DeleteContent(ContentOperation operation, - ContentOperationList operations_list, + std::unique_ptr content_mutation, ConfirmationCallback callback); void DeleteContentByPrefix(ContentOperation operation, - ContentOperationList operations_list, + std::unique_ptr content_mutation, ConfirmationCallback callback); void DeleteAllContent(ContentOperation operation, - ContentOperationList operations_list, + std::unique_ptr content_mutation, ConfirmationCallback callback); // Callback methods given to |storage_database_| for async responses. @@ -110,7 +110,7 @@ class FeedContentDatabase { ContentKeyCallback callback, bool success, std::unique_ptr> keys); - void OnOperationCommitted(ContentOperationList operations_list, + void OnOperationCommitted(std::unique_ptr content_mutation, ConfirmationCallback callback, bool success); diff --git a/components/feed/core/feed_content_mutation.cc b/components/feed/core/feed_content_mutation.cc index 607b4251dbf2fa..06b1f7080e94e0 100644 --- a/components/feed/core/feed_content_mutation.cc +++ b/components/feed/core/feed_content_mutation.cc @@ -6,35 +6,43 @@ #include +#include "base/logging.h" #include "components/feed/core/feed_content_operation.h" namespace feed { -ContentMutation::ContentMutation() {} +ContentMutation::ContentMutation() = default; -ContentMutation::~ContentMutation() {} +ContentMutation::~ContentMutation() = default; -void ContentMutation::AppendDeleteOperation(const std::string& key) { - operations_list_.emplace_back(ContentOperation::CreateDeleteOperation(key)); +void ContentMutation::AppendDeleteOperation(std::string key) { + operations_list_.emplace_back( + ContentOperation::CreateDeleteOperation(std::move(key))); } void ContentMutation::AppendDeleteAllOperation() { operations_list_.emplace_back(ContentOperation::CreateDeleteAllOperation()); } -void ContentMutation::AppendDeleteByPrefixOperation(const std::string& prefix) { +void ContentMutation::AppendDeleteByPrefixOperation(std::string prefix) { operations_list_.emplace_back( - ContentOperation::CreateDeleteByPrefixOperation(prefix)); + ContentOperation::CreateDeleteByPrefixOperation(std::move(prefix))); } -void ContentMutation::AppendUpsertOperation(const std::string& key, - const std::string& value) { - operations_list_.emplace_back( - ContentOperation::CreateUpsertOperation(key, value)); +void ContentMutation::AppendUpsertOperation(std::string key, + std::string value) { + operations_list_.emplace_back(ContentOperation::CreateUpsertOperation( + std::move(key), std::move(value))); +} + +bool ContentMutation::Empty() { + return operations_list_.empty(); } -ContentOperationList ContentMutation::TakeOperations() { - return std::move(operations_list_); +ContentOperation ContentMutation::TakeFristOperation() { + ContentOperation operation = std::move(operations_list_.front()); + operations_list_.pop_front(); + return operation; } } // namespace feed diff --git a/components/feed/core/feed_content_mutation.h b/components/feed/core/feed_content_mutation.h index e7195499c3e9f2..d37ef282f576c0 100644 --- a/components/feed/core/feed_content_mutation.h +++ b/components/feed/core/feed_content_mutation.h @@ -13,7 +13,6 @@ namespace feed { class ContentOperation; -using ContentOperationList = std::list; // Native counterpart of ContentMutation.java. // To commit a set of ContentOperation into FeedContentDatabase, user need to @@ -25,16 +24,20 @@ class ContentMutation { ContentMutation(); ~ContentMutation(); - void AppendDeleteOperation(const std::string& key); + void AppendDeleteOperation(std::string key); void AppendDeleteAllOperation(); - void AppendDeleteByPrefixOperation(const std::string& prefix); - void AppendUpsertOperation(const std::string& key, const std::string& value); + void AppendDeleteByPrefixOperation(std::string prefix); + void AppendUpsertOperation(std::string key, std::string value); - // This will std::move the |operations_list_| out. - ContentOperationList TakeOperations(); + // Check if mutation has ContentOperation left. + bool Empty(); + + // This will remove the first ContentOperation in |operations_list_| and + // return it to caller. + ContentOperation TakeFristOperation(); private: - ContentOperationList operations_list_; + std::list operations_list_; DISALLOW_COPY_AND_ASSIGN(ContentMutation); }; diff --git a/components/feed/core/feed_content_mutation_unittest.cc b/components/feed/core/feed_content_mutation_unittest.cc new file mode 100644 index 00000000000000..89a88789284fc4 --- /dev/null +++ b/components/feed/core/feed_content_mutation_unittest.cc @@ -0,0 +1,80 @@ +// 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 "components/feed/core/feed_content_mutation.h" + +#include "components/feed/core/feed_content_operation.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace feed { + +namespace { + +const char kContentkey[] = "ContentKey"; +const char kContentValue[] = "Value"; +const char kContentPrefix[] = "Content"; + +} // namespace + +class FeedContentMutationTest : public testing::Test { + public: + FeedContentMutationTest() = default; + + private: + DISALLOW_COPY_AND_ASSIGN(FeedContentMutationTest); +}; + +TEST_F(FeedContentMutationTest, AppendDeleteOperation) { + ContentMutation mutation; + EXPECT_TRUE(mutation.Empty()); + + mutation.AppendDeleteOperation(kContentkey); + EXPECT_FALSE(mutation.Empty()); + + ContentOperation operation = mutation.TakeFristOperation(); + EXPECT_TRUE(mutation.Empty()); + EXPECT_EQ(operation.type(), ContentOperation::CONTENT_DELETE); + EXPECT_EQ(operation.key(), kContentkey); +} + +TEST_F(FeedContentMutationTest, AppendDeleteAllOperation) { + ContentMutation mutation; + EXPECT_TRUE(mutation.Empty()); + + mutation.AppendDeleteAllOperation(); + EXPECT_FALSE(mutation.Empty()); + + ContentOperation operation = mutation.TakeFristOperation(); + EXPECT_TRUE(mutation.Empty()); + EXPECT_EQ(operation.type(), ContentOperation::CONTENT_DELETE_ALL); +} + +TEST_F(FeedContentMutationTest, AppendDeleteByPrefixOperation) { + ContentMutation mutation; + EXPECT_TRUE(mutation.Empty()); + + mutation.AppendDeleteByPrefixOperation(kContentPrefix); + EXPECT_FALSE(mutation.Empty()); + + ContentOperation operation = mutation.TakeFristOperation(); + EXPECT_TRUE(mutation.Empty()); + EXPECT_EQ(operation.type(), ContentOperation::CONTENT_DELETE_BY_PREFIX); + EXPECT_EQ(operation.prefix(), kContentPrefix); +} + +TEST_F(FeedContentMutationTest, AppendUpsertOperation) { + ContentMutation mutation; + EXPECT_TRUE(mutation.Empty()); + + mutation.AppendUpsertOperation(kContentkey, kContentValue); + EXPECT_FALSE(mutation.Empty()); + + ContentOperation operation = mutation.TakeFristOperation(); + EXPECT_TRUE(mutation.Empty()); + EXPECT_EQ(operation.type(), ContentOperation::CONTENT_UPSERT); + EXPECT_EQ(operation.key(), kContentkey); + EXPECT_EQ(operation.value(), kContentValue); +} + +} // namespace feed diff --git a/components/feed/core/feed_content_operation.cc b/components/feed/core/feed_content_operation.cc index 1cd4b35ce4fb4c..650600cda056e8 100644 --- a/components/feed/core/feed_content_operation.cc +++ b/components/feed/core/feed_content_operation.cc @@ -10,38 +10,34 @@ namespace feed { -ContentOperation::ContentOperation(const ContentOperation& operation) - : type_(operation.type_), - key_(operation.key_), - value_(operation.value_), - prefix_(operation.prefix_) {} - -ContentOperation::ContentOperation(ContentOperation&& operation) - : type_(operation.type_), - key_(std::move(operation.key_)), - value_(std::move(operation.value_)), - prefix_(std::move(operation.prefix_)) {} - +// static ContentOperation ContentOperation::CreateDeleteOperation(std::string key) { - return ContentOperation(CONTENT_DELETE, key, std::string(), std::string()); + return ContentOperation(CONTENT_DELETE, std::move(key), std::string(), + std::string()); } +// static ContentOperation ContentOperation::CreateDeleteAllOperation() { return ContentOperation(CONTENT_DELETE_ALL, std::string(), std::string(), std::string()); } +// static ContentOperation ContentOperation::CreateDeleteByPrefixOperation( std::string prefix) { return ContentOperation(CONTENT_DELETE_BY_PREFIX, std::string(), - std::string(), prefix); + std::string(), std::move(prefix)); } +// static ContentOperation ContentOperation::CreateUpsertOperation(std::string key, std::string value) { - return ContentOperation(CONTENT_UPSERT, key, value, std::string()); + return ContentOperation(CONTENT_UPSERT, std::move(key), std::move(value), + std::string()); } +ContentOperation::ContentOperation(ContentOperation&& operation) = default; + ContentOperation::Type ContentOperation::type() { return type_; } @@ -65,6 +61,9 @@ ContentOperation::ContentOperation(Type type, std::string key, std::string value, std::string prefix) - : type_(type), key_(key), value_(value), prefix_(prefix) {} + : type_(type), + key_(std::move(key)), + value_(std::move(value)), + prefix_(std::move(prefix)) {} } // namespace feed diff --git a/components/feed/core/feed_content_operation.h b/components/feed/core/feed_content_operation.h index cd15123be688a2..103e902de8b19c 100644 --- a/components/feed/core/feed_content_operation.h +++ b/components/feed/core/feed_content_operation.h @@ -5,9 +5,10 @@ #ifndef COMPONENTS_FEED_CORE_FEED_CONTENT_OPERATION_H_ #define COMPONENTS_FEED_CORE_FEED_CONTENT_OPERATION_H_ -#include #include +#include "base/macros.h" + namespace feed { // Native counterpart of ContentOperation.java. @@ -26,10 +27,6 @@ class ContentOperation { static ContentOperation CreateUpsertOperation(std::string key, std::string value); - // Copy constructor - explicit ContentOperation(const ContentOperation& operation); - - // Move constructor ContentOperation(ContentOperation&& operation); Type type(); @@ -47,6 +44,8 @@ class ContentOperation { const std::string key_; const std::string value_; const std::string prefix_; + + DISALLOW_COPY_AND_ASSIGN(ContentOperation); }; } // namespace feed diff --git a/components/feed/core/feed_journal_mutation.cc b/components/feed/core/feed_journal_mutation.cc new file mode 100644 index 00000000000000..7e4e6184ffaef0 --- /dev/null +++ b/components/feed/core/feed_journal_mutation.cc @@ -0,0 +1,46 @@ +// 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 "components/feed/core/feed_journal_mutation.h" + +#include + +#include "components/feed/core/feed_journal_operation.h" + +namespace feed { + +JournalMutation::JournalMutation(std::string journal_name) + : journal_name_(std::move(journal_name)) {} + +JournalMutation::~JournalMutation() = default; + +void JournalMutation::AddAppendOperation(std::string value) { + operations_list_.emplace_back( + JournalOperation::CreateAppendOperation(std::move(value))); +} + +void JournalMutation::AddCopyOperation(std::string to_journal_name) { + operations_list_.emplace_back( + JournalOperation::CreateCopyOperation(std::move(to_journal_name))); +} + +void JournalMutation::AddDeleteOperation() { + operations_list_.emplace_back(JournalOperation::CreateDeleteOperation()); +} + +const std::string& JournalMutation::journal_name() { + return journal_name_; +} + +bool JournalMutation::Empty() { + return operations_list_.empty(); +} + +JournalOperation JournalMutation::TakeFristOperation() { + JournalOperation operation = std::move(operations_list_.front()); + operations_list_.pop_front(); + return operation; +} + +} // namespace feed diff --git a/components/feed/core/feed_journal_mutation.h b/components/feed/core/feed_journal_mutation.h new file mode 100644 index 00000000000000..25cb3c0753d607 --- /dev/null +++ b/components/feed/core/feed_journal_mutation.h @@ -0,0 +1,50 @@ +// 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 COMPONENTS_FEED_CORE_FEED_JOURNAL_MUTATION_H_ +#define COMPONENTS_FEED_CORE_FEED_JOURNAL_MUTATION_H_ + +#include +#include + +#include "base/macros.h" + +namespace feed { + +class JournalOperation; + +// Native counterpart of JournalMutation.java. +// To commit a set of JournalOperation into FeedJournalDatabase, first, +// JournalMutation need to be created, then use Add* methods to add operations +// into the mutation, and pass the JournalMutation to +// FeedJournalDatabase::CommitJournalMutation to commit operations. +class JournalMutation { + public: + explicit JournalMutation(std::string journal_name); + ~JournalMutation(); + + void AddAppendOperation(std::string value); + void AddCopyOperation(std::string to_journal_name); + void AddDeleteOperation(); + + const std::string& journal_name(); + + // Check if mutation has JournalOperation left. + bool Empty(); + + // This will remove the first JournalOperation in |operations_list_| and + // return it to caller. + JournalOperation TakeFristOperation(); + + private: + const std::string journal_name_; + + std::list operations_list_; + + DISALLOW_COPY_AND_ASSIGN(JournalMutation); +}; + +} // namespace feed + +#endif // COMPONENTS_FEED_CORE_FEED_JOURNAL_MUTATION_H_ diff --git a/components/feed/core/feed_journal_mutation_unittest.cc b/components/feed/core/feed_journal_mutation_unittest.cc new file mode 100644 index 00000000000000..f6c83467e0c9cd --- /dev/null +++ b/components/feed/core/feed_journal_mutation_unittest.cc @@ -0,0 +1,69 @@ +// 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 "components/feed/core/feed_journal_mutation.h" + +#include "components/feed/core/feed_journal_operation.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace feed { + +namespace { + +const char kJournalName1[] = "Journal1"; +const char kJournalName2[] = "Journal2"; +const char kJournalValue[] = "value"; + +} // namespace + +class FeedJournalMutationTest : public testing::Test { + public: + FeedJournalMutationTest() = default; + + private: + DISALLOW_COPY_AND_ASSIGN(FeedJournalMutationTest); +}; + +TEST_F(FeedJournalMutationTest, AddAppendOperation) { + JournalMutation mutation(kJournalName1); + EXPECT_EQ(mutation.journal_name(), kJournalName1); + EXPECT_TRUE(mutation.Empty()); + + mutation.AddAppendOperation(kJournalValue); + EXPECT_FALSE(mutation.Empty()); + + JournalOperation operation = mutation.TakeFristOperation(); + EXPECT_TRUE(mutation.Empty()); + EXPECT_EQ(operation.type(), JournalOperation::JOURNAL_APPEND); + EXPECT_EQ(operation.value(), kJournalValue); +} + +TEST_F(FeedJournalMutationTest, AddCopyOperation) { + JournalMutation mutation(kJournalName1); + EXPECT_EQ(mutation.journal_name(), kJournalName1); + EXPECT_TRUE(mutation.Empty()); + + mutation.AddCopyOperation(kJournalName2); + EXPECT_FALSE(mutation.Empty()); + + JournalOperation operation = mutation.TakeFristOperation(); + EXPECT_TRUE(mutation.Empty()); + EXPECT_EQ(operation.type(), JournalOperation::JOURNAL_COPY); + EXPECT_EQ(operation.to_journal_name(), kJournalName2); +} + +TEST_F(FeedJournalMutationTest, AddDeleteOperation) { + JournalMutation mutation(kJournalName1); + EXPECT_EQ(mutation.journal_name(), kJournalName1); + EXPECT_TRUE(mutation.Empty()); + + mutation.AddDeleteOperation(); + EXPECT_FALSE(mutation.Empty()); + + JournalOperation operation = mutation.TakeFristOperation(); + EXPECT_TRUE(mutation.Empty()); + EXPECT_EQ(operation.type(), JournalOperation::JOURNAL_DELETE); +} + +} // namespace feed diff --git a/components/feed/core/feed_journal_operation.cc b/components/feed/core/feed_journal_operation.cc new file mode 100644 index 00000000000000..4e561427818810 --- /dev/null +++ b/components/feed/core/feed_journal_operation.cc @@ -0,0 +1,53 @@ +// 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 "components/feed/core/feed_journal_operation.h" + +#include + +#include "base/logging.h" + +namespace feed { + +// static +JournalOperation JournalOperation::CreateAppendOperation(std::string value) { + return JournalOperation(JOURNAL_APPEND, std::move(value), std::string()); +} + +// static +JournalOperation JournalOperation::CreateCopyOperation( + std::string to_journal_name) { + return JournalOperation(JOURNAL_COPY, std::string(), + std::move(to_journal_name)); +} + +// static +JournalOperation JournalOperation::CreateDeleteOperation() { + return JournalOperation(JOURNAL_DELETE, std::string(), std::string()); +} + +JournalOperation::JournalOperation(JournalOperation&& operation) = default; + +JournalOperation::Type JournalOperation::type() { + return type_; +} + +const std::string& JournalOperation::value() { + DCHECK_EQ(type_, JOURNAL_APPEND); + return value_; +} + +const std::string& JournalOperation::to_journal_name() { + DCHECK_EQ(type_, JOURNAL_COPY); + return to_journal_name_; +} + +JournalOperation::JournalOperation(Type type, + std::string value, + std::string to_journal_name) + : type_(type), + value_(std::move(value)), + to_journal_name_(std::move(to_journal_name)) {} + +} // namespace feed diff --git a/components/feed/core/feed_journal_operation.h b/components/feed/core/feed_journal_operation.h new file mode 100644 index 00000000000000..7e447950498d3b --- /dev/null +++ b/components/feed/core/feed_journal_operation.h @@ -0,0 +1,49 @@ +// 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 COMPONENTS_FEED_CORE_FEED_JOURNAL_OPERATION_H_ +#define COMPONENTS_FEED_CORE_FEED_JOURNAL_OPERATION_H_ + +#include + +#include "base/macros.h" + +namespace feed { + +// Native counterpart of JournalOperation.java. +class JournalOperation { + public: + enum Type { + JOURNAL_APPEND, + JOURNAL_COPY, + JOURNAL_DELETE, + }; + + static JournalOperation CreateAppendOperation(std::string value); + static JournalOperation CreateCopyOperation(std::string to_journal_name); + static JournalOperation CreateDeleteOperation(); + + JournalOperation(JournalOperation&& operation); + + Type type(); + const std::string& value(); + const std::string& to_journal_name(); + + private: + JournalOperation(Type type, std::string value, std::string to_journal_name); + + const Type type_; + + // Used for JOURNAL_APPEND, |value_| will be append to the end of journal. + const std::string value_; + + // Used for JOURNAL_COPY, copy current journal to |to_journal_name_|. + const std::string to_journal_name_; + + DISALLOW_COPY_AND_ASSIGN(JournalOperation); +}; + +} // namespace feed + +#endif // COMPONENTS_FEED_CORE_FEED_JOURNAL_OPERATION_H_