Skip to content

Commit

Permalink
b/332929552
Browse files Browse the repository at this point in the history
Address comments.
Use smart pointer to manage object PlatformServiceImpl
Use std::string to manage service name object
Replace macro with const ntwhen defining platform service name
Create ctor
  • Loading branch information
yzzhang11 committed Apr 16, 2024
1 parent ebbcf14 commit d5728e4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
18 changes: 8 additions & 10 deletions starboard/linux/shared/platform_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ CobaltExtensionPlatformService Open(void* context,
reinterpret_cast<const CobaltPlatformServiceApi*>(
platform_service_registry.at(name)());

char* service_name = new char[strlen(name) + 1];
snprintf(service_name, strlen(name) + 1, "%s", name);

service = new CobaltExtensionPlatformServicePrivate(
{context, receive_callback, service_name});
{context, receive_callback, (std::string)name});

service->platform_service_impl = api->Open(context, receive_callback);
service->platform_service_impl = std::unique_ptr<PlatformServiceImpl>(
api->Open(context, receive_callback));
SB_LOG(INFO) << "Open() created service: " << name;
}

Expand Down Expand Up @@ -107,8 +105,8 @@ void Close(CobaltExtensionPlatformService service) {

const CobaltPlatformServiceApi* api =
reinterpret_cast<const CobaltPlatformServiceApi*>(
platform_service_registry.at(service->name)());
api->Close(service->platform_service_impl);
platform_service_registry.at(service->name.c_str())());
api->Close(service->platform_service_impl.get());

delete static_cast<CobaltExtensionPlatformServicePrivate*>(service);
}
Expand All @@ -122,9 +120,9 @@ void* Send(CobaltExtensionPlatformService service,

const CobaltPlatformServiceApi* api =
reinterpret_cast<const CobaltPlatformServiceApi*>(
platform_service_registry.at(service->name)());
return api->Send(service->platform_service_impl, data, length, output_length,
invalid_state);
platform_service_registry.at(service->name.c_str())());
return api->Send(service->platform_service_impl.get(), data, length,
output_length, invalid_state);
}

const CobaltExtensionPlatformServiceApi kPlatformServiceApi = {
Expand Down
14 changes: 12 additions & 2 deletions starboard/linux/shared/platform_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@
#ifndef STARBOARD_LINUX_SHARED_PLATFORM_SERVICE_H_
#define STARBOARD_LINUX_SHARED_PLATFORM_SERVICE_H_

#include <memory>
#include <string>

#include "starboard/extension/platform_service.h"

typedef struct PlatformServiceImpl {
void* context;
ReceiveMessageCallback receive_callback;

PlatformServiceImpl(void* context, ReceiveMessageCallback receive_callback)
: context(context), receive_callback(receive_callback) {}

PlatformServiceImpl() = default;
} PlatformServiceImpl;

typedef struct CobaltPlatformServiceApi {
Expand All @@ -44,6 +52,8 @@ typedef struct CobaltPlatformServiceApi {
ReceiveMessageCallback receive_callback);

// Perform operations before closing the service passed in as |service|.
// Function Close shouldn't manually delete PlatformServiceImpl pointer,
// because it is managed by unique_ptr in Platform Service.
void (*Close)(PlatformServiceImpl* service);

// Send |data| of length |length| to |service|. If there is a synchronous
Expand All @@ -64,8 +74,8 @@ typedef struct CobaltPlatformServiceApi {
typedef struct CobaltExtensionPlatformServicePrivate {
void* context;
ReceiveMessageCallback receive_callback;
const char* name;
PlatformServiceImpl* platform_service_impl;
std::string name;
std::unique_ptr<PlatformServiceImpl> platform_service_impl;
} CobaltExtensionPlatformServicePrivate;

// Well-defined value for an invalid |PlatformServiceImpl|.
Expand Down
17 changes: 11 additions & 6 deletions starboard/linux/shared/soft_mic_platform_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ namespace shared {
namespace {
typedef struct SoftMicPlatformServiceImpl : public PlatformServiceImpl {
// Define additional data field.
// variable_1, variable_2,...
SoftMicPlatformServiceImpl(void* context,
ReceiveMessageCallback receive_callback)
: PlatformServiceImpl(context, receive_callback) {}

// Default constructor
SoftMicPlatformServiceImpl() = default;

} SoftMicPlatformServiceImpl;

const char kGetMicSupport[] = "\"getMicSupport\"";
Expand All @@ -49,17 +57,14 @@ bool Has(const char* name) {
PlatformServiceImpl* Open(void* context,
ReceiveMessageCallback receive_callback) {
SB_DCHECK(context);

PlatformServiceImpl* impl;

SB_LOG(INFO) << "Open() service created: " << kSoftMicPlatformServiceName;
SoftMicPlatformServiceImpl* soft_mic_impl =
new SoftMicPlatformServiceImpl({context, receive_callback});

return soft_mic_impl;
return new SoftMicPlatformServiceImpl(context, receive_callback);
}

void Close(PlatformServiceImpl* service) {
// Function Close shouldn't manually delete PlatformServiceImpl pointer,
// because it is managed by unique_ptr in Platform Service.
SB_LOG(INFO) << "Perform actions before gracefully shutting down the service";
}

Expand Down
2 changes: 1 addition & 1 deletion starboard/linux/shared/soft_mic_platform_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace starboard {
namespace shared {

#define kSoftMicPlatformServiceName "com.google.youtube.tv.SoftMic"
const char* const kSoftMicPlatformServiceName = "com.google.youtube.tv.SoftMic";

const void* GetSoftMicPlatformServiceApi();

Expand Down

0 comments on commit d5728e4

Please sign in to comment.