necko walkthrough

40
NECKO WALKTHROUGH (Data Protocol) Tommy Kuo [:KuoE0] [email protected] 2015.04.23 @Mozilla Taiwan

Upload: chih-hsuan-kuo

Post on 15-Jul-2015

166 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Necko walkthrough

NECKO WALKTHROUGH(Data Protocol)

Tommy Kuo [:KuoE0] [email protected]

2015.04.23 @Mozilla Taiwan

Page 2: Necko walkthrough

Outline• Create protocol channel

• Listener interface

• Open protocol channel

• Data transaction

Page 3: Necko walkthrough

Protocol

Page 4: Necko walkthrough

URI Syntax<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]

http : //google.com/search ? q=Mozilla

https : //www.facebook.com/pages/郝神好神/871866229523862

file : ///Downloads/郝神好帥.jpg

about : config

mailto : [email protected]

data : text/html;charset=utf-8;base64,6YOd56We5aW95bil

line : //shop/detail/xxx

Page 5: Necko walkthrough

Data Protocol

data:[<MIME-type>][;charset=<encoding>][;base64],<data>

A very simple protocol.

Page 6: Necko walkthrough

data:text/html;charset=utf-8;base64,6YOd56We5aW95bil

Page 7: Necko walkthrough

data:text/html;charset=utf-8;base64,6YOd56We5aW95bil

After base64 decoding…

Page 8: Necko walkthrough

Create Protocol Channel

Page 9: Necko walkthrough

handleCommand

openUILinkIn

openLinkIn

loadURIWithFlags (tabbrowser)

loadURIWithFlags (browser)

LoadURI(char16_t*, …)

LoadURIWithOptions

LoadURI(nsIURI*, …)

InternalLoad

DoURILoad

DoChannelLoad

NS_NewURI

NS_NewChannelInternal

Browser UI (XUL)

DocShell Necko

GetProtocolHandler

GetProtocolHandler

nsIURI

nsIChannel

NewURI

NewChannel2

Page 10: Necko walkthrough

GetProtocolHandler

1. Get protocol handler if it is cached.

2. Lookup in ‘about:config’ to check internal support.

3. For internal protocol, get internal protocol hander service and cache it.

4. For external protocol, get default external protocol handler service.

netwerk/base/nsIOService.cpp#406

Page 11: Necko walkthrough

data:text/html;charset=utf-8;base64,6YOd56We5aW95bil

GetProtocolHandler

nsDataHandler

nsSimpleURI nsDataChannel

NewURI() NewChannel2()

Page 12: Necko walkthrough

Protocol Handler API

Format the URI Create the channel Get flags

NewChannel2(…)

NewProxiedChannel2(…)

NewURI(…) GetProtocolFlags(…)

Page 13: Necko walkthrough

Protocol Handler

Internal External

lookup in /netwerk/protocol/ lookup in about:config

Page 14: Necko walkthrough

Internal protocol handler in /netwerk/protocol/

Page 15: Necko walkthrough

External protocol handler in about:config

Page 16: Necko walkthrough

Listener Interface

Page 17: Necko walkthrough

nsIStreamListener• onStartRequest()

• Called to signify the beginning of an asynchronous request.

• onStopRequest()

• Called to signify the end of an asynchronous request.

• onDataAvailable()

• This method is called when the next chunk of data for the ongoing request may be read without blocking the calling thread.

Page 18: Necko walkthrough

nsInputStreamPump… nsDataChannelnsHtml5StreamParser

OnStartRequest

OnStartRequest

OnStartRequest

OnDataAvailable

OnDataAvailable

OnDataAvailable

Read / ReadSegment

OnStopRequest

OnStopRequest

OnStopRequest

Page 19: Necko walkthrough

Open Protocol Channel

Page 20: Necko walkthrough

Initialize & Start

Page 21: Necko walkthrough

nsDataChannel

nsPipeInputStreamInitialize Stream & Pump

nsInputStreamPump

(event dispatcher)

(data source) notify on ready

create create

Page 22: Necko walkthrough

nsDocumentOpenInfo

nsDataChannel

nsInputStreamPump

Setup Listener Chain

listen

listen

notify

notify

Page 23: Necko walkthrough

Start Waiting nsInputStreamPump

nsDataChannel

nsPipeInputStream

AsyncRead()

AsyncWait()OnInputStreamReady()

Page 24: Necko walkthrough

Data Transaction

Page 25: Necko walkthrough

IDLE START

STOP TRANSFER

State of nsInputStreamPump

Must be in main thread!!

Page 26: Necko walkthrough

OnStateStart

Page 27: Necko walkthrough

OnStateStart

Page 28: Necko walkthrough

Create Content Viewer

nsDocShell

nsHTMLDocument

nsDocumentViewer

load

Page 29: Necko walkthrough

Set Content Listener

nsHtml5StreamParser

nsHtml5StreamListener

nsDocumentOpenInfo

listen

listen

notify

notify

Page 30: Necko walkthrough

nsDocumentOpenInfoCall OnStartRequest()

nsDataChannel

nsInputStreamPump

nsHtml5StreamParser

nsHtml5StreamListener

listen notify

listen notify

listen notify

listen notify

Page 31: Necko walkthrough

OnStateTransfer

Page 32: Necko walkthrough

OnStateTransfer

Page 33: Necko walkthrough

nsDocumentOpenInfoCall OnDataAvailable()

nsDataChannel

nsInputStreamPump

nsHtml5StreamParser

nsHtml5StreamListener

listen notify

listen notify

listen notify

listen notify

Page 34: Necko walkthrough

Read Data

nsPipeInputStream

Read/ReadSegment

nsHtml5StreamParser

Page 35: Necko walkthrough

nsPipeInputStream

data segment

nsHtml5StreamParser

parser buffer

temporal buffer

Call Read() in Main Thread

copy

dispatch to parser thread

Page 36: Necko walkthrough

nsPipeInputStream

data segment

nsHtml5StreamParser

parser buffer

Call ReadSegment() in Parser Thread

copy

Page 37: Necko walkthrough

OnStateStop

Page 38: Necko walkthrough

OnStateStop

Page 39: Necko walkthrough

nsDocumentOpenInfoCall OnStopRequest()

nsDataChannel

nsInputStreamPump

nsHtml5StreamParser

nsHtml5StreamListener

listen notify

listen notify

listen notify

listen notify

Page 40: Necko walkthrough

Thank you!