| 1 | require 'osx/cocoa' |
|---|
| 2 | include OSX |
|---|
| 3 | |
|---|
| 4 | class CHMInternalURLProtocol < NSURLProtocol |
|---|
| 5 | #+ (void)registerContainer:(CHMContainer *)container; |
|---|
| 6 | #+ (void)unregisterContainer:(CHMContainer *)container; |
|---|
| 7 | #+ (CHMContainer *)containerForUniqueId:(NSString *)uniqueId; |
|---|
| 8 | #+ (NSURL *)URLWithPath:(NSString *)path inContainer:(CHMContainer *)container; |
|---|
| 9 | |
|---|
| 10 | SCHEME = "chm-internal" |
|---|
| 11 | |
|---|
| 12 | def self.url_for(doc, path) |
|---|
| 13 | url = "#{SCHEME}://#{doc.object_id}#{path}" |
|---|
| 14 | NSURL.URLWithString_relativeToURL(url, "#{SCHEME}://#{doc.object_id}/") |
|---|
| 15 | end |
|---|
| 16 | |
|---|
| 17 | # protocol |
|---|
| 18 | |
|---|
| 19 | #+ (BOOL)canHandleURL:(NSURL *)anURL; |
|---|
| 20 | def self.canHandleURL(url) |
|---|
| 21 | url.scheme == SCHEME |
|---|
| 22 | end |
|---|
| 23 | |
|---|
| 24 | #+ (BOOL)canInitWithRequest:(NSURLRequest *)request |
|---|
| 25 | def self.canInitWithRequest(req) |
|---|
| 26 | canHandleURL(req.URL) |
|---|
| 27 | end |
|---|
| 28 | |
|---|
| 29 | #+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request |
|---|
| 30 | def self.canonicalRequestForRequest(req) |
|---|
| 31 | req |
|---|
| 32 | end |
|---|
| 33 | |
|---|
| 34 | #-(NSURLRequest *)request |
|---|
| 35 | # def request |
|---|
| 36 | # super_request |
|---|
| 37 | # end |
|---|
| 38 | |
|---|
| 39 | #-(void)startLoading |
|---|
| 40 | def startLoading |
|---|
| 41 | log "startLoading" |
|---|
| 42 | url = request.URL |
|---|
| 43 | chm = ObjectSpace._id2ref(url.host.to_s.to_i) |
|---|
| 44 | |
|---|
| 45 | text = chm.retrieve_object(url.path.to_s) |
|---|
| 46 | # data = NSData.dataWithBytesNoCopy_length(text, text.length) |
|---|
| 47 | data = NSData.dataWithBytes_length(text, text.length) |
|---|
| 48 | |
|---|
| 49 | response = NSURLResponse.alloc.objc_send( |
|---|
| 50 | :initWithURL, url, |
|---|
| 51 | #:MIMEType, "application/octet-stream", |
|---|
| 52 | :MIMEType, "text/html", |
|---|
| 53 | :expectedContentLength, data.length, |
|---|
| 54 | :textEncodingName, nil |
|---|
| 55 | ) |
|---|
| 56 | self.client.objc_send( |
|---|
| 57 | :URLProtocol, self, |
|---|
| 58 | :didReceiveResponse, response, |
|---|
| 59 | :cacheStoragePolicy, NSURLCacheStorageNotAllowed |
|---|
| 60 | ) |
|---|
| 61 | self.client.URLProtocol_didLoadData(self, data); |
|---|
| 62 | self.client.URLProtocolDidFinishLoading(self); |
|---|
| 63 | end |
|---|
| 64 | |
|---|
| 65 | #-(void)stopLoading |
|---|
| 66 | def stopLoading |
|---|
| 67 | log "stopLoading" |
|---|
| 68 | end |
|---|
| 69 | |
|---|
| 70 | #-(NSCachedURLResponse *)cachedResponse |
|---|
| 71 | |
|---|
| 72 | end |
|---|
| 73 | |
|---|
| 74 | require "chm" |
|---|
| 75 | class AppController < NSObject |
|---|
| 76 | |
|---|
| 77 | def awakeFromNib |
|---|
| 78 | end |
|---|
| 79 | |
|---|
| 80 | def applicationWillFinishLaunching(n) |
|---|
| 81 | r = NSURLProtocol.registerClass CHMInternalURLProtocol |
|---|
| 82 | log "Register: #{r}" |
|---|
| 83 | |
|---|
| 84 | # "/Users/cho45/tmp/ruby-refm-rdp-1.9.0-ja-htmlhelp_css/rubymanjp.chm" |
|---|
| 85 | end |
|---|
| 86 | |
|---|
| 87 | def applicationWillTerminate(n) |
|---|
| 88 | NSURLProtocol.unregisterClass CHMInternalURLProtocol |
|---|
| 89 | log "Unregister" |
|---|
| 90 | end |
|---|
| 91 | |
|---|
| 92 | end |
|---|
| 93 | |
|---|
| 94 | |
|---|