Chromium Code Reviews| Index: chrome/browser/policy/device_policy_controller.cc |
| diff --git a/chrome/browser/policy/device_policy_controller.cc b/chrome/browser/policy/device_policy_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..75ddb3ef66b8574e89a122bf31ef43a8b731d751 |
| --- /dev/null |
| +++ b/chrome/browser/policy/device_policy_controller.cc |
| @@ -0,0 +1,107 @@ |
| +// Copyright (c) 2011 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/policy/device_policy_controller.h" |
| + |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/chromeos/login/ownership_service.h" |
| +#include "chrome/browser/chromeos/login/user_manager.h" |
| +#include "chrome/browser/net/gaia/token_service.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/common/net/gaia/gaia_constants.h" |
| +#include "chrome/common/notification_service.h" |
| +#include "chrome/common/notification_type.h" |
| + |
| +namespace policy { |
| + |
| +DevicePolicyController::DevicePolicyController() |
| + : should_register_(false) { |
| + registrar_.Add(this, |
| + NotificationType::TOKEN_AVAILABLE, |
| + NotificationService::AllSources()); |
| + registrar_.Add(this, |
| + NotificationType::LOGIN_USER_CHANGED, |
| + NotificationService::AllSources()); |
| + registrar_.Add(this, |
| + NotificationType::OWNERSHIP_TAKEN, |
| + NotificationService::AllSources()); |
| + |
| + // TODO(mnissler): Figure out how to read the machine id. |
| + machine_id_ = "dummy-cros-machine-ID"; |
| +} |
| + |
| +std::string DevicePolicyController::GetDeviceToken() { |
| + return device_token_; |
| +} |
| + |
| +std::string DevicePolicyController::GetDeviceID() { |
| + return machine_id_; |
| +} |
| + |
| +bool DevicePolicyController::GetCredentials(std::string* username, |
| + std::string* auth_token) { |
| + // Only register if requested. |
| + if (!should_register_) |
| + return false; |
| + |
| + // Need to know the machine id. |
| + if (machine_id_.empty()) |
| + return false; |
| + |
| + // 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.
|
| + if (!chromeos::OwnershipService::GetSharedInstance()->CurrentUserIsOwner()) |
| + return false; |
| + |
| + // We need to know about the profile of the logged in user. |
| + Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile(); |
| + if (!profile) { |
| + NOTREACHED() << "Current user profile inaccessible"; |
| + return false; |
| + } |
| + |
| + *username = chromeos::UserManager::Get()->logged_in_user().email(); |
| + *auth_token = profile->GetTokenService()->GetTokenForService( |
| + GaiaConstants::kDeviceManagementService); |
| + |
| + return !username->empty() && !auth_token->empty(); |
| +} |
| + |
| +void DevicePolicyController::OnTokenAvailable(const std::string& token) { |
| + DCHECK(!machine_id_.empty()); |
| + |
| + // Reset registration flag, so we only attempt registration once. |
| + should_register_ = false; |
| + |
| + device_token_ = token; |
| + NotifyTokenChanged(); |
| +} |
| + |
| +void DevicePolicyController::CheckAndTriggerFetch() { |
| + std::string username; |
| + std::string auth_token; |
| + if (GetCredentials(&username, &auth_token)) |
| + NotifyAuthChanged(); |
| +} |
| + |
| +void DevicePolicyController::Observe(NotificationType type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) { |
| + if (type == NotificationType::TOKEN_AVAILABLE) { |
| + const TokenService::TokenAvailableDetails* token_details = |
| + Details<const TokenService::TokenAvailableDetails>(details).ptr(); |
| + if (token_details->service() == GaiaConstants::kDeviceManagementService) |
| + CheckAndTriggerFetch(); |
| + } else if (type == NotificationType::LOGIN_USER_CHANGED) { |
| + should_register_ = false; |
| + CheckAndTriggerFetch(); |
| + } else if (type == NotificationType::OWNERSHIP_TAKEN) { |
| + should_register_ = true; |
| + CheckAndTriggerFetch(); |
| + } else { |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +} // namespace policy |