Skip to content

Commit

Permalink
update plugin module for iOS support addEventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
hxxft committed Jun 3, 2018
1 parent 5c0e078 commit 34f1757
Show file tree
Hide file tree
Showing 42 changed files with 1,391 additions and 612 deletions.
23 changes: 23 additions & 0 deletions Core/content/lynx_thread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2017 The Lynx Authors. All rights reserved.

#ifndef LYNX_CORE_CONTENT_LYNX_THREAD_H_
#define LYNX_CORE_CONTENT_LYNX_THREAD_H_

#include "base/threading/thread.h"

namespace content {
class LynxThread {
public:
enum ID {
UI,
IO,
JS,
PLUGIN,
ID_COUNT,
};
static void Initialize();
static void PostTask(ID identifier, base::Closure* task);
};
} // namespace content

#endif // LYNX_CORE_CONTENT_LYNX_THREAD_H_
52 changes: 52 additions & 0 deletions Core/content/lynx_thread_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2017 The Lynx Authors. All rights reserved.

#include "content/lynx_thread.h"
#include "base/lazy_instance.h"

namespace content {

namespace {
static const char* g_lynx_thread_names[LynxThread::ID_COUNT] = {
"",
"Lynx_IOThread",
"Lynx_JSThread",
"Lynx_PluginThread",
};

static const base::MessageLoop::MESSAGE_LOOP_TYPE g_lynx_thread_loop_type[LynxThread::ID_COUNT] = {
base::MessageLoop::MESSAGE_LOOP_NONE,
base::MessageLoop::MESSAGE_LOOP_NONE,
base::MessageLoop::MESSAGE_LOOP_NONE,
base::MessageLoop::MESSAGE_LOOP_POSIX,
};

class ThreadManager {
public:
void Initialize() {
for(int i = 0; i < LynxThread::ID_COUNT; ++i) {
if(g_lynx_thread_loop_type[i] != base::MessageLoop::MESSAGE_LOOP_NONE) {
threads[i] = lynx_new base::Thread(g_lynx_thread_loop_type[i], g_lynx_thread_names[i]);
threads[i]->Start();
}
}
}
void PostTask(LynxThread::ID identifier, base::Closure* task) {
threads[identifier]->Looper()->PostTask(task);
}
private:
base::Thread* threads[LynxThread::ID_COUNT];
};

base::LazyInstance<ThreadManager> g_thread_manager;
}

void LynxThread::Initialize() {
g_thread_manager.Get()->Initialize();
}

void LynxThread::PostTask(ID identifier, base::Closure* task) {
g_thread_manager.Get()->PostTask(identifier, task);
}

}

19 changes: 0 additions & 19 deletions Core/plugin/base/ios/plugin_builder.mm

This file was deleted.

21 changes: 10 additions & 11 deletions Core/plugin/base/ios/plugin_impl.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// Copyright 2017 The Lynx Authors. All rights reserved.

#ifndef LYNX_PLUGIN_BASE_IOS_PLUGIN_MANAGER_H_
#define LYNX_PLUGIN_BASE_IOS_PLUGIN_MANAGER_H_

#include "plugin/base/plugin.h"
#include "plugin/base/plugin_manager.h"

@protocol PluginProtocol <NSObject>
#include <Foundation/Foundation.h>

@required
-(void) Exec:(NSArray*)args;

@end
@interface LynxPlugin : NSObject

@property (atomic, readwrite)NSString* pluginName;
-(void) exec:(NSInteger)clientId argments:(NSArray*)args;
-(void) returnBack:(NSInteger)clientId method:(NSNumber*)methodId successed:(Boolean)successed argments:(NSArray*)args;
-(void) addEvent:(NSString*) event;
-(void) removeEvent:(NSString*) event;
-(void) dispatchEvent:(NSString*) event argments:(NSArray*)args;

@interface LynxPlugin : NSObject<PluginProtocol>
-(id) initWithName:(NSString*)name pluginManager:(plugin::PluginManager*)manager;
-(void) Return:(NSNumber*) methodId resultType:(plugin::Plugin::ResultType)type argments:(NSArray*)args;
@end

#endif // LYNX_PLUGIN_BASE_IOS_PLUGIN_MANAGER_H_
86 changes: 58 additions & 28 deletions Core/plugin/base/ios/plugin_impl.mm
Original file line number Diff line number Diff line change
@@ -1,58 +1,88 @@
// Copyright 2017 The Lynx Authors. All rights reserved.

#include "plugin/base/plugin.h"
#include "plugin/plugin_server.h"

#include "plugin/base/ios/plugin_impl.h"
#include "plugin/base/plugin_manager.h"
#include "base/log/logging.h"
#include "base/ios/oc_helper.h"

#include <objc/message.h>
#include <Foundation/Foundation.h>

namespace plugin {
class PluginImpl : public Plugin {
public:
PluginImpl(const char* name, PluginManager* manager, LynxPlugin* instance) : Plugin(manager), instance_(instance){
manager->Register(name, this);
}

virtual void Exec(base::ScopedPtr<jscore::LynxArray> args) {
NSArray* array = base::ios::OCHelper::ConvertToOCArray(args.Get());
[instance_ Exec:array];
}

private:
LynxPlugin* instance_;
};
}

@implementation LynxPlugin {
plugin::Plugin* delegate_;
}

-(id) initWithName:(NSString*)name pluginManager:(plugin::PluginManager*)manager {
self = [super init];
if(self != nil) {
delegate_ = new plugin::PluginImpl([name UTF8String], manager, self);
}
return self;
-(void) setDelegate:(plugin::Plugin*) delegate {
delegate_ = delegate;
}

-(void) Exec:(NSArray*)args {
NSString* methodName = [NSString stringWithFormat:@"%@:arguments:", [args objectAtIndex:1]];
-(void) exec:(NSInteger)clientId argments:(NSArray*)args {
NSString* methodName = [NSString stringWithFormat:@"%@:method:arguments:", [args objectAtIndex:1]];
SEL normalSelector = NSSelectorFromString(methodName);
if ([self respondsToSelector:normalSelector]) {
((void (*)(id, SEL, id, id))objc_msgSend)(self, normalSelector, [args objectAtIndex:2], [args objectAtIndex:3]);
((void (*)(id, SEL, NSInteger, id, id))objc_msgSend)(self, normalSelector, clientId, [args objectAtIndex:2], [args objectAtIndex:3]);
} else {
NSLog(@"ERROR: Method '%@' not defined in Plugin '%@'", methodName, [args objectAtIndex:0]);
}
}

-(void) Return:(NSNumber*) methodId resultType:(plugin::Plugin::ResultType)type argments:(NSArray*)args {
-(void) returnBack:(NSInteger)clientId method:(NSNumber*) method successed:(Boolean)successed argments:(NSArray*)args {
base::ScopedPtr<jscore::LynxArray> array = base::ios::OCHelper::ConvertToLynxArray(args);
delegate_->Return([methodId intValue], type, array);
delegate_->Return(clientId, [method intValue], successed, array);
}

-(void) addEvent:(NSString*) event {
NSLog(@"ERROR: AddEvent not implement in Plugin '%@'", _pluginName);
}

-(void) removeEvent:(NSString*) event {
NSLog(@"ERROR: RemoveEvent not implement in Plugin '%@'", _pluginName);
}

-(void) dispatchEvent:(NSString*) event argments:(NSArray*)args {
base::ScopedPtr<jscore::LynxArray> array = base::ios::OCHelper::ConvertToLynxArray(args);
std::string plugin_name([_pluginName UTF8String]);
std::string plugin_event([event UTF8String]);
delegate_->DispatchEvent(plugin_name, plugin_event, array);
}

@end

namespace plugin {
class PluginImpl : public Plugin {
public:
PluginImpl(){}
virtual ~PluginImpl() {}
PluginImpl(LynxPlugin* instance) : Plugin(), instance_(instance){
[instance_ setDelegate:this];
}

virtual void Exec(long client_id, base::ScopedPtr<jscore::LynxArray> args) {
NSArray* array = base::ios::OCHelper::ConvertToOCArray(args.Get());
[instance_ exec:client_id argments:array];
}

virtual void AddEvent(const std::string& event) {
[instance_ addEvent:[NSString stringWithUTF8String:event.c_str()]];
}

virtual void RemoveEvent(const std::string& event) {
[instance_ removeEvent:[NSString stringWithUTF8String:event.c_str()]];
}
private:
LynxPlugin* instance_;
};

Plugin* Plugin::Create(const std::string &name) {
NSString* plugin_clazz_name = [NSString stringWithFormat:@"%@Plugin", [NSString stringWithUTF8String:name.c_str()]];
Class clazz = NSClassFromString(plugin_clazz_name);
LynxPlugin* lynx_plugin = [[clazz alloc] init];
return new PluginImpl(lynx_plugin);
}
}



12 changes: 9 additions & 3 deletions Core/plugin/base/plugin.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// Copyright 2017 The Lynx Authors. All rights reserved.

#include "plugin/base/plugin.h"
#include "plugin/base/plugin_manager.h"
#include "plugin/plugin_server.h"

namespace plugin {
void Plugin::Return(int method_id, ResultType type, base::ScopedPtr<jscore::LynxArray> args){
manager_->Return(method_id, type, args);
void Plugin::Return(long client, int method, bool successed, base::ScopedPtr<jscore::LynxArray> args) {
PluginServer::Return(client, method, successed, args);
}

void Plugin::DispatchEvent(const std::string& name, const std::string& event, base::ScopedPtr<jscore::LynxArray> args) {
PluginServer::DispatchEvent(name, event, args);
}
}

29 changes: 18 additions & 11 deletions Core/plugin/base/plugin.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
// Copyright 2017 The Lynx Authors. All rights reserved.

#ifndef LYNX_PLUGIN_PLUGIN_H_
#define LYNX_PLUGIN_PLUGIN_H_

#include "runtime/base/lynx_value.h"
#include "runtime/base/lynx_array.h"

#include "render/event_target.h"

#include <unordered_set>
#include <string>

namespace plugin {
class PluginManager;
class Plugin {
public:
enum ResultType {
ResultType_Fail,
ResultType_Success,
ResultType_Event
};

Plugin(PluginManager* manager):manager_(manager){}
Plugin(){}
virtual ~Plugin(){}
virtual void Exec(base::ScopedPtr<jscore::LynxArray> args){}
void AddEventListener(base::ScopedPtr<jscore::LynxArray> args){}
void Return(int method_id, ResultType type, base::ScopedPtr<jscore::LynxArray> args);
private:
PluginManager* manager_;
virtual void Exec(long client, base::ScopedPtr<jscore::LynxArray> args){}
void Return(long client_id, int method_id, bool successed, base::ScopedPtr<jscore::LynxArray> args);

virtual void AddEvent(const std::string& event) {}
virtual void RemoveEvent(const std::string& event) {}
void DispatchEvent(const std::string& name, const std::string& event, base::ScopedPtr<jscore::LynxArray> args);

static Plugin* Create(const std::string& name);
protected:
std::unordered_set<std::string> register_events_;
};
}

Expand Down
13 changes: 0 additions & 13 deletions Core/plugin/base/plugin_builder.h

This file was deleted.

70 changes: 0 additions & 70 deletions Core/plugin/base/plugin_manager.cc

This file was deleted.

Loading

0 comments on commit 34f1757

Please sign in to comment.