• 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 #include "remote_command_factory.h"
17 
18 #include <mutex>
19 
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 std::recursive_mutex g_instanceMutex;
25 }
26 
GetInstance()27 RemoteCommandFactory &RemoteCommandFactory::GetInstance()
28 {
29     static RemoteCommandFactory* instance = nullptr;
30     if (instance == nullptr) {
31         std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
32         if (instance == nullptr) {
33             RemoteCommandFactory* tmp = new (std::nothrow) RemoteCommandFactory();
34             instance = std::move(tmp);
35         }
36     }
37     return *instance;
38 }
39 
NewSyncRemoteHapTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,AccessTokenID tokenID)40 std::shared_ptr<SyncRemoteHapTokenCommand> RemoteCommandFactory::NewSyncRemoteHapTokenCommand(
41     const std::string &srcDeviceId, const std::string &dstDeviceId, AccessTokenID tokenID)
42 {
43     return std::make_shared<SyncRemoteHapTokenCommand>(srcDeviceId, dstDeviceId, tokenID);
44 }
45 
NewDeleteRemoteTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,AccessTokenID tokenID)46 std::shared_ptr<DeleteRemoteTokenCommand> RemoteCommandFactory::NewDeleteRemoteTokenCommand(
47     const std::string &srcDeviceId, const std::string &dstDeviceId, AccessTokenID tokenID)
48 {
49     return std::make_shared<DeleteRemoteTokenCommand>(srcDeviceId, dstDeviceId, tokenID);
50 }
51 
NewUpdateRemoteHapTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,const HapTokenInfoForSync & tokenInfo)52 std::shared_ptr<UpdateRemoteHapTokenCommand> RemoteCommandFactory::NewUpdateRemoteHapTokenCommand(
53     const std::string &srcDeviceId, const std::string &dstDeviceId, const HapTokenInfoForSync& tokenInfo)
54 {
55     return std::make_shared<UpdateRemoteHapTokenCommand>(srcDeviceId, dstDeviceId, tokenInfo);
56 }
57 
NewRemoteCommandFromJson(const std::string & commandName,const std::string & commandJsonString)58 std::shared_ptr<BaseRemoteCommand> RemoteCommandFactory::NewRemoteCommandFromJson(
59     const std::string &commandName, const std::string &commandJsonString)
60 {
61     const std::string SYNC_HAP_COMMAND_NAME = "SyncRemoteHapTokenCommand";
62     const std::string DELETE_TOKEN_COMMAND_NAME = "DeleteRemoteTokenCommand";
63     const std::string UPDATE_HAP_COMMAND_NAME = "UpdateRemoteHapTokenCommand";
64 
65     if (commandName == SYNC_HAP_COMMAND_NAME) {
66         return std::make_shared<SyncRemoteHapTokenCommand>(commandJsonString);
67     }
68     if (commandName == DELETE_TOKEN_COMMAND_NAME) {
69         return std::make_shared<DeleteRemoteTokenCommand>(commandJsonString);
70     }
71     if (commandName == UPDATE_HAP_COMMAND_NAME) {
72         return std::make_shared<UpdateRemoteHapTokenCommand>(commandJsonString);
73     }
74     return nullptr;
75 }
76 }  // namespace AccessToken
77 }  // namespace Security
78 }  // namespace OHOS
79