File: omnibox_utils.cc

package info (click to toggle)
chromium 143.0.7499.109-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,786,824 kB
  • sloc: cpp: 35,783,839; ansic: 7,477,365; javascript: 3,962,116; python: 1,480,521; xml: 764,832; asm: 710,816; pascal: 188,028; sh: 88,717; perl: 88,692; objc: 79,984; sql: 57,625; cs: 42,265; fortran: 24,101; makefile: 22,509; tcl: 15,277; php: 14,018; yacc: 9,043; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 3,399 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/search/omnibox_utils.h"

#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/omnibox/omnibox_controller.h"
#include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
#include "chrome/browser/ui/omnibox/omnibox_view.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/browser/web_contents.h"

namespace search {

namespace {

LocationBar* GetLocationBar(content::WebContents* web_contents) {
  if (!web_contents) {
    return nullptr;
  }

  const auto* tab = tabs::TabInterface::MaybeGetFromContents(web_contents);
  if (!tab) {
    return nullptr;
  }

  auto* bwi =
      const_cast<BrowserWindowInterface*>(tab->GetBrowserWindowInterface());
  if (!bwi) {
    return nullptr;
  }

  return bwi->GetFeatures().location_bar();
}

OmniboxView* GetOmniboxView(content::WebContents* web_contents) {
  auto* location_bar = GetLocationBar(web_contents);
  if (!location_bar) {
    return nullptr;
  }

  return location_bar->GetOmniboxView();
}

OmniboxEditModel* GetOmniboxEditModel(content::WebContents* web_contents) {
  auto* controller = GetOmniboxController(web_contents);
  if (!controller) {
    return nullptr;
  }

  return controller->edit_model();
}

}  // namespace

OmniboxController* GetOmniboxController(content::WebContents* web_contents) {
  auto* location_bar = GetLocationBar(web_contents);
  if (!location_bar) {
    return nullptr;
  }

  return location_bar->GetOmniboxController();
}

void FocusOmnibox(bool focus, content::WebContents* web_contents) {
  auto* omnibox_view = GetOmniboxView(web_contents);
  if (!omnibox_view) {
    return;
  }
  auto* controller = GetOmniboxController(web_contents);
  if (!controller) {
    return;
  }
  auto* edit_model = controller->edit_model();

  if (focus) {
    // This is an invisible focus to support "fakebox" implementations on NTPs
    // (including other search providers). We shouldn't consider it as the user
    // explicitly focusing the omnibox.
    omnibox_view->SetFocus(/*is_user_initiated=*/false);
    edit_model->SetCaretVisibility(false);
    // If the user clicked on the fakebox, any text already in the omnibox
    // should get cleared when they start typing. Selecting all the existing
    // text is a convenient way to accomplish this. It also gives a slight
    // visual cue to users who really understand selection state about what
    // will happen if they start typing.
    omnibox_view->SelectAll(false);
    omnibox_view->ShowVirtualKeyboardIfEnabled();
  } else {
    // Remove focus only if the popup is closed. This will prevent someone
    // from changing the omnibox value and closing the popup without user
    // interaction.
    if (!controller->IsPopupOpen()) {
      web_contents->Focus();
    }
  }
}

bool IsOmniboxInputInProgress(content::WebContents* web_contents) {
  auto* edit_model = GetOmniboxEditModel(web_contents);
  if (!edit_model) {
    return false;
  }
  return edit_model->user_input_in_progress() &&
         edit_model->focus_state() == OMNIBOX_FOCUS_VISIBLE;
}

}  // namespace search