File: wifi_credential_syncable_service_factory.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (103 lines) | stat: -rw-r--r-- 3,669 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync_wifi/wifi_credential_syncable_service_factory.h"

#include <string>

#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "build/build_config.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/sync_wifi/wifi_config_delegate.h"
#include "components/sync_wifi/wifi_credential_syncable_service.h"
#include "content/public/browser/browser_context.h"

#if defined(OS_CHROMEOS)
#include "base/files/file_path.h"
#include "chromeos/login/login_state.h"
#include "chromeos/network/network_handler.h"
#include "components/sync_wifi/wifi_config_delegate_chromeos.h"
#endif

namespace sync_wifi {

namespace {

#if defined(OS_CHROMEOS)
// Returns a string identifying a ChromeOS network settings profile,
// by that profile's UserHash property. This value may be communicated
// to the ChromeOS connection manager ("Shill"), but must not be
// exposed to any untrusted code (e.g., via web APIs).
std::string GetUserHash(content::BrowserContext* context,
                        bool use_login_state) {
  if (use_login_state) {
    const chromeos::LoginState* login_state = chromeos::LoginState::Get();
    DCHECK(login_state->IsUserLoggedIn());
    DCHECK(!login_state->primary_user_hash().empty());
    // TODO(quiche): Verify that |context| is the primary user's context.
    return login_state->primary_user_hash();
  } else {
    // In WiFi credential sync tests, LoginState is not
    // available. Instead, those tests set their Shill profiles'
    // UserHashes based on the corresponding BrowserContexts' storage
    // paths.
    return context->GetPath().BaseName().value();
  }
}
#endif

}  // namespace

// static
WifiCredentialSyncableService*
WifiCredentialSyncableServiceFactory::GetForBrowserContext(
    content::BrowserContext* browser_context) {
  return static_cast<WifiCredentialSyncableService*>(
      GetInstance()->GetServiceForBrowserContext(browser_context, true));
}

// static
WifiCredentialSyncableServiceFactory*
WifiCredentialSyncableServiceFactory::GetInstance() {
  return base::Singleton<WifiCredentialSyncableServiceFactory>::get();
}

// Private methods.

WifiCredentialSyncableServiceFactory::WifiCredentialSyncableServiceFactory()
    : BrowserContextKeyedServiceFactory(
          "WifiCredentialSyncableService",
          BrowserContextDependencyManager::GetInstance()) {}

WifiCredentialSyncableServiceFactory::~WifiCredentialSyncableServiceFactory() {}

KeyedService* WifiCredentialSyncableServiceFactory::BuildServiceInstanceFor(
    content::BrowserContext* context) const {
// TODO(quiche): Figure out if this behaves properly for multi-profile.
// crbug.com/430681.
#if defined(OS_CHROMEOS)
  return new WifiCredentialSyncableService(
      BuildWifiConfigDelegateChromeOs(context));
#else
  NOTREACHED();
  return nullptr;
#endif
}

#if defined(OS_CHROMEOS)
std::unique_ptr<WifiConfigDelegate>
WifiCredentialSyncableServiceFactory::BuildWifiConfigDelegateChromeOs(
    content::BrowserContext* context) const {
  // Note: NetworkHandler is a singleton that is managed by
  // ChromeBrowserMainPartsChromeos, and destroyed after all
  // KeyedService instances are destroyed.
  chromeos::NetworkHandler* network_handler = chromeos::NetworkHandler::Get();
  return base::MakeUnique<WifiConfigDelegateChromeOs>(
      GetUserHash(context, !ignore_login_state_for_test_),
      network_handler->managed_network_configuration_handler());
}
#endif

}  // namespace sync_wifi