Skip to content

Commit

Permalink
add plugin for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
hxxft committed May 22, 2018
1 parent 62a59ec commit eff17ee
Show file tree
Hide file tree
Showing 19 changed files with 574 additions and 4 deletions.
19 changes: 19 additions & 0 deletions Core/plugin/base/ios/plugin_builder.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// plugin_register.m
// lynx
//
// Created by dli on 2018/5/20.
// Copyright © 2018年 lynx. All rights reserved.
//


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

#import "plugin/impl/net_info/ios/net_info_plugin.h"

namespace plugin {
void PluginBuilder::build(PluginManager* manager) {
[NetInfoPlugin createWithManager:manager];
}
}
20 changes: 20 additions & 0 deletions Core/plugin/base/ios/plugin_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#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>

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

@end


@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_
58 changes: 58 additions & 0 deletions Core/plugin/base/ios/plugin_impl.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "plugin/base/plugin.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>

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) Exec:(NSArray*)args {
NSString* methodName = [NSString stringWithFormat:@"%@: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]);
} else {
NSLog(@"ERROR: Method '%@' not defined in Plugin '%@'", methodName, [args objectAtIndex:0]);
}
}

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



@end



9 changes: 9 additions & 0 deletions Core/plugin/base/plugin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "plugin/base/plugin.h"
#include "plugin/base/plugin_manager.h"

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

27 changes: 27 additions & 0 deletions Core/plugin/base/plugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef LYNX_PLUGIN_PLUGIN_H_
#define LYNX_PLUGIN_PLUGIN_H_

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

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

Plugin(PluginManager* manager):manager_(manager){}
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_;
};
}

#endif
13 changes: 13 additions & 0 deletions Core/plugin/base/plugin_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#ifndef LYNX_PLUGIN_PLUGIN_BUILDER_H_
#define LYNX_PLUGIN_PLUGIN_BUILDER_H_

namespace plugin {
class PluginManager;
class PluginBuilder {
public:
static void build(PluginManager* manager);
};
}

#endif
70 changes: 70 additions & 0 deletions Core/plugin/base/plugin_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "plugin/base/plugin_manager.h"

#include "plugin/base/plugin.h"
#include "runtime/base/lynx_value.h"
#include "runtime/base/lynx_array.h"
#include "runtime/js/class_template.h"

#include "base/log/logging.h"

#include "plugin/base/plugin_builder.h"

namespace plugin {

#define FOR_EACH_METHOD_BINDING(V) \
V(PluginManager, Exec) \
V(PluginManager, Init) \

// Defines methods and fields
FOR_EACH_METHOD_BINDING(DEFINE_METHOD_CALLBACK)

// Defines default ClassTemplate
DEFINE_CLASS_TEMPLATE_START(PluginManager)
EXPOSE_CONSTRUCTOR(true)
FOR_EACH_METHOD_BINDING(REGISTER_METHOD_CALLBACK)
DEFINE_CLASS_TEMPLATE_END

PluginManager::PluginManager(jscore::JSContext* context) : LynxObject(context, DEFAULT_CLASS_TEMPLATE(context)), context_(context){

}

PluginManager::~PluginManager() {

}

void PluginManager::Register(const char* name, Plugin* plugin) {
plugins_.add(name, plugin);
}

base::ScopedPtr<jscore::LynxValue> PluginManager::Init(base::ScopedPtr<jscore::LynxArray>& array) {
PluginBuilder::build(this);
callback_.Reset(array->Get(0)->data_.lynx_function);
return base::ScopedPtr<jscore::LynxValue>(NULL);
}

base::ScopedPtr<jscore::LynxValue> PluginManager::Exec(base::ScopedPtr<jscore::LynxArray>& array) {
std::string module_name(array->Get(0)->data_.str);
Plugins::iterator iter = plugins_.find(module_name);
if(iter == plugins_.end()) {
DLOG(ERROR) << "";
return base::ScopedPtr<jscore::LynxValue>(NULL);
}
iter->second->Exec(array);
return base::ScopedPtr<jscore::LynxValue>(NULL);
}

void PluginManager::Return(int method_id, Plugin::ResultType type, base::ScopedPtr<jscore::LynxArray>& result){
if(context_) {
context_->runtime()->thread_manager()->RunOnJSThread(base::Bind(&PluginManager::ReturnOnJSThread,
base::ScopedRefPtr<PluginManager>(this), method_id, type, result));
}
}

void PluginManager::ReturnOnJSThread(int method_id, Plugin::ResultType type, base::ScopedPtr<jscore::LynxArray> result) {
base::ScopedPtr<jscore::LynxArray> args(lynx_new jscore::LynxArray);
args->Push(jscore::LynxValue::MakeInt(method_id).Release());
args->Push(jscore::LynxValue::MakeInt(type).Release());
args->Push(result.Release());
callback_->Run(this, args.Get());
}
}
33 changes: 33 additions & 0 deletions Core/plugin/base/plugin_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef LYNX_PLUGIN_PLUGIN_MANAGER_H_
#define LYNX_PLUGIN_PLUGIN_MANAGER_H_

#include "runtime/base/lynx_object.h"
#include "runtime/runtime.h"
#include "base/scoped_ptr_map.h"
#include "plugin/base/plugin.h"

namespace plugin {

class PluginManager : public jscore::LynxObject {
public:
PluginManager(jscore::JSContext* context);
virtual ~PluginManager();

base::ScopedPtr<jscore::LynxValue> Init(base::ScopedPtr<jscore::LynxArray>& array);
base::ScopedPtr<jscore::LynxValue> Exec(base::ScopedPtr<jscore::LynxArray>& array);
void Return(int method_id, Plugin::ResultType type, base::ScopedPtr<jscore::LynxArray>& result);
void Register(const char* name, Plugin* plugin);
private:
void ReturnOnJSThread(int method_id, Plugin::ResultType type, base::ScopedPtr<jscore::LynxArray> result);

typedef base::ScopedPtrMap<std::string, Plugin> Plugins;
Plugins plugins_;

base::ScopedPtr<jscore::LynxFunction> callback_;

jscore::JSContext* context_;
private:
DISALLOW_COPY_AND_ASSIGN(PluginManager);
};
}
#endif
9 changes: 9 additions & 0 deletions Core/plugin/impl/clipboard/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var Clipboard = {}
Clipboard.getString = function(callback) {
exec('NetInfo', 'getString', [], callback, null)
}

Clipboard.setString = function(content) {
exec('NetInfo', 'setString', [content], null, null)
}

11 changes: 11 additions & 0 deletions Core/plugin/impl/net_info/ios/net_info_plugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "plugin/base/ios/plugin_impl.h"

#include "plugin/base/plugin_manager.h"

@interface NetInfoPlugin : LynxPlugin

@property (atomic, readwrite) NSString* networkStatus;

+(void) createWithManager: (plugin::PluginManager*)manager;

@end
Loading

0 comments on commit eff17ee

Please sign in to comment.