Search Bar On Browser_2

Search Bar On Browser

摘要: 本文介绍主流浏览器的搜索引擎插件标准。本文还解释如何在浏览器的添加搜索引擎插件

 

本文内容:

简介

一、       Sherlock标准

二、       OpenSearch标准

三、       MozSearch标准

四、       .ini配置文件

五、       如何添加搜索引擎

 

简介

现今主流的浏览器基本都支持在搜索栏上添加自定义的搜索引擎。当然,不同的浏览器采用的搜索引擎添加方式不同,下面就我所知道的4种方式逐一作一介绍。

             OpenSearch标准(www.opensearch.org)

OpenSearch支持Mozilla Firefox 2.0以上版本及Internet Explorer 7.0.

 

Firefox 2 supports the OpenSearch description format for search plugins. Plugins that use the OpenSearch description syntax are compatible with IE 7 and Firefox. Because of this, they are the recommended format for use on the web.

 

Firefox also supports additional search capabilities not included in the OpenSearch description syntax, such as search suggestions and the SearchForm element. This article will focus on creating OpenSearch-compatible search plugins that support these additional Firefox-specific features.

 

OpenSearch description files can also be advertised as described in Autodiscovery of search plugins, and can be installed programmatically as described in Adding search engines from web pages.

 

开放式搜索描述文件,

参见http://developer.mozilla.org/en/docs/Creating_OpenSearch_plugins_for_Firefox

XML文件描述一个搜索引擎实际上是很简单的,按照下面基本的模版,斜体字部分需要用户根据用户特定搜索插件的需要自定义。

<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
   
                       xmlns:moz="http://www.mozilla.org/2006/browser/search/">
   
<ShortName>engineName</ShortName>
   
<Description>engineDescription</Description>
   
<InputEncoding>inputEncoding</InputEncoding>
   
<Image width="16" height="16">data:image/x-icon;base64,imageData</Image>
   
<Url type="text/html" method="method" template="searchURL">
   
  <Param name="paramName1" value="paramName1"/>
   
  ...
   
  <Param name="paramNameN" value="paramValueN"/>
   
</Url>
   
<Url type="application/x-suggestions+json" template="suggestionURL"/>
   
<moz:SearchForm>searchFormURL</moz:SearchForm>
   
</OpenSearchDescription>
   

1

ShortName

搜索引擎的短名

 

2

Description

搜索引擎的简要描述

 

3

InputEncoding

对输入搜索引擎的数据进行编码

 

4

Image

基于64位编码16X16大小的搜索引擎标志图片。

 

5

URL

描述搜索使用的URL,方法取决于是否使用GETPOST请求来获取结果。模版显示了查询的基本URL

注意,IE7部支持POST请求。

你可以指定2URL形式:

type="text/html" is used to specify the URL for the actual search query itself.

type="application/x-suggestions+json" is used to specify the URL to use for fetching search suggestions.

 

任何一种形式的URL,都可以用于替代用户在搜索条中输入的搜索条件。其他支持的动态查询参数的描述见OpenSearch 1.1 parameters. 参见http://www.opensearch.org/Specifications/OpenSearch/1.1/Draft_3#OpenSearch_1.1_parameters

 

怎样在一台服务器执行查询支持,详细的参见Supporting search suggestions in search plugins. http://developer.mozilla.org/en/docs/Supporting_search_suggestions_in_search_plugins

 

6

Param

参数需要和查询一起,形成一对如key/value ,当指定值的时候 ,可以将用户在搜索栏中输入的插入到搜索条件中

注意,IE7不支持此元素

 

7

SearchForm

URL通过插件的搜索打开网站中的搜索页面,这样为Firefox提供了一种直接访问网站的方法。

注意,这个元素为Firefox特定的,不是OpenSearch说明书中的一部分,我们在上面的例子中使用”moz:”作为XML命名空间前缀来保证其他用户代理不支持这个元素安全的忽略它。

 

8

Autodiscovery of search plugins

一个网站提供一个搜索引擎并宣传它,这样Firefox的用户很容易下载和安装此插件。为了支持自动发现,你只需简单地在网站页面增加一行<HEAD>部分:

<link rel="search" type="application/opensearchdescription+xml" title="searchTitle" href="pluginURL">
   

斜体字说明如下:

searchTitle

搜索的执行名称,比如”Search MDC” 或是”Yahoo!Search”。这个值对应于你插件文件的ShortName.

 

PluginURL

XML搜索插件的URL,任何浏览器都可以下载它。

如果你的网站提供多种搜索插件,你可以使他们支持自动发现。例如:

<link rel="search" type="application/opensearchdescription+xml" title="MySite: By Author" href="http://www.mysite.com/mysiteauthor.xml">
   
<link rel="search" type="application/opensearchdescription+xml" title="MySite: By Title" href="http://www.mysite.com/mysitetitle.xml">
   

这样,你的站点提供的插件不仅是作者搜索而且可以为单独实体的标题。

 

9

Troubleshooting Tips

http://developer.mozilla.org/en/docs/Creating_OpenSearch_plugins_for_Firefox#Troubleshooting_Tips

 

8 Safari上的搜索栏

9 IE7.0上的搜索栏

10添加搜索插件

11

 

import sys import json import os import warnings from datetime import datetime from PyQt5.QtCore import QUrl, Qt, QDateTime from PyQt5.QtWidgets import ( QApplication, QMainWindow, QLineEdit, QPushButton, QListWidget, QTabWidget, QVBoxLayout, QWidget, QHBoxLayout, QAction, QToolBar, QStatusBar, QProgressBar, QInputDialog, QFileDialog, QMessageBox ) from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage, QWebEngineSettings from PyQt5.QtNetwork import QNetworkProxy, QNetworkCookie # 修复代理设置导入问题 # 忽略弃用警告 warnings.filterwarnings("ignore", category=DeprecationWarning) class WebEnginePage(QWebEnginePage): def __init__(self, profile=None, parent=None): """修复参数传递问题:支持带profile和不带profile两种初始化方式""" if profile: super().__init__(profile, parent) # 使用profile初始化 else: super().__init__(parent) # 标准初始化 self.parent_window = parent.parent_window if parent and hasattr(parent, 'parent_window') else None def acceptNavigationRequest(self, url, _type, isMainFrame): # 检测视频流协议并调用播放器 if url.scheme() in ['rtmp', 'http-flv', 'ws-flv']: if self.parent_window: self.parent_window.play_with_jessibuca(url.toString()) return False return super().acceptNavigationRequest(url, _type, isMainFrame) def createWindow(self, type): # 在新标签页打开链接 if type == QWebEnginePage.WebBrowserTab: if self.parent_window: new_tab = self.parent_window.add_browser_tab("加载中...") return new_tab.page return super().createWindow(type) def javaScriptConsoleMessage(self, level, message, lineNumber, sourceID): """修复:正确映射JavaScript控制台日志级别""" # 创建日志级别映射字典 level_map = { QWebEnginePage.InfoMessageLevel: "INFO", QWebEnginePage.WarningMessageLevel: "WARNING", QWebEnginePage.ErrorMessageLevel: "ERROR" } # 获取可读的日志级别名称 level_str = level_map.get(level, "UNKNOWN") log_msg = f"[JS {level_str}] {sourceID}:{lineNumber} - {message}" if self.parent_window: self.parent_window.status_bar.showMessage(log_msg, 5000) print(log_msg) # 同时输出到控制台 class WebEngineView(QWebEngineView): def __init__(self, parent=None): super().__init__(parent) self.parent_window = parent # 修复:正确传递profile参数 self.page = WebEnginePage(profile=self.parent_window.profile, parent=self) self.setPage(self.page) self.page.loadFinished.connect(self.inject_jessibuca) def inject_jessibuca(self): self.page.runJavaScript(""" if (typeof window.Jessibuca === 'undefined') { const script = document.createElement('script'); script.src = 'https://cdn.jsdelivr.net/npm/jessibuca@latest/dist/jessibuca.js'; script.onerror = () => console.error('Jessibuca加载失败'); document.head.appendChild(script); const container = document.createElement('div'); container.id = 'jessibuca-container'; container.style.position = 'fixed'; container.style.zIndex = '99999'; container.style.top = '0'; container.style.left = '0'; container.style.width = '100%'; container.style.height = '100%'; container.style.backgroundColor = 'black'; container.style.display = 'none'; document.body.appendChild(container); } """) def createWindow(self, type): # 创建新标签页 if type == QWebEnginePage.WebBrowserTab: new_tab = WebEngineView(self.parent_window) # 修复:直接返回page属性而非调用page() return new_tab.page return super().createWindow(type) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("B站多账号浏览器") self.setMinimumSize(1200, 800) # 初始化数据存储 self.account_db = {} self.cookies_db = [] # 创建持久化Cookie配置 self.cookie_storage_path = "cookies_storage" os.makedirs(self.cookie_storage_path, exist_ok=True) self.profile = QWebEngineProfile("BiliCookieProfile", self) self.profile.setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies) self.profile.setPersistentStoragePath(self.cookie_storage_path) # 关键性能优化1: 全局禁用网络代理[8](@ref) QNetworkProxy.setApplicationProxy(QNetworkProxy(QNetworkProxy.NoProxy)) # 关键性能优化2: 启用HTTP内存缓存[9](@ref) self.profile.setHttpCacheType(QWebEngineProfile.MemoryHttpCache) # 启用HTML5支持的设置 profile_settings = self.profile.settings() profile_settings.setAttribute(QWebEngineSettings.PlaybackRequiresUserGesture, False) profile_settings.setAttribute(QWebEngineSettings.FullScreenSupportEnabled, True) profile_settings.setAttribute(QWebEngineSettings.WebGLEnabled, True) profile_settings.setAttribute(QWebEngineSettings.Accelerated2dCanvasEnabled, True) profile_settings.setAttribute(QWebEngineSettings.PluginsEnabled, True) profile_settings.setAttribute(QWebEngineSettings.AllowRunningInsecureContent, True) # 主界面布局 central_widget = QWidget() self.setCentralWidget(central_widget) main_layout = QVBoxLayout(central_widget) # 导航工具栏 navbar = QToolBar("导航栏") navbar.setMovable(False) # 禁止工具栏拖动提升性能 self.addToolBar(navbar) back_btn = QAction("←", self) back_btn.triggered.connect(self.navigate_back) navbar.addAction(back_btn) forward_btn = QAction("→", self) forward_btn.triggered.connect(self.navigate_forward) navbar.addAction(forward_btn) reload_btn = QAction("↻", self) reload_btn.triggered.connect(self.reload_page) navbar.addAction(reload_btn) home_btn = QAction("🏠", self) home_btn.triggered.connect(self.navigate_home) navbar.addAction(home_btn) navbar.addSeparator() self.url_bar = QLineEdit("https://www.bilibili.com") self.url_bar.setPlaceholderText("输入网址或搜索内容...") self.url_bar.returnPressed.connect(self.load_url) navbar.addWidget(self.url_bar) # 浏览器标签页 self.browser_tabs = QTabWidget() self.browser_tabs.setTabsClosable(True) self.browser_tabs.tabCloseRequested.connect(self.close_tab) self.browser_tabs.currentChanged.connect(self.update_url_bar) # 关键性能优化3: 启用标签页滚动 self.browser_tabs.setUsesScrollButtons(True) main_layout.addWidget(self.browser_tabs, 8) # 添加初始标签页 self.add_browser_tab("首页", "https://www.bilibili.com") # 管理面板 self.tabs = QTabWidget() self.cookie_list = QListWidget() self.tabs.addTab(self.cookie_list, "Cookie列表") self.account_list = QListWidget() self.tabs.addTab(self.account_list, "账号切换") self.account_list.itemClicked.connect(self.switch_account) main_layout.addWidget(self.tabs, 2) # 功能按钮 btn_layout = QHBoxLayout() self.import_btn = QPushButton("导入Cookie") self.import_btn.clicked.connect(self.import_cookies) self.save_btn = QPushButton("保存账号") self.save_btn.clicked.connect(self.save_account) self.export_btn = QPushButton("导出Cookie") self.export_btn.clicked.connect(self.export_cookies) self.new_tab_btn = QPushButton("新建标签页") self.new_tab_btn.clicked.connect(self.create_new_tab) for btn in [self.import_btn, self.save_btn, self.export_btn, self.new_tab_btn]: btn_layout.addWidget(btn) main_layout.addLayout(btn_layout) # 状态栏 self.status_bar = QStatusBar() self.setStatusBar(self.status_bar) self.progress_bar = QProgressBar() self.progress_bar.setMaximum(100) self.progress_bar.setVisible(False) self.progress_bar.setFixedWidth(200) self.status_bar.addPermanentWidget(self.progress_bar) # 加载数据 self.load_accounts() def add_browser_tab(self, title, url=None): browser = WebEngineView(self) browser.titleChanged.connect(lambda title, b=browser: self.update_tab_title(b, title)) browser.loadProgress.connect(self.update_progress) browser.loadFinished.connect(self.on_load_finished) index = self.browser_tabs.addTab(browser, title) self.browser_tabs.setCurrentIndex(index) # 关键性能优化4: 延迟加载提升启动速度 if url: QApplication.processEvents() # 确保UI更新 browser.load(QUrl(url)) return browser def update_tab_title(self, browser, title): index = self.browser_tabs.indexOf(browser) if index != -1: self.browser_tabs.setTabText(index, title[:15] + "..." if len(title) > 15 else title) def update_progress(self, progress): self.progress_bar.setVisible(progress < 100) self.progress_bar.setValue(progress) def close_tab(self, index): """修复资源释放问题:移除多余的括号""" if self.browser_tabs.count() > 1: widget = self.browser_tabs.widget(index) # 关键修复:直接访问page属性而非调用page() page = widget.page if page: # 关键优化: 正确的资源释放顺序 profile = page.profile() profile.cookieStore().deleteAllCookies() # 先删除页面再删除视图 page.deleteLater() widget.deleteLater() self.browser_tabs.removeTab(index) else: self.create_new_tab() def create_new_tab(self): self.add_browser_tab("新标签页", "about:blank") def navigate_back(self): current_browser = self.browser_tabs.currentWidget() if current_browser: current_browser.back() def navigate_forward(self): current_browser = self.browser_tabs.currentWidget() if current_browser: current_browser.forward() def reload_page(self): current_browser = self.browser_tabs.currentWidget() if current_browser: current_browser.reload() def navigate_home(self): self.load_url("https://www.bilibili.com") def on_load_finished(self, success): browser = self.sender() if browser and self.browser_tabs.currentWidget() == browser: current_url = browser.url().toString() self.url_bar.setText(current_url) self.status_bar.showMessage("页面加载完成" if success else "页面加载失败", 2000) # 关键优化: 登录页面自动处理 if "login" in current_url or "passport" in current_url: self.status_bar.showMessage("检测到登录页面,请完成登录", 5000) self.auto_handle_login_page(browser) def auto_handle_login_page(self, browser): """自动处理登录页面的JavaScript""" browser.page.runJavaScript(""" // 尝试自动填充已知的登录表单 const loginForm = document.querySelector('form[action*="login"]'); if (loginForm) { // 尝试填充测试账号 const usernameInput = loginForm.querySelector('input[name="username"], input[name="user"]'); const passwordInput = loginForm.querySelector('input[name="password"]'); if (usernameInput && passwordInput) { usernameInput.value = "test_account"; passwordInput.value = "test_password"; console.log("自动填充了登录表单"); } } """) def update_url_bar(self): current_browser = self.browser_tabs.currentWidget() if current_browser: current_url = current_browser.url().toString() self.url_bar.setText(current_url) # 登录页面特殊处理 if "login" in current_url or "passport" in current_url: self.url_bar.setStyleSheet("background-color: #FFF8E1;") else: self.url_bar.setStyleSheet("") def load_url(self, url_text=None): if url_text is None: url_text = self.url_bar.text().strip() if not url_text: return # 关键优化: 更智能的URL处理[10](@ref) if not url_text.startswith(("http://", "https://", "file://", "ftp://")): if "." in url_text: # 包含域名 url_text = "https://" + url_text else: # 可能是搜索内容 url_text = f"https://www.bilibili.com/search?keyword={url_text}" current_browser = self.browser_tabs.currentWidget() if current_browser: current_browser.load(QUrl(url_text)) self.status_bar.showMessage(f"正在加载: {url_text}", 3000) def play_with_jessibuca(self, stream_url): """使用Jessibuca播放视频流""" current_browser = self.browser_tabs.currentWidget() if not current_browser: return self.status_bar.showMessage("Jessibuca播放中...", 3000) current_browser.page.runJavaScript(f""" const container = document.getElementById('jessibuca-container'); if (container) {{ container.style.display = 'block'; if (!window.jessibucaPlayer) {{ window.jessibucaPlayer = new Jessibuca({{ container: container, videoBuffer: 0.2, isResize: true, text: '直播加载中...', decoder: 'ffmpeg.js', forceNoOffscreen: true }}); }} window.jessibucaPlayer.play('{stream_url}'); }} """) def import_cookies(self): """导入Cookie JSON文件""" file_path, _ = QFileDialog.getOpenFileName( self, "选择Cookie文件", "", "JSON文件 (*.json)" ) if file_path: try: with open(file_path, "r", encoding='utf-8') as f: cookies = json.load(f) if isinstance(cookies, list): self.cookies_db = cookies self.cookie_list.clear() self.cookie_list.addItems([f"{c['name']}: {c['value'][:10]}..." for c in cookies]) self.status_bar.showMessage(f"成功导入 {len(cookies)} 个Cookie", 3000) self.inject_cookies(cookies) else: QMessageBox.warning(self, "错误", "无效的Cookie格式") except Exception as e: QMessageBox.critical(self, "导入失败", f"错误: {str(e)}") def export_cookies(self): """导出Cookie到文件""" file_path, _ = QFileDialog.getSaveFileName( self, "保存Cookie文件", "", "JSON文件 (*.json)" ) if file_path: try: cookies = self.get_current_cookies() with open(file_path, "w", encoding='utf-8') as f: json.dump(cookies, f, indent=2, ensure_ascii=False) self.status_bar.showMessage("Cookie导出成功", 3000) except Exception as e: QMessageBox.critical(self, "导出失败", f"错误: {str(e)}") def get_current_cookies(self): """获取当前标签页的Cookie(模拟)""" # 实际实现需要异步获取,这里简化处理 return self.cookies_db if self.cookies_db else [] def inject_cookies(self, cookies): """将Cookie注入当前页面 - 修复登录问题[9](@ref)""" current_browser = self.browser_tabs.currentWidget() if current_browser: store = current_browser.page.profile().cookieStore() # 先清除现有Cookie store.deleteAllCookies() # 异步注入新的Cookie for cookie_data in cookies: # 创建Qt的Cookie对象 qt_cookie = QNetworkCookie( cookie_data['name'].encode('utf-8'), cookie_data['value'].encode('utf-8') ) # 设置Cookie属性 if 'domain' in cookie_data: qt_cookie.setDomain(cookie_data['domain']) if 'path' in cookie_data: qt_cookie.setPath(cookie_data['path']) if 'expiry' in cookie_data: # 转换为QDateTime expiry = QDateTime.fromSecsSinceEpoch(cookie_data['expiry']) qt_cookie.setExpirationDate(expiry) # 设置安全属性 qt_cookie.setSecure(cookie_data.get('secure', False)) qt_cookie.setHttpOnly(cookie_data.get('httpOnly', False)) # 注入Cookie store.setCookie(qt_cookie, QUrl(cookie_data.get('url', 'https://www.bilibili.com'))) self.status_bar.showMessage("Cookie注入成功,请刷新页面", 3000) def save_account(self): """保存当前账号配置""" account_name, ok = QInputDialog.getText( self, "保存账号", "输入账号名称:" ) if ok and account_name: cookies = self.get_current_cookies() if cookies: self.account_db[account_name] = { "cookies": cookies, "saved_date": datetime.now().isoformat() } self.account_list.addItem(account_name) # 持久化存储账号数据 try: with open("accounts.json", "w", encoding='utf-8') as f: json.dump(self.account_db, f, indent=2, ensure_ascii=False) QMessageBox.information(self, "成功", "账号保存成功") except Exception as e: QMessageBox.critical(self, "保存失败", f"账号保存失败: {str(e)}") else: QMessageBox.warning(self, "错误", "没有获取到有效Cookie") def switch_account(self, item): """切换账号""" account_name = item.text() if account_name in self.account_db: cookies = self.account_db[account_name].get("cookies", []) self.inject_cookies(cookies) self.status_bar.showMessage(f"已切换至账号: {account_name}", 3000) # 自动刷新当前页面 self.reload_page() else: QMessageBox.warning(self, "错误", "找不到该账号的Cookie信息") def load_accounts(self): """从文件加载保存的账号""" try: if os.path.exists("accounts.json"): with open("accounts.json", "r", encoding='utf-8') as f: self.account_db = json.load(f) self.account_list.addItems(self.account_db.keys()) except Exception as e: print(f"加载账号失败: {str(e)}") def closeEvent(self, event): """窗口关闭时释放全局资源[8](@ref)""" # 清理HTTP缓存和访问记录 self.profile.clearHttpCache() self.profile.clearAllVisitedLinks() # 保存账号数据 try: with open("accounts.json", "w", encoding='utf-8') as f: json.dump(self.account_db, f, indent=2, ensure_ascii=False) except Exception as e: print(f"保存账号失败: {str(e)}") super().closeEvent(event) if __name__ == "__main__": # 关键性能优化: 启用WebEngine调试日志[9](@ref) os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--enable-logging" app = QApplication(sys.argv) # 关键性能优化: 启用Qt内置的OpenGL渲染[7](@ref) app.setAttribute(Qt.AA_UseOpenGLES) window = MainWindow() window.show() sys.exit(app.exec_())
07-09
详细分析解释一下chromium源码中的下面的函数: class OmniboxViewViews : public OmniboxView, public views::Textfield, #if BUILDFLAG(IS_CHROMEOS) public ash::input_method::InputMethodManager::CandidateWindowObserver, #endif public views::TextfieldController, public ui::CompositorObserver, public TemplateURLServiceObserver { METADATA_HEADER(OmniboxViewViews, views::Textfield) public: // Max width of the gradient mask used to smooth ElideAnimation edges. static const int kSmoothingGradientMaxWidth = 15; OmniboxViewViews(std::unique_ptr<OmniboxClient> client, bool popup_window_mode, LocationBarView* location_bar_view, const gfx::FontList& font_list); OmniboxViewViews(const OmniboxViewViews&) = delete; OmniboxViewViews& operator=(const OmniboxViewViews&) = delete; ~OmniboxViewViews() override; // Initialize, create the underlying views, etc. void Init(); // Exposes the RenderText for tests. #if defined(UNIT_TEST) gfx::RenderText* GetRenderText() { return views::Textfield::GetRenderText(); } #endif // For use when switching tabs, this saves the current state onto the tab so // that it can be restored during a later call to Update(). void SaveStateToTab(content::WebContents* tab); // Called when the window's active tab changes. void OnTabChanged(const content::WebContents* web_contents); // Called to clear the saved state for |web_contents|. void ResetTabState(content::WebContents* web_contents); // Installs the placeholder text with the name of the current default search // provider. For example, if Google is the default search provider, this shows // "Search Google or type a URL" when the Omnibox is empty and unfocused. void InstallPlaceholderText(); // Indicates if the cursor is at the end of the input. Requires that both // ends of the selection reside there. bool GetSelectionAtEnd() const; // Returns the width in pixels needed to display the current text. The // returned value includes margins. int GetTextWidth() const; // Returns the width in pixels needed to display the current text unelided. int GetUnelidedTextWidth() const; // Returns the omnibox's width in pixels. int GetWidth() const; // OmniboxView: void EmphasizeURLComponents() override; void Update() override; std::u16string GetText() const override; using OmniboxView::SetUserText; void SetUserText(const std::u16string& text, bool update_popup) override; void SetWindowTextAndCaretPos(const std::u16string& text, size_t caret_pos, bool update_popup, bool notify_text_changed) override; void SetAdditionalText(const std::u16string& additional_text) override; void EnterKeywordModeForDefaultSearchProvider() override; bool IsSelectAll() const override; void GetSelectionBounds(std::u16string::size_type* start, std::u16string::size_type* end) const override; void SelectAll(bool reversed) override; void RevertAll() override; void SetFocus(bool is_user_initiated) override; bool IsImeComposing() const override; gfx::NativeView GetRelativeWindowForPopup() const override; bool IsImeShowingPopup() const override; // views::Textfield: gfx::Size GetMinimumSize() const override; bool OnMousePressed(const ui::MouseEvent& event) override; bool OnMouseDragged(const ui::MouseEvent& event) override; void OnMouseReleased(const ui::MouseEvent& event) override; void OnPaint(gfx::Canvas* canvas) override; void ExecuteCommand(int command_id, int event_flags) override; void OnInputMethodChanged() override; void AddedToWidget() override; void RemovedFromWidget() override; std::u16string GetLabelForCommandId(int command_id) const override; bool IsCommandIdEnabled(int command_id) const override; // For testing only. OmniboxPopupView* GetPopupViewForTesting() const; protected: // OmniboxView: void UpdateSchemeStyle(const gfx::Range& range) override; // views::Textfield: void OnThemeChanged() override; bool IsDropCursorForInsertion() const override; // Wrappers around Textfield methods that tests can override. virtual void ApplyColor(SkColor color, const gfx::Range& range); virtual void ApplyStyle(gfx::TextStyle style, bool value, const gfx::Range& range); private: friend class TestingOmniboxView; FRIEND_TEST_ALL_PREFIXES(OmniboxPopupViewViewsTest, EmitAccessibilityEvents); // TODO(tommycli): Remove the rest of these friends after porting these // browser tests to unit tests. FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, CloseOmniboxPopupOnTextDrag); FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, FriendlyAccessibleLabel); FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, DoNotNavigateOnDrop); FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, AyncDropCallback); FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, AccessibleTextSelectBoundTest); enum class UnelisionGesture { HOME_KEY_PRESSED, MOUSE_RELEASE, OTHER, }; // Update the field with |text| and set the selection. |ranges| should not be // empty; even text with no selections must have at least 1 empty range in // |ranges| to indicate the cursor position. void SetTextAndSelectedRange(const std::u16string& text, const gfx::Range& selection); // Returns the selected text. std::u16string_view GetSelectedText() const; void UpdateAccessibleTextSelection() override; // Paste text from the clipboard into the omnibox. // Textfields implementation of Paste() pastes the contents of the clipboard // as is. We want to strip whitespace and other things (see GetClipboardText() // for details). The function invokes OnBefore/AfterPossibleChange() as // necessary. void OnOmniboxPaste(); // Handle keyword hint tab-to-search and tabbing through dropdown results. bool HandleEarlyTabActions(const ui::KeyEvent& event); void ClearAccessibilityLabel(); void SetAccessibilityLabel(const std::u16string& display_text, const AutocompleteMatch& match, bool notify_text_changed) override; // Returns true if the user text was updated with the full URL (without // steady-state elisions). |gesture| is the user gesture causing unelision. bool UnapplySteadyStateElisions(UnelisionGesture gesture); #if BUILDFLAG(IS_MAC) void AnnounceFriendlySuggestionText(); #endif // Get the preferred text input type, this checks the IME locale on Windows. ui::TextInputType GetPreferredTextInputType() const; // OmniboxView: void SetCaretPos(size_t caret_pos) override; void UpdatePopup() override; void ApplyCaretVisibility() override; void OnTemporaryTextMaybeChanged(const std::u16string& display_text, const AutocompleteMatch& match, bool save_original_selection, bool notify_text_changed) override; void OnInlineAutocompleteTextMaybeChanged( const std::u16string& user_text, const std::u16string& inline_autocompletion) override; void OnInlineAutocompleteTextCleared() override; void OnRevertTemporaryText(const std::u16string& display_text, const AutocompleteMatch& match) override; void OnBeforePossibleChange() override; bool OnAfterPossibleChange(bool allow_keyword_ui_change) override; void OnKeywordPlaceholderTextChange() override; gfx::NativeView GetNativeView() const override; void ShowVirtualKeyboardIfEnabled() override; void HideImeIfNeeded() override; int GetOmniboxTextLength() const override; void SetEmphasis(bool emphasize, const gfx::Range& range) override; // views::View void OnMouseMoved(const ui::MouseEvent& event) override; void OnMouseExited(const ui::MouseEvent& event) override; // views::Textfield: bool IsItemForCommandIdDynamic(int command_id) const override; void OnGestureEvent(ui::GestureEvent* event) override; bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) override; bool HandleAccessibleAction(const ui::AXActionData& action_data) override; void OnFocus() override; void OnBlur() override; std::u16string GetSelectionClipboardText() const override; void DoInsertChar(char16_t ch) override; bool IsTextEditCommandEnabled(ui::TextEditCommand command) const override; void ExecuteTextEditCommand(ui::TextEditCommand command) override; bool ShouldShowPlaceholderText() const override; void UpdateAccessibleValue() override; // ash::input_method::InputMethodManager::CandidateWindowObserver: #if BUILDFLAG(IS_CHROMEOS) void CandidateWindowOpened( ash::input_method::InputMethodManager* manager) override; void CandidateWindowClosed( ash::input_method::InputMethodManager* manager) override; #endif // views::TextfieldController: void ContentsChanged(views::Textfield* sender, const std::u16string& new_contents) override; bool HandleKeyEvent(views::Textfield* sender, const ui::KeyEvent& key_event) override; void OnBeforeUserAction(views::Textfield* sender) override; void OnAfterUserAction(views::Textfield* sender) override; void OnAfterCutOrCopy(ui::ClipboardBuffer clipboard_buffer) override; void OnWriteDragData(ui::OSExchangeData* data) override; void OnGetDragOperationsForTextfield(int* drag_operations) override; void AppendDropFormats( int* formats, std::set<ui::ClipboardFormatType>* format_types) override; ui::mojom::DragOperation OnDrop(const ui::DropTargetEvent& event) override; views::View::DropCallback CreateDropCallback( const ui::DropTargetEvent& event) override; void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) override; // ui::SimpleMenuModel::Delegate: bool IsCommandIdChecked(int id) const override; // ui::CompositorObserver: void OnCompositingDidCommit(ui::Compositor* compositor) override; void OnCompositingStarted(ui::Compositor* compositor, base::TimeTicks start_time) override; void OnDidPresentCompositorFrame( uint32_t frame_token, const gfx::PresentationFeedback& feedback) override; void OnCompositingShuttingDown(ui::Compositor* compositor) override; // TemplateURLServiceObserver: void OnTemplateURLServiceChanged() override; // Permits launch of the external protocol handler after user actions in // the omnibox. The handler needs to be informed that omnibox input should // always be considered "user gesture-triggered", lest it always return BLOCK. void PermitExternalProtocolHandler(); // Drops dragged text and updates `output_drag_op` accordingly. void PerformDrop(const ui::DropTargetEvent& event, ui::mojom::DragOperation& output_drag_op, std::unique_ptr<ui::LayerTreeOwner> drag_image_layer_owner); // Helper method to construct part of the context menu. void MaybeAddSendTabToSelfItem(ui::SimpleMenuModel* menu_contents); // Called when the popup view becomes visible. void OnPopupOpened(); // Helper for updating placeholder color depending on whether its a keyword or // DSE placeholder. void UpdatePlaceholderTextColor(); // When true, the location bar view is read only and also is has a slightly // different presentation (smaller font size). This is used for popups. bool popup_window_mode_; // Owns either an OmniboxPopupViewViews or an OmniboxPopupViewWebUI. std::unique_ptr<OmniboxPopupView> popup_view_; base::CallbackListSubscription popup_view_opened_subscription_; // Selection persisted across temporary text changes, like popup suggestions. gfx::Range saved_temporary_selection_; // Holds the user's selection across focus changes. There is only a saved // selection if this range IsValid(). gfx::Range saved_selection_for_focus_change_; // Tracking state before and after a possible change. State state_before_change_; bool ime_composing_before_change_ = false; // |location_bar_view_| can be NULL in tests. raw_ptr<LocationBarView> location_bar_view_; #if BUILDFLAG(IS_CHROMEOS) // True if the IME candidate window is open. When this is true, we want to // avoid showing the popup. So far, the candidate window is detected only // on Chrome OS. bool ime_candidate_window_open_ = false; #endif // True if any mouse button is currently depressed. bool is_mouse_pressed_ = false; // Applies a minimum threshold to drag events after unelision. Because the // text shifts after unelision, we don't want unintentional mouse drags to // change the selection. bool filter_drag_events_for_unelision_ = false; // Should we select all the text when we see the mouse button get released? // We select in response to a click that focuses the omnibox, but we defer // until release, setting this variable back to false if we saw a drag, to // allow the user to select just a portion of the text. bool select_all_on_mouse_release_ = false; // Indicates if we want to select all text in the omnibox when we get a // GESTURE_TAP. We want to select all only when the textfield is not in focus // and gets a tap. So we use this variable to remember focus state before tap. bool select_all_on_gesture_tap_ = false; // Whether the user should be notified if the clipboard is restricted. bool show_rejection_ui_if_any_ = false; // Keep track of the word that would be selected if URL is unelided between // a single and double click. This is an edge case where the elided URL is // selected. On the double click, unelision is performed in between the first // and second clicks. This results in both the wrong word to be selected and // the wrong selection length. For example, if example.com is shown and you // try to double click on the "x", it unelides to https://example.com after // the first click, resulting in "https" being selected. size_t next_double_click_selection_len_ = 0; size_t next_double_click_selection_offset_ = 0; // The time of the first character insert operation that has not yet been // painted. Used to measure omnibox responsiveness with a histogram. base::TimeTicks insert_char_time_; // The state machine for logging the Omnibox.CharTypedToRepaintLatency // histogram. enum { NOT_ACTIVE, // Not currently tracking a char typed event. CHAR_TYPED, // Character was typed. ON_PAINT_CALLED, // Character was typed and OnPaint() called. COMPOSITING_COMMIT, // Compositing was committed after OnPaint(). COMPOSITING_STARTED, // Compositing was started. } latency_histogram_state_; // The currently selected match, if any, with additional labelling text // such as the document title and the type of search, for example: // "Google https://google.com location from bookmark", or // "cats are liquid search suggestion". std::u16string friendly_suggestion_text_; // The number of added labelling characters before editable text begins. // For example, "Google https://google.com location from history", // this is set to 7 (the length of "Google "). int friendly_suggestion_text_prefix_length_; base::ScopedObservation<ui::Compositor, ui::CompositorObserver> scoped_compositor_observation_{this}; base::ScopedObservation<TemplateURLService, TemplateURLServiceObserver> scoped_template_url_service_observation_{this}; PrefChangeRegistrar pref_change_registrar_; base::WeakPtrFactory<OmniboxViewViews> weak_factory_{this}; };
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值