OLD | NEW |
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/content_settings/content_settings_pref_provider.h" | 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/prefs/default_pref_store.h" | 11 #include "base/prefs/default_pref_store.h" |
12 #include "base/prefs/overlay_user_pref_store.h" | 12 #include "base/prefs/overlay_user_pref_store.h" |
13 #include "base/prefs/public/pref_change_registrar.h" | 13 #include "base/prefs/public/pref_change_registrar.h" |
| 14 #include "base/prefs/public/pref_observer.h" |
14 #include "base/prefs/testing_pref_store.h" | 15 #include "base/prefs/testing_pref_store.h" |
15 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 18 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
18 #include "chrome/browser/content_settings/content_settings_utils.h" | 19 #include "chrome/browser/content_settings/content_settings_utils.h" |
19 #include "chrome/browser/prefs/browser_prefs.h" | 20 #include "chrome/browser/prefs/browser_prefs.h" |
20 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
21 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 22 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
22 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 23 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
23 #include "chrome/common/chrome_notification_types.h" | |
24 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
26 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
27 #include "chrome/test/base/testing_pref_service.h" | 27 #include "chrome/test/base/testing_pref_service.h" |
28 #include "chrome/test/base/testing_profile.h" | 28 #include "chrome/test/base/testing_profile.h" |
29 #include "content/public/browser/notification_observer.h" | |
30 #include "content/public/test/test_browser_thread.h" | 29 #include "content/public/test/test_browser_thread.h" |
31 #include "googleurl/src/gurl.h" | 30 #include "googleurl/src/gurl.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
33 | 32 |
34 using ::testing::_; | 33 using ::testing::_; |
35 using content::BrowserThread; | 34 using content::BrowserThread; |
36 | 35 |
37 namespace content_settings { | 36 namespace content_settings { |
38 | 37 |
39 class DeadlockCheckerThread : public base::PlatformThread::Delegate { | 38 class DeadlockCheckerThread : public base::PlatformThread::Delegate { |
40 public: | 39 public: |
41 explicit DeadlockCheckerThread(PrefProvider* provider) | 40 explicit DeadlockCheckerThread(PrefProvider* provider) |
42 : provider_(provider) {} | 41 : provider_(provider) {} |
43 | 42 |
44 virtual void ThreadMain() { | 43 virtual void ThreadMain() { |
45 bool got_lock = provider_->lock_.Try(); | 44 bool got_lock = provider_->lock_.Try(); |
46 EXPECT_TRUE(got_lock); | 45 EXPECT_TRUE(got_lock); |
47 if (got_lock) | 46 if (got_lock) |
48 provider_->lock_.Release(); | 47 provider_->lock_.Release(); |
49 } | 48 } |
50 private: | 49 private: |
51 PrefProvider* provider_; | 50 PrefProvider* provider_; |
52 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerThread); | 51 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerThread); |
53 }; | 52 }; |
54 | 53 |
55 // A helper for observing an preference changes and testing whether | 54 // A helper for observing an preference changes and testing whether |
56 // |PrefProvider| holds a lock when the preferences change. | 55 // |PrefProvider| holds a lock when the preferences change. |
57 class DeadlockCheckerObserver : public content::NotificationObserver { | 56 class DeadlockCheckerObserver : public PrefObserver { |
58 public: | 57 public: |
59 // |DeadlockCheckerObserver| doesn't take the ownership of |prefs| or | 58 // |DeadlockCheckerObserver| doesn't take the ownership of |prefs| or |
60 // ||provider|. | 59 // ||provider|. |
61 DeadlockCheckerObserver(PrefService* prefs, PrefProvider* provider) | 60 DeadlockCheckerObserver(PrefService* prefs, PrefProvider* provider) |
62 : provider_(provider), | 61 : provider_(provider), |
63 notification_received_(false) { | 62 notification_received_(false) { |
64 pref_change_registrar_.Init(prefs); | 63 pref_change_registrar_.Init(prefs); |
65 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this); | 64 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this); |
66 } | 65 } |
67 virtual ~DeadlockCheckerObserver() {} | 66 virtual ~DeadlockCheckerObserver() {} |
68 | 67 |
69 virtual void Observe(int type, | 68 virtual void OnPreferenceChanged(PrefServiceBase* service, |
70 const content::NotificationSource& source, | 69 const std::string& pref_name) { |
71 const content::NotificationDetails& details) { | 70 // Check whether |provider_| holds its lock. For this, we need a |
72 ASSERT_EQ(type, chrome::NOTIFICATION_PREF_CHANGED); | 71 // separate thread. |
73 // Check whether |provider_| holds its lock. For this, we need a separate | |
74 // thread. | |
75 DeadlockCheckerThread thread(provider_); | 72 DeadlockCheckerThread thread(provider_); |
76 base::PlatformThreadHandle handle = base::kNullThreadHandle; | 73 base::PlatformThreadHandle handle = base::kNullThreadHandle; |
77 ASSERT_TRUE(base::PlatformThread::Create(0, &thread, &handle)); | 74 ASSERT_TRUE(base::PlatformThread::Create(0, &thread, &handle)); |
78 base::PlatformThread::Join(handle); | 75 base::PlatformThread::Join(handle); |
79 notification_received_ = true; | 76 notification_received_ = true; |
80 } | 77 } |
81 | 78 |
82 bool notification_received() const { | 79 bool notification_received() const { |
83 return notification_received_; | 80 return notification_received_; |
84 } | 81 } |
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 DictionaryValue* mutable_settings = update.Get(); | 568 DictionaryValue* mutable_settings = update.Get(); |
572 mutable_settings->SetWithoutPathExpansion("www.example.com,*", | 569 mutable_settings->SetWithoutPathExpansion("www.example.com,*", |
573 new base::DictionaryValue()); | 570 new base::DictionaryValue()); |
574 } | 571 } |
575 EXPECT_TRUE(observer.notification_received()); | 572 EXPECT_TRUE(observer.notification_received()); |
576 | 573 |
577 provider.ShutdownOnUIThread(); | 574 provider.ShutdownOnUIThread(); |
578 } | 575 } |
579 | 576 |
580 } // namespace content_settings | 577 } // namespace content_settings |
OLD | NEW |