1 // Copyright 2020 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 #ifndef CHROME_UPDATER_APP_APP_SERVER_H_
6 #define CHROME_UPDATER_APP_APP_SERVER_H_
7 
8 #include <memory>
9 
10 #include "base/bind.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "chrome/updater/app/app.h"
13 
14 namespace updater {
15 
16 class Configurator;
17 class ControlService;
18 class GlobalPrefs;
19 class LocalPrefs;
20 class UpdateService;
21 
22 // AppServer runs as the updater server process. Multiple servers of different
23 // application versions can be run side-by-side. Each such server is called a
24 // "candidate". Exactly one candidate is "active" at any given time. Only the
25 // active candidate is permitted to mutate shared system state such as the
26 // global prefs file or versions of managed applications.
27 class AppServer : public App {
28  public:
29   AppServer();
30 
31  protected:
32   ~AppServer() override;
33 
34   // Overrides of App.
35   void Uninitialize() override;
36 
37  private:
38   // Overrides of App.
39   void Initialize() final;
40   void FirstTaskRun() final;
41 
42   // Set up the server for normal active version functions using the provided
43   // services.
44   virtual void ActiveDuty(scoped_refptr<UpdateService> update_service,
45                           scoped_refptr<ControlService> control_service) = 0;
46 
47   // Set up all non-side-by-side RPC interfaces to point to this candidate
48   // server.
49   virtual bool SwapRPCInterfaces() = 0;
50 
51   // Uninstall this candidate version of the updater.
52   virtual void UninstallSelf() = 0;
53 
54   // As part of initialization, an AppServer must do a mode check to determine
55   // what mode of operation it should continue in. Possible modes include:
56   //  - Qualify: this candidate is not yet qualified or active.
57   //  - ActiveDuty: this candidate is the active candidate.
58   //  - UninstallSelf: this candidate is older than the  active candidate.
59   //  - Shutdown: none of the above.
60   // If this candidate is already qualified but not yet active, or the state of
61   // the system is consistent with an incomplete swap, ModeCheck may have the
62   // side effect of promoting this candidate to the active candidate.
63   base::OnceClosure ModeCheck();
64   void Qualify(std::unique_ptr<LocalPrefs> local_prefs);
65   bool SwapVersions(GlobalPrefs* global_prefs);
66 
67   base::OnceClosure first_task_;
68   scoped_refptr<Configurator> config_;
69 
70   // If true, this version of the updater should uninstall itself during
71   // shutdown.
72   bool uninstall_ = false;
73 };
74 
75 scoped_refptr<App> AppServerInstance();
76 
77 }  // namespace updater
78 
79 #endif  // CHROME_UPDATER_APP_APP_SERVER_H_
80