您的位置:首页 > 其它

谷歌浏览器的源码分析(14)

2008-09-26 22:09 246 查看
上一次说到控制类的派生类LocationBarView,现在就来分析这个函数的功能,看看它又把URL连接传到那里去,立即就去看代码,在这行代码controller_->OnAutocompleteAccept里,可以看到调用函数OnAutocompleteAccept,它的代码如下:
#001 void LocationBarView::OnAutocompleteAccept(
#002 const std::wstring& url,
#003 WindowOpenDisposition disposition,
#004 PageTransition::Type transition,
#005 const std::wstring& alternate_nav_url) {

判断输入的URL连接是否为空。
#006 if (url.empty())
#007 return;
#008

保存相应的参数。
#009 location_input_ = url;
#010 disposition_ = disposition;
#011 transition_ = transition;
#012

调用控制器controller_来打开这个连接。
#013 if (controller_) {
#014 if (alternate_nav_url.empty()) {
#015 controller_->ExecuteCommand(IDC_OPENURL);
#016 return;
#017 }
#018

打开候选的连接。
#019 scoped_ptr<AlternateNavURLFetcher> fetcher(
#020 new AlternateNavURLFetcher(alternate_nav_url));
#021 // The AlternateNavURLFetcher will listen for the pending navigation
#022 // notification that will be issued as a result of the "open URL." It
#023 // will automatically install itself into that navigation controller.
#024 controller_->ExecuteCommand(IDC_OPENURL);
#025 if (fetcher->state() == AlternateNavURLFetcher::NOT_STARTED) {
#026 // I'm not sure this should be reachable, but I'm not also sure enough
#027 // that it shouldn't to stick in a NOTREACHED(). In any case, this is
#028 // harmless; we can simply let the fetcher get deleted here and it will
#029 // clean itself up properly.
#030 } else {
#031 fetcher.release(); // The navigation controller will delete the fetcher.
#032 }
#033 }
#034 }

上面的代码主要保存传入来的参数,然后紧接着又调用了控制器controller_的函数ExecuteCommand来执行命令,这个命令是IDC_OPENURL。为什么要使用命令的方式呢?仔细地思考一下,原来这种方式是便于使用自动化测试,测试时可以自动使用程序来不断传入命令来执行。

我们再来分析这行代码:
controller_->ExecuteCommand(IDC_OPENURL);
controller_是类CommandController的实例,它主要是由MVC设计模式的控制类,可见这里可以学习怎么样把MVC设计模式应用到实际例子里,使用这种模式主要是分离面渲染、逻辑控制和不同的数据来源,这样方便维护代码。

其实所有的命令并不是CommandController来处理,它只是一个中传站,把命令发往不同的浏览器对象,如下面的代码:
#001 void CommandController::ExecuteCommand(int id) {
#002 if (IsCommandEnabled(id))
#003 handler_->ExecuteCommand(id);
#004 }

这样就把命令发送到handler_处理了,而这里的handler_是什么呢?其实它就是浏览器对象类Browser的实例,因此命令就是发送给浏览器对象来处理,它是怎么样处理命令的呢?下一次再来分析。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: