We are not there yet – Code Rush 雜感

感謝 MozTWIrvin 等人籌辦活動,今天終於有機會坐在電影院好好看完 Code Rush 這部電影。

這部電影是從 1998 年 3 月 31 日,Netscape 釋出 Mozilla 原始碼,建立 Mozilla 自由軟體專案的前幾週開始講起,一路講到最後 Netscape 決定賣給 AOL 的時刻。1998 年,Netscape 在微軟強大的競爭之下,決定以釋出原始碼,建立自由軟體專案的方式奮力一博來維繫自己的影響力,以及作為一家公司的營利命脈。這件事,就我們後來所知,是完全失敗的。Netscape 逃不了售出的命運(雖然換股的價碼很不錯),員工在併購之後各奔東西,微軟成功壟斷瀏覽器市場;作為故事背景的 Mozilla 自由軟體專案,直到 2004 年才以 Firefox 這個產品重新樹立影響力,如鳳凰般從灰燼中重生(Firefox 最早的產品名是 Phoenix)。

在電影院沉浸的感受是強烈的。就我去了幾次的粗淺感受,矽谷的確和 1998 年一樣,是個掏金的地方。辦公室有堆滿點心的廚房、懶骨頭沙發、熔岩燈——程式設計師可以帶著一技之長去大公司(「establishment」)享受看似成功的人生,或是到新創公司充滿夢想,拿股票選擇權,即片中 Jamie Zawinski 所稱的樂透彩票。Milestone 前的風雨欲來,朝向「Zarro Boogs」前進的感覺也是一模一樣的(連 face-palm 也一樣)。片中的 Michael Toy、Jamie Zawinski、Tara Hernandez 等人趕上了 1998 年 3 月 31 日 Milestone,然後再隨著 Netscape 的瓦解往人生下一階段前進。

我喜歡片中 Zawinski 對 Mozilla 自由軟體專案的精準描述。很欣慰的,雖然沒有成功拯救 Netscape,Mozilla 後來的確也重生了,發揮了使命與影響力(順便賞了我一個工作)。但 1998 年與 2012 年的差異真的不大。Mozilla 基金會與公司作為 Mozilla 軟體專案的主要貢獻者,必須在主導專案,維繫 Mozilla 使命的同時保持自己的影響力與金流。Mozilla 抵抗的依然是奉行封閉、獨佔的公司與其軟體生態圈。Zawinski 對網路也有精準的預測:「我們處在一個產業的起點。誰知道這個產業會怎樣發展,搞不好又會變成像電視一樣,被少數公司所掌控,決定我們能看到、聽到什麼,這有太多先例了。」

感謝 OrinX 的聯絡,今天的活動最後有一段 Tara 向參與同好的打招呼影片;她後來加入了 Pixar 動畫,在影片中也和我們展示了他們現在用的一模一樣的工具:Tinderbox、Bugzilla …。她說我們好像真的成功了,「網路已經無所不在 … 想必台灣和美國一樣,走在路上看到的廣告上面一定都有個網址」。但其實我們還沒有成功:現在在路上的廣告出現的並不是網址,而是「請上 Yahoo! 奇摩搜尋『…』」或是「請上 Facebook 『…』粉絲團」,人們偏好的是封閉平台的「體驗」與「安全」,而不是開放與自由。

最後我聽到 Irvin 在活動會場宣傳 Mozilla TaiwanFacebook 粉絲團以及線上 Code Rush 電影院活動,心中覺得相當的諷刺。沒有不敬的意思,這是當下能做把事情做好的方法。但有一天,我希望我們能不用在「對」與「好」之間選擇。

有一天。


如果有興趣的話,可以 clone mozilla-central repository,然後輸入 git log --topo-order --reverse | head,你會得到這個:

commit 781c48087175615674b38b31fcc0aae17f0651b6
Author: ltabb 
Date:   Sat Mar 28 02:44:41 1998 +0000

    Free the lizard

mozilla-central 即 Mozilla 所有程式,包含目前 Firefox 的程式碼的 repository。歷史呀。

我也寫了輸入法,而且用 Javascript!

聽完 jserv 在 COSCUP 2012 講的新酷音輸入法專案,決定來把這篇欠很久的文章寫完,順便換個標題。

在 Mozilla 工作,加入 Boot to Gecko 專案的第一個工作是處理 Gaia 的輸入法,弄了大約一兩個月,最後做出了可以在瀏覽器獨立運作(不需要從伺服器選字)的自動選字注音輸入法:

Gaia 注音輸入法「JS 注音」瀏覽器示範網頁

詞庫

詞庫要匯進程式勢必要轉成 JSON 格式。原本是從新酷音的詞庫轉出來的,後來發現 LGPL 跟專案不相符(我們用 Apache),找了很久,最後找到 mjhsieh 維護的小麥注音詞庫。至於轉檔,本來一度寫了 phantomJS 的 script 來處理,後來改用自家的 Javascript shell。比較麻煩的就是 jsshell 要用 -U 啟動不然字串不會被當成 UTF-8。

資料庫

要把 3MB 的 JSON 整包放在記憶體也不是不行,只是這樣放手機是跑不動的。另外的後果就是這樣每次網頁載入就要每次重新下載,還不能保證會不會碰到 HTTP cache。

Gecko 提供的 database 解決方案是 IndexedDB。也說不上來好用或是不好用,反正我沒得選(笑)。比起直接用 key 從 JS Object 拿資料,IndexedDB 的 async 流程當然是比較複雜,但也不會說難用(而且 Disk I/O 本來就應該要 off main-thread 才對)。比較複雜的是一些手機輸入法的進階找字功能(例如輸入「ㄊㄅ」就要查到「台北」)就需要用到 IndexedDB 的 range search 等等。

演算法

我沒有實作像是 tree search 之類的搜尋方式,而是傻傻的用窮舉法,把 N 個音可能的斷詞通通切開然後送進資料庫查詞還有問積分。和文字雲一樣,用了很簡單的演算法但是卻很有效。

剩下的部份與接下來

剩下就是一些 code organization 的問題。牽涉到這麼多 function 的程式不太容易整理好,我自己改寫了兩次,後來被北京的同事 port 成拼音輸入法的時候他又改成 prototype 的寫法,看起來又更整齊。Boot to Gecko(產品名 Firefox OS)最初的上市目標是南美洲,所以中文支援的功能就先放著,先去處理系統的部份。

我有很刻意的把注音的 JS 檔保持著可以讓其他環境與架構順利載入與 fall back 的狀況(還偷偷加了 CommonJS 的 define())。DEMO 網頁連 iPhone 都可以用,沒有 IndexedDB 的瀏覽器也會每次載入詞庫 JSON。有一些 fancy 的 idea 是像是可以用 node-gtk 或是其他方法執行,反攻桌面或是 Android 之類的。不過這記憶體消耗比別人大,演算法也沒有比別人強,要做只是證明因為做的到吧 … XD

有什麼意見還請不吝賜教。

Common misconceptions about Boot-to-Gecko, and the Web

中文版刊登於謀智台客部落格。

Being asked to introduce the Boot to Gecko project to various audiences, I think it’s appropriate for me to write some FAQ here on common misconception of the project, and the Web in general.

Misconception #1: Boot to Gecko is yet another mobile platform for apps.

To run you apps on Boot to Gecko (and any other devices with a browser), you would just have to deliver your apps on the plain old-school Web on Internet.

Boot to Gecko is aim to (re)boot to the web. That is, to bring the Open Web to a level that it could compete with proprietary mobile platforms. To application developers, the Web is yet another platform to develop (you would need to port your apps from Obj-C/Java to HTML5), but the good news is your app would now becomes the cross-platform web app, not B2G Apps, PhoneGap Apps, nor Metro-style Windows 8 Apps, and it’s always available to everyone on every platform, one web address away. It would also make your app free from platform vendor control, since no one owns the Web.

Misconception #2: Phone need to be always on-line to use Boot to Gecko and web apps.

Even though every apps on B2G phone is from a web address, even the home screen itself, offline capability can be easily achieved with HTML5 Offline AppCache technology. It’s something that can be done today, available to many browsers available on both desktop and mobile. The only thing you should take care of is how network dependent your app really is. Even on iOS, Twitter app or Facebook app is useless without network connection. You should define a clear boundary between program assets and online information in your HTML5 app. After all, it’s not just an website anymore.

Misconception #3: Web app is crappier than “native apps” in turns of UX.

This is a notion I strongly disagree. The only reason mobile browsers on devices perform worse than the native app is because the venders of the device did not invest enough effort on it. There is a conflict of interest here. Limiting the capability of APIs accessible, besides permission management issue, is the same thing. At Mozilla, we can show that with proper engineering, mobile browser, or web runtime, can run smoothly as silk on devices and deliver the so-called “native” experiences. Other venders investing the web is also doing the same thing. For devices running B2G or Chrome OS, the native app is web apps, there is no point to make it slow, intentionally or unintentionally.

Misconception #4: Web apps running on B2G is dependent on B2G or Mozilla Web APIs.

Yes and no. If you need specific access like SMS message database or phone dialing, initially B2G would be the only platform that make these features available to you. However, we design these Web APIs with standardization in mind, and work closely with standardizing bodies to made these APIs ready for other venders to implement. Evidently, our Web APIs does not live under PhoneGap.*, Windows.*, nor Ti.*; they are design to be at where it should be, like navigator.* (Sure, as experimental features, they came with moz prefix right now, and usual feature detection is advised).

If you choose other packaged web app solution, then, your web app is depend on it. Forever. Mozilla would like you to develop and distribute apps directly to the Web, and you should.

Misconception #5: Apps on Web are free of charge, developers will never make money out of it.

Yeah, like no one had ever become billionaire because of applications or services they put on the web.

There are tons of way to provide services on the web in exchange for money, surely there are ways to make money with web apps, other than bagging for US$0.99 from users. Sure, closed platform with single distribution “app store” seems effective in terms of delivering revenue to developers (besides having 30% of your money being taken along the away), but Mozilla believes choices matters, and an open platform deliver choices to both users and developers.

Mozilla is also exploring effective distribution and monetize channel for the Open Web, in our Open Web App Project and the Mozilla Marketplace website. The goal of offering choices and freedom is deeply embedded in the feature design, both in browser App API and in storefront, and with user authentication and authorization (which is the scope of the Mozilla Persona project, previously known as BrowserID).

Conclusion

Just like Mozilla did with Firefox years ago, we intend to deliver great product that could influence the market, and let the market influence other venders. No one owns the Web, and no one should. Mozilla is here not to make money or become monopoly, but to bring the goodness to the Web that could drive innovation and opportunities centuries to come.

The CTO of Opera Software, Håkon Wium Lie, once said the Web will last for at least 500 years, and made impact to the society just like Gutenberg’s printing press. I totally agree with him, even though none of us will be around and told me I was wrong, as he puts it. Let’s make it great.

Disclaimer: This post express the opinion of my own and does not necessarily represents the point of view of Mozilla nor Mozilla Corp.