blob: c020b42ff020bd568155c5b00cd0ad4533729b05 [file] [log] [blame]
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_CONTEXT_MENU_CONTROLLER_H_
#define CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_CONTEXT_MENU_CONTROLLER_H_
#include <string>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/time/time.h"
#include "components/omnibox/browser/searchbox.mojom.h"
#include "ui/menus/simple_menu_model.h"
#include "url/gurl.h"
class FaviconService;
class OmniboxPopupFileSelector;
class OmniboxEditModel;
namespace favicon_base {
struct FaviconImageResult;
} // namespace favicon_base
namespace ui {
class ImageModel;
} // namespace ui
namespace content {
class WebContents;
} // namespace content
namespace contextual_search {
class ContextualSearchContextController;
}
// OmniboxContextMenuController creates and manages state for the context menu
// shown for the omnibox.
class OmniboxContextMenuController : public ui::SimpleMenuModel::Delegate {
public:
explicit OmniboxContextMenuController(OmniboxPopupFileSelector* file_selector,
content::WebContents* web_contents);
struct TabInfo {
int tab_id;
std::u16string title;
GURL url;
base::TimeTicks last_active;
};
OmniboxContextMenuController(const OmniboxContextMenuController&) = delete;
OmniboxContextMenuController& operator=(const OmniboxContextMenuController&) =
delete;
~OmniboxContextMenuController() override;
ui::SimpleMenuModel* menu_model() { return menu_model_.get(); }
void ExecuteCommand(int command_id, int event_flags) override;
bool IsCommandIdVisible(int command_id) const override;
void AddTabContext(const TabInfo& tab_info);
void UpdateSearchboxContext(
std::optional<TabInfo> tab_info,
std::optional<searchbox::mojom::ToolMode> tool_mode);
private:
void BuildMenu();
// Adds a IDC_* style command to the menu with a string16.
void AddItem(int id, const std::u16string str);
// Adds a IDC_* style command to the menu with a localized string and icon.
void AddItemWithStringIdAndIcon(int id,
int localization_id,
const ui::ImageModel& icon);
// Adds a IDC_* style command to the menu with a string16 and icon.
void AddItemWithIcon(int command_id,
const std::u16string& label,
const ui::ImageModel& icon);
// Adds a separator to the menu.
void AddSeparator();
// Adds recent tabs as items to the menu.
void AddRecentTabItems();
// Adds the static items with icons.
void AddStaticItems();
// Adds a title with a localized string to the menu.
void AddTitleWithStringId(int localization_id);
// Gets the most recent tabs.
std::vector<OmniboxContextMenuController::TabInfo> GetRecentTabs();
// Adds the tabs favicon to the menu.
void AddTabFavicon(int command_id,
const GURL& url,
const std::u16string& label);
// Callback for when the tab favicon is available.
void OnFaviconDataAvailable(
int command_id,
const favicon_base::FaviconImageResult& image_result);
void UpdateSearchboxContextToolMode(searchbox::mojom::ToolMode tool_mode);
raw_ptr<contextual_search::ContextualSearchContextController>
GetQueryController();
raw_ptr<OmniboxEditModel> GetEditModel();
std::unique_ptr<ui::SimpleMenuModel> menu_model_;
base::WeakPtr<OmniboxPopupFileSelector> file_selector_;
base::WeakPtr<content::WebContents> web_contents_;
raw_ptr<OmniboxEditModel> edit_model_;
// Needed for using FaviconService.
base::CancelableTaskTracker cancelable_task_tracker_;
raw_ptr<FaviconService> favicon_service_;
int next_command_id_ = 0;
base::WeakPtrFactory<OmniboxContextMenuController> weak_ptr_factory_{this};
};
#endif // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_CONTEXT_MENU_CONTROLLER_H_