-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update plugin module for iOS support addEventListener
- Loading branch information
Showing
42 changed files
with
1,391 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.