Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/prefs/preferences_connection_manager.h" | |
| 6 | |
| 7 #include "chrome/browser/prefs/preferences_manager.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "chrome/browser/profiles/profile_manager.h" | |
| 10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 11 #include "components/keyed_service/content/browser_context_keyed_service_shutdow n_notifier_factory.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Required singleton subclass to control dependencies between | |
| 16 // PreferenceConnectionManager and the KeyedServiceShutdownNotifier. | |
| 17 class ShutdownNotifierFactory | |
| 18 : public BrowserContextKeyedServiceShutdownNotifierFactory { | |
| 19 public: | |
| 20 static ShutdownNotifierFactory* GetInstance() { | |
| 21 return base::Singleton<ShutdownNotifierFactory>::get(); | |
| 22 } | |
| 23 | |
| 24 private: | |
| 25 friend struct base::DefaultSingletonTraits<ShutdownNotifierFactory>; | |
| 26 | |
| 27 ShutdownNotifierFactory() | |
| 28 : BrowserContextKeyedServiceShutdownNotifierFactory( | |
| 29 "PreferencesConnectionManager") { | |
| 30 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | |
| 31 } | |
| 32 ~ShutdownNotifierFactory() override {} | |
| 33 | |
| 34 DISALLOW_COPY_AND_ASSIGN(ShutdownNotifierFactory); | |
| 35 }; | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 PreferencesConnectionManager::PreferencesConnectionManager() | |
| 40 : profile_shutdown_notification_( | |
| 41 ShutdownNotifierFactory::GetInstance() | |
| 42 ->Get(ProfileManager::GetActiveUserProfile()) | |
| 43 ->Subscribe( | |
| 44 base::Bind(&PreferencesConnectionManager::OnProfileDestroyed, | |
| 45 base::Unretained(this)))) {} | |
| 46 | |
| 47 PreferencesConnectionManager::~PreferencesConnectionManager() {} | |
| 48 | |
| 49 void PreferencesConnectionManager::ProcessRequest( | |
| 50 prefs::mojom::PreferencesManagerRequest request) { | |
| 51 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 52 mojo::StrongBindingPtr<prefs::mojom::PreferencesManager> binding = | |
| 53 mojo::MakeStrongBinding(base::MakeUnique<PreferencesManager>(profile), | |
| 54 std::move(request)); | |
| 55 // Copying the base::WeakPtr for future deletion. | |
| 56 binding->set_connection_error_handler( | |
| 57 base::Bind(&PreferencesConnectionManager::OnConnectionError, | |
| 58 base::Unretained(this), binding)); | |
| 59 bindings_.push_back(std::move(binding)); | |
| 60 } | |
| 61 | |
| 62 void PreferencesConnectionManager::OnConnectionError( | |
| 63 mojo::StrongBindingPtr<prefs::mojom::PreferencesManager> binding) { | |
| 64 if (!binding) | |
| 65 return; | |
| 66 auto it = std::begin(bindings_); | |
| 67 while (it != std::end(bindings_)) { | |
|
dcheng
2016/12/15 16:17:08
I don't think the iterator is advanced here (it's
jonross
2016/12/19 15:58:13
Done.
| |
| 68 if (it->get() == binding.get()) { | |
| 69 bindings_.erase(it); | |
| 70 return; | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 void PreferencesConnectionManager::OnProfileDestroyed() { | |
| 76 for (auto& it : bindings_) { | |
| 77 // Shutdown any PreferenceManager that is still alive. | |
| 78 if (it) | |
| 79 it->Close(); | |
| 80 } | |
| 81 | |
| 82 profile_shutdown_notification_.reset(); | |
| 83 } | |
| OLD | NEW |