forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a reland for 1133315. A global function was mistakenly moved lower, and was not visible to calls that happened before it. Refactor BrowserDMTokenStorage for easier platform implementations. This CL refactors BrowserDMTokenStorage & BrowserDMTokenStorageWin to move code that is common to all platforms. After this CL, adding a new platform requires implementing the following methods: - virtual std::string InitClientId(); - virtual std::string InitEnrollmentToken(); - virtual std::string InitDMToken(); - virtual void SaveDMToken(const std::string& token); Note that this CL keeps the existing Windows implementation, it only refactors the code (no functional change). BUG=812641 TBR: pastarmovj Change-Id: I51d6b4efef6066d602a59c514e9db88e414e4337 Reviewed-on: https://chromium-review.googlesource.com/1138539 Reviewed-by: Georges Khalil <georgesak@chromium.org> Commit-Queue: Georges Khalil <georgesak@chromium.org> Cr-Commit-Position: refs/heads/master@{#575356}
- Loading branch information
Showing
11 changed files
with
420 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright 2018 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/browser_dm_token_storage.h" | ||
|
||
#include <memory> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "base/base64.h" | ||
#include "base/bind.h" | ||
#include "base/bind_helpers.h" | ||
#include "base/callback.h" | ||
#include "base/logging.h" | ||
#include "base/no_destructor.h" | ||
#include "base/strings/string16.h" | ||
#include "base/strings/utf_string_conversions.h" | ||
#include "base/task_runner_util.h" | ||
#include "base/task_scheduler/post_task.h" | ||
#include "base/threading/sequenced_task_runner_handle.h" | ||
#include "base/threading/thread_task_runner_handle.h" | ||
#include "build/build_config.h" | ||
#include "content/public/browser/browser_thread.h" | ||
|
||
namespace policy { | ||
|
||
// static | ||
BrowserDMTokenStorage* BrowserDMTokenStorage::storage_for_testing_ = nullptr; | ||
|
||
// Static function that can't be overridden. Implementation is only compiled for | ||
// non-supported platforms. | ||
#if !defined(OS_WIN) | ||
// static | ||
BrowserDMTokenStorage* BrowserDMTokenStorage::Get() { | ||
if (storage_for_testing_) | ||
return storage_for_testing_; | ||
|
||
static base::NoDestructor<BrowserDMTokenStorage> storage; | ||
return storage.get(); | ||
} | ||
#endif | ||
|
||
BrowserDMTokenStorage::BrowserDMTokenStorage() : is_initialized_(false) { | ||
DETACH_FROM_SEQUENCE(sequence_checker_); | ||
|
||
// We don't call InitIfNeeded() here so that the global instance can be | ||
// created early during startup if needed. The tokens and client ID are read | ||
// from the system as part of the first retrieve or store operation. | ||
} | ||
|
||
BrowserDMTokenStorage::~BrowserDMTokenStorage() { | ||
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | ||
} | ||
|
||
std::string BrowserDMTokenStorage::RetrieveClientId() { | ||
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | ||
|
||
InitIfNeeded(); | ||
return client_id_; | ||
} | ||
|
||
std::string BrowserDMTokenStorage::RetrieveEnrollmentToken() { | ||
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | ||
|
||
InitIfNeeded(); | ||
return enrollment_token_; | ||
} | ||
|
||
void BrowserDMTokenStorage::StoreDMToken(const std::string& dm_token, | ||
StoreCallback callback) { | ||
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | ||
DCHECK(!store_callback_); | ||
InitIfNeeded(); | ||
|
||
dm_token_ = dm_token; | ||
|
||
store_callback_ = std::move(callback); | ||
|
||
SaveDMToken(dm_token); | ||
} | ||
|
||
std::string BrowserDMTokenStorage::RetrieveDMToken() { | ||
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | ||
DCHECK(!store_callback_); | ||
|
||
InitIfNeeded(); | ||
return dm_token_; | ||
} | ||
|
||
void BrowserDMTokenStorage::InitIfNeeded() { | ||
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | ||
|
||
if (is_initialized_) | ||
return; | ||
|
||
is_initialized_ = true; | ||
client_id_ = InitClientId(); | ||
DVLOG(1) << "Client ID = " << client_id_; | ||
if (client_id_.empty()) | ||
return; | ||
|
||
enrollment_token_ = InitEnrollmentToken(); | ||
DVLOG(1) << "Enrollment token = " << enrollment_token_; | ||
|
||
dm_token_ = InitDMToken(); | ||
DVLOG(1) << "DM Token = " << dm_token_; | ||
} | ||
|
||
void BrowserDMTokenStorage::OnDMTokenStored(bool success) { | ||
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | ||
DCHECK(store_callback_); | ||
|
||
if (!store_callback_.is_null()) | ||
std::move(store_callback_).Run(success); | ||
} | ||
|
||
// Stub implementation. This function will become virtual pure once Mac & Linux | ||
// implementations are done. | ||
std::string BrowserDMTokenStorage::InitClientId() { | ||
return std::string(); | ||
} | ||
|
||
// Stub implementation. This function will become virtual pure once Mac & Linux | ||
// implementations are done. | ||
std::string BrowserDMTokenStorage::InitEnrollmentToken() { | ||
return std::string(); | ||
} | ||
|
||
// Stub implementation. This function will become virtual pure once Mac & Linux | ||
// implementations are done. | ||
std::string BrowserDMTokenStorage::InitDMToken() { | ||
return std::string(); | ||
} | ||
|
||
// Stub implementation. This function will become virtual pure once Mac & Linux | ||
// implementations are done. | ||
void BrowserDMTokenStorage::SaveDMToken(const std::string& token) { | ||
base::ThreadTaskRunnerHandle::Get()->PostTask( | ||
FROM_HERE, base::BindOnce(std::move(store_callback_), false)); | ||
} | ||
|
||
} // namespace policy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.