1 // Copyright 2018 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_BROWSER_CHROMEOS_POLICY_ARC_APP_INSTALL_EVENT_LOG_UPLOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_ARC_APP_INSTALL_EVENT_LOG_UPLOADER_H_
7 
8 #include "base/callback.h"
9 #include "base/memory/weak_ptr.h"
10 #include "chrome/browser/chromeos/policy/install_event_log_uploader_base.h"
11 #include "components/policy/core/common/cloud/cloud_policy_client.h"
12 
13 namespace enterprise_management {
14 class AppInstallReportRequest;
15 }
16 
17 class Profile;
18 
19 namespace policy {
20 
21 // Adapter between the system that captures and stores ARC++ app push-install
22 // event logs and the policy system which uploads them to the management server.
23 class ArcAppInstallEventLogUploader : public InstallEventLogUploaderBase {
24  public:
25   // The delegate that event logs will be retrieved from.
26   class Delegate {
27    public:
28     // Callback invoked by the delegate with the ARC++ logs to be uploaded in
29     // |report|.
30     using SerializationCallback = base::OnceCallback<void(
31         const enterprise_management::AppInstallReportRequest* report)>;
32 
33     // Requests that the delegate serialize the current logs into a protobuf
34     // and pass it to |callback|.
35     virtual void SerializeForUpload(SerializationCallback callback) = 0;
36 
37     // Notification to the delegate that the logs passed via the most recent
38     // |SerializationCallback| have been successfully uploaded to the server and
39     // can be pruned from storage.
40     virtual void OnUploadSuccess() = 0;
41 
42    protected:
43     virtual ~Delegate();
44   };
45 
46   // |client| must outlive |this|.
47   ArcAppInstallEventLogUploader(CloudPolicyClient* client, Profile* profile);
48   ~ArcAppInstallEventLogUploader() override;
49 
50   // Sets the delegate. The delegate must either outlive |this| or be explicitly
51   // removed by calling |SetDelegate(nullptr)|. Removing the delegate cancels
52   // the pending log upload, if any.
53   void SetDelegate(Delegate* delegate);
54 
55  private:
56   // InstallEventLogUploaderBase:
57   void CheckDelegateSet() override;
58   void PostTaskForStartSerialization() override;
59   void CancelClientUpload() override;
60   void OnUploadSuccess() override;
61   void StartSerialization() override;
62 
63   // Callback invoked by the delegate with the app logs to be uploaded in
64   // |report|. Forwards the logs to the client for upload.
65   void OnSerialized(
66       const enterprise_management::AppInstallReportRequest* report);
67 
68   // The delegate that provides serialized logs to be uploaded.
69   Delegate* delegate_ = nullptr;
70 
71   // Weak pointer factory for invalidating callbacks passed to the delegate and
72   // scheduled retries when the upload request is canceled or |this| is
73   // destroyed.
74   base::WeakPtrFactory<ArcAppInstallEventLogUploader> weak_factory_{this};
75 };
76 
77 }  // namespace policy
78 
79 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_ARC_APP_INSTALL_EVENT_LOG_UPLOADER_H_
80