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 "nlohmann/json.hpp"
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              instance = new RemoteCommandFactory();
34          }
35      }
36      return *instance;
37  }
38  
NewSyncRemoteHapTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,AccessTokenID tokenID)39  std::shared_ptr<SyncRemoteHapTokenCommand> RemoteCommandFactory::NewSyncRemoteHapTokenCommand(
40      const std::string &srcDeviceId, const std::string &dstDeviceId, AccessTokenID tokenID)
41  {
42      return std::make_shared<SyncRemoteHapTokenCommand>(srcDeviceId, dstDeviceId, tokenID);
43  }
44  
NewDeleteRemoteTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,AccessTokenID tokenID)45  std::shared_ptr<DeleteRemoteTokenCommand> RemoteCommandFactory::NewDeleteRemoteTokenCommand(
46      const std::string &srcDeviceId, const std::string &dstDeviceId, AccessTokenID tokenID)
47  {
48      return std::make_shared<DeleteRemoteTokenCommand>(srcDeviceId, dstDeviceId, tokenID);
49  }
50  
NewUpdateRemoteHapTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,const HapTokenInfoForSync & tokenInfo)51  std::shared_ptr<UpdateRemoteHapTokenCommand> RemoteCommandFactory::NewUpdateRemoteHapTokenCommand(
52      const std::string &srcDeviceId, const std::string &dstDeviceId, const HapTokenInfoForSync& tokenInfo)
53  {
54      return std::make_shared<UpdateRemoteHapTokenCommand>(srcDeviceId, dstDeviceId, tokenInfo);
55  }
56  
NewRemoteCommandFromJson(const std::string & commandName,const std::string & commandJsonString)57  std::shared_ptr<BaseRemoteCommand> RemoteCommandFactory::NewRemoteCommandFromJson(
58      const std::string &commandName, const std::string &commandJsonString)
59  {
60      const std::string SYNC_HAP_COMMAND_NAME = "SyncRemoteHapTokenCommand";
61      const std::string DELETE_TOKEN_COMMAND_NAME = "DeleteRemoteTokenCommand";
62      const std::string UPDATE_HAP_COMMAND_NAME = "UpdateRemoteHapTokenCommand";
63  
64      if (commandName == SYNC_HAP_COMMAND_NAME) {
65          return std::make_shared<SyncRemoteHapTokenCommand>(commandJsonString);
66      }
67      if (commandName == DELETE_TOKEN_COMMAND_NAME) {
68          return std::make_shared<DeleteRemoteTokenCommand>(commandJsonString);
69      }
70      if (commandName == UPDATE_HAP_COMMAND_NAME) {
71          return std::make_shared<UpdateRemoteHapTokenCommand>(commandJsonString);
72      }
73      return nullptr;
74  }
75  }  // namespace AccessToken
76  }  // namespace Security
77  }  // namespace OHOS
78