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

Side by Side Diff: chrome/browser/extensions/test_extension_prefs.cc

Issue 11027070: Moved JsonPrefStore to use SequencedWorkerPool (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/extensions/test_extension_prefs.h" 5 #include "chrome/browser/extensions/test_extension_prefs.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/message_loop_proxy.h" 12 #include "base/message_loop_proxy.h"
13 #include "base/run_loop.h"
14 #include "base/sequenced_task_runner.h"
13 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
14 #include "base/values.h" 16 #include "base/values.h"
15 #include "chrome/browser/extensions/extension_pref_store.h" 17 #include "chrome/browser/extensions/extension_pref_store.h"
16 #include "chrome/browser/extensions/extension_pref_value_map.h" 18 #include "chrome/browser/extensions/extension_pref_value_map.h"
17 #include "chrome/browser/extensions/extension_prefs.h" 19 #include "chrome/browser/extensions/extension_prefs.h"
18 #include "chrome/browser/prefs/pref_service.h" 20 #include "chrome/browser/prefs/pref_service.h"
19 #include "chrome/browser/prefs/pref_service_mock_builder.h" 21 #include "chrome/browser/prefs/pref_service_mock_builder.h"
20 #include "chrome/browser/prefs/pref_value_store.h" 22 #include "chrome/browser/prefs/pref_value_store.h"
21 #include "chrome/common/extensions/extension.h" 23 #include "chrome/common/extensions/extension.h"
22 #include "chrome/common/extensions/extension_manifest_constants.h" 24 #include "chrome/common/extensions/extension_manifest_constants.h"
23 #include "chrome/common/json_pref_store.h" 25 #include "chrome/common/json_pref_store.h"
24 #include "content/public/browser/browser_thread.h" 26 #include "content/public/browser/browser_thread.h"
25 #include "sync/api/string_ordinal.h" 27 #include "sync/api/string_ordinal.h"
26 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
27 29
28 using content::BrowserThread; 30 using content::BrowserThread;
29 31
30 namespace extensions { 32 namespace extensions {
31 33
32 namespace { 34 namespace {
33 35
36 void DoNothing() {}
37
34 // Mock ExtensionPrefs class with artificial clock to guarantee that no two 38 // Mock ExtensionPrefs class with artificial clock to guarantee that no two
35 // extensions get the same installation time stamp and we can reliably 39 // extensions get the same installation time stamp and we can reliably
36 // assert the installation order in the tests below. 40 // assert the installation order in the tests below.
37 class MockExtensionPrefs : public ExtensionPrefs { 41 class MockExtensionPrefs : public ExtensionPrefs {
38 public: 42 public:
39 MockExtensionPrefs(PrefService* prefs, 43 MockExtensionPrefs(PrefService* prefs,
40 const FilePath& root_dir, 44 const FilePath& root_dir,
41 ExtensionPrefValueMap* extension_pref_value_map) 45 ExtensionPrefValueMap* extension_pref_value_map)
42 : ExtensionPrefs(prefs, root_dir, extension_pref_value_map), 46 : ExtensionPrefs(prefs, root_dir, extension_pref_value_map),
43 currentTime(base::Time::Now()) {} 47 currentTime(base::Time::Now()) {}
44 ~MockExtensionPrefs() {} 48 ~MockExtensionPrefs() {}
45 49
46 protected: 50 protected:
47 mutable base::Time currentTime; 51 mutable base::Time currentTime;
48 52
49 virtual base::Time GetCurrentTime() const { 53 virtual base::Time GetCurrentTime() const {
50 currentTime += base::TimeDelta::FromSeconds(10); 54 currentTime += base::TimeDelta::FromSeconds(10);
51 return currentTime; 55 return currentTime;
52 } 56 }
53 }; 57 };
54 58
55 } // namespace 59 } // namespace
56 60
57 TestExtensionPrefs::TestExtensionPrefs() 61 TestExtensionPrefs::TestExtensionPrefs(
58 : pref_service_(NULL), 62 base::SequencedTaskRunner* task_runner) : pref_service_(NULL),
59 extensions_disabled_(false) { 63 extensions_disabled_(false) {
64 task_runner_ = task_runner;
akalin 2012/10/19 02:00:51 initialize in constructor list
zel 2012/10/19 18:45:07 Done.
60 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); 65 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
61 preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); 66 preferences_file_ = temp_dir_.path().AppendASCII("Preferences");
62 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); 67 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions");
63 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); 68 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_));
64 69
65 RecreateExtensionPrefs(); 70 RecreateExtensionPrefs();
66 } 71 }
67 72
68 TestExtensionPrefs::~TestExtensionPrefs() {} 73 TestExtensionPrefs::~TestExtensionPrefs() {
74 }
69 75
70 void TestExtensionPrefs::RecreateExtensionPrefs() { 76 void TestExtensionPrefs::RecreateExtensionPrefs() {
71 // We persist and reload the PrefService's PrefStores because this process 77 // We persist and reload the PrefService's PrefStores because this process
72 // deletes all empty dictionaries. The ExtensionPrefs implementation 78 // deletes all empty dictionaries. The ExtensionPrefs implementation
73 // needs to be able to handle this situation. 79 // needs to be able to handle this situation.
74 if (pref_service_.get()) { 80 if (pref_service_.get()) {
75 // The PrefService writes its persistent file on the file thread, so we 81 pref_service_->CommitPendingWrite();
akalin 2012/10/19 02:00:51 probably worth adding a comment. Something like:
zel 2012/10/19 18:45:07 Done.
76 // need to wait for any pending I/O to complete before creating a new 82 base::RunLoop run_loop;
77 // PrefService. 83 ASSERT_TRUE(
78 base::WaitableEvent io_finished(false, false); 84 task_runner_->PostTaskAndReply(
79 pref_service_-> CommitPendingWrite(); 85 FROM_HERE,
80 EXPECT_TRUE(BrowserThread::PostTask( 86 base::Bind(&DoNothing),
81 BrowserThread::FILE, 87 run_loop.QuitClosure()));
82 FROM_HERE, 88 run_loop.Run();
83 base::Bind(&base::WaitableEvent::Signal,
84 base::Unretained(&io_finished))));
85
86 // If the FILE thread is in fact the current thread (possible in testing
87 // scenarios), we have to ensure the task has a chance to run. If the FILE
88 // thread is a different thread, the test must ensure that thread is running
89 // (otherwise the Wait below will hang).
90 MessageLoop::current()->RunAllPending();
91
92 io_finished.Wait();
93 } 89 }
94 90
95 extension_pref_value_map_.reset(new ExtensionPrefValueMap); 91 extension_pref_value_map_.reset(new ExtensionPrefValueMap);
96 PrefServiceMockBuilder builder; 92 PrefServiceMockBuilder builder;
97 builder.WithUserFilePrefs(preferences_file_); 93 builder.WithUserFilePrefs(preferences_file_, task_runner_);
98 builder.WithExtensionPrefs( 94 builder.WithExtensionPrefs(
99 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); 95 new ExtensionPrefStore(extension_pref_value_map_.get(), false));
100 pref_service_.reset(builder.Create()); 96 pref_service_.reset(builder.Create());
101 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); 97 ExtensionPrefs::RegisterUserPrefs(pref_service_.get());
102 98
103 prefs_.reset(new MockExtensionPrefs(pref_service_.get(), 99 prefs_.reset(new MockExtensionPrefs(pref_service_.get(),
104 temp_dir_.path(), 100 temp_dir_.path(),
105 extension_pref_value_map_.get())); 101 extension_pref_value_map_.get()));
106 prefs_->Init(extensions_disabled_); 102 prefs_->Init(extensions_disabled_);
107 } 103 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { 154 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const {
159 return pref_service_->CreateIncognitoPrefService( 155 return pref_service_->CreateIncognitoPrefService(
160 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); 156 new ExtensionPrefStore(extension_pref_value_map_.get(), true));
161 } 157 }
162 158
163 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) { 159 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) {
164 extensions_disabled_ = extensions_disabled; 160 extensions_disabled_ = extensions_disabled;
165 } 161 }
166 162
167 } // namespace extensions 163 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698