1 // Copyright 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/arc/optin/arc_terms_of_service_oobe_negotiator.h"
6 
7 #include "base/bind.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/chromeos/login/ui/login_display_host.h"
10 #include "chrome/browser/ui/webui/chromeos/login/arc_terms_of_service_screen_handler.h"
11 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
12 
13 namespace arc {
14 
15 namespace {
16 
17 chromeos::ArcTermsOfServiceScreenView* g_view_for_testing = nullptr;
18 
GetScreenView()19 chromeos::ArcTermsOfServiceScreenView* GetScreenView() {
20   // Inject testing instance.
21   if (g_view_for_testing)
22     return g_view_for_testing;
23 
24   chromeos::LoginDisplayHost* host = chromeos::LoginDisplayHost::default_host();
25   DCHECK(host);
26   DCHECK(host->GetOobeUI());
27   return host->GetOobeUI()->GetView<chromeos::ArcTermsOfServiceScreenHandler>();
28 }
29 
30 }  // namespace
31 
32 // static
SetArcTermsOfServiceScreenViewForTesting(chromeos::ArcTermsOfServiceScreenView * view)33 void ArcTermsOfServiceOobeNegotiator::SetArcTermsOfServiceScreenViewForTesting(
34     chromeos::ArcTermsOfServiceScreenView* view) {
35   g_view_for_testing = view;
36 }
37 
38 ArcTermsOfServiceOobeNegotiator::ArcTermsOfServiceOobeNegotiator() = default;
39 
~ArcTermsOfServiceOobeNegotiator()40 ArcTermsOfServiceOobeNegotiator::~ArcTermsOfServiceOobeNegotiator() {
41   // During tests shutdown screen_view_ might still be alive.
42   if (!screen_view_)
43     return;
44 
45   DCHECK(g_browser_process->IsShuttingDown());
46   // Handle test shutdown gracefully.
47   screen_view_->RemoveObserver(this);
48 }
49 
StartNegotiationImpl()50 void ArcTermsOfServiceOobeNegotiator::StartNegotiationImpl() {
51   DCHECK(!screen_view_);
52   screen_view_ = GetScreenView();
53   DCHECK(screen_view_);
54   screen_view_->AddObserver(this);
55 }
56 
HandleTermsAccepted(bool accepted)57 void ArcTermsOfServiceOobeNegotiator::HandleTermsAccepted(bool accepted) {
58   DCHECK(screen_view_);
59   screen_view_->RemoveObserver(this);
60   screen_view_ = nullptr;
61   ReportResult(accepted);
62 }
63 
OnAccept(bool)64 void ArcTermsOfServiceOobeNegotiator::OnAccept(bool /* review_arc_settings */) {
65   HandleTermsAccepted(true);
66 }
67 
OnViewDestroyed(chromeos::ArcTermsOfServiceScreenView * view)68 void ArcTermsOfServiceOobeNegotiator::OnViewDestroyed(
69     chromeos::ArcTermsOfServiceScreenView* view) {
70   DCHECK_EQ(view, screen_view_);
71   HandleTermsAccepted(false);
72 }
73 
74 }  // namespace arc
75