• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DOWNLOAD_SERVICE_ABILITY_H
17 #define DOWNLOAD_SERVICE_ABILITY_H
18 
19 #include <mutex>
20 #include <stdint.h>
21 #include <iosfwd>
22 #include <functional>
23 #include <mutex>
24 #include <map>
25 #include <string>
26 #include <vector>
27 #include <memory>
28 #include "nocopyable.h"
29 #include "refbase.h"
30 #include "event_handler.h"
31 #include "system_ability.h"
32 #include "download_info.h"
33 #include "download_config.h"
34 #include "download_notify_interface.h"
35 #include "download_service_stub.h"
36 
37 
38 namespace OHOS::Request::Download {
39 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
40 class IKeyguardStateCallback;
41 
42 class DownloadServiceAbility : public SystemAbility, public DownloadServiceStub {
43     DECLARE_SYSTEM_ABILITY(DownloadServiceAbility);
44 
45 public:
46     DISALLOW_COPY_AND_MOVE(DownloadServiceAbility);
47     DownloadServiceAbility(int32_t systemAbilityId, bool runOnCreate);
48     DownloadServiceAbility();
49     ~DownloadServiceAbility();
50     static sptr<DownloadServiceAbility> GetInstance();
51 
52     int32_t Request(const DownloadConfig &config, ExceptionError &err) override;
53     bool Pause(uint32_t taskId) override;
54     bool Query(uint32_t taskId, DownloadInfo &info) override;
55     bool QueryMimeType(uint32_t taskId, std::string &mimeType) override;
56     bool Remove(uint32_t taskId) override;
57     bool Resume(uint32_t taskId) override;
58 
59     bool On(uint32_t taskId, const std::string &type, const sptr<DownloadNotifyInterface> &listener) override;
60     bool Off(uint32_t taskId, const std::string &type) override;
61 
62     bool CheckPermission() override;
63 
64     int Dump(int fd, const std::vector<std::u16string> &args) override;
65 
66     static void NotifyHandler(const std::string& type, uint32_t taskId, int64_t argv1, int64_t argv2, bool isNotify);
67 
68 protected:
69     void OnDump() override;
70     void OnStart() override;
71     void OnStop() override;
72 
73 private:
74     int32_t Init();
75     void InitServiceHandler();
76     void ManualStart();
77     void AddUnregisteredNotify(uint32_t taskId, const std::string &type);
78     bool DoUnregisteredNotify(uint32_t taskId, const std::string &type);
79 private:
80     ServiceRunningState state_;
81     static std::mutex instanceLock_;
82     static sptr<DownloadServiceAbility> instance_;
83     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
84     std::map<std::string, sptr<DownloadNotifyInterface>> registeredListeners_;
85     std::vector<sptr<DownloadNotifyInterface>> unlockVecListeners_;
86     std::mutex listenerMapMutex_;
87     std::mutex lock_;
88     const int32_t startTime_ = 1900;
89     const int32_t extraMonth_ = 1;
90     std::mutex unregisteredNotifyMutex_;
91     std::map<std::string, uint32_t> unregisteredNotify_;
92 };
93 } // namespace OHOS::Request::Download
94 #endif // DOWNLOAD_SYSTEM_ABILITY_H
95