OLD | NEW |
---|---|
(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_controller.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 DevicePolicyController::DevicePolicyController() | |
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 | |
31 // TODO(mnissler): Figure out how to read the machine id. | |
32 machine_id_ = "dummy-cros-machine-ID"; | |
33 } | |
34 | |
35 std::string DevicePolicyController::GetDeviceToken() { | |
36 return device_token_; | |
37 } | |
38 | |
39 std::string DevicePolicyController::GetDeviceID() { | |
40 return machine_id_; | |
41 } | |
42 | |
43 bool DevicePolicyController::GetCredentials(std::string* username, | |
44 std::string* auth_token) { | |
45 // Only register if requested. | |
46 if (!should_register_) | |
47 return false; | |
48 | |
49 // Need to know the machine id. | |
50 if (machine_id_.empty()) | |
51 return false; | |
52 | |
53 // Only fetch policy when the owner is logged in. | |
Jakob Kummerow
2011/02/07 16:00:09
s/policy/token/ ? Or am I missing something here?
Jakob Kummerow
2011/02/14 13:50:34
Done.
| |
54 if (!chromeos::OwnershipService::GetSharedInstance()->CurrentUserIsOwner()) | |
55 return false; | |
56 | |
57 // We need to know about the profile of the logged in user. | |
58 Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile(); | |
59 if (!profile) { | |
60 NOTREACHED() << "Current user profile inaccessible"; | |
61 return false; | |
62 } | |
63 | |
64 *username = chromeos::UserManager::Get()->logged_in_user().email(); | |
65 *auth_token = profile->GetTokenService()->GetTokenForService( | |
66 GaiaConstants::kDeviceManagementService); | |
67 | |
68 return !username->empty() && !auth_token->empty(); | |
69 } | |
70 | |
71 void DevicePolicyController::OnTokenAvailable(const std::string& token) { | |
72 DCHECK(!machine_id_.empty()); | |
73 | |
74 // Reset registration flag, so we only attempt registration once. | |
75 should_register_ = false; | |
76 | |
77 device_token_ = token; | |
78 NotifyTokenChanged(); | |
79 } | |
80 | |
81 void DevicePolicyController::CheckAndTriggerFetch() { | |
82 std::string username; | |
83 std::string auth_token; | |
84 if (GetCredentials(&username, &auth_token)) | |
85 NotifyAuthChanged(); | |
86 } | |
87 | |
88 void DevicePolicyController::Observe(NotificationType type, | |
89 const NotificationSource& source, | |
90 const NotificationDetails& details) { | |
91 if (type == NotificationType::TOKEN_AVAILABLE) { | |
92 const TokenService::TokenAvailableDetails* token_details = | |
93 Details<const TokenService::TokenAvailableDetails>(details).ptr(); | |
94 if (token_details->service() == GaiaConstants::kDeviceManagementService) | |
95 CheckAndTriggerFetch(); | |
96 } else if (type == NotificationType::LOGIN_USER_CHANGED) { | |
97 should_register_ = false; | |
98 CheckAndTriggerFetch(); | |
99 } else if (type == NotificationType::OWNERSHIP_TAKEN) { | |
100 should_register_ = true; | |
101 CheckAndTriggerFetch(); | |
102 } else { | |
103 NOTREACHED(); | |
104 } | |
105 } | |
106 | |
107 } // namespace policy | |
OLD | NEW |