Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 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/chromeos/policy/android_management_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/guid.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "components/policy/core/common/cloud/device_management_service.h" | |
| 12 #include "google_apis/gaia/gaia_constants.h" | |
| 13 #include "google_apis/gaia/google_service_auth_error.h" | |
| 14 #include "policy/proto/device_management_backend.pb.h" | |
| 15 | |
| 16 namespace em = enterprise_management; | |
| 17 | |
| 18 namespace { | |
| 19 const char* fake_access_token = nullptr; | |
| 20 } | |
| 21 | |
| 22 namespace policy { | |
| 23 | |
| 24 AndroidManagementClient::AndroidManagementClient( | |
| 25 DeviceManagementService* service, | |
| 26 scoped_refptr<net::URLRequestContextGetter> request_context, | |
| 27 const std::string& account_id, | |
| 28 OAuth2TokenService* token_service) | |
| 29 : OAuth2TokenService::Consumer("android_management_client"), | |
| 30 service_(service), | |
| 31 request_context_(request_context), | |
| 32 account_id_(account_id), | |
| 33 token_service_(token_service), | |
| 34 weak_ptr_factory_(this) {} | |
| 35 | |
| 36 AndroidManagementClient::~AndroidManagementClient() {} | |
| 37 | |
| 38 void AndroidManagementClient::StartCheckAndroidManagement( | |
| 39 const StatusCallback& callback) { | |
| 40 DCHECK(service_); | |
| 41 DCHECK(callback_.is_null()); | |
| 42 | |
| 43 callback_ = callback; | |
| 44 | |
| 45 if (fake_access_token) | |
| 46 CheckAndroidManagement(fake_access_token); | |
| 47 else | |
| 48 RequestAccessToken(); | |
| 49 } | |
| 50 | |
| 51 // static | |
| 52 void AndroidManagementClient::SetAccessTokenForTesting( | |
|
Thiemo Nagel
2016/04/28 13:26:59
What would you think of passing the access token t
Polina Bondarenko
2016/04/28 16:18:40
Removed fake access token at all. It's possible to
| |
| 53 const char* access_token) { | |
| 54 fake_access_token = access_token; | |
| 55 } | |
| 56 | |
| 57 void AndroidManagementClient::OnGetTokenSuccess( | |
| 58 const OAuth2TokenService::Request* request, | |
| 59 const std::string& access_token, | |
| 60 const base::Time& expiration_time) { | |
| 61 DCHECK_EQ(token_request_.get(), request); | |
| 62 token_request_.reset(); | |
| 63 | |
| 64 CheckAndroidManagement(access_token); | |
| 65 } | |
| 66 | |
| 67 void AndroidManagementClient::OnGetTokenFailure( | |
| 68 const OAuth2TokenService::Request* request, | |
| 69 const GoogleServiceAuthError& error) { | |
| 70 DCHECK_EQ(token_request_.get(), request); | |
| 71 token_request_.reset(); | |
| 72 LOG(ERROR) << "Token request failed: " << error.ToString(); | |
| 73 | |
| 74 callback_.Run(policy::AndroidManagementClient::Result::RESULT_ERROR); | |
|
Thiemo Nagel
2016/04/28 13:26:59
It should be possible to drop the namespace and cl
Polina Bondarenko
2016/04/28 16:18:40
Done.
| |
| 75 callback_.Reset(); | |
| 76 } | |
| 77 | |
| 78 void AndroidManagementClient::RequestAccessToken() { | |
| 79 DCHECK(!token_request_); | |
| 80 // The user must be signed in already. | |
| 81 DCHECK(token_service_->RefreshTokenIsAvailable(account_id_)); | |
| 82 | |
| 83 OAuth2TokenService::ScopeSet scopes; | |
| 84 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); | |
| 85 scopes.insert(GaiaConstants::kOAuthWrapBridgeUserInfoScope); | |
| 86 token_request_ = token_service_->StartRequest(account_id_, scopes, this); | |
| 87 } | |
| 88 | |
| 89 void AndroidManagementClient::CheckAndroidManagement( | |
| 90 const std::string& access_token) { | |
| 91 request_job_.reset(service_->CreateJob( | |
| 92 DeviceManagementRequestJob::TYPE_ANDROID_MANAGEMENT_CHECK, | |
| 93 request_context_.get())); | |
| 94 request_job_->SetOAuthToken(access_token); | |
| 95 request_job_->SetClientID(base::GenerateGUID()); | |
| 96 request_job_->GetRequest()->mutable_check_android_management_request(); | |
| 97 | |
| 98 request_job_->Start( | |
| 99 base::Bind(&AndroidManagementClient::OnAndroidManagementChecked, | |
| 100 weak_ptr_factory_.GetWeakPtr())); | |
| 101 } | |
| 102 | |
| 103 void AndroidManagementClient::OnAndroidManagementChecked( | |
| 104 DeviceManagementStatus status, | |
| 105 int net_error, | |
| 106 const em::DeviceManagementResponse& response) { | |
| 107 if (status == DM_STATUS_SUCCESS && | |
| 108 !response.has_check_android_management_response()) { | |
| 109 LOG(WARNING) << "Invalid check android management response."; | |
| 110 status = DM_STATUS_RESPONSE_DECODING_ERROR; | |
| 111 } | |
| 112 | |
| 113 Result result; | |
| 114 switch (status) { | |
| 115 case DM_STATUS_SUCCESS: | |
| 116 result = RESULT_UNMANAGED; | |
| 117 break; | |
| 118 case DM_STATUS_SERVICE_DEVICE_ID_CONFLICT: | |
| 119 result = RESULT_MANAGED; | |
| 120 break; | |
| 121 default: | |
| 122 result = RESULT_ERROR; | |
| 123 } | |
| 124 | |
| 125 callback_.Run(result); | |
| 126 callback_.Reset(); | |
| 127 request_job_.reset(); | |
|
Thiemo Nagel
2016/04/28 13:26:59
Nit: I'd reset the |request_job_| as early as poss
Polina Bondarenko
2016/04/28 16:18:40
Done.
| |
| 128 } | |
| 129 | |
| 130 } // namespace policy | |
| OLD | NEW |