1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef UPDATE_ENGINE_BINDER_SERVICE_ANDROID_H_
18 #define UPDATE_ENGINE_BINDER_SERVICE_ANDROID_H_
19 
20 #include <stdint.h>
21 
22 #include <string>
23 #include <vector>
24 
25 #include <utils/Errors.h>
26 #include <utils/String16.h>
27 #include <utils/StrongPointer.h>
28 
29 #include "android/os/BnUpdateEngine.h"
30 #include "android/os/IUpdateEngineCallback.h"
31 #include "update_engine/service_delegate_android_interface.h"
32 #include "update_engine/service_observer_interface.h"
33 
34 namespace chromeos_update_engine {
35 
36 class BinderUpdateEngineAndroidService : public android::os::BnUpdateEngine,
37                                          public ServiceObserverInterface {
38  public:
39   explicit BinderUpdateEngineAndroidService(
40       ServiceDelegateAndroidInterface* service_delegate);
41   ~BinderUpdateEngineAndroidService() override = default;
42 
43   const char* ServiceName() const { return "android.os.UpdateEngineService"; }
44 
45   // ServiceObserverInterface overrides.
46   void SendStatusUpdate(
47       const update_engine::UpdateEngineStatus& update_engine_status) override;
48   void SendPayloadApplicationComplete(ErrorCode error_code) override;
49 
50   // android::os::BnUpdateEngine overrides.
51   android::binder::Status applyPayload(
52       const android::String16& url,
53       int64_t payload_offset,
54       int64_t payload_size,
55       const std::vector<android::String16>& header_kv_pairs) override;
56   android::binder::Status applyPayloadFd(
57       const ::android::os::ParcelFileDescriptor& pfd,
58       int64_t payload_offset,
59       int64_t payload_size,
60       const std::vector<android::String16>& header_kv_pairs) override;
61   android::binder::Status bind(
62       const android::sp<android::os::IUpdateEngineCallback>& callback,
63       bool* return_value) override;
64   android::binder::Status unbind(
65       const android::sp<android::os::IUpdateEngineCallback>& callback,
66       bool* return_value) override;
67   android::binder::Status suspend() override;
68   android::binder::Status resume() override;
69   android::binder::Status cancel() override;
70   android::binder::Status resetStatus() override;
71   android::binder::Status verifyPayloadApplicable(
72       const android::String16& metadata_filename, bool* return_value) override;
73   android::binder::Status allocateSpaceForPayload(
74       const android::String16& metadata_filename,
75       const std::vector<android::String16>& header_kv_pairs,
76       int64_t* return_value) override;
77   android::binder::Status cleanupSuccessfulUpdate(
78       const android::sp<android::os::IUpdateEngineCallback>& callback) override;
79 
80  private:
81   // Remove the passed |callback| from the list of registered callbacks. Called
82   // on unbind() or whenever the callback object is destroyed.
83   // Returns true on success.
84   bool UnbindCallback(const IBinder* callback);
85 
86   // List of currently bound callbacks.
87   std::vector<android::sp<android::os::IUpdateEngineCallback>> callbacks_;
88 
89   // Cached copy of the last status update sent. Used to send an initial
90   // notification when bind() is called from the client.
91   int last_status_{-1};
92   double last_progress_{0.0};
93 
94   ServiceDelegateAndroidInterface* service_delegate_;
95 };
96 
97 }  // namespace chromeos_update_engine
98 
99 #endif  // UPDATE_ENGINE_BINDER_SERVICE_ANDROID_H_
100