Commit fd71dbbe authored by Yafei Duan's avatar Yafei Duan Committed by Commit Bot

[Offline Pages] Implementing ClearDigestTask.

Implemented ClearDigestTask which is responsible for cleaning the digest
field of an offline page in the metadata store.
Also had minor fix for MarkPageAccessedTask and AddPageTask.

Bug: 753595
Change-Id: I74757781d50d93faa14590892dd7eecdec8ec7b5
Reviewed-on: https://chromium-review.googlesource.com/699157
Commit-Queue: Yafei Duan <romax@chromium.org>
Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506625}
parent 26fe88fb
...@@ -18,6 +18,8 @@ static_library("core") { ...@@ -18,6 +18,8 @@ static_library("core") {
"client_policy_controller.h", "client_policy_controller.h",
"model/add_page_task.cc", "model/add_page_task.cc",
"model/add_page_task.h", "model/add_page_task.h",
"model/clear_digest_task.cc",
"model/clear_digest_task.h",
"model/clear_storage_task.cc", "model/clear_storage_task.cc",
"model/clear_storage_task.h", "model/clear_storage_task.h",
"model/create_archive_task.cc", "model/create_archive_task.cc",
...@@ -126,6 +128,7 @@ source_set("unit_tests") { ...@@ -126,6 +128,7 @@ source_set("unit_tests") {
"archive_manager_unittest.cc", "archive_manager_unittest.cc",
"client_policy_controller_unittest.cc", "client_policy_controller_unittest.cc",
"model/add_page_task_unittest.cc", "model/add_page_task_unittest.cc",
"model/clear_digest_task_unittest.cc",
"model/clear_storage_task_unittest.cc", "model/clear_storage_task_unittest.cc",
"model/create_archive_task_unittest.cc", "model/create_archive_task_unittest.cc",
"model/delete_page_task_unittest.cc", "model/delete_page_task_unittest.cc",
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef COMPONENTS_OFFLINE_PAGES_CORE_TASKS_ADD_PAGE_TASK_H_ #ifndef COMPONENTS_OFFLINE_PAGES_CORE_MODEL_ADD_PAGE_TASK_H_
#define COMPONENTS_OFFLINE_PAGES_CORE_TASKS_ADD_PAGE_TASK_H_ #define COMPONENTS_OFFLINE_PAGES_CORE_MODEL_ADD_PAGE_TASK_H_
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
...@@ -49,4 +49,4 @@ class AddPageTask : public Task { ...@@ -49,4 +49,4 @@ class AddPageTask : public Task {
} // namespace offline_pages } // namespace offline_pages
#endif // COMPONENTS_OFFLINE_PAGES_CORE_TASKS_ADD_PAGE_TASK_H #endif // COMPONENTS_OFFLINE_PAGES_CORE_MODEL_ADD_PAGE_TASK_H_
// Copyright 2017 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/offline_pages/core/model/clear_digest_task.h"
#include "base/bind.h"
#include "components/offline_pages/core/offline_page_metadata_store_sql.h"
#include "sql/connection.h"
#include "sql/statement.h"
namespace offline_pages {
namespace {
bool ClearDigestSync(int64_t offline_id, sql::Connection* db) {
const char kSql[] =
"UPDATE OR IGNORE offlinepages_v1"
" SET digest = '' "
" WHERE offline_id = ?";
sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
statement.BindInt64(0, offline_id);
return statement.Run();
}
} // namespace
ClearDigestTask::ClearDigestTask(OfflinePageMetadataStoreSQL* store,
int64_t offline_id)
: store_(store), offline_id_(offline_id), weak_ptr_factory_(this) {}
ClearDigestTask::~ClearDigestTask(){};
void ClearDigestTask::Run() {
store_->Execute(base::BindOnce(&ClearDigestSync, offline_id_),
base::BindOnce(&ClearDigestTask::OnClearDigestDone,
weak_ptr_factory_.GetWeakPtr()));
}
void ClearDigestTask::OnClearDigestDone(bool result) {
// The result is currently omitted, since there's no callback needed for this
// task.
// TODO(romax): Add a UMA collection here, recording the result of db
// operation.
TaskComplete();
}
} // namespace offline_pages
// Copyright 2017 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_OFFLINE_PAGES_CORE_MODEL_CLEAR_DIGEST_TASK_H_
#define COMPONENTS_OFFLINE_PAGES_CORE_MODEL_CLEAR_DIGEST_TASK_H_
#include "stdint.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/offline_pages/core/task.h"
namespace offline_pages {
class OfflinePageMetadataStoreSQL;
// Task that clears the digest field of a page in the metadata store. It takes
// the offline ID of the page that needs to have digest cleared.
// There is no callback needed for this task.
class ClearDigestTask : public Task {
public:
ClearDigestTask(OfflinePageMetadataStoreSQL* store, int64_t offline_id);
~ClearDigestTask() override;
// Task implementation.
void Run() override;
private:
void OnClearDigestDone(bool result);
// The metadata store used to clear the digest. Not owned.
OfflinePageMetadataStoreSQL* store_;
int64_t offline_id_;
base::WeakPtrFactory<ClearDigestTask> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ClearDigestTask);
};
} // namespace offline_pages
#endif // COMPONENTS_OFFLINE_PAGES_CORE_MODEL_CLEAR_DIGEST_TASK_H_
// Copyright 2017 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/offline_pages/core/model/clear_digest_task.h"
#include "base/memory/ptr_util.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/offline_pages/core/model/offline_page_item_generator.h"
#include "components/offline_pages/core/offline_page_metadata_store_test_util.h"
#include "components/offline_pages/core/test_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace offline_pages {
namespace {
const char kTestDigest[] = "ktesTDIgest==";
} // namespace
class ClearDigestTaskTest : public testing::Test {
public:
ClearDigestTaskTest();
~ClearDigestTaskTest() override;
void SetUp() override;
void TearDown() override;
OfflinePageMetadataStoreSQL* store() { return store_test_util_.store(); }
OfflinePageMetadataStoreTestUtil* store_test_util() {
return &store_test_util_;
}
OfflinePageItemGenerator* generator() { return &generator_; }
TestTaskRunner* runner() { return &runner_; }
private:
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
base::ThreadTaskRunnerHandle task_runner_handle_;
OfflinePageMetadataStoreTestUtil store_test_util_;
OfflinePageItemGenerator generator_;
TestTaskRunner runner_;
};
ClearDigestTaskTest::ClearDigestTaskTest()
: task_runner_(new base::TestSimpleTaskRunner()),
task_runner_handle_(task_runner_),
store_test_util_(task_runner_),
runner_(task_runner_) {}
ClearDigestTaskTest::~ClearDigestTaskTest() {}
void ClearDigestTaskTest::SetUp() {
store_test_util_.BuildStoreInMemory();
}
void ClearDigestTaskTest::TearDown() {
store_test_util_.DeleteStore();
}
TEST_F(ClearDigestTaskTest, ClearDigest) {
OfflinePageItem page = generator()->CreateItem();
page.digest = kTestDigest;
store_test_util()->InsertItem(page);
auto task = base::MakeUnique<ClearDigestTask>(store(), page.offline_id);
runner()->RunTask(std::move(task));
// Check the digest of the page is cleared.
auto offline_page = store_test_util()->GetPageByOfflineId(page.offline_id);
EXPECT_TRUE(offline_page);
EXPECT_TRUE(offline_page->digest.empty());
}
} // namespace offline_pages
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
#include "components/offline_pages/core/model/mark_page_accessed_task.h" #include "components/offline_pages/core/model/mark_page_accessed_task.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/time/clock.h"
#include "base/time/default_clock.h"
#include "components/offline_pages/core/offline_page_metadata_store_sql.h" #include "components/offline_pages/core/offline_page_metadata_store_sql.h"
#include "components/offline_pages/core/offline_time_utils.h" #include "components/offline_pages/core/offline_time_utils.h"
#include "sql/connection.h" #include "sql/connection.h"
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef COMPONENTS_OFFLINE_PAGES_CORE_TASKS_MARK_PAGE_ACCESSED_TASK_H_ #ifndef COMPONENTS_OFFLINE_PAGES_CORE_MODEL_MARK_PAGE_ACCESSED_TASK_H_
#define COMPONENTS_OFFLINE_PAGES_CORE_TASKS_MARK_PAGE_ACCESSED_TASK_H_ #define COMPONENTS_OFFLINE_PAGES_CORE_MODEL_MARK_PAGE_ACCESSED_TASK_H_
#include "base/macros.h" #include "base/macros.h"
#include "components/offline_pages/core/task.h" #include "components/offline_pages/core/task.h"
...@@ -16,8 +16,8 @@ namespace offline_pages { ...@@ -16,8 +16,8 @@ namespace offline_pages {
class OfflinePageMetadataStoreSQL; class OfflinePageMetadataStoreSQL;
// Task that mark a page accessed in the metadata store. It takes the offline ID // Task that marks a page accessed in the metadata store. It takes the offline
// of the page accessed, and the time when it was accessed. // ID of the page accessed, and the time when it was accessed.
// There is no callback needed for this task. // There is no callback needed for this task.
class MarkPageAccessedTask : public Task { class MarkPageAccessedTask : public Task {
public: public:
...@@ -41,4 +41,4 @@ class MarkPageAccessedTask : public Task { ...@@ -41,4 +41,4 @@ class MarkPageAccessedTask : public Task {
} // namespace offline_pages } // namespace offline_pages
#endif // COMPONENTS_OFFLINE_PAGES_CORE_TASKS_MARK_PAGE_ACCESSED_TASK_H_ #endif // COMPONENTS_OFFLINE_PAGES_CORE_MODEL_MARK_PAGE_ACCESSED_TASK_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment