OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "components/wifi_sync/wifi_credential_syncable_service_factory.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "build/build_config.h" | |
12 #include "components/keyed_service/content/browser_context_dependency_manager.h" | |
13 #include "components/wifi_sync/wifi_config_delegate.h" | |
14 #include "components/wifi_sync/wifi_credential_syncable_service.h" | |
15 #include "content/public/browser/browser_context.h" | |
16 | |
17 #if defined(OS_CHROMEOS) | |
18 #include "base/files/file_path.h" | |
19 #include "chromeos/login/login_state.h" | |
20 #include "chromeos/network/network_handler.h" | |
21 #include "components/wifi_sync/wifi_config_delegate_chromeos.h" | |
22 #endif | |
23 | |
24 namespace wifi_sync { | |
25 | |
26 namespace { | |
27 | |
28 #if defined(OS_CHROMEOS) | |
29 // Returns a string identifying a ChromeOS network settings profile, | |
30 // by that profile's UserHash property. This value may be communicated | |
31 // to the ChromeOS connection manager ("Shill"), but must not be | |
32 // exposed to any untrusted code (e.g., via web APIs). | |
33 std::string GetUserHash(content::BrowserContext* context, | |
34 bool use_login_state) { | |
35 if (use_login_state) { | |
36 const chromeos::LoginState* login_state = chromeos::LoginState::Get(); | |
37 DCHECK(login_state->IsUserLoggedIn()); | |
38 DCHECK(!login_state->primary_user_hash().empty()); | |
39 // TODO(quiche): Verify that |context| is the primary user's context. | |
40 return login_state->primary_user_hash(); | |
41 } else { | |
42 // In WiFi credential sync tests, LoginState is not | |
43 // available. Instead, those tests set their Shill profiles' | |
44 // UserHashes based on the corresponding BrowserContexts' storage | |
45 // paths. | |
46 return context->GetPath().BaseName().value(); | |
47 } | |
48 } | |
49 #endif | |
50 | |
51 } // namespace | |
52 | |
53 // static | |
54 WifiCredentialSyncableService* | |
55 WifiCredentialSyncableServiceFactory::GetForBrowserContext( | |
56 content::BrowserContext* browser_context) { | |
57 return static_cast<WifiCredentialSyncableService*>( | |
58 GetInstance()->GetServiceForBrowserContext(browser_context, true)); | |
59 } | |
60 | |
61 // static | |
62 WifiCredentialSyncableServiceFactory* | |
63 WifiCredentialSyncableServiceFactory::GetInstance() { | |
64 return base::Singleton<WifiCredentialSyncableServiceFactory>::get(); | |
65 } | |
66 | |
67 // Private methods. | |
68 | |
69 WifiCredentialSyncableServiceFactory::WifiCredentialSyncableServiceFactory() | |
70 : BrowserContextKeyedServiceFactory( | |
71 "WifiCredentialSyncableService", | |
72 BrowserContextDependencyManager::GetInstance()) { | |
73 } | |
74 | |
75 WifiCredentialSyncableServiceFactory::~WifiCredentialSyncableServiceFactory() { | |
76 } | |
77 | |
78 KeyedService* WifiCredentialSyncableServiceFactory::BuildServiceInstanceFor( | |
79 content::BrowserContext* context) const { | |
80 // TODO(quiche): Figure out if this behaves properly for multi-profile. | |
81 // crbug.com/430681. | |
82 #if defined(OS_CHROMEOS) | |
83 return new WifiCredentialSyncableService( | |
84 BuildWifiConfigDelegateChromeOs(context)); | |
85 #else | |
86 NOTREACHED(); | |
87 return nullptr; | |
88 #endif | |
89 } | |
90 | |
91 #if defined(OS_CHROMEOS) | |
92 std::unique_ptr<WifiConfigDelegate> | |
93 WifiCredentialSyncableServiceFactory::BuildWifiConfigDelegateChromeOs( | |
94 content::BrowserContext* context) const { | |
95 // Note: NetworkHandler is a singleton that is managed by | |
96 // ChromeBrowserMainPartsChromeos, and destroyed after all | |
97 // KeyedService instances are destroyed. | |
98 chromeos::NetworkHandler* network_handler = chromeos::NetworkHandler::Get(); | |
99 return base::MakeUnique<WifiConfigDelegateChromeOs>( | |
100 GetUserHash(context, !ignore_login_state_for_test_), | |
101 network_handler->managed_network_configuration_handler()); | |
102 } | |
103 #endif | |
104 | |
105 } // namespace wifi_sync | |
OLD | NEW |