summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/ownership/owner_settings_service.cc
blob: fa7a21a7010e6ba2a61436a3192a4e940ceb9dfb (plain)
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
// 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 "chrome/browser/chromeos/ownership/owner_settings_service.h"

#include <string>

#include "base/bind.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/login/users/user.h"
#include "chrome/browser/chromeos/login/users/user_manager.h"
#include "chrome/browser/chromeos/ownership/owner_settings_service_factory.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "crypto/nss_util_internal.h"
#include "crypto/scoped_nss_types.h"

using content::BrowserThread;

namespace chromeos {

OwnerSettingsService::OwnerSettingsService(Profile* profile)
    : profile_(profile), weak_factory_(this) {
  registrar_.Add(this,
                 chrome::NOTIFICATION_PROFILE_CREATED,
                 content::Source<Profile>(profile_));
}

OwnerSettingsService::~OwnerSettingsService() {
}

void OwnerSettingsService::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  if (type != chrome::NOTIFICATION_PROFILE_CREATED) {
    NOTREACHED();
    return;
  }

  Profile* profile = content::Source<Profile>(source).ptr();
  if (profile != profile_) {
    NOTREACHED();
    return;
  }

  ReloadOwnerKey();
}

void OwnerSettingsService::ReloadOwnerKey() {
  if (!UserManager::IsInitialized())
    return;
  const User* user = UserManager::Get()->GetUserByProfile(profile_);
  if (!user || !user->is_profile_created())
    return;
  std::string user_id = user->email();
  if (user_id != OwnerSettingsServiceFactory::GetInstance()->GetUsername())
    return;
  BrowserThread::PostTaskAndReplyWithResult(
      BrowserThread::IO,
      FROM_HERE,
      base::Bind(&crypto::GetPublicSlotForChromeOSUser, user->username_hash()),
      base::Bind(&DeviceSettingsService::InitOwner,
                 base::Unretained(DeviceSettingsService::Get()),
                 user_id));
}

}  // namespace chromeos