Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1032)

Side by Side Diff: content/browser/dom_storage/local_storage_context_mojo_unittest.cc

Issue 2722293002: Fix lifetime of leveldb::MojoEnv instances. (Closed)
Patch Set: annotate leaks Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/dom_storage/local_storage_context_mojo.h" 5 #include "content/browser/dom_storage/local_storage_context_mojo.h"
6 6
7 #include "base/debug/leak_annotations.h"
7 #include "base/files/file_enumerator.h" 8 #include "base/files/file_enumerator.h"
8 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
9 #include "base/run_loop.h" 10 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
11 #include "components/filesystem/public/interfaces/file_system.mojom.h" 12 #include "components/filesystem/public/interfaces/file_system.mojom.h"
13 #include "components/leveldb/env_mojo.h"
12 #include "components/leveldb/public/cpp/util.h" 14 #include "components/leveldb/public/cpp/util.h"
13 #include "content/browser/dom_storage/dom_storage_area.h" 15 #include "content/browser/dom_storage/dom_storage_area.h"
14 #include "content/browser/dom_storage/dom_storage_context_impl.h" 16 #include "content/browser/dom_storage/dom_storage_context_impl.h"
15 #include "content/browser/dom_storage/dom_storage_namespace.h" 17 #include "content/browser/dom_storage/dom_storage_namespace.h"
16 #include "content/browser/dom_storage/dom_storage_task_runner.h" 18 #include "content/browser/dom_storage/dom_storage_task_runner.h"
17 #include "content/common/dom_storage/dom_storage_types.h" 19 #include "content/common/dom_storage/dom_storage_types.h"
18 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/local_storage_usage_info.h" 21 #include "content/public/browser/local_storage_usage_info.h"
20 #include "content/public/test/test_browser_thread_bundle.h" 22 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "content/test/mock_leveldb_database.h" 23 #include "content/test/mock_leveldb_database.h"
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 } 612 }
611 613
612 namespace { 614 namespace {
613 615
614 class ServiceTestClient : public service_manager::test::ServiceTestClient, 616 class ServiceTestClient : public service_manager::test::ServiceTestClient,
615 public service_manager::mojom::ServiceFactory, 617 public service_manager::mojom::ServiceFactory,
616 public service_manager::InterfaceFactory< 618 public service_manager::InterfaceFactory<
617 service_manager::mojom::ServiceFactory> { 619 service_manager::mojom::ServiceFactory> {
618 public: 620 public:
619 explicit ServiceTestClient(service_manager::test::ServiceTest* test) 621 explicit ServiceTestClient(service_manager::test::ServiceTest* test)
620 : service_manager::test::ServiceTestClient(test) { 622 : service_manager::test::ServiceTestClient(test),
623 leveldb_env_(new leveldb::MojoEnv(
624 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE))) {
625 ANNOTATE_LEAKING_OBJECT_PTR(leveldb_env_);
621 registry_.AddInterface<service_manager::mojom::ServiceFactory>(this); 626 registry_.AddInterface<service_manager::mojom::ServiceFactory>(this);
622 } 627 }
623 ~ServiceTestClient() override {} 628 ~ServiceTestClient() override {}
624 629
625 protected: 630 protected:
626 void OnBindInterface(const service_manager::ServiceInfo& source_info, 631 void OnBindInterface(const service_manager::ServiceInfo& source_info,
627 const std::string& interface_name, 632 const std::string& interface_name,
628 mojo::ScopedMessagePipeHandle interface_pipe) override { 633 mojo::ScopedMessagePipeHandle interface_pipe) override {
629 registry_.BindInterface(source_info.identity, interface_name, 634 registry_.BindInterface(source_info.identity, interface_name,
630 std::move(interface_pipe)); 635 std::move(interface_pipe));
631 } 636 }
632 637
633 void CreateService(service_manager::mojom::ServiceRequest request, 638 void CreateService(service_manager::mojom::ServiceRequest request,
634 const std::string& name) override { 639 const std::string& name) override {
635 if (name == file::mojom::kServiceName) { 640 if (name == file::mojom::kServiceName) {
636 file_service_context_.reset(new service_manager::ServiceContext( 641 file_service_context_.reset(new service_manager::ServiceContext(
637 file::CreateFileService( 642 file::CreateFileService(
638 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE), 643 leveldb_env_,
639 BrowserThread::GetTaskRunnerForThread(BrowserThread::DB)), 644 BrowserThread::GetTaskRunnerForThread(BrowserThread::DB)),
640 std::move(request))); 645 std::move(request)));
641 } 646 }
642 } 647 }
643 648
644 void Create(const service_manager::Identity& remote_identity, 649 void Create(const service_manager::Identity& remote_identity,
645 service_manager::mojom::ServiceFactoryRequest request) override { 650 service_manager::mojom::ServiceFactoryRequest request) override {
646 service_factory_bindings_.AddBinding(this, std::move(request)); 651 service_factory_bindings_.AddBinding(this, std::move(request));
647 } 652 }
648 653
649 private: 654 private:
650 service_manager::BinderRegistry registry_; 655 service_manager::BinderRegistry registry_;
651 mojo::BindingSet<service_manager::mojom::ServiceFactory> 656 mojo::BindingSet<service_manager::mojom::ServiceFactory>
652 service_factory_bindings_; 657 service_factory_bindings_;
653 std::unique_ptr<service_manager::ServiceContext> file_service_context_; 658 std::unique_ptr<service_manager::ServiceContext> file_service_context_;
659 leveldb::MojoEnv* leveldb_env_;
654 }; 660 };
655 661
656 } // namespace 662 } // namespace
657 663
658 class LocalStorageContextMojoTestWithService 664 class LocalStorageContextMojoTestWithService
659 : public service_manager::test::ServiceTest { 665 : public service_manager::test::ServiceTest {
660 public: 666 public:
661 LocalStorageContextMojoTestWithService() 667 LocalStorageContextMojoTestWithService()
662 : ServiceTest("content_unittests", false), 668 : ServiceTest("content_unittests", false),
663 thread_bundle_(TestBrowserThreadBundle::REAL_FILE_THREAD) {} 669 thread_bundle_(TestBrowserThreadBundle::REAL_FILE_THREAD) {}
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
811 DoTestPut(context.get(), key, value); 817 DoTestPut(context.get(), key, value);
812 std::vector<uint8_t> result; 818 std::vector<uint8_t> result;
813 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 819 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
814 EXPECT_EQ(value, result); 820 EXPECT_EQ(value, result);
815 821
816 context.reset(); 822 context.reset();
817 base::RunLoop().RunUntilIdle(); 823 base::RunLoop().RunUntilIdle();
818 824
819 { 825 {
820 // Mess up version number in database. 826 // Mess up version number in database.
821 leveldb_env::ChromiumEnv env;
822 leveldb::DB* db = nullptr; 827 leveldb::DB* db = nullptr;
823 leveldb::Options options; 828 leveldb::Options options;
824 options.env = &env;
825 base::FilePath db_path = 829 base::FilePath db_path =
826 temp_path().Append(test_path).Append(FILE_PATH_LITERAL("leveldb")); 830 temp_path().Append(test_path).Append(FILE_PATH_LITERAL("leveldb"));
827 ASSERT_TRUE(leveldb::DB::Open(options, db_path.AsUTF8Unsafe(), &db).ok()); 831 ASSERT_TRUE(leveldb::DB::Open(options, db_path.AsUTF8Unsafe(), &db).ok());
828 std::unique_ptr<leveldb::DB> db_owner(db); 832 std::unique_ptr<leveldb::DB> db_owner(db);
829 ASSERT_TRUE(db->Put(leveldb::WriteOptions(), "VERSION", "argh").ok()); 833 ASSERT_TRUE(db->Put(leveldb::WriteOptions(), "VERSION", "argh").ok());
830 } 834 }
831 835
832 // Make sure data is gone. 836 // Make sure data is gone.
833 context = base::MakeUnique<LocalStorageContextMojo>( 837 context = base::MakeUnique<LocalStorageContextMojo>(
834 connector(), nullptr, base::FilePath(), test_path); 838 connector(), nullptr, base::FilePath(), test_path);
835 EXPECT_FALSE(DoTestGet(context.get(), key, &result)); 839 EXPECT_FALSE(DoTestGet(context.get(), key, &result));
836 840
837 // Write data again. 841 // Write data again.
838 DoTestPut(context.get(), key, value); 842 DoTestPut(context.get(), key, value);
839 843
840 context.reset(); 844 context.reset();
841 base::RunLoop().RunUntilIdle(); 845 base::RunLoop().RunUntilIdle();
842 846
843 // Data should have been preserved now. 847 // Data should have been preserved now.
844 context = base::MakeUnique<LocalStorageContextMojo>( 848 context = base::MakeUnique<LocalStorageContextMojo>(
845 connector(), nullptr, base::FilePath(), test_path); 849 connector(), nullptr, base::FilePath(), test_path);
846 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 850 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
847 EXPECT_EQ(value, result); 851 EXPECT_EQ(value, result);
848 } 852 }
849 853
850 } // namespace content 854 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698