Home | History | Annotate | Download | only in omnibox
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // This file defines the interface class OmniboxView.  Each toolkit will
      6 // implement the edit view differently, so that code is inherently platform
      7 // specific.  However, the OmniboxEditModel needs to do some communication with
      8 // the view.  Since the model is shared between platforms, we need to define an
      9 // interface that all view implementations will share.
     10 
     11 #ifndef CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_
     12 #define CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_
     13 
     14 #include <string>
     15 
     16 #include "base/strings/string16.h"
     17 #include "base/strings/string_util.h"
     18 #include "base/strings/utf_string_conversions.h"
     19 #include "chrome/browser/autocomplete/autocomplete_match.h"
     20 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
     21 #include "content/public/common/url_constants.h"
     22 #include "ui/base/window_open_disposition.h"
     23 #include "ui/gfx/native_widget_types.h"
     24 
     25 class CommandUpdater;
     26 class GURL;
     27 class OmniboxEditController;
     28 class OmniboxViewMacTest;
     29 class Profile;
     30 class ToolbarModel;
     31 
     32 namespace content {
     33 class WebContents;
     34 }
     35 
     36 #if defined(TOOLKIT_VIEWS)
     37 // TODO(beng): Move all views-related code to a views-specific sub-interface.
     38 namespace gfx {
     39 class Font;
     40 }
     41 
     42 namespace views {
     43 class View;
     44 }
     45 
     46 namespace ui {
     47 class DropTargetEvent;
     48 }
     49 #endif
     50 
     51 class OmniboxView {
     52  public:
     53   virtual ~OmniboxView();
     54 
     55   // Used by the automation system for getting at the model from the view.
     56   OmniboxEditModel* model() { return model_.get(); }
     57   const OmniboxEditModel* model() const { return model_.get(); }
     58 
     59   CommandUpdater* command_updater() { return command_updater_; }
     60   const CommandUpdater* command_updater() const { return command_updater_; }
     61 
     62   // For use when switching tabs, this saves the current state onto the tab so
     63   // that it can be restored during a later call to Update().
     64   virtual void SaveStateToTab(content::WebContents* tab) = 0;
     65 
     66   // Called when the window's active tab changes.
     67   virtual void OnTabChanged(const content::WebContents* web_contents) = 0;
     68 
     69   // Called when any relevant state changes other than changing tabs.
     70   virtual void Update() = 0;
     71 
     72   // Asks the browser to load the specified match's |destination_url|, which
     73   // is assumed to be one of the popup entries, using the supplied disposition
     74   // and transition type. |alternate_nav_url|, if non-empty, contains the
     75   // alternate navigation URL for for this match. See comments on
     76   // AutocompleteResult::GetAlternateNavURL().
     77   //
     78   // |selected_line| is passed to SendOpenNotification(); see comments there.
     79   //
     80   // This may close the popup.
     81   virtual void OpenMatch(const AutocompleteMatch& match,
     82                          WindowOpenDisposition disposition,
     83                          const GURL& alternate_nav_url,
     84                          size_t selected_line);
     85 
     86   // Returns the current text of the edit control, which could be the
     87   // "temporary" text set by the popup, the "permanent" text set by the
     88   // browser, or just whatever the user has currently typed.
     89   virtual base::string16 GetText() const = 0;
     90 
     91   // |true| if the user is in the process of editing the field, or if
     92   // the field is empty.
     93   bool IsEditingOrEmpty() const;
     94 
     95   // Returns the resource ID of the icon to show for the current text.
     96   int GetIcon() const;
     97 
     98   // The user text is the text the user has manually keyed in.  When present,
     99   // this is shown in preference to the permanent text; hitting escape will
    100   // revert to the permanent text.
    101   void SetUserText(const base::string16& text);
    102   virtual void SetUserText(const base::string16& text,
    103                            const base::string16& display_text,
    104                            bool update_popup);
    105 
    106   // Sets the window text and the caret position. |notify_text_changed| is true
    107   // if the model should be notified of the change.
    108   virtual void SetWindowTextAndCaretPos(const base::string16& text,
    109                                         size_t caret_pos,
    110                                         bool update_popup,
    111                                         bool notify_text_changed) = 0;
    112 
    113   // Sets the edit to forced query mode.  Practically speaking, this means that
    114   // if the edit is not in forced query mode, its text is set to "?" with the
    115   // cursor at the end, and if the edit is in forced query mode (its first
    116   // non-whitespace character is '?'), the text after the '?' is selected.
    117   //
    118   // In the future we should display the search engine UI for the default engine
    119   // rather than '?'.
    120   virtual void SetForcedQuery() = 0;
    121 
    122   // Returns true if all text is selected or there is no text at all.
    123   virtual bool IsSelectAll() const = 0;
    124 
    125   // Returns true if the user deleted the suggested text.
    126   virtual bool DeleteAtEndPressed() = 0;
    127 
    128   // Fills |start| and |end| with the indexes of the current selection's bounds.
    129   // It is not guaranteed that |*start < *end|, as the selection can be
    130   // directed.  If there is no selection, |start| and |end| will both be equal
    131   // to the current cursor position.
    132   virtual void GetSelectionBounds(size_t* start, size_t* end) const = 0;
    133 
    134   // Selects all the text in the edit.  Use this in place of SetSelAll() to
    135   // avoid selecting the "phantom newline" at the end of the edit.
    136   virtual void SelectAll(bool reversed) = 0;
    137 
    138   // Sets focus, disables search term replacement, reverts the omnibox, and
    139   // selects all.
    140   void ShowURL();
    141 
    142   // Re-enables search term replacement on the ToolbarModel, and reverts the
    143   // edit and popup back to their unedited state (permanent text showing, popup
    144   // closed, no user input in progress).
    145   virtual void RevertAll();
    146 
    147   // Like RevertAll(), but does not touch the search term replacement state.
    148   void RevertWithoutResettingSearchTermReplacement();
    149 
    150   // Updates the autocomplete popup and other state after the text has been
    151   // changed by the user.
    152   virtual void UpdatePopup() = 0;
    153 
    154   // Closes the autocomplete popup, if it's open. The name |ClosePopup|
    155   // conflicts with the OSX class override as that has a base class that also
    156   // defines a method with that name.
    157   virtual void CloseOmniboxPopup();
    158 
    159   // Sets the focus to the autocomplete view.
    160   virtual void SetFocus() = 0;
    161 
    162   // Shows or hides the caret based on whether the model's is_caret_visible() is
    163   // true.
    164   virtual void ApplyCaretVisibility() = 0;
    165 
    166   // Called when the temporary text in the model may have changed.
    167   // |display_text| is the new text to show; |save_original_selection| is true
    168   // when there wasn't previously a temporary text and thus we need to save off
    169   // the user's existing selection. |notify_text_changed| is true if the model
    170   // should be notified of the change.
    171   virtual void OnTemporaryTextMaybeChanged(const base::string16& display_text,
    172                                            bool save_original_selection,
    173                                            bool notify_text_changed) = 0;
    174 
    175   // Called when the inline autocomplete text in the model may have changed.
    176   // |display_text| is the new text to show; |user_text_length| is the length of
    177   // the user input portion of that (so, up to but not including the inline
    178   // autocompletion).  Returns whether the display text actually changed.
    179   virtual bool OnInlineAutocompleteTextMaybeChanged(
    180       const base::string16& display_text, size_t user_text_length) = 0;
    181 
    182   // Called when the inline autocomplete text in the model has been cleared.
    183   virtual void OnInlineAutocompleteTextCleared() = 0;
    184 
    185   // Called when the temporary text has been reverted by the user.  This will
    186   // reset the user's original selection.
    187   virtual void OnRevertTemporaryText() = 0;
    188 
    189   // Checkpoints the current edit state before an operation that might trigger
    190   // a new autocomplete run to open or modify the popup. Call this before
    191   // user-initiated edit actions that trigger autocomplete, but *not* for
    192   // automatic changes to the textfield that should not affect autocomplete.
    193   virtual void OnBeforePossibleChange() = 0;
    194   // OnAfterPossibleChange() returns true if there was a change that caused it
    195   // to call UpdatePopup().
    196   virtual bool OnAfterPossibleChange() = 0;
    197 
    198   // Returns the gfx::NativeView of the edit view.
    199   virtual gfx::NativeView GetNativeView() const = 0;
    200 
    201   // Gets the relative window for the pop up window of OmniboxPopupView. The pop
    202   // up window will be shown under the relative window. When an IME is attached
    203   // to the rich edit control, the IME window is the relative window. Otherwise,
    204   // the top-most window is the relative window.
    205   virtual gfx::NativeView GetRelativeWindowForPopup() const = 0;
    206 
    207   // Shows |input| as gray suggested text after what the user has typed.
    208   virtual void SetGrayTextAutocompletion(const base::string16& input) = 0;
    209 
    210   // Returns the current gray suggested text.
    211   virtual base::string16 GetGrayTextAutocompletion() const = 0;
    212 
    213   // Returns the width in pixels needed to display the current text. The
    214   // returned value includes margins.
    215   virtual int GetTextWidth() const = 0;
    216 
    217   // Returns the omnibox's width in pixels.
    218   virtual int GetWidth() const = 0;
    219 
    220   // Returns true if the user is composing something in an IME.
    221   virtual bool IsImeComposing() const = 0;
    222 
    223   // Returns true if we know for sure that an IME is showing a popup window,
    224   // which may overlap the omnibox's popup window.
    225   virtual bool IsImeShowingPopup() const;
    226 
    227   // Returns true if the view is displaying UI that indicates that query
    228   // refinement will take place when the user selects the current match.  For
    229   // search matches, this will cause the omnibox to search over the existing
    230   // corpus (e.g. Images) rather than start a new Web search.  This method will
    231   // only ever return true on mobile ports.
    232   virtual bool IsIndicatingQueryRefinement() const;
    233 
    234 #if defined(TOOLKIT_VIEWS)
    235   // Performs the drop of a drag and drop operation on the view.
    236   virtual int OnPerformDrop(const ui::DropTargetEvent& event) = 0;
    237 #endif
    238 
    239   // Returns |text| with any leading javascript schemas stripped.
    240   static base::string16 StripJavascriptSchemas(const base::string16& text);
    241 
    242   // First, calls StripJavascriptSchemas().  Then automatically collapses
    243   // internal whitespace as follows:
    244   // * If the only whitespace in |text| is newlines, users are most likely
    245   // pasting in URLs split into multiple lines by terminals, email programs,
    246   // etc. So all newlines are removed.
    247   // * Otherwise, users may be pasting in search data, e.g. street addresses. In
    248   // this case, runs of whitespace are collapsed down to single spaces.
    249   static base::string16 SanitizeTextForPaste(const base::string16& text);
    250 
    251   // Returns the current clipboard contents as a string that can be pasted in.
    252   // In addition to just getting CF_UNICODETEXT out, this can also extract URLs
    253   // from bookmarks on the clipboard.
    254   static base::string16 GetClipboardText();
    255 
    256  protected:
    257   OmniboxView(Profile* profile,
    258               OmniboxEditController* controller,
    259               CommandUpdater* command_updater);
    260 
    261   // Internally invoked whenever the text changes in some way.
    262   virtual void TextChanged();
    263 
    264   // Return the number of characters in the current buffer. The name
    265   // |GetTextLength| can't be used as the Windows override of this class
    266   // inherits from a class that defines a method with that name.
    267   virtual int GetOmniboxTextLength() const = 0;
    268 
    269   // Try to parse the current text as a URL and colorize the components.
    270   virtual void EmphasizeURLComponents() = 0;
    271 
    272   OmniboxEditController* controller() { return controller_; }
    273   const OmniboxEditController* controller() const { return controller_; }
    274 
    275  private:
    276   friend class OmniboxViewMacTest;
    277   FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ShowURL);
    278 
    279   // |model_| can be NULL in tests.
    280   scoped_ptr<OmniboxEditModel> model_;
    281   OmniboxEditController* controller_;
    282 
    283   // The object that handles additional command functionality exposed on the
    284   // edit, such as invoking the keyword editor.
    285   CommandUpdater* command_updater_;
    286 };
    287 
    288 #endif  // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_
    289