diff --git a/README.md b/README.md new file mode 100644 index 0000000..70a8bdb --- /dev/null +++ b/README.md @@ -0,0 +1,419 @@ +# pasteBoardService + +## Introduction + +​ As a functional component of the stray subsystem, the clipboard service provides the ability to manage the system clipboard and supports the system copy and paste functions. The system clipboard supports package text, hypertext, URIs and other content operations. + +**picture 1** Subsystem Architecture Diagram +![](figures/subsystem_architecture.png "Subsystem architecture") + +​ The clipboard service provides functions that support application developers to use clipboard-related services conveniently and efficiently. Its main components include clipboard management client and clipboard service. The clipboard management client is responsible for clipboard interface management, providing clipboard northbound JS API to applications; creating clipboard data on the application framework side, requesting clipboard SA to perform clipboard creation, deletion, query, text conversion, configuration, etc. The clipboard service is responsible for clipboard event management, manages the life cycle of clipboard SA (startup, destruction, multi-user, etc.); executes application requests, notifies clipboard data management, and returns the results to the clipboard management client. + + + +## Directory Structure + +``` +/base/miscservices/time +├── etc # Configuration files for the processes contained in the component +├── figures # Framework diagram +├── framework # innerKit interface +├── interfaces # The interface code provided by the component externally +│ └── kits # Interface provided to the application +├── profile # Configuration files for system services contained in the component +├── services # clipboard service implementation +│ └── core # Core code implementation +│ └── test # native test code +│ └── zidl # Cross-process communication code implementation +├── utils # Tests or services use mocked data +└──README.md # Instructions for use +``` + +## illustrate + +### Interface Description + +**list 1** PasteBoard open main method + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

interface name

+

describe

+

createHtmlData(htmlText: string): PasteData;

+

Create a PasteData object of type MIMETYPE_TEXT_HTML for HTML type data

+

createWantData(want: Want): PasteData;

+

Create a PasteData object of type MIMETYPE_TEXT_WANT for data of type want

+

createPlainTextData(text: string): PasteData

+

Create a PasteData object of type MIMETYPE_TEXT_PLAIN for plain text data

+

createUriData(uri: string): PasteData;

+

Create a PasteData object of type MIMETYPE_TEXT_URI for data of type URI

+

createHtmlTextRecord(htmlText: string): PasteDataRecord;

+

Create a PasteDataRecord object of type RecordMIMETYPE_TEXT_HTML for hypertext type data

+

createWantRecord(want: Want): PasteDataRecord;

+

Create a PasteDataRecord object of type MIMETYPE_TEXT_WANT for data of type want

+

createPlainTextRecord(text: string): PasteDataRecord;

+

Create a PasteDataRecord object of type MIMETYPE_TEXT_PLAIN for plain text data

+

createUriRecord(uri: string): PasteDataRecord;

+

Create a PasteDataRecord object of type MIMETYPE_TEXT_URI for data of type URI

+

getSystemPasteboard(): SystemPasteboard

+

Get system clipboard

+
+ + + +**list 2** SystemPasteboard open main method + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

interface name

+

describe

+

on(type:'update', callback: () => void): void;

+

Callback called when the open pasteboard content changes

+

off(type: 'update', callback?: () => void): void

+

Callback called when the content of the pasteboard is closed

+

clear(callback: AsyncCallback): void

+

clear clipboard

+

clear(): Promise;

+

clear clipboard

+

getPasteData(callback: AsyncCallback<PasteData>): void

+

Get system clipboard data object

+

getPasteData():Promise<PasteData>

+

Get system clipboard data object

+

hasPasteData(callback: AsyncCallback<boolean>:void;

+

Check if there is content in the pasteboard

+

hasPasteData(): Promise<boolean>

+

Check if there is content in the pasteboard

+

setPasteData(data: PasteData, callback: AsyncCallback<void>:void;

+

Write PasteData to the pasteboard

+

setPasteData(data: PasteData): Promise<void>

+

Write PasteData to the pasteboard

+
+ + + +**list 3** PasteData open main method + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

interface name

+

describe

+

addHtmlRecord(htmlText: string): void;

+

Add the HTML text record to the PasteData object and update the MIME type to PasteData#MIMETYPE_TEXT_HTML in the DataProperty.

+

addWantRecord(want: Want): void;

+

Add want record to PasteData object and update MIME type to PasteData#MIMETYPE_TEXT_WANT in DataProperty

+

addRecord(record: PasteDataRecord): void;

+

Add PasteRecord to paste data object and update MIME type in data attribute

+

addTextRecord(text: string): void;;

+

Add plain text records to PasteData object and update MIME type to PasteData#MIMETYPE_TEXT_PLAIN in DataProperty

+

addUriRecord(uri: string): void;

+

Add URI record to PasteData object and update MIME type to PasteData#MIMETYPE_TEXT_URI in DataProperty

+

getMimeTypes(): Array<string>

+

MIME type of everything on the pasteboard

+

getPrimaryHtml(): string;

+

HTML text of the main record in the PasteData object

+

getPrimaryWant(): Want;

+

The want of the master record in the PasteData object

+

getPrimaryMimeType(): string;

+

The MIME type of the master record in the PasteData object

+

getPrimaryUri(): string;

+

URI of the master record in the PasteData object

+

getProperty(): PasteDataProperty;

+

Get the properties of the clipboard data object

+

getRecordAt(index: number): PasteDataRecord;

+

records based on the specified index

+

getRecordCount(): number;

+

Number of records in PasteData object

+

hasMimeType(mimeType: string): boolean;

+

Checks if data of the specified MIME type exists in the DataProperty

+

removeRecordAt(index: number): boolean;

+

Delete records based on specified index

+

replaceRecordAt(index: number, record: PasteDataRecord): boolean;

+

Replace the specified record with a new record

+
+ + + + + +**list 4** PasteDataRecord open main method + + + + + + + + + + + + +

interface name

+

describe

+

convertToText(callback: AsyncCallback<string>): void;

+

Convert PasteData to text content

+

convertToText(): Promise<void>

+

Convert PasteData to text content

+
+ + + +**list 5** PasteDataProperty Parameter Description + + + + + + + + + + + + + + + + + + + + + + + + + + + +

name

+

type

+

illustrate

+

additions{[key:string]}

+

object

+

Additional property data key-value pair

+

mimeTypes

+

Array

+

Distinct MIME types for all records in PasteData

+

tag

+

string

+

User-defined labels for PasteData objects

+

timestamp

+

number

+

Timestamp indicating when the data was written to the system clipboard.

+

localOnly

+

boolean

+

Check if PasteData is set for local access only.

+
+ + +### Instructions for use + +Clipboard module usage example: + +``` +// import module +import pasteboard from '@ohos.pasteboard' +//text copy +console.log('Get SystemPasteboard') +var systemPasteboard = pasteboard.getSystemPasteboard() +systemPasteboard.clear() + +var textData = 'Hello World!' +console.log('createPlainTextData = ' + textData) +var pasteData = pasteboard.createPlainTextData(textData) + +console.log('Writes PasteData to the pasteboard') +systemPasteboard.setPasteData(pasteData) + +console.log('Checks there is content in the pasteboard') +assert.equal(systemPasteboard.hasPasteData(), true) + +console.log('Checks the number of records') +pasteData = systemPasteboard.getPasteData() +assert.equal(pasteData.getRecordCount(), 1) + +console.log('Checks the pasteboard content') +assert.equal(pasteData.getPrimaryText(), textData) + +console.log('Checks there is a MIMETYPE_TEXT_PLAIN MIME type of data') +assert.equal(pasteData.hasMimeType(MIMETYPE_TEXT_PLAIN), true) +assert.equal(pasteData.getPrimaryMimeType(), MIMETYPE_TEXT_PLAIN) + +//clipboard change listener +console.log('Off the content changes') +var systemPasteboard = pasteboard.getSystemPasteboard() +systemPasteboard.off(contentChanges) +systemPasteboard.clear() + +var textData = 'Hello World!' +console.log('createUriData = ' + textData) +var pasteData = pasteboard.createUriData(textData) + +console.log('Writes PasteData to the pasteboard') +systemPasteboard.setPasteData(pasteData) + +console.log('Checks there is content in the pasteboard') +assert.equal(systemPasteboard.hasPasteData(), true) + +console.log('Checks the number of records') +pasteData = systemPasteboard.getPasteData() +assert.equal(pasteData.getRecordCount(), 1) + +console.log('On the content changes') +systemPasteboard.on(contentChanges) + +console.log('Removes the Record') +assert.equal(pasteData.removeRecordAt(0), true) + +console.log('Writes PasteData to the pasteboard') +systemPasteboard.setPasteData(pasteData) + +console.log('Checks the number of records') +pasteData = systemPasteboard.getPasteData() +assert.equal(pasteData.getRecordCount(), 0) + +console.log('Checks there is no content in the pasteboard') +assert.equal(systemPasteboard.hasPasteData(), false) + +var textDataNew = 'Hello World!-New' +console.log('createUriData = ' + textDataNew) +var pasteData = pasteboard.createUriData(textDataNew) + +console.log('Writes PasteData to the pasteboard') +systemPasteboard.setPasteData(pasteData) + +console.log('Checks there is content in the pasteboard') +assert.equal(systemPasteboard.hasPasteData(), true) + +console.log('Checks the number of records') +pasteData = systemPasteboard.getPasteData() +assert.equal(pasteData.getRecordCount(), 1) + +console.log('Checks the pasteboard content') +assert.equal(pasteData.getRecordAt(0).plainText, textDataNew) + +``` + + + +## Related warehouse + +**Miscsoftware service subsystem** + +[miscservices\_paste](https://gitee.com/openharmony/miscservices_pasteboard) + diff --git a/figures/subsystem_architecture.png b/figures/subsystem_architecture.png new file mode 100644 index 0000000..162c993 Binary files /dev/null and b/figures/subsystem_architecture.png differ diff --git a/framework/innerkits/src/paste_data.cpp b/framework/innerkits/src/paste_data.cpp index ddff147..d62c3e5 100644 --- a/framework/innerkits/src/paste_data.cpp +++ b/framework/innerkits/src/paste_data.cpp @@ -206,7 +206,7 @@ bool PasteData::Marshalling(Parcel &parcel) const { PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start."); auto length = records_.size(); - PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "length: %{public}d.",static_cast(length)); + PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "length: %{public}zu.", length); // write length if (!parcel.WriteUint32(static_cast(length))) { PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end."); @@ -229,7 +229,7 @@ bool PasteData::ReadFromParcel(Parcel &parcel) records_.clear(); // read vector length auto length = parcel.ReadUint32(); - PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "length: %{public}d.",static_cast(length)); + PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "length: %{public}u.", length); for (uint32_t i = 0; i < length; i++) { PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "for."); std::unique_ptr record(parcel.ReadParcelable()); diff --git a/framework/innerkits/src/paste_data_record.cpp b/framework/innerkits/src/paste_data_record.cpp index f77b7fc..e629ab6 100644 --- a/framework/innerkits/src/paste_data_record.cpp +++ b/framework/innerkits/src/paste_data_record.cpp @@ -20,7 +20,7 @@ namespace MiscServices { namespace { constexpr int MAX_TEXT_LEN = 500 * 1024; } -std::shared_ptr PasteDataRecord::NewHtmlRecord(const std::string &htmlText) +std::shared_ptr PasteDataRecord::NewHtmlRecord(const std::string &htmlText) { if (htmlText.length() >= MAX_TEXT_LEN) { return nullptr; diff --git a/services/core/src/pasteboard_storage.cpp b/services/core/src/pasteboard_storage.cpp index 8ff8fb6..664b302 100644 --- a/services/core/src/pasteboard_storage.cpp +++ b/services/core/src/pasteboard_storage.cpp @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include #include "pasteboard_storage.h" +#include #include "paste_data.h" namespace OHOS { @@ -21,14 +21,16 @@ namespace MiscServices { namespace nlohmann { template struct adl_serializer> { - static void to_json(json& j, const std::shared_ptr& ptr) { + static void to_json(json& j, const std::shared_ptr& ptr) + { if (ptr.get()) { j = *ptr; } else { j = nullptr; } } - static void from_json(const json& j, std::shared_ptr& ptr) { + static void from_json(const json& j, std::shared_ptr& ptr) + { if (j.is_null()) { ptr = nullptr; } else { @@ -38,7 +40,8 @@ template }; } -void to_json(nlohmann::json& j, const PasteDataRecord& r) { +void to_json(nlohmann::json& j, const PasteDataRecord& r) +{ j["mime_type"]= r.GetMimeType(); j["html_text"]= r.GetHtmlText(); j["plain_text"]= r.GetPlainText(); @@ -51,7 +54,8 @@ void to_json(nlohmann::json& j, const PasteDataRecord& r) { } } -void from_json(const nlohmann::json& j, PasteDataRecord& r) { +void from_json(const nlohmann::json& j, PasteDataRecord& r) +{ std::string mimeType = j.at("mime_type"); std::shared_ptr htmlText = j.at("html_text"); std::shared_ptr plainText = j.at("plain_text"); @@ -65,24 +69,28 @@ void from_json(const nlohmann::json& j, PasteDataRecord& r) { r = PasteDataRecord(mimeType, htmlText, want, plainText, uri); } -void to_json(nlohmann::json& j, const PasteData& p) { +void to_json(nlohmann::json& j, const PasteData& p) +{ j = p.AllRecords(); } -void from_json(const nlohmann::json& j, PasteData& p) { +void from_json(const nlohmann::json& j, PasteData& p) +{ auto records = j.get>>(); p = PasteData(records); } -std::shared_ptr PasteboardStorage::Create(const std::string &file) { +std::shared_ptr PasteboardStorage::Create(const std::string &file) +{ return std::shared_ptr(std::make_shared PasteboardStorage(file)); } PasteboardStorage::PasteboardStorage(std::string file) : file{std::move(file)} { } -void PasteboardStorage::SaveData(std::map> data) { +void PasteboardStorage::SaveData(std::map> data) +{ try { nlohmann::json jsonData = data; auto str = jsonData.dump(); @@ -95,7 +103,8 @@ void PasteboardStorage::SaveData(std::map> d } } -std::map> PasteboardStorage::LoadData() { +std::map> PasteboardStorage::LoadData() +{ std::map> data; try { std::ifstream instream(this->file); diff --git a/services/test/unittest/src/paste_service_test.cpp b/services/test/unittest/src/paste_service_test.cpp index cd9965f..2926918 100644 --- a/services/test/unittest/src/paste_service_test.cpp +++ b/services/test/unittest/src/paste_service_test.cpp @@ -149,7 +149,7 @@ HWTEST_F(PasteboardServiceTest, PasteDataTest001, TestSize.Level0) auto data = PasteboardClient::GetInstance()->CreateWantData(want); EXPECT_TRUE(data != nullptr); auto has = PasteboardClient::GetInstance()->HasPasteData(); - EXPECT_TRUE(has != true); + EXPECT_TRUE(has != true); PasteboardClient::GetInstance()->SetPasteData(*data); has = PasteboardClient::GetInstance()->HasPasteData(); EXPECT_TRUE(has == true); @@ -175,7 +175,7 @@ HWTEST_F(PasteboardServiceTest, PasteDataTest002, TestSize.Level0) EXPECT_TRUE(has != true); PasteboardClient::GetInstance()->SetPasteData(*data); has = PasteboardClient::GetInstance()->HasPasteData(); - EXPECT_TRUE(has == true); + EXPECT_TRUE(has == true); PasteData pasteData; auto ok = PasteboardClient::GetInstance()->GetPasteData(pasteData); EXPECT_TRUE(ok == true); @@ -195,7 +195,7 @@ HWTEST_F(PasteboardServiceTest, PasteDataTest003, TestSize.Level0) EXPECT_TRUE(data != nullptr); PasteboardClient::GetInstance()->Clear(); auto has = PasteboardClient::GetInstance()->HasPasteData(); - EXPECT_TRUE(has != true); + EXPECT_TRUE(has != true); PasteboardClient::GetInstance()->SetPasteData(*data); has = PasteboardClient::GetInstance()->HasPasteData(); EXPECT_TRUE(has == true); @@ -221,7 +221,7 @@ HWTEST_F(PasteboardServiceTest, PasteDataTest004, TestSize.Level0) EXPECT_TRUE(has != true); PasteboardClient::GetInstance()->SetPasteData(*data); has = PasteboardClient::GetInstance()->HasPasteData(); - EXPECT_TRUE(has == true); + EXPECT_TRUE(has == true); PasteData pasteData; auto ok = PasteboardClient::GetInstance()->GetPasteData(pasteData); EXPECT_TRUE(ok == true); diff --git a/utils/mock/src/mock_permission.cpp b/utils/mock/src/mock_permission.cpp index 8646051..d4528b4 100644 --- a/utils/mock/src/mock_permission.cpp +++ b/utils/mock/src/mock_permission.cpp @@ -18,7 +18,8 @@ namespace OHOS { namespace MiscServices { namespace MockPermission { -bool VerifyPermission(const std::string &bundleName, const std::string &permissionName, int userId) { +bool VerifyPermission(const std::string &bundleName, const std::string &permissionName, int userId) +{ return true; } } // OHOS diff --git a/utils/native/include/pasteboard_sp_singleton.h b/utils/native/include/pasteboard_sp_singleton.h index bfdf964..40cdc78 100644 --- a/utils/native/include/pasteboard_sp_singleton.h +++ b/utils/native/include/pasteboard_sp_singleton.h @@ -23,11 +23,12 @@ namespace OHOS { namespace MiscServices { -#define DECLARE_DELAYED_SP_SINGLETON(MyClass)\ -public:\ - ~MyClass();\ -private:\ - friend DelayedSpSingleton;\ +#define DECLARE_DELAYED_SP_SINGLETON(MyClass) \ +public: \ + ~MyClass(); \ + \ +private: \ + friend DelayedSpSingleton; \ MyClass(); template