日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

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

發布時間:2025/3/21 HTML 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 谷歌chrome浏览器的源码分析(三) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上一次介紹到怎么樣從其它地方返回搜索到的超級連接,現在就來分析一下使用搜索引擎去查找的類SearchProvider,它是通過搜索引擎來查找出來的,在這里是通過GOOGLE搜索引擎來查找出來。它的聲明如下:

#001??// Autocomplete provider for searches and suggestions from a search engine.

#002??//

#003??// After construction, the autocomplete controller repeatedly calls Start()

#004??// with some user input, each time expecting to receive a small set of the best

#005??// matches (either synchronously or asynchronously).

#006??//

#007??// Initially the provider creates a match that searches for the current input

#008??// text.??It also starts a task to query the Suggest servers.??When that data

#009??// comes back, the provider creates and returns matches for the best

#010??// suggestions.

?

SearchProvider類是繼承AutocompleteProvider和URLFetcher類,AutocompleteProvider提供一個自動完成的結果,URLFetcher主要提供從URL獲取數據和狀態。

#011??class SearchProvider : public AutocompleteProvider,

#012?????????????????????????public URLFetcher::Delegate {

#013???public:

#014????SearchProvider(ACProviderListener* listener, Profile* profile)

#015????????: AutocompleteProvider(listener, profile, "Search"),

#016??????????last_default_provider_(NULL),

#017??????????fetcher_(NULL),

#018??????????history_request_pending_(false),

#019??????????have_history_results_(false),

#020??????????suggest_results_pending_(false),

#021??????????have_suggest_results_(false) {

#022????}

#023?

?

?

開始獲取。

#024????// AutocompleteProvider

#025????virtual void Start(const AutocompleteInput& input,

#026???????????????????????bool minimal_changes,

#027???????????????????????bool synchronous_only);

?

停止查找。

#028????virtual void Stop();

#029?

?

當獲取到數據回來時響應。

#030????// URLFetcher::Delegate

#031????virtual void OnURLFetchComplete(const URLFetcher* source,

#032????????????????????????????????????const GURL& url,

#033????????????????????????????????????const URLRequestStatus& status,

#034????????????????????????????????????int response_code,

#035????????????????????????????????????const ResponseCookies& cookies,

#036????????????????????????????????????const std::string& data);

#037?

#038???private:

#039????struct NavigationResult {

#040??????NavigationResult(const std::wstring& url, const std::wstring& site_name)

#041??????????: url(url),

#042????????????site_name(site_name) {

#043??????}

#044?

#045??????// The URL.

#046??????std::wstring url;

#047?

#048??????// Name for the site.

#049??????std::wstring site_name;

#050????};

#051?

?

保存返回的結果。

#052????typedef std::vector<std::wstring> SuggestResults;

#053????typedef std::vector<NavigationResult> NavigationResults;

#054????typedef std::vector<history::KeywordSearchTermVisit> HistoryResults;

#055????typedef std::map<std::wstring, AutocompleteMatch> MatchMap;

#056?

?

運行獲取搜索引擎數據。

#057????// Called when timer_ expires.

#058????void Run();

#059?

#060????// Determines whether an asynchronous subcomponent query should run for the

#061????// current input.??If so, starts it if necessary; otherwise stops it.

#062????// NOTE: These functions do not update |done_|.??Callers must do so.

#063????void StartOrStopHistoryQuery(bool minimal_changes, bool synchronous_only);

#064????void StartOrStopSuggestQuery(bool minimal_changes, bool synchronous_only);

#065?

#066????// Functions to stop the separate asynchronous subcomponents.

#067????// NOTE: These functions do not update |done_|.??Callers must do so.

#068????void StopHistory();

#069????void StopSuggest();

#070?

#071????// Called back by the history system to return searches that begin with the

#072????// input text.

#073????void OnGotMostRecentKeywordSearchTerms(

#074????????CancelableRequestProvider::Handle handle,

#075????????HistoryResults* results);

#076?

#077????// Parses the results from the Suggest server and stores up to kMaxMatches of

#078????// them in server_results_.??Returns whether parsing succeeded.

#079????bool ParseSuggestResults(Value* root_val);

#080?

#081????// Converts the parsed server results in server_results_ to a set of

#082????// AutocompleteMatches and adds them to |matches_|.??This also sets |done_|

#083????// correctly.

#084????void ConvertResultsToAutocompleteMatches();

#085?

#086????// Determines the relevance for a particular match.??We use different scoring

#087????// algorithms for the different types of matches.

#088????int CalculateRelevanceForWhatYouTyped() const;

#089????// |time| is the time at which this query was last seen.

#090????int CalculateRelevanceForHistory(const Time& time) const;

#091????// |suggestion_value| is which suggestion this is in the list returned from

#092????// the server; the best suggestion is suggestion number 0.

#093????int CalculateRelevanceForSuggestion(size_t suggestion_value) const;

#094????// |suggestion_value| is same as above.

#095????int CalculateRelevanceForNavigation(size_t suggestion_value) const;

#096?

#097????// Creates an AutocompleteMatch for "Search <engine> for |query_string|" with

#098????// the supplied relevance.??Adds this match to |map|; if such a match already

#099????// exists, whichever one has lower relevance is eliminated.

#100????void AddMatchToMap(const std::wstring& query_string,

#101???????????????????????int relevance,

#102???????????????????????int accepted_suggestion,

#103???????????????????????MatchMap* map);

#104????// Returns an AutocompleteMatch for a navigational suggestion.

#105????AutocompleteMatch NavigationToMatch(const NavigationResult& query_string,

#106????????????????????????????????????????int relevance);

#107?

#108????// Trims "http:" and up to two subsequent slashes from |url|.??Returns the

#109????// number of characters that were trimmed.

#110????// TODO(kochi): this is duplicate from history_autocomplete

#111????static size_t TrimHttpPrefix(std::wstring* url);

#112?

#113????// Don't send any queries to the server until some time has elapsed after

#114????// the last keypress, to avoid flooding the server with requests we are

#115????// likely to end up throwing away anyway.

#116????static const int kQueryDelayMs;

#117?

#118????// The user's input.

#119????AutocompleteInput input_;

#120?

#121????TemplateURL default_provider_;??// Cached across the life of a query so we

#122????????????????????????????????????// behave consistently even if the user

#123????????????????????????????????????// changes their default while the query is

#124????????????????????????????????????// running.

#125????const TemplateURL* last_default_provider_;

#126????????????????????????????????????// TODO(pkasting): http://b/1162970??We

#127????????????????????????????????????// shouldn't need this.

#128?

#129????// An object we can use to cancel history requests.

#130????CancelableRequestConsumer history_request_consumer_;

#131?

#132????// Searches in the user's history that begin with the input text.

#133????HistoryResults history_results_;

#134?

#135????// Whether history_results_ is valid (so we can tell invalid apart from

#136????// empty).

#137????bool have_history_results_;

#138?

#139????// Whether we are waiting for a history request to finish.

#140????bool history_request_pending_;

#141?

#142????// True if we're expecting suggest results that haven't yet arrived.??This

#143????// could be because either |timer_| or |fetcher| is still running (see below).

#144????bool suggest_results_pending_;

#145?

#146????// A timer to start a query to the suggest server after the user has stopped

#147????// typing for long enough.

#148????base::OneShotTimer<SearchProvider> timer_;

#149?

#150????// The fetcher that retrieves suggest results from the server.

#151????scoped_ptr<URLFetcher> fetcher_;

#152?

#153????// Suggestions returned by the Suggest server for the input text.

#154????SuggestResults suggest_results_;

#155?

#156????// Navigational suggestions returned by the server.

#157????NavigationResults navigation_results_;

#158?

#159????// Whether suggest_results_ is valid.

#160????bool have_suggest_results_;

#161?

#162????DISALLOW_EVIL_CONSTRUCTORS(SearchProvider);

#163??};

#164?

?

在這個類里先調用函數SearchProvider::Start來獲取缺省的搜索引擎,然后停止以前的搜索,接著SearchProvider::Run()函數里使用URLFetcher獲取數據回來,它的代碼如下:

#001???void SearchProvider::Run() {

#002????// Start a new request with the current input.

#003????DCHECK(!done_);

?

獲取搜索的URL。

#004????const TemplateURLRef* const suggestions_url =

#005????????default_provider_.suggestions_url();

?

建議代替的字符。

#006????DCHECK(suggestions_url->SupportsReplacement());

?

開始新的搜索。

#007????fetcher_.reset(new URLFetcher(GURL(suggestions_url->ReplaceSearchTerms(

#008????????default_provider_, input_.text(),

#009????????TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())),

#010????????URLFetcher::GET, this));

#011????fetcher_->set_request_context(profile_->GetRequestContext());

#012????fetcher_->Start();

#013??}

?

當前上面的搜索完成時,就會通知SearchProvider::OnURLFetchComplete函數來分析返回的結果,最后調用SearchProvider::ConvertResultsToAutocompleteMatches()函數來把結果轉換自動完成的列表項。

?

通過上面的分析,就了解通過GOOGLE搜索引擎自動完成功能的實現。





當我們根據自動提示,或者完全自己輸入連接后,再按下回車鍵,這時瀏覽器就會去打開網頁,或者去查找內容。那么瀏覽器是怎么樣實現這些功能的呢?又是怎么判斷去打開網頁,還是去打開GOOGLE的搜索引擎的呢?下面就來分析這部份的代碼,如下:

#001??bool AutocompleteEdit::OnKeyDownOnlyWritable(TCHAR key,

#002???????????????????????????????????????????????UINT repeat_count,

#003???????????????????????????????????????????????UINT flags) {

#004????// NOTE: Annoyingly, ctrl-alt-<key> generates WM_KEYDOWN rather than

#005????// WM_SYSKEYDOWN, so we need to check (flags & KF_ALTDOWN) in various places

#006????// in this function even with a WM_SYSKEYDOWN handler.

#007?

#008????int count = repeat_count;

#009????switch (key) {

#010??????case VK_RETURN:

#011????????AcceptInput((flags & KF_ALTDOWN) ? NEW_FOREGROUND_TAB : CURRENT_TAB,

#012????????????????????false);

#013????????return true;

#014?

#015??????case VK_UP:

#016????????count = -count;

#017????????// FALL THROUGH

#018??????case VK_DOWN:

?

當用戶按下回車鍵,就會調用上面的函數OnKeyDownOnlyWritable,并且在VK_RETURN按鍵處理回車的事件,接著就是調用函數AcceptInput來處理。

這個函數的代碼如下:

#001??void AutocompleteEdit::AcceptInput(WindowOpenDisposition disposition,

#002?????????????????????????????????????bool for_drop) {

#003????// Get the URL and transition type for the selected entry.

#004????PageTransition::Type transition;

#005????bool is_history_what_you_typed_match;

#006????std::wstring alternate_nav_url;

?

保存當前獲取的URL連接串。

#007????const std::wstring url(GetURLForCurrentText(&transition,

#008???????????????????????????????????????????????&is_history_what_you_typed_match,

#009???????????????????????????????????????????????&alternate_nav_url));

#010????if (url.empty())

#011??????return;

#012?

?

?

判斷是否重新加載當前的網頁。

#013????if (url == permanent_text_) {

#014??????// When the user hit enter on the existing permanent URL, treat it like a

#015??????// reload for scoring purposes.??We could detect this by just checking

#016??????// user_input_in_progress_, but it seems better to treat "edits" that end

#017??????// up leaving the URL unchanged (e.g. deleting the last character and then

#018??????// retyping it) as reloads too.

#019??????transition = PageTransition::RELOAD;

#020????} else if (for_drop || ((paste_state_ != NONE) &&

#021????????????????????????????is_history_what_you_typed_match)) {

?

下面是打開一個新的連接。

#022??????// When the user pasted in a URL and hit enter, score it like a link click

#023??????// rather than a normal typed URL, so it doesn't get inline autocompleted

#024??????// as aggressively later.

#025??????transition = PageTransition::LINK;

#026????}

#027?

?

這里是調用OpenURL函數打開這個連接的內容。

#028????OpenURL(url, disposition, transition, alternate_nav_url,

#029????????????AutocompletePopupModel::kNoMatch,

#030????????????is_keyword_hint_ ? std::wstring() : keyword_);

#031??}

?

這段代碼的流程很清楚,就是先通過判斷按鍵輸入,是否按下回車鍵,如果是回車鍵就調用函數AcceptInput處理,然后在這個函數就判斷這個連接是否已經打開了,如果已經打開,只需要重新加載就行了,如果不是當前的,就是打開一個新的連接。下一次再來分析OpenURL函數是怎么樣通過連接來重新加載,還是打開一個新網頁。







上一次說到調用函數OpenURL來打開網絡連接,這僅是網絡瀏覽的開始,現在再來分析它怎么樣去下載網頁數據,然后再顯示出來。

#001??void AutocompleteEdit::OpenURL(const std::wstring& url,

#002????????????????????????????????WindowOpenDisposition disposition,

#003????????????????????????????????PageTransition::Type transition,

#004?????????????????????????????????const std::wstring& alternate_nav_url,

#005?????????????????????????????????size_t selected_line,

#006?????????????????????????????????const std::wstring& keyword) {

#007????if (url.empty())

#008??????return;

#009?

#010????ScopedFreeze freeze(this, GetTextObjectModel());

#011????SendOpenNotification(selected_line, keyword);

#012?

#013????if (disposition != NEW_BACKGROUND_TAB)

#014??????RevertAll();??// Revert the box to its unedited state

#015????controller_->OnAutocompleteAccept(url, disposition, transition,

#016?????????????????????????????????????alternate_nav_url);

#017??}

?

在這個函數里第一個參數url是要打開的網絡連接;第二個參數disposition是顯示位置,比如新添加一個TAB顯示,還是在原來的TAB顯示;第三個參數transition是下載的類型,比如是重新加載,還是新的連接下載;第四個參數alternate_nav_url是候選的連接;第五個參數是選擇那一行提示菜單;第六個參數keyword是關鍵字。

?

第7行判斷打開的連接是否為空,如果為空就不用打開連接了。

第10行鎖定輸入框。

第11行通知選中的關鍵字。

第13行,14行關閉當前輸入提示,直接在當前窗口顯示,清除一些狀態和內容。

第15行調控制實例來打開輸入的連接。

?

上面的代碼在AutocompleteEdit類里處理完成了,然后就把打開網絡連接放到別的類里來執行,這樣可以提高代碼的復用性,降低了代碼復雜程度。那么這個controller_是何方神圣呢?一看AutocompleteEdit類有Controller類的定義,肯定就是它了,但再仔細一看它,它只是一個接口類,所有函數都是純虛函數,真是百思不得其解時,突然想起,既然它是接口類,肯定就有其它類繼承它的,那么再通過搜索,一查看,果然找到一個繼承它的類LocationBarView,接著查看它的聲明,如下:

#001?/

#002??//

#003??// LocationBarView class

#004??//

#005??//???The LocationBarView class is a View subclass that paints the background

#006??//???of the URL bar strip and contains its content.

#007??//

#008?/

#009??class LocationBarView : public ChromeViews::View,

#010??????????????????????????public AutocompleteEdit::Controller {

#011???public:

?

可見類LocationBarView是繼承ChromeViews::View類,并且繼承AutocompleteEdit::Controller類。說明它是一個窗口類,并且是控制類,那么就是說所有接口的功能都是在這個類里實現的,只需要分析這個類里的內容,就知道它是怎么樣打開連接的了,下一次再來分析它。






上一次說到控制類的派生類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的實例,因此命令就是發送給瀏覽器對象來處理,它是怎么樣處理命令的呢?下一次再來分析。






上一次說到發送命令給瀏覽器對象打開網頁顯示,但還沒有分析它是怎么實現的,現在就來分析這方面的內容,如下:

#001??void Browser::ExecuteCommand(int id) {

#002????if (!IsCommandEnabled(id)) {

#003??????NOTREACHED() << id;

#004??????return;

#005????}

#006????// This might happen during QMU testing.

#007????if (!GetSelectedTabContents())

#008??????return;

#009?

#010????switch (id) {

#011??????case IDC_BACK:

#012????????UserMetrics::RecordAction(L"Back", profile_);

#013????????GoBack();

#014????????break;

#015???????

#016????????...

#017???????

#018???????

#019?????????case IDC_OPENURL:

#020????????UserMetrics::RecordAction(L"LoadURL", profile_);

#021????????{

#022??????????LocationBarView* lbv = GetLocationBarView();

#023??????????if (lbv) {

#024????????????OpenURL(GURL(lbv->location_input()), lbv->disposition(),

#025????????????????????lbv->transition());

#026??????????} else {

#027????????????OpenURL(GURL(), CURRENT_TAB, PageTransition::TYPED);

#028??????????}

#029????????}

#030????????break;

#031?

?

可以看到這段代碼,第19行就是處理打開網頁的命令處理,當然在這個函數里不僅僅處理打開網頁的連接命令,還有很多其它的命令,目前先分析這個命令的代碼。

第20行里先記錄當前執行的動作。

第22行里查找到當前顯示BAR窗口。

如果找到LocationBarView窗口,就把當前的輸入連接生成GURL對象,從窗口獲取顯示位置,傳送的類型。否則,就是使用空的連接,并打開當前頁面。

?

下面再來分析函數OpenURL的實現,它是調用TabContentsDelegate類里的OpenURL函數:

#001????virtual void OpenURL(const GURL& url,

#002?????????????????????????WindowOpenDisposition disposition,

#003?????????????????????????PageTransition::Type transition) {

#004??????OpenURLFromTab(NULL, url, disposition, transition, std::string());

#005????}

?

在這個函數繼續調用OpenURLFromTab函數來實現打開網頁的功能,這個函數比較復雜,下一次再來分析它。



from:?http://blog.csdn.net/caimouse/article/details/2999022

總結

以上是生活随笔為你收集整理的谷歌chrome浏览器的源码分析(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。