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/chromeos/login/ownership_status_checker.h"
6
7 #include "content/browser/browser_thread.h"
8
9 namespace chromeos {
10
OwnershipStatusChecker(Callback * callback)11 OwnershipStatusChecker::OwnershipStatusChecker(Callback* callback)
12 : core_(new Core(callback)) {
13 core_->Check();
14 }
15
~OwnershipStatusChecker()16 OwnershipStatusChecker::~OwnershipStatusChecker() {
17 core_->Cancel();
18 }
19
Core(Callback * callback)20 OwnershipStatusChecker::Core::Core(Callback* callback)
21 : callback_(callback),
22 origin_loop_(base::MessageLoopProxy::CreateForCurrentThread()) {}
23
Check()24 void OwnershipStatusChecker::Core::Check() {
25 DCHECK(origin_loop_->BelongsToCurrentThread());
26 OwnershipService::Status status =
27 OwnershipService::GetSharedInstance()->GetStatus(false);
28 if (status != OwnershipService::OWNERSHIP_UNKNOWN) {
29 // Take a spin on the message loop in order to avoid reentrancy in callers.
30 origin_loop_->PostTask(
31 FROM_HERE,
32 NewRunnableMethod(this,
33 &OwnershipStatusChecker::Core::ReportResult,
34 status));
35 } else {
36 // Switch to the file thread to make the blocking call.
37 BrowserThread::PostTask(
38 BrowserThread::FILE, FROM_HERE,
39 NewRunnableMethod(this,
40 &OwnershipStatusChecker::Core::CheckOnFileThread));
41 }
42 }
43
Cancel()44 void OwnershipStatusChecker::Core::Cancel() {
45 DCHECK(origin_loop_->BelongsToCurrentThread());
46 callback_.reset();
47 }
48
CheckOnFileThread()49 void OwnershipStatusChecker::Core::CheckOnFileThread() {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
51 OwnershipService::Status status =
52 OwnershipService::GetSharedInstance()->GetStatus(true);
53 origin_loop_->PostTask(
54 FROM_HERE,
55 NewRunnableMethod(this,
56 &OwnershipStatusChecker::Core::ReportResult,
57 status));
58 }
59
ReportResult(OwnershipService::Status status)60 void OwnershipStatusChecker::Core::ReportResult(
61 OwnershipService::Status status) {
62 DCHECK(origin_loop_->BelongsToCurrentThread());
63 if (callback_.get()) {
64 callback_->Run(status);
65 callback_.reset();
66 }
67 }
68
69 } // namespace chromeos
70