Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(722)

Side by Side Diff: chrome/browser/policy/device_policy_identity_strategy.cc

Issue 6520008: Device policy infrastructure (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/policy/device_policy_identity_strategy.h"
6
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/chromeos/login/ownership_service.h"
9 #include "chrome/browser/chromeos/login/user_manager.h"
10 #include "chrome/browser/net/gaia/token_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/common/net/gaia/gaia_constants.h"
14 #include "chrome/common/notification_service.h"
15 #include "chrome/common/notification_type.h"
16
17 namespace policy {
18
19 DevicePolicyIdentityStrategy::DevicePolicyIdentityStrategy()
20 : should_register_(false) {
21 registrar_.Add(this,
22 NotificationType::TOKEN_AVAILABLE,
23 NotificationService::AllSources());
24 registrar_.Add(this,
25 NotificationType::LOGIN_USER_CHANGED,
26 NotificationService::AllSources());
27 registrar_.Add(this,
28 NotificationType::OWNERSHIP_TAKEN,
29 NotificationService::AllSources());
30 registrar_.Add(this,
31 NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED,
32 NotificationService::AllSources());
33
34 // TODO(mnissler): Figure out how to read the machine id.
35 machine_id_ = "dummy-cros-machine-ID";
36 }
37
38 std::string DevicePolicyIdentityStrategy::GetDeviceToken() {
39 return device_token_;
40 }
41
42 std::string DevicePolicyIdentityStrategy::GetDeviceID() {
43 return machine_id_;
44 }
45
46 bool DevicePolicyIdentityStrategy::GetCredentials(std::string* username,
47 std::string* auth_token) {
48 // Only register if requested.
49 if (!should_register_)
50 return false;
51
52 // Need to know the machine id.
53 if (machine_id_.empty())
54 return false;
55
56 // Only fetch credentials (and, subsequently, token/policy) when the owner
57 // is logged in.
58 if (!chromeos::OwnershipService::GetSharedInstance()->CurrentUserIsOwner())
59 return false;
60
61 // We need to know about the profile of the logged in user.
62 Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile();
63 if (!profile) {
64 NOTREACHED() << "Current user profile inaccessible";
65 return false;
66 }
67
68 *username = chromeos::UserManager::Get()->logged_in_user().email();
69 *auth_token = profile->GetTokenService()->GetTokenForService(
70 GaiaConstants::kDeviceManagementService);
71
72 return !username->empty() && !auth_token->empty();
73 }
74
75 void DevicePolicyIdentityStrategy::OnDeviceTokenAvailable(
76 const std::string& token) {
77 DCHECK(!machine_id_.empty());
78
79 // Reset registration flag, so we only attempt registration once.
80 should_register_ = false;
81
82 device_token_ = token;
83 NotifyDeviceTokenChanged();
84 }
85
86 void DevicePolicyIdentityStrategy::CheckAndTriggerFetch() {
87 std::string username;
88 std::string auth_token;
89 if (GetCredentials(&username, &auth_token))
90 NotifyAuthChanged();
91 }
92
93 void DevicePolicyIdentityStrategy::Observe(NotificationType type,
94 const NotificationSource& source,
95 const NotificationDetails& details) {
96 if (type == NotificationType::TOKEN_AVAILABLE) {
97 const TokenService::TokenAvailableDetails* token_details =
98 Details<const TokenService::TokenAvailableDetails>(details).ptr();
99 if (token_details->service() == GaiaConstants::kDeviceManagementService)
100 CheckAndTriggerFetch();
101 } else if (type == NotificationType::LOGIN_USER_CHANGED) {
102 should_register_ = false;
103 CheckAndTriggerFetch();
104 } else if (type == NotificationType::OWNERSHIP_TAKEN) {
105 should_register_ = true;
106 CheckAndTriggerFetch();
107 } else if (type == NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED) {
108 CheckAndTriggerFetch();
109 } else {
110 NOTREACHED();
111 }
112 }
113
114 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698