上一次說到控制類的派生類
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
的實例,因此命令就是發送給瀏覽器對象來處理,它是怎么樣處理命令的呢?下一次再來分析。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
