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