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 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 DevicePolicyController::GetDeviceToken() { |
| 39 return device_token_; |
| 40 } |
| 41 |
| 42 std::string DevicePolicyController::GetDeviceID() { |
| 43 return machine_id_; |
| 44 } |
| 45 |
| 46 bool DevicePolicyController::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 policy when the owner is logged in. |
| 57 if (!chromeos::OwnershipService::GetSharedInstance()->CurrentUserIsOwner()) |
| 58 return false; |
| 59 |
| 60 // We need to know about the profile of the logged in user. |
| 61 Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile(); |
| 62 if (!profile) { |
| 63 NOTREACHED() << "Current user profile inaccessible"; |
| 64 return false; |
| 65 } |
| 66 |
| 67 *username = chromeos::UserManager::Get()->logged_in_user().email(); |
| 68 *auth_token = profile->GetTokenService()->GetTokenForService( |
| 69 GaiaConstants::kDeviceManagementService); |
| 70 |
| 71 return !username->empty() && !auth_token->empty(); |
| 72 } |
| 73 |
| 74 void DevicePolicyController::OnTokenAvailable(const std::string& token) { |
| 75 DCHECK(!machine_id_.empty()); |
| 76 |
| 77 // Reset registration flag, so we only attempt registration once. |
| 78 should_register_ = false; |
| 79 |
| 80 device_token_ = token; |
| 81 NotifyTokenChanged(); |
| 82 } |
| 83 |
| 84 void DevicePolicyController::CheckAndTriggerFetch() { |
| 85 std::string username; |
| 86 std::string auth_token; |
| 87 if (GetCredentials(&username, &auth_token)) |
| 88 NotifyAuthChanged(); |
| 89 } |
| 90 |
| 91 void DevicePolicyController::Observe(NotificationType type, |
| 92 const NotificationSource& source, |
| 93 const NotificationDetails& details) { |
| 94 if (type == NotificationType::TOKEN_AVAILABLE) { |
| 95 const TokenService::TokenAvailableDetails* token_details = |
| 96 Details<const TokenService::TokenAvailableDetails>(details).ptr(); |
| 97 if (token_details->service() == GaiaConstants::kDeviceManagementService) |
| 98 CheckAndTriggerFetch(); |
| 99 } else if (type == NotificationType::LOGIN_USER_CHANGED) { |
| 100 should_register_ = false; |
| 101 CheckAndTriggerFetch(); |
| 102 } else if (type == NotificationType::OWNERSHIP_TAKEN) { |
| 103 should_register_ = true; |
| 104 CheckAndTriggerFetch(); |
| 105 } else if (type == NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED) { |
| 106 CheckAndTriggerFetch(); |
| 107 } else { |
| 108 NOTREACHED(); |
| 109 } |
| 110 } |
| 111 |
| 112 } // namespace policy |
OLD | NEW |