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/browser_process.h" |
| 8 #include "chrome/browser/prefs/preferences_manager.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/profiles/profile_manager.h" |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 12 #include "components/keyed_service/content/browser_context_keyed_service_shutdow
n_notifier_factory.h" |
| 13 #include "services/service_manager/public/cpp/interface_registry.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Required singleton subclass to control dependencies between |
| 18 // PreferenceConnectionManager and the KeyedServiceShutdownNotifier. |
| 19 class ShutdownNotifierFactory |
| 20 : public BrowserContextKeyedServiceShutdownNotifierFactory { |
| 21 public: |
| 22 static ShutdownNotifierFactory* GetInstance() { |
| 23 return base::Singleton<ShutdownNotifierFactory>::get(); |
| 24 } |
| 25 |
| 26 private: |
| 27 friend struct base::DefaultSingletonTraits<ShutdownNotifierFactory>; |
| 28 |
| 29 ShutdownNotifierFactory() |
| 30 : BrowserContextKeyedServiceShutdownNotifierFactory( |
| 31 "PreferencesConnectionManager") { |
| 32 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
| 33 } |
| 34 ~ShutdownNotifierFactory() override {} |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(ShutdownNotifierFactory); |
| 37 }; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 PreferencesConnectionManager::PreferencesConnectionManager() {} |
| 42 |
| 43 PreferencesConnectionManager::~PreferencesConnectionManager() {} |
| 44 |
| 45 void PreferencesConnectionManager::OnConnectionError( |
| 46 mojo::StrongBindingPtr<prefs::mojom::PreferencesManager> binding) { |
| 47 if (!binding) |
| 48 return; |
| 49 for (auto it = std::begin(bindings_); it != std::end(bindings_); ++it) { |
| 50 if (it->get() == binding.get()) { |
| 51 bindings_.erase(it); |
| 52 return; |
| 53 } |
| 54 } |
| 55 } |
| 56 |
| 57 void PreferencesConnectionManager::OnProfileDestroyed() { |
| 58 for (auto& it : bindings_) { |
| 59 // Shutdown any PreferenceManager that is still alive. |
| 60 if (it) |
| 61 it->Close(); |
| 62 } |
| 63 |
| 64 profile_shutdown_notification_.reset(); |
| 65 } |
| 66 |
| 67 void PreferencesConnectionManager::Create( |
| 68 const service_manager::Identity& remote_identity, |
| 69 prefs::mojom::PreferencesManagerRequest request) { |
| 70 // Certain tests have no profiles to connect to, and static initializers |
| 71 // which block the creation of test profiles. |
| 72 if (!g_browser_process->profile_manager()->GetNumberOfProfiles()) |
| 73 return; |
| 74 |
| 75 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 76 mojo::StrongBindingPtr<prefs::mojom::PreferencesManager> binding = |
| 77 mojo::MakeStrongBinding(base::MakeUnique<PreferencesManager>(profile), |
| 78 std::move(request)); |
| 79 // Copying the base::WeakPtr for future deletion. |
| 80 binding->set_connection_error_handler( |
| 81 base::Bind(&PreferencesConnectionManager::OnConnectionError, |
| 82 base::Unretained(this), binding)); |
| 83 bindings_.push_back(std::move(binding)); |
| 84 } |
| 85 |
| 86 void PreferencesConnectionManager::OnStart() { |
| 87 // Certain tests have no profiles to connect to, and static initializers |
| 88 // which block the creation of test profiles. |
| 89 if (!g_browser_process->profile_manager()->GetNumberOfProfiles()) |
| 90 return; |
| 91 |
| 92 profile_shutdown_notification_ = |
| 93 ShutdownNotifierFactory::GetInstance() |
| 94 ->Get(ProfileManager::GetActiveUserProfile()) |
| 95 ->Subscribe( |
| 96 base::Bind(&PreferencesConnectionManager::OnProfileDestroyed, |
| 97 base::Unretained(this))); |
| 98 } |
| 99 |
| 100 bool PreferencesConnectionManager::OnConnect( |
| 101 const service_manager::ServiceInfo& remote_info, |
| 102 service_manager::InterfaceRegistry* registry) { |
| 103 registry->AddInterface<prefs::mojom::PreferencesManager>(this); |
| 104 return true; |
| 105 } |
OLD | NEW |