1 /*
2  * Copyright (c) 2023 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 OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_H
17 #define OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_H
18 
19 #include <iostream>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 #include <set>
25 #include <map>
26 #include <unordered_set>
27 #include <refbase.h>
28 #include "iremote_stub.h"
29 #include "system_ability.h"
30 #include "drm_host_manager.h"
31 #include "drm_log.h"
32 #include "mediakeysystemfactory_service_stub.h"
33 #include "mediakeysystem_service.h"
34 
35 #include "v1_0/media_key_system_factory_proxy.h"
36 #include "v1_0/media_key_system_proxy.h"
37 
38 namespace OHOS {
39 namespace DrmStandard {
40 using namespace OHOS::HDI::Drm::V1_0;
41 using namespace OHOS::HDI;
42 class MediaKeySystemFactoryService : public SystemAbility,
43     public MediaKeySystemFactoryServiceStub,
44     public DrmHostManager::StatusCallback,
45     public IMediaKeySystemServiceOperatorsCallback {
46     DECLARE_SYSTEM_ABILITY(MediaKeySystemFactoryService);
47 
48 public:
49     DISALLOW_COPY_AND_MOVE(MediaKeySystemFactoryService);
50     explicit MediaKeySystemFactoryService(int32_t systemAbilityId, bool runOnCreate = true);
51     ~MediaKeySystemFactoryService() override;
52     void OnDump() override;
53     void OnStart() override;
54     void OnStop() override;
55     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
56     int32_t IsMediaKeySystemSupported(std::string &name, bool *isSurpported) override;
57     int32_t IsMediaKeySystemSupported(std::string &name, std::string &mimeType, bool *isSurpported) override;
58     int32_t IsMediaKeySystemSupported(std::string &name, std::string &mimeType, int32_t securityLevel,
59         bool *isSurpported) override;
60     int32_t CreateMediaKeySystem(std::string &name, sptr<IMediaKeySystemService> &mediaKeySystemProxy) override;
61     int32_t CloseMediaKeySystemService(sptr<MediaKeySystemService> mediaKeySystemService) override;
62     void DistroyForClientDied(pid_t pid) override;
63     int32_t GetMediaKeySystems(std::map<std::string, std::string> &mediaKeySystemNames) override;
64     int32_t GetMediaKeySystemUuid(std::string &name, std::string &uuid) override;
65     void OnDrmPluginDied(std::string &name) override;
66 
67 private:
68     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
69     void InitStatisticsInfo(const sptr<IMediaKeySystem> &hdiMediaKeySystem,
70         std::string pluginName, StatisticsInfo &statisticsInfo);
71     int32_t WriteDumpInfo(int32_t fd, std::string &dumpString);
72     std::recursive_mutex mutex_;
73     sptr<DrmHostManager> drmHostManager_;
74     std::map<int32_t, std::set<sptr<MediaKeySystemService>>> mediaKeySystemForPid_;
75     std::map<std::string, int32_t> currentMediaKeySystemNum_;
76 };
77 } // DrmStandard
78 } // OHOS
79 
80 #endif // OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_H