当我们通过TAB栏上的"+"标签增加一个新的空TAB时,Chrome内部的代码是怎么样的呢?这篇文章将会介绍它内部的流程。
首先用一个callstack截图来看下它的前一段处理流程:
TAB旁边的那个“+”(注意不是"New Tab (Ctrl+T)")菜单,在Chrome中是一个button。这个button是放置在TabStrip 视图类中的。TabStrip类捕捉到了这个button pressed事件,立即响应,调用TabStripModel实例的AddBlankTab函数(前面文章讲到过TabStrip维护一个TabStripModel的指针,同时TabStrip视图也是TabStripModel数据类的观察者)。看上去很简单。
TabStripModel::AddBlankTab函数实现也很简单,创建一个TabContents实例,然后将其添加到TabContentsData列表中进行管理。
TabContents* TabStripModel::AddBlankTab(bool foreground) {
TabContents* contents = delegate_->CreateTabContentsForURL(
delegate_->GetBlankTabURL(), GURL(), profile_, PageTransition::TYPED,
false, NULL);
AddTabContents(contents, -1, PageTransition::TYPED, foreground);
return contents;
}
上面的delegate_是Browser实例。Browser才是真正干活的人。根据content类型(Web/DOMUI/,etc)创建一个具体的TabContents实例。注意此时传入的instance为NULL,表明这是个空的site instance,不会进行导航。而且def_load为false。
TabContents* contents = TabContents::CreateWithType(type, profile, instance);
contents->SetupController(profile);
if (!defer_load) {
// Load the initial URL before adding the new tab contents to the tab strip
// so that the tab contents has navigation state.
contents->controller()->LoadURL(url, referrer, transition);
}
TabContents::CreateWithType做了很多很多的事情。因为WebContents是一个很复杂的类,看看它的构造函数就知道了。
WebContents::WebContents(Profile* pro