出处:https://www.mobilespan.com/content/chrome-is-the-new-c-runtime

Chrome Is The New C Runtime

Date: 
Friday, January 17, 2014
Author: 
Sanjeev Radhakrishnan

Cross-platform app development is more important than ever. 10 years ago, you just whipped out your Visual Studio when you needed a client application, but not anymore. With “app-ification” going mainstream on Android, iOS, Windows, and Mac, what is a developer
to do?

Web apps are a good solution some of the time (except for that little detail called IE!).

But what about when you need the features and performance of native code, across platforms? And you’re a startup with a small team and impossible deadlines?

Well, at MobileSpan we’ve been living in this world for the past 2 years, and we want to share our approach.

We chose to build our application by integrating with the source code of Chromium.

Chromium is the open-source base of Google Chrome. My co-founder and I are Xooglers from the Chrome team, so we were very familiar with it and it was an easy choice for us. But you don't have to spend 4 years at Google to take advantage of Chrome's rich code
base.

But... I'm not building a browser!

So, why would Chrome source be useful to me for cross-platform app development? I'm not building a browser...

In reality, Chrome is much more than just a browser. Chrome code is highly tuned for performance, reliability, and cross-platform compatibility across PCs and iOS + Android devices.

Out of necessity, the Chrome team has created cross-platform abstractions for many low-level platform features. We use this source as the core API on which we build our business logic, and it's made the bulk of our app cross-platform with little effort.

Most importantly -- Chrome code has been battle-tested like almost nothing else, with an installed base in the hundreds of millions. That makes all the difference when you want to spend your days working on your company’s business logic instead of debugging
platform issues.

Basically, you can structure your code like the diagram below, where you only write your application logic, and let Chromium do the heavy lifting.

So what's in Chrome that's so great?

Well, consider the parts of a modern browser. Chrome contains high-performance, cross-platform implementations of:

  • Concurrency handling
  • Compression
  • Encryption
  • Certificate handling
  • Low-level socket interfaces
  • High-level protocol implementations like HTTP, HTTPS, FTP)
  • DNS resolution
  • Proxy handling
  • Complex disk caching
  • Cookie handling

... and more

What it lets you do is build a single, cross-platform application layer, on top of all this goodness.

Chrome code also has other unexpected, higher-level goodies like:

  • Chrome Remote Desktop,
  • a full P2P (TURN, STUN, etc) stack (used by the Chrome Remote Desktop code),
  • an XMPP client (used by Chrome Sync as well as Chrome Remote Desktop)

Where Do I Start?

First Things First: The right tools to generate your project

The first step in starting your project is to create the appropriate project file for your platform (Visual Studio, XCode etc). Chromium uses GYP to
declaratively specify files and project settings in a platform independent manner. I strongly recommend starting your project as a GYP file. GYP generates project files for each platform (Visual Studio solution and project files, XCode project files and Android
.mk files). In addition, powerful dependency options in GYP allow compiler and linker settings needed by the various Chromium project files to automatically flow into the projects that depend on them. This drastically reduces your own build headaches, as well
as problems when you update your checkout of Chrome sources.

A basketful of helpers prevent your project from becoming a basket-case.

Once you get to the meat of your app, you will find yourself needing all sorts of helper libraries for everything ranging from string manipulation, concurrency handling, synchronization, thread pools, message loops, logging, file manipulation, timers, shared
memory management, and more.

If you find yourself starting to write a generic helper class or library, search the Chromium sources first. Chances are very high that you will find just the class you want, with very good unit-test coverage to boot.

The base library in the Chromium sources (found in src/base) provides a vast array of cross-platform tools that cover all the areas mentioned above and a lot more. There are also helpers for platform-specific areas such as the Windows registry or the iOS keychain.

It has become quite the game for developers at MobileSpan to search the Chromium sources for helpers they need.

Network stack, anyone?

Unless you are building your app for Windows 3.1, chances are that you want to talk to a server of some kind. This might involve simple HTTP or HTTPS API calls or low-level socket calls or anything in between.

The net library in Chromium (src/net) is your friend here. You’ll find a full cross-platform HTTP and HTTPS stack, code for cookie handling, caching, TCP and UDP sockets and socket pools, SSL certificate handling, DNS resolution, proxy server resolution ...,
well, you get the idea, pretty much anything network related.

Encryption

Need to handle public/private keys, encrypt data store secrets? The crypto library (src/crypto) is another excellent cross-platform library that is almost sure to have the encryption or key management routine you are looking for. By now, you get the picture
of how the sources are organized.

XMPP, P2P, Protocol Buffers etc.

These aren’t things you would normally expect to find in a web browser but Chromium includes an extensive XMPP and P2P client library built on top of libjingle (look at src/jingle and src/remoting). If you use protocol buffers in your code, GYP has support
for .proto files. Just add the .proto files to your GYP file and it will do the right thing of building the protoc compiler and generate wrapper code for your protobufs. This even works for iOS projects.

Testing

Your code is only as good the unit-tests you write for them, right? Though not strictly a part of the Chrome, the GTest and GMock libraries that are part of the Chrome checkout provide an excellent framework for writing unit-tests and mocking your C++ classes.
All Chrome tests are written using these frameworks so you have a big sample codebase to get inspired by.

GYP even creates platform-appropriate containers for your tests. For example, on iOS, it will automatically create an iOS app to contain your tests so you can run them in the simulator. You just write your tests in cc files, add them to a gyp file, add the
right dependencies and viola, you have cross-platform unit-tests.

At MobileSpan, we implemented the core of our business logic in a cross-platform library that is built using Chrome. We then built our UI per platform that uses this underlying library. Porting our app to a new platform means mainly building the UI layer on
the new platform and then tying it together with the cross-platform client library.

Conclusion: Tying It Together

If I am sounding like a Chrome fan-boy, that’s because I am one. Since we embraced Chromium more than 2 years ago, we have found it to work really well for us as a dev platform, saving countless person-hours. It has allowed us to reuse some really well-written
and, more importantly, well-tested code across several client platforms, and to concentrate our efforts on making a solid product that works equally well on multiple platforms.

Items for a future post

This just scratches the surface of using Chrome source as a development platform. What should I cover in a next post?

Some options include:

  • Deep dive into specific libraries like Network, Crypto, etc.
  • Keeping up to date with Chromium sources
  • How to fork and keep your sanity (and knowing when to fork certain sources)
  • Process of checking out Chromium sources
  • More details on cross-platform UI development

Have you poked at Chrome's codebase or used it to build a cool product? Tell us about it in the comments section.

Sanjeev Radhakrishnan

Co-Founder and CTO, Sanjeev leads technology development and architecture at MobileSpan. He brings many years of experience from working on Google Chrome's security and sandboxing.

    googleplus

Chrome Is The New C Runtime的更多相关文章

  1. Chrome 控制台报错Unchecked runtime.lastError: The message port closed before a response was received

    Chrome浏览器控制台报错提示 Unchecked runtime.lastError: The message port closed before a response was received ...

  2. Chrome报错提示Unchecked runtime.lastError: The message port closed before a response was received.

    经过查询,此错误是Chrome扩展插件引起的.由于Chrome修改了API接口,原来的请求被拦截.(Chrome 73 onwards disallows cross-origin requests ...

  3. chrome插件报错原因

    Chrome报错提示Unchecked runtime.lastError: The message port closed before a response was received. 出错原因: ...

  4. 那些用JavaScript写的操作系统

    之前有人说过Chrome是新的C语言运行环境(Chrome Is The New C Runtime) ,不过笔者更倾向于Web是新的C语言运行环境,而且这种技术绝对没有版权问题,也绝不会被一家公司垄 ...

  5. 动手做第一个Chrome插件

    Chrome插件是令人惊讶的简单,一旦你弄懂它的工作和实现原理.它是由一部分HTML,一部分Js,然后混合了一个叫做manifest.json的Json文件组合而成的整体.这意味着你可以使用你最擅长的 ...

  6. chrome拓展开发实战:页面脚本的拦截注入

    原文请访问个人博客:chrome拓展开发实战:页面脚本的拦截注入 目前公司产品的无线站点已经实现了业务平台组件化,所有业务组件的转场都是通过路由来完成,而各个模块是通过requirejs进行统一管理, ...

  7. Chrome 扩展机制

    据说,今年9月份开始,谷歌将在Chrome浏览器中全面禁用NPAPI插件,Chrome 45以后将无法再加载NPAPI插件,并推出了一种新的机制:扩展. 其实,如果把浏览器看作一块画布的话,NPAPI ...

  8. selenium启动Chrome浏览器和禁止证书错误提示弹出

    要把ChromeDriver放到代码中的文件夹中c://*******Application public static WebDriver WebDriverRun(WebDriver driver ...

  9. Webdriver设置Chrome属性

    1. ChromeDriver加载插件 File file = new File ("files\\youtube.crx"); ChromeOptions options = n ...

随机推荐

  1. C语言提高 (7) 第七天 回调函数 预处理函数DEBUG 动态链接库

    链表就是一个结构体 指针指向自身结构体类型 双向链表插入的时候 先改变自身 再改变两边 双向链表删除的时候 先改变两边 再改变自己 逆序一个单向链表 回调函数 指向函数的指针 4链表的遍历回调用法 / ...

  2. 免费ftp服务器FileZilla Server配置

    FileZilla Server下载安装完成后,必须启动软件进行设置,由于此软件是英文,本来就是一款陌生的软件,再加上英文,配置难度可想而知,小编从网上找到一篇非常详细的教程进行整理了一番,确保读到这 ...

  3. jQuery练习总结(一)

    第一个jQuery程序: 注意:使用jQuery时候定义单击事件使用的方法是click(function(){处理单击事件产生时所要执行的代码}) , 而JavaScript定义的单击事件则是 xx. ...

  4. Strtus配置Tomcat出现问题

    在使用 eclipse 过程中,如果你加入了某些自定义的框架(比如 struts)后,在启动 services 后, 控制台出现一片红色的字样,表明有问题.这时仔细查看 eclipse 控制台信息, ...

  5. BA-siemens-BA模块特性

    PXC24(包含UEC24的模块特性) DO点可以接220vac的电压,渠道人员告知电流不要超过2A AO点只能输出0-10V的电压,不能输出4-20ma的电流,说明书上是错误的 AO点输出10v失败 ...

  6. js代码从页面移植到文件里失效或js代码改动后不起作用的解决的方法

    近期在做关于站点的项目,总是发生这种问题 写的javascript代码在页面上没有问题,可是将js代码移植到.js的文件里,在页面上进行调用,总是出现失效等错误 另外改动后的js代码,又一次刷新网页仍 ...

  7. Qt on Android:资源文件系统qrc与assets

    使用 Qt 为 Android 开发应用时,有时我们的应用会携带一些资源文件,如 png . jpg 等,也可能有一些配置文件,如 xml 等.这些文件放在哪里呢? 有两种方式: qrc assets ...

  8. FetchType与FetchMode的差别

    使用例: @OneToMany(mappedBy="item",cascade=CascadeType.ALL,fetch=FetchType.EAGER) @Fetch(valu ...

  9. 开源ext2read代码走读之-在windows下怎样推断有几个硬盘设备?

    int get_ndisks() {     HANDLE hDevice;               // handle to the drive to be examined     int n ...

  10. 2016.03.27,英语,《Vocabulary Builder》Unit 06

    equ: from Latin aequus, meaning 'equal', equalize:使相等; equivalent:[ɪ'kwɪvələnt], A is equivalent to ...