1. 程式人生 > >谷歌瀏覽器的源碼分析 10

谷歌瀏覽器的源碼分析 10

ner const sele ali max 為什麽 分享 Edito history

分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!http://www.captainbed.net

上一次已經分析到輸入字符後,就需要把這些關鍵字去查找歷史的連接,或者相關的內容,那麽可多米的瀏覽器又是從那裏去找到這些數據呢?現在就來分析這方面相關的內容。它主要通下面的函數來實現:

#001 void AutocompleteEdit::UpdatePopup() {

凍結輸入。

#002 ScopedFreeze freeze(this, GetTextObjectModel());

設置正在輸入過程中。

#003 SetInputInProgress(true);

#004

如果輸入的EDIT框沒有焦點,就直接返回。

#005 if (!has_focus_) {

#006 // When we‘re in the midst of losing focus, don‘t rerun autocomplete. This

#007 // can happen when losing focus causes the IME to cancel/finalize a

#008 // composition.

We still want to note that user input is in progress, we

#009 // just don‘t want to do anything else.

#010 //

#011 // Note that in this case the ScopedFreeze above was unnecessary; however,

#012 // we‘re inside the callstack of OnKillFocus(), which has already frozen the

#013

// edit, so this will never result in an unnecessary UpdateWindow() call.

#014 return;

#015 }

#016

#017 // Figure out whether the user is trying to compose something in an IME.

判斷是否從輸入法打開,如果是就從輸入法窗口裏獲取字符串。

#018 bool ime_composing = false;

#019 HIMC context = ImmGetContext(m_hWnd);

#020 if (context) {

#021 ime_composing = !!ImmGetCompositionString(context, GCS_COMPSTR, NULL, 0);

#022 ImmReleaseContext(m_hWnd, context);

#023 }

#024

#025 // Don‘t inline autocomplete when:

#026 // * The user is deleting text

#027 // * The caret/selection isn‘t at the end of the text

#028 // * The user has just pasted in something that replaced all the text

#029 // * The user is trying to compose something in an IME

獲取當前選擇的字符串。

#030 CHARRANGE sel;

#031 GetSel(sel);

根據用戶輸入的字符串來查找智能提示菜單的內容。

#032 popup_->StartAutocomplete(user_text_, GetDesiredTLD(),

#033 just_deleted_text_ || (sel.cpMax < GetTextLength()) ||

#034 (paste_state_ != NONE) || ime_composing);

#035 }

在這個函數裏主要調用類AutocompletePopupModel的函數StartAutocomplete來完成智能提示。而類AutocompletePopupModel的聲明如下:

class AutocompletePopupModel : public ACControllerListener, public Task {

public:

AutocompletePopupModel(const ChromeFont& font,

AutocompleteEdit* editor,

Profile* profile);

~AutocompletePopupModel();

從這個類裏可以看到它是繼承類ACControllerListener,說明它是響應一個返回結果的監聽器;繼承類Task說明它是一個任務線程類。由這兩個類可以看出,它是把關鍵字給一個線程,然後讓這個線程去查詢結果,當結果返回時,就再更新到顯示窗口裏。

雖然上面理解它的查詢過程了,但是向誰查詢呢?這是一個一定需要了解的問題。現在就來分析類AutocompleteController,它在構造函數時,就會創建三個查詢的對象:

#001 AutocompleteController::AutocompleteController(ACControllerListener* listener,

#002 Profile* profile)

#003 : listener_(listener) {

#004 providers_.push_back(new SearchProvider(this, profile));

#005 providers_.push_back(new HistoryURLProvider(this, profile));

#006 keyword_provider_ = new KeywordProvider(this, profile);

#007 providers_.push_back(keyword_provider_);

#008 if (listener) {

#009 // These providers are async-only, so there‘s no need to create them when

#010 // we‘ll only be doing synchronous queries.

#011 history_contents_provider_ = new HistoryContentsProvider(this, profile);

#012 providers_.push_back(history_contents_provider_);

#013 } else {

#014 history_contents_provider_ = NULL;

#015 }

#016 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); ++i)

#017 (*i)->AddRef();

#018 }

從上面的代碼,可以看到它是向SearchProviderHistoryURLProviderKeywordProviderHistoryContentsProvider來查找到合適的智能提示。類SearchProvider是從搜索引擎裏查找合適的內容;類HistoryURLProvider是從歷史的URL裏查找合適的內容;類KeywordProvider是從關鍵字搜索引擎裏查找合適的內容;類HistoryContentsProvider是從歷史內容裏查找合適內容。從上面四種智能提示裏,在以前的瀏覽器裏一般只能做到從歷史的URL裏提示,現在“可多米”可以做到從搜索引擎和關鍵字引擎裏查找到相應的結果回來,可見它是智能提示完美的體現,智能的水平可想而知了。這就是強大的雲計算典型應用,如果沒有強大的服務器群是做不到幾億人輸入關鍵字時,還能快速返回結果的。

分析到這裏,也許知道為什麽GOOGLE開發瀏覽器的原因了吧,如果其它瀏覽是不可能采用這樣的技術來分析用戶的輸入的,頂多是到歷史記錄裏查找一下就算了。

雖然提供這麽強大的搜索,它們又是怎麽樣實現的呢?下一次再來分析它們。

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!http://www.captainbed.net

谷歌瀏覽器的源碼分析 10