File: arc_pai_starter.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (155 lines) | stat: -rw-r--r-- 4,509 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/app_list/arc/arc_pai_starter.h"

#include <algorithm>
#include <memory>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chrome/browser/ash/arc/arc_optin_uma.h"
#include "chrome/browser/ash/arc/policy/arc_policy_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "chromeos/ash/experiences/arc/session/arc_service_manager.h"
#include "components/prefs/pref_service.h"
#include "ui/events/event_constants.h"

// Enable VLOG level 1.
#undef ENABLED_VLOG_LEVEL
#define ENABLED_VLOG_LEVEL 1

namespace arc {

namespace {

constexpr base::TimeDelta kMinRetryTime = base::Minutes(10);
constexpr base::TimeDelta kMaxRetryTime = base::Minutes(60);

}  // namespace

ArcPaiStarter::ArcPaiStarter(Profile* profile)
    : profile_(profile),
      pref_service_(profile->GetPrefs()),
      retry_interval_(kMinRetryTime) {
  ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_);
  // Prefs may not available in some unit tests.
  if (!prefs)
    return;
  prefs->AddObserver(this);
  MaybeStartPai();
}

ArcPaiStarter::~ArcPaiStarter() {
  ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_);
  if (!prefs)
    return;
  prefs->RemoveObserver(this);
}

// static
std::unique_ptr<ArcPaiStarter> ArcPaiStarter::CreateIfNeeded(Profile* profile) {
  if (profile->GetPrefs()->GetBoolean(prefs::kArcPaiStarted))
    return nullptr;

  // No PAI is expected for managed user.
  if (arc::policy_util::IsAccountManaged(profile))
    return nullptr;

  return std::make_unique<ArcPaiStarter>(profile);
}

void ArcPaiStarter::AddOnStartCallback(base::OnceClosure callback) {
  if (started_) {
    std::move(callback).Run();
    return;
  }

  onstart_callbacks_.push_back(std::move(callback));
}

void ArcPaiStarter::TriggerRetryForTesting() {
  retry_timer_.FireNow();
}

void ArcPaiStarter::MaybeStartPai() {
  // Clear retry timer. It is only used to call |MaybeStartPai| in case of PAI
  // flow failed and no condition is changed to trigger |MaybeStartPai|.
  retry_timer_.Stop();

  if (started_ || pending_ || IsArcPlayAutoInstallDisabled()) {
    return;
  }

  ArcAppListPrefs* const prefs = ArcAppListPrefs::Get(profile_);
  DCHECK(prefs);

  std::unique_ptr<ArcAppListPrefs::AppInfo> app_info =
      prefs->GetApp(kPlayStoreAppId);
  if (!app_info || !app_info->ready)
    return;

  arc::mojom::AppInstance* app_instance = ARC_GET_INSTANCE_FOR_METHOD(
      arc::ArcServiceManager::Get()->arc_bridge_service()->app(), StartPaiFlow);

  VLOG(1) << "Start PAI flow";
  pending_ = true;
  request_start_time_ = base::Time::Now();
  app_instance->StartPaiFlow(base::BindOnce(&ArcPaiStarter::OnPaiRequested,
                                            weak_ptr_factory_.GetWeakPtr()));
}

void ArcPaiStarter::OnPaiDone() {
  DCHECK(!pending_);
  ArcAppListPrefs* const prefs = ArcAppListPrefs::Get(profile_);
  DCHECK(prefs);

  started_ = true;
  pref_service_->SetBoolean(prefs::kArcPaiStarted, true);

  prefs->RemoveObserver(this);

  for (auto& callback : onstart_callbacks_)
    std::move(callback).Run();
  onstart_callbacks_.clear();
}

void ArcPaiStarter::OnPaiRequested(mojom::PaiFlowState state) {
  DCHECK(pending_);
  pending_ = false;
  VLOG(1) << "PAI flow state " << state;

  UpdatePlayAutoInstallRequestState(state, profile_);

  if (state != mojom::PaiFlowState::SUCCEEDED) {
    retry_timer_.Start(
        FROM_HERE, retry_interval_,
        base::BindOnce(&ArcPaiStarter::MaybeStartPai, base::Unretained(this)));
    retry_interval_ = std::min(retry_interval_ * 2, kMaxRetryTime);
    return;
  }

  arc::UpdatePlayAutoInstallRequestTime(base::Time::Now() - request_start_time_,
                                        profile_);
  OnPaiDone();
}

void ArcPaiStarter::OnAppRegistered(const std::string& app_id,
                                    const ArcAppListPrefs::AppInfo& app_info) {
  OnAppStatesChanged(app_id, app_info);
}

void ArcPaiStarter::OnAppStatesChanged(
    const std::string& app_id,
    const ArcAppListPrefs::AppInfo& app_info) {
  if (app_id == kPlayStoreAppId && app_info.ready)
    MaybeStartPai();
}

}  // namespace arc