ページ

ラベル WebKit Plug-in の投稿を表示しています。 すべての投稿を表示
ラベル WebKit Plug-in の投稿を表示しています。 すべての投稿を表示

2009年10月9日金曜日

WebKit Plug-in Study (5) 全てのページでプラグインを動作させるには?#2

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク

続き:Cocoaの日々: WebKit Plug-in Study (4) 全てのページでプラグインを動作させるには?


Info.plist 内の WebPluginMIMETypesFilename に存在しないファイル名を指定して試した所、+[load] メソッドが呼出されたのが確認できた。ちょっとトリッキーだがこんな方法もあるのか。

ただここから先は WebKit Plugin の作法が通用しない。WebKit Plugin はMIMEタイプに紐付けられるので、そうでない場合は WebPlugin のコールバックメソッドが呼出されることは無い。つまり +[load] から先は自分で全部やる必要がある。そう SIMBLプラグインとやることは同じである。
こうなると SIMBL 自体が必要か不要かだけの違いになる。

うーむ。そうなると非公式の動作をあてにしてまで WebKit Plugin の体裁にこだわる理由が薄れていく。SIMBLのインストールが不要なのは魅力的なんだが。一つの戦略としては、とりあえずこの動作を当てにして作っておき、それが通用しなくなった時に SIMBL に切り替えるということが考えられる。Evernote とかはその時どうするのだろうか。

いづれにしろ、実装方式は SIMBLプラグインと同等になるので、今度は SIMBLプラグインについて調べることにする。


+++++ WebKit Plug-In シリーズ +++++

2009年10月6日火曜日

WebKit Plug-in Study (2) クラスの構成とひな形のソース

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク

WebKit Plug-in 関連のプロトコルは3つある。




WebPlugIn は、初期化や終了時、プラグインの処理が必要となるタイミングで WebKit側からコールバックされるメソッドが定義されている。ここにプラグインのメインとなる処理を記述する。

WebPlugInContainer はプラグインを格納しているアプリケーション(container)へアクセスするためのメソッドが定義されている。

最後の WebPlugInViewFactory はプラグインのインスタンスを生成するファクトリメソッドが定義されている(プラグインのクラスは NSViewのサブクラスとして定義するののでこういう名前が付いている)。

※WebPlugIn と WebPlugInContainer は非形式 (informal) プロトコル


それぞれの関係はこんなイメージになる。



WebPlugInViewFactory はプラグインクラスではなく別のクラスで実装してもかまわない。また WebPluginContainer プロトコルを実装したインスタンスは、プラグインインスタンス生成時に呼び出される  +[WebPlugInViewFactory plugInViewWithArguments:] の引数(WebPlugInContainerKey)から取得できる。



さてこれらをふまえて自動生成された各メソッドをざっと眺めてみよう。


+ (NSView *)plugInViewWithArguments:(NSDictionary *)newArguments
{
    return [[[self alloc] _initWithArguments:newArguments] autorelease];
}


WebPlugInViewFactory で定義されたクラスメソッド。内部定義してあるメソッドを呼び出している。

内部定義のメソッドはカテゴリで定義されている。


@interface WebkitPlugInStudyView (Internal)
- (id)_initWithArguments:(NSDictionary *)arguments;
@end


@implementation WebkitPlugInStudyView (Internal)

- (id)_initWithArguments:(NSDictionary *)newArguments
{
    if (!(self = [super initWithFrame:NSZeroRect]))
        return nil;
  
    return self;
}



ひな形のソースでは矩形サイズが0のインスタンスを作成して返している。


続いて WebPlugIn(非形式プロトコル)で定義されたメソッドが並ぶ。中身は基本的に何も書かれていない。


// WebPlugIn informal protocol

- (void)webPlugInInitialize
{
    // This method will be only called once per instance of the plug-in object, and will be called
    // before any other methods in the WebPlugIn protocol.
    // You are not required to implement this method.  It may safely be removed.
}

- (void)webPlugInStart
{
    // The plug-in usually begins drawing, playing sounds and/or animation in this method.
    // You are not required to implement this method.  It may safely be removed.
}

- (void)webPlugInStop
{
    // The plug-in normally stop animations/sounds in this method.
    // You are not required to implement this method.  It may safely be removed.
}

- (void)webPlugInDestroy
{
    // Perform cleanup and prepare to be deallocated.
    // You are not required to implement this method.  It may safely be removed.
}

- (void)webPlugInSetIsSelected:(BOOL)isSelected
{
    // This is typically used to allow the plug-in to alter its appearance when selected.
    // You are not required to implement this method.  It may safely be removed.
}

- (id)objectForWebScript
{
    // Returns the object that exposes the plug-in's interface.  The class of this object can implement
    // methods from the WebScripting informal protocol.
    // You are not required to implement this method.  It may safely be removed.
    return self;
}


プラグイン開発はこれらのメソッドをコーディングすることになる。

次回は Safari へ組み込んで動作を確認してみよう。



+++++ WebKit Plug-in シリーズ +++++

+++++ WebKit Plug-in 関連情報 +++++




2009年10月5日月曜日

WebKit Plug-in Study (1) プラグインのひな形を作る

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク

WebKit Plug-in を作ってみよう。幸いな事に Xcode ではテンプレートが用意されている。これを使ってまずはプロジェクトを作成する。











自動的に必要なメソッドが書かれた NSView のサブクラスが生成される。












中身はこんな感じ。


WebkitPlugInStudyView.h
#import
@interface WebkitPlugInStudyView : NSView <WebPlugInViewFactory>
{
}
@end

WebkitPlugInStudyView.m

#import "WebkitPlugInStudyView.h"


@interface WebkitPlugInStudyView (Internal)
- (id)_initWithArguments:(NSDictionary *)arguments;
@end

@implementation WebkitPlugInStudyView

// WebPlugInViewFactory protocol
// The principal class of the plug-in bundle must implement this protocol.

+ (NSView *)plugInViewWithArguments:(NSDictionary *)newArguments
{
    return [[[self alloc] _initWithArguments:newArguments] autorelease];
}

// WebPlugIn informal protocol

- (void)webPlugInInitialize
{
    // This method will be only called once per instance of the plug-in object, and will be called
    // before any other methods in the WebPlugIn protocol.
    // You are not required to implement this method.  It may safely be removed.
}

- (void)webPlugInStart
{
    // The plug-in usually begins drawing, playing sounds and/or animation in this method.
    // You are not required to implement this method.  It may safely be removed.
}

- (void)webPlugInStop
{
    // The plug-in normally stop animations/sounds in this method.
    // You are not required to implement this method.  It may safely be removed.
}

- (void)webPlugInDestroy
{
    // Perform cleanup and prepare to be deallocated.
    // You are not required to implement this method.  It may safely be removed.
}

- (void)webPlugInSetIsSelected:(BOOL)isSelected
{
    // This is typically used to allow the plug-in to alter its appearance when selected.
    // You are not required to implement this method.  It may safely be removed.
}

- (id)objectForWebScript
{
    // Returns the object that exposes the plug-in's interface.  The class of this object can implement
    // methods from the WebScripting informal protocol.
    // You are not required to implement this method.  It may safely be removed.
    return self;
}

@end

@implementation WebkitPlugInStudyView (Internal)

- (id)_initWithArguments:(NSDictionary *)newArguments
{
    if (!(self = [super initWithFrame:NSZeroRect]))
        return nil;
  
    return self;
}

@end




次回はメソッドを確認していこう。