剪贴板动态启停适配

Signed-off-by: zuotao <zuotao2@huawei.com>
This commit is contained in:
openharmony_ci 2023-04-06 11:19:05 +00:00 committed by zuotao
commit 4428e1d18d
12 changed files with 259 additions and 82 deletions

View File

@ -9,10 +9,11 @@
],
"services" : [{
"name" : "pasteboard_service",
"path" : ["/system/bin/sa_main", "/system/profile/pasteboard_service.xml"],
"path" : ["/system/bin/sa_main", "/system/profile/pasteboard_service.json"],
"uid" : "pasteboard",
"gid" : ["pasteboard", "shell", "dfs_share", "ddms"],
"caps" : [],
"ondemand" : true,
"jobs" : {
"on-start" : "services:pasteboard_service"
},

View File

@ -38,6 +38,7 @@ ohos_shared_library("pasteboard_client") {
"src/paste_data.cpp",
"src/paste_data_record.cpp",
"src/pasteboard_client.cpp",
"src/pasteboard_load_callback.cpp",
"src/pasteboard_observer.cpp",
]
configs = [ ":pasteboard_client_config" ]

View File

@ -73,7 +73,6 @@ public:
std::shared_ptr<OHOS::AAFwk::Want> GetPrimaryWant();
std::shared_ptr<PasteDataRecord> GetRecordAt(std::size_t index);
std::size_t GetRecordCount();
std::string GetTag();
bool RemoveRecordAt(std::size_t number);
bool ReplaceRecordAt(std::size_t number, std::shared_ptr<PasteDataRecord> record);
bool HasMimeType(const std::string &mimeType);
@ -92,6 +91,9 @@ public:
void SetRemote(bool isRemote);
bool IsRemote();
void SetTime(const std::string &time);
void SetTag(std::string &tag);
std::string GetTag();
void SetAdditions(AAFwk::WantParams &additions);
virtual bool Marshalling(Parcel &parcel) const override;
static PasteData *Unmarshalling(Parcel &parcel);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -18,7 +18,7 @@
#include <functional>
#include <singleton.h>
#include <condition_variable>
#include "i_pasteboard_service.h"
#include "paste_data.h"
#include "paste_data_record.h"
@ -202,8 +202,29 @@ public:
*/
void RemovePasteboardEventObserver(sptr<PasteboardObserver> callback);
/**
* OnRemoteSaDied
* @descrition
* @param object systemAbility proxy object
* @return void.
*/
void OnRemoteSaDied(const wptr<IRemoteObject> &object);
/**
* LoadSystemAbilitySuccess
* @descrition inherit SystemAbilityLoadCallbackStub override LoadSystemAbilitySuccess
* @param remoteObject systemAbility proxy object.
* @return void.
*/
void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
/**
* LoadSystemAbilityFail
* @descrition inherit SystemAbilityLoadCallbackStub override LoadSystemAbilityFail
* @return void.
*/
void LoadSystemAbilityFail();
private:
void ConnectService();
static void RetainUri(PasteData &pasteData);
@ -213,6 +234,9 @@ private:
sptr<IRemoteObject::DeathRecipient> deathRecipient_{ nullptr };
std::condition_variable proxyConVar_;
bool LoadPasteboardService();
class StaticDestoryMonitor {
public:
StaticDestoryMonitor() : destoryed_(false) {}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_PASTE_BOARD_LOAD_CALLBACK_H
#define OHOS_PASTE_BOARD_LOAD_CALLBACK_H
#include <cstdint>
#include <string>
#include "refbase.h"
#include "system_ability_load_callback_stub.h"
namespace OHOS {
namespace MiscServices {
class PasteboardLoadCallback : public SystemAbilityLoadCallbackStub {
public:
void OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
const sptr<IRemoteObject> &remoteObject) override;
void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
};
} // namespace MiscServices
} // namespace OHOS
#endif // OHOS_PASTE_BOARD_LOAD_CALLBACK_H

View File

@ -248,11 +248,6 @@ bool PasteData::HasMimeType(const std::string &mimeType)
return false;
}
std::string PasteData::GetTag()
{
return props_.tag;
}
std::vector<std::shared_ptr<PasteDataRecord>> PasteData::AllRecords() const
{
return this->records_;
@ -298,6 +293,19 @@ void PasteData::SetTime(const std::string &setTime)
props_.setTime = setTime;
}
void PasteData::SetTag(std::string &tag)
{
props_.tag = tag;
}
std::string PasteData::GetTag()
{
return props_.tag;
}
void PasteData::SetAdditions(AAFwk::WantParams &additions)
{
props_.additions = additions;
}
void PasteData::RefreshMimeProp()
{
std::vector<std::string> mimeTypes;

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -12,16 +12,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "pasteboard_client.h"
#include <if_system_ability_manager.h>
#include <ipc_skeleton.h>
#include <iservice_registry.h>
#include <chrono>
#include "hiview_adapter.h"
#include "hitrace_meter.h"
#include "pasteboard_client.h"
#include "pasteboard_error.h"
#include "pasteboard_load_callback.h"
#include "pasteboard_observer.h"
#include "string_ex.h"
#include "system_ability_definition.h"
@ -31,6 +32,7 @@ using namespace OHOS::Media;
namespace OHOS {
namespace MiscServices {
constexpr const int32_t HITRACE_GETPASTEDATA = 0;
constexpr int32_t LOADSA_TIMEOUT_MS = 10000;
sptr<IPasteboardService> PasteboardClient::pasteboardServiceProxy_;
PasteboardClient::StaticDestoryMonitor PasteboardClient::staticDestoryMonitor_;
std::mutex PasteboardClient::instanceLock_;
@ -131,9 +133,60 @@ std::shared_ptr<PasteData> PasteboardClient::CreateKvData(
return pasteData;
}
bool PasteboardClient::LoadPasteboardService()
{
std::unique_lock<std::mutex> lock(instanceLock_);
sptr<PasteboardLoadCallback> loadCallback = new PasteboardLoadCallback();
if (loadCallback == nullptr) {
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "loadCallback is nullptr.");
return false;
}
auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgrProxy == nullptr) {
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "Get SystemAbilityManager failed");
return false;
}
int32_t ret = samgrProxy->LoadSystemAbility(PASTEBOARD_SERVICE_ID, loadCallback);
if (ret != ERR_OK) {
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "Failed to load systemAbility");
return false;
}
auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(LOADSA_TIMEOUT_MS),
[this]() { return pasteboardServiceProxy_ != nullptr; });
if (!waitStatus) {
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "Load systemAbility timeout");
return false;
}
return true;
}
void PasteboardClient::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
{
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "PasteboardClient finish start systemAbility");
std::lock_guard<std::mutex> lock(instanceLock_);
if (deathRecipient_ == nullptr) {
deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new PasteboardSaDeathRecipient());
}
if (remoteObject != nullptr) {
remoteObject->AddDeathRecipient(deathRecipient_);
pasteboardServiceProxy_ = iface_cast<IPasteboardService>(remoteObject);
proxyConVar_.notify_one();
}
}
void PasteboardClient::LoadSystemAbilityFail()
{
std::lock_guard<std::mutex> lock(instanceLock_);
pasteboardServiceProxy_ = nullptr;
}
void PasteboardClient::Clear()
{
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "Clear start.");
if (pasteboardServiceProxy_ == nullptr) {
PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "Redo ConnectService");
ConnectService();
@ -144,14 +197,14 @@ void PasteboardClient::Clear()
return;
}
pasteboardServiceProxy_->Clear();
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "end.");
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "Clear end.");
return;
}
int32_t PasteboardClient::GetPasteData(PasteData &pasteData)
{
StartAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetPasteData", HITRACE_GETPASTEDATA);
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "GetPasteData start.");
if (pasteboardServiceProxy_ == nullptr) {
PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "Redo ConnectService");
ConnectService();
@ -165,9 +218,10 @@ int32_t PasteboardClient::GetPasteData(PasteData &pasteData)
RetainUri(pasteData);
FinishAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetPasteData", HITRACE_GETPASTEDATA);
HiViewAdapter::ReportUseBehaviour(pasteData, HiViewAdapter::PASTE_STATE, ret);
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "end.");
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "GetPasteData end.");
return ret;
}
void PasteboardClient::RetainUri(PasteData &pasteData)
{
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start");
@ -186,14 +240,14 @@ void PasteboardClient::RetainUri(PasteData &pasteData)
bool PasteboardClient::HasPasteData()
{
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "HasPasteData start.");
if (pasteboardServiceProxy_ == nullptr) {
PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "Redo ConnectService");
ConnectService();
}
if (pasteboardServiceProxy_ == nullptr) {
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "HasPasteData quit");
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "HasPasteData quit.");
return false;
}
return pasteboardServiceProxy_->HasPasteData();
@ -201,7 +255,7 @@ bool PasteboardClient::HasPasteData()
int32_t PasteboardClient::SetPasteData(PasteData &pasteData)
{
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "SetPasteData start.");
if (pasteboardServiceProxy_ == nullptr) {
PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "Redo ConnectService");
ConnectService();
@ -270,44 +324,43 @@ void PasteboardClient::RemovePasteboardEventObserver(sptr<PasteboardObserver> ca
void PasteboardClient::ConnectService()
{
std::lock_guard<std::mutex> lock(instanceLock_);
if (pasteboardServiceProxy_ != nullptr) {
return;
{
std::lock_guard<std::mutex> lock(instanceLock_);
if (pasteboardServiceProxy_ != nullptr) {
return;
}
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "ConnectService start.");
sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgrProxy == nullptr) {
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Get SystemAbilityManager failed.");
pasteboardServiceProxy_ = nullptr;
return;
}
sptr<IRemoteObject> remoteObject = samgrProxy->CheckSystemAbility(PASTEBOARD_SERVICE_ID);
if (remoteObject != nullptr) {
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Get PasteboardServiceProxy succeed.");
if (deathRecipient_ == nullptr) {
deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new PasteboardSaDeathRecipient());
}
remoteObject->AddDeathRecipient(deathRecipient_);
pasteboardServiceProxy_ = iface_cast<IPasteboardService>(remoteObject);
return;
}
}
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (sam == nullptr) {
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Getting SystemAbilityManager failed.");
pasteboardServiceProxy_ = nullptr;
return;
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "remoteObject is null.");
if (LoadPasteboardService()) {
std::lock_guard<std::mutex> lock(instanceLock_);
if (pasteboardServiceProxy_ == nullptr) {
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Get PasteboardServiceProxy failed.");
return;
}
}
sptr<IRemoteObject> remoteObject = sam->CheckSystemAbility(PASTEBOARD_SERVICE_ID);
if (remoteObject == nullptr) {
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "GetSystemAbility failed!");
return;
}
deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new PasteboardSaDeathRecipient());
if (deathRecipient_ == nullptr) {
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Getting deathRecipient_ failed.");
return;
}
if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Add death recipient to paste service failed.");
return;
}
pasteboardServiceProxy_ = iface_cast<IPasteboardService>(remoteObject);
if (pasteboardServiceProxy_ == nullptr) {
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Get PasteboardServiceProxy from SA failed.");
return;
}
PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "Getting PasteboardServiceProxy succeeded.");
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Load Pasteboard Service failed.");
}
void PasteboardClient::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
{
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "OnRemoteSaDied start.");
ConnectService();
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "pasteboard_load_callback.h"
#include "pasteboard_client.h"
namespace OHOS {
namespace MiscServices {
void PasteboardLoadCallback::OnLoadSystemAbilitySuccess(
int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
{
PasteboardClient::GetInstance()->LoadSystemAbilitySuccess(remoteObject);
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "Load system ability success!");
}
void PasteboardLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
{
PasteboardClient::GetInstance()->LoadSystemAbilityFail();
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "Load system ability failed!");
}
}
}

View File

@ -846,7 +846,7 @@ void PasteDataNapi::SetProperty(napi_env env, napi_value in, PasteDataNapi *obj)
NAPI_CALL_RETURN_VOID(env, napi_get_property_names(env, in, &propertyNames));
uint32_t propertyNamesNum = 0;
NAPI_CALL_RETURN_VOID(env, napi_get_array_length(env, propertyNames, &propertyNamesNum));
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "propertyNamesNum = %{public}d", propertyNamesNum);
PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "propertyNamesNum = %{public}d", propertyNamesNum);
for (uint32_t i = 0; i < propertyNamesNum; i++) {
napi_value propertyNameNapi = nullptr;
@ -855,15 +855,29 @@ void PasteDataNapi::SetProperty(napi_env env, napi_value in, PasteDataNapi *obj)
char str[STR_MAX_SIZE] = { 0 };
NAPI_CALL_RETURN_VOID(env, napi_get_value_string_utf8(env, propertyNameNapi, str, STR_MAX_SIZE, &len));
std::string propertyName = str;
PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "shareOptionName = %{public}s,", propertyName.c_str());
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "propertyName = %{public}s,", propertyName.c_str());
napi_value propertyNameValueNapi = nullptr;
NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, in, str, &propertyNameValueNapi));
if (propertyName == "shareOption") {
napi_value shareOptionValueNapi = nullptr;
NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, in, str, &shareOptionValueNapi));
int32_t shareOptionValue = 0;
NAPI_CALL_RETURN_VOID(env, napi_get_value_int32(env, shareOptionValueNapi, &shareOptionValue));
NAPI_CALL_RETURN_VOID(env, napi_get_value_int32(env, propertyNameValueNapi, &shareOptionValue));
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "shareOptionValue = %{public}d,", shareOptionValue);
obj->value_->SetShareOption(static_cast<ShareOption>(shareOptionValue));
break;
}
if (propertyName == "tag") {
char tagValue[STR_MAX_SIZE] = { 0 };
size_t tagValueLen = 0;
NAPI_CALL_RETURN_VOID(env,
napi_get_value_string_utf8(env, propertyNameValueNapi, tagValue, STR_MAX_SIZE, &tagValueLen));
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "tagValue = %{public}s,", tagValue);
std::string tagValueStr = tagValue;
obj->value_->SetTag(tagValueStr);
}
if (propertyName == "additions") {
AAFwk::WantParams additions;
bool ret = OHOS::AppExecFwk::UnwrapWantParams(env, propertyNameValueNapi, additions);
PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "additions ret = %{public}d,", ret);
obj->value_->SetAdditions(additions);
}
}
}

28
profile/3701.json Normal file
View File

@ -0,0 +1,28 @@
{
"process": "pasteboard_service",
"systemability": [
{
"name": 3701,
"libpath": "libpasteboard_service.z.so",
"run-on-create": false,
"distributed": true,
"dump_level": 1,
"start-on-demand": {
"deviceonline": [
{
"name": "deviceonline",
"value": "on"
}
]
},
"stop-on-demand": {
"deviceonline": [
{
"name": "deviceonline",
"value": "off"
}
]
}
}
]
}

View File

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2021 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<info>
<process>pasteboard_service</process>
<systemability>
<name>3701</name>
<libpath>libpasteboard_service.z.so</libpath>
<run-on-create>true</run-on-create>
<distributed>true</distributed>
<dump-level>1</dump-level>
</systemability>
</info>

View File

@ -1,4 +1,4 @@
# Copyright (C) 2021 Huawei Device Co., Ltd.
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@ -14,6 +14,6 @@
import("//build/ohos/sa_profile/sa_profile.gni")
ohos_sa_profile("distributeddatamgr_pasteboard_sa_profiles") {
sources = [ "3701.xml" ]
sources = [ "3701.json" ]
part_name = "pasteboard"
}