1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
9ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_ptr.h"
10513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch#include "base/string16.h"
11dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/browser/autocomplete/autocomplete_controller_delegate.h"
12513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch#include "chrome/browser/autocomplete/autocomplete_match.h"
13ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/common/instant_types.h"
14ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/common/page_transition_types.h"
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "googleurl/src/gurl.h"
1672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "ui/gfx/native_widget_types.h"
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "webkit/glue/window_open_disposition.h"
18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
19dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass AutocompleteController;
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass AutocompleteEditController;
21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass AutocompleteEditModel;
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass AutocompleteEditView;
23dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass AutocompletePopupModel;
24513209b27ff55e2841eac0e4120199c23acce758Ben Murdochclass AutocompleteResult;
25dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass InstantController;
26dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass Profile;
27dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass SkBitmap;
28dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass TabContentsWrapper;
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
303345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merricknamespace gfx {
313345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickclass Rect;
323345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick}
333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// TODO(pkasting): The names and contents of the classes in
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// this file are temporary.  I am in hack-and-slash mode right now.
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// http://code.google.com/p/chromium/issues/detail?id=6772
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Embedders of an AutocompleteEdit widget must implement this class.
39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass AutocompleteEditController {
40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // When the user presses enter or selects a line with the mouse, this
42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // function will get called synchronously with the url to open and
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // disposition and transition to use when opening it.
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // |alternate_nav_url|, if non-empty, contains the alternate navigation URL
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // for |url|, which the controller can check for existence.  See comments on
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // AutocompleteResult::GetAlternateNavURL().
48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void OnAutocompleteAccept(const GURL& url,
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                    WindowOpenDisposition disposition,
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                    PageTransition::Type transition,
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                    const GURL& alternate_nav_url) = 0;
52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called when anything has changed that might affect the layout or contents
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // of the views around the edit, including the text of the edit and the
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // status of any keyword- or hint-related state.
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void OnChanged() = 0;
57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
584a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Called when the selection of the AutocompleteEditView changes.
594a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual void OnSelectionBoundsChanged() = 0;
604a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called whenever the user starts or stops an input session (typing,
62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // interacting with the edit, etc.).  When user input is not in progress,
63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the edit is guaranteed to be showing the permanent text.
64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void OnInputInProgress(bool in_progress) = 0;
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called whenever the autocomplete edit is losing focus.
67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void OnKillFocus() = 0;
68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called whenever the autocomplete edit gets focused.
70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void OnSetFocus() = 0;
71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the favicon of the current page.
73ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  virtual SkBitmap GetFavicon() const = 0;
74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the title of the current page.
7672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual string16 GetTitle() const = 0;
77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
78dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Returns the InstantController, or NULL if instant is not enabled.
79dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  virtual InstantController* GetInstant() = 0;
80dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
81dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Returns the TabContentsWrapper of the currently selected tab.
82ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  virtual TabContentsWrapper* GetTabContentsWrapper() const = 0;
83dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch protected:
85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual ~AutocompleteEditController();
86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
88dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass AutocompleteEditModel : public AutocompleteControllerDelegate {
89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  struct State {
91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    State(bool user_input_in_progress,
9272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen          const string16& user_text,
9372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen          const string16& keyword,
9472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen          bool is_keyword_hint);
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    ~State();
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    bool user_input_in_progress;
9872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    const string16 user_text;
9972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    const string16 keyword;
100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    const bool is_keyword_hint;
101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  };
102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompleteEditModel(AutocompleteEditView* view,
104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                        AutocompleteEditController* controller,
105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                        Profile* profile);
106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ~AutocompleteEditModel();
107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
108dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  AutocompleteController* autocomplete_controller() const {
109dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    return autocomplete_controller_.get();
110dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  }
111dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
112dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  void set_popup_model(AutocompletePopupModel* popup_model) {
113dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    popup_ = popup_model;
114dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  }
115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
1163f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // TODO: The edit and popup should be siblings owned by the LocationBarView,
1173f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // making this accessor unnecessary.
118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompletePopupModel* popup_model() const { return popup_; }
119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Invoked when the profile has changed.
121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetProfile(Profile* profile);
122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Profile* profile() const { return profile_; }
124c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
125c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the current state.  This assumes we are switching tabs, and changes
126c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the internal state appropriately.
127c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const State GetStateForTabSwitch();
128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Restores local state from the saved |state|.
130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void RestoreState(const State& state);
131c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
1323345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Returns the match for the current text. If the user has not edited the text
1333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // this is the match corresponding to the permanent text.
1343345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  AutocompleteMatch CurrentMatch();
1353345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
136c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called when the user wants to export the entire current text as a URL.
137c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Sets the url, and if known, the title and favicon.
13872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void GetDataForURLExport(GURL* url, string16* title, SkBitmap* favicon);
139c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
140201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // Returns true if a verbatim query should be used for instant. A verbatim
141201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // query is forced in certain situations, such as pressing delete at the end
142201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // of the edit.
143201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  bool UseVerbatimInstant();
1444a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
145c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // If the user presses ctrl-enter, it means "add .com to the the end".  The
146c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // desired TLD is the TLD the user desires to add to the end of the current
147c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // input, if any, based on their control key state and any other actions
148c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // they've taken.
14972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  string16 GetDesiredTLD() const;
150c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
151c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns true if the current edit contents will be treated as a
152c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // URL/navigation, as opposed to a search.
153c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool CurrentTextIsURL() const;
154c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
155c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the match type for the current edit contents.
156c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompleteMatch::Type CurrentTextType() const;
157c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
158c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Invoked to adjust the text before writting to the clipboard for a copy
159c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // (e.g. by adding 'http' to the front). |sel_min| gives the minimum position
160c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // of the selection e.g. min(selection_start, selection_end). |text| is the
161c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // currently selected text. If |is_all_selected| is true all the text in the
162c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // edit is selected. If the url should be copied to the clipboard |write_url|
163c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // is set to true and |url| set to the url to write.
164c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void AdjustTextForCopy(int sel_min,
165c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                         bool is_all_selected,
16672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                         string16* text,
167c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                         GURL* url,
168c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                         bool* write_url);
169c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
170c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool user_input_in_progress() const { return user_input_in_progress_; }
171c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
172c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Sets the state of user_input_in_progress_, and notifies the observer if
173c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // that state has changed.
174c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetInputInProgress(bool in_progress);
175c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
176c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Updates permanent_text_ to |new_permanent_text|.  Returns true if this
177c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // change should be immediately user-visible, because either the user is not
178c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // editing or the edit does not have focus.
17972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool UpdatePermanentText(const string16& new_permanent_text);
180c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
181dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Returns the URL corresponding to the permanent text.
182dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  GURL PermanentURL();
183dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
184c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Sets the user_text_ to |text|.  Only the View should call this.
18572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void SetUserText(const string16& text);
186c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
187201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // Calls through to SearchProvider::FinalizeInstantQuery.
18872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // If |skip_inline_autocomplete| is true then the |suggest_text| will be
18972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // turned into final text instead of inline autocomplete suggest.
19072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void FinalizeInstantQuery(const string16& input_text,
19172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                            const string16& suggest_text,
19272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                            bool skip_inline_autocomplete);
193201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
194dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Sets the suggestion text.
195ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void SetSuggestedText(const string16& text,
196ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                        InstantCompleteBehavior behavior);
197dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
198dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Commits the suggested text. If |skip_inline_autocomplete| is true then the
199dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // suggested text will be committed as final text as if it's inputted by the
200dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // user, rather than as inline autocomplete suggest.
201dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Returns true if the text was committed.
202dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // TODO: can the return type be void?
203dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  bool CommitSuggestedText(bool skip_inline_autocomplete);
204dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
205dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Accepts the currently showing instant preview, if any, and returns true.
206dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Returns false if there is no instant preview showing.
207dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  bool AcceptCurrentInstantPreview();
208dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
209dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Invoked any time the text may have changed in the edit. Updates instant and
210dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // notifies the controller.
211dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  void OnChanged();
212dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
213c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Reverts the edit model back to its unedited state (permanent text showing,
214c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // no user input in progress).
215c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Revert();
216c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
217c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Directs the popup to start autocomplete.
2183345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  void StartAutocomplete(bool has_selected_text,
2193345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick                         bool prevent_inline_autocomplete) const;
220c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
221dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Closes the popup and cancels any pending asynchronous queries.
222dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  void StopAutocomplete();
223dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
224c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Determines whether the user can "paste and go", given the specified text.
225c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // This also updates the internal paste-and-go-related state variables as
226c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // appropriate so that the controller doesn't need to be repeatedly queried
227c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // for the same text in every clipboard-related function.
22872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool CanPasteAndGo(const string16& text) const;
229c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
230c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Navigates to the destination last supplied to CanPasteAndGo.
231c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void PasteAndGo();
232c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
233c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the url set by way of CanPasteAndGo.
234c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const GURL& paste_and_go_url() const { return paste_and_go_url_; }
235c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
236c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns true if this is a paste-and-search rather than paste-and-go (or
237c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // nothing).
238c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool is_paste_and_search() const {
239c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return (paste_and_go_transition_ != PageTransition::TYPED);
240c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
241c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
242c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Asks the browser to load the popup's currently selected item, using the
243c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // supplied disposition.  This may close the popup. If |for_drop| is true,
244c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // it indicates the input is being accepted as part of a drop operation and
245c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the transition should be treated as LINK (so that it won't trigger the
246c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // URL to be autocompleted).
247c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void AcceptInput(WindowOpenDisposition disposition,
248c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                   bool for_drop);
249c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
250c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Asks the browser to load the item at |index|, with the given properties.
251c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void OpenURL(const GURL& url,
252c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch               WindowOpenDisposition disposition,
253c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch               PageTransition::Type transition,
254c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch               const GURL& alternate_nav_url,
255c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch               size_t index,
25672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen               const string16& keyword);
257c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
258c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool has_focus() const { return has_focus_; }
259c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
260c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Accessors for keyword-related state (see comments on keyword_ and
261c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // is_keyword_hint_).
26272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  const string16& keyword() const { return keyword_; }
263c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool is_keyword_hint() const { return is_keyword_hint_; }
264c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
26572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Accepts the current keyword hint as a keyword. It always returns true for
26672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // caller convenience.
26772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool AcceptKeyword();
268c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
269c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Clears the current keyword.  |visible_text| is the (non-keyword) text
270c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // currently visible in the edit.
27172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void ClearKeyword(const string16& visible_text);
272c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
273c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the current autocomplete result.  This logic should in the future
274c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // live in AutocompleteController but resides here for now.  This method is
275c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // used by AutomationProvider::AutocompleteEditGetMatches.
276c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const AutocompleteResult& result() const;
277c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
278c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called when the view is gaining focus.  |control_down| is whether the
279c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // control key is down (at the time we're gaining focus).
280c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void OnSetFocus(bool control_down);
281c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
282dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Sent before |OnKillFocus| and before the popup is closed.
283dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  void OnWillKillFocus(gfx::NativeView view_gaining_focus);
284dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
285c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called when the view is losing focus.  Resets some state.
286c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void OnKillFocus();
287c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
288c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called when the user presses the escape key.  Decides what, if anything, to
289c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // revert about any current edits.  Returns whether the key was handled.
290c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool OnEscapeKeyPressed();
291c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
292c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called when the user presses or releases the control key.  Changes state as
293c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // necessary.
294c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void OnControlKeyChanged(bool pressed);
295c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
29672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Called when the user pastes in text.
29772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void on_paste() { paste_state_ = PASTING; }
298c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
299c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called when the user presses up or down.  |count| is a repeat count,
300c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // negative for moving up, positive for moving down.
301c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void OnUpOrDownKeyPressed(int count);
302c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
303c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called when any relevant data changes.  This rolls together several
304c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // separate pieces of data into one call so we can update all the UI
305c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // efficiently:
306c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //   |text| is either the new temporary text from the user manually selecting
307c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //     a different match, or the inline autocomplete text.  We distinguish by
308c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //     checking if |destination_for_temporary_text_change| is NULL.
309c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //   |destination_for_temporary_text_change| is NULL (if temporary text should
3104a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  //     not change) or the pre-change destination URL (if temporary text should
311c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //     change) so we can save it off to restore later.
312c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //   |keyword| is the keyword to show a hint for if |is_keyword_hint| is true,
313c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //     or the currently selected keyword if |is_keyword_hint| is false (see
314c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //     comments on keyword_ and is_keyword_hint_).
315c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void OnPopupDataChanged(
31672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen      const string16& text,
317c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      GURL* destination_for_temporary_text_change,
31872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen      const string16& keyword,
319c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      bool is_keyword_hint);
320c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
321c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called by the AutocompleteEditView after something changes, with details
322c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // about what state changes occured.  Updates internal state, updates the
323c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // popup if necessary, and returns true if any significant changes occurred.
32472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // If |allow_keyword_ui_change| is false then the change should not affect
32572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // keyword ui state, even if the text matches a keyword exactly. This value
326ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // may be false when the user is composing a text with an IME.
32772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool OnAfterPossibleChange(const string16& new_text,
328ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                             size_t selection_start,
329ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                             size_t selection_end,
330c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                             bool selection_differs,
331c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                             bool text_differs,
332c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                             bool just_deleted_text,
33372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                             bool allow_keyword_ui_change);
334c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
3353345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Invoked when the popup is going to change its bounds to |bounds|.
3363345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  void PopupBoundsChangedTo(const gfx::Rect& bounds);
3373345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
338ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#if defined(UNIT_TEST)
339ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  InstantCompleteBehavior instant_complete_behavior() const {
340ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return instant_complete_behavior_;
341ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
342ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#endif
343ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
344c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private:
345c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  enum PasteState {
34672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    NONE,           // Most recent edit was not a paste.
34772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    PASTING,        // In the middle of doing a paste. We need this intermediate
34872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                    // state because OnPaste() does the actual detection of
34972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                    // paste, but OnAfterPossibleChange() has to update the
35072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                    // paste state for every edit. If OnPaste() set the state
35172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                    // directly to PASTED, OnAfterPossibleChange() wouldn't know
352c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                    // whether that represented the current edit or a past one.
35372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    PASTED,         // Most recent edit was a paste.
354c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  };
355c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
356c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  enum ControlKeyState {
357c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    UP,                   // The control key is not depressed.
358c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    DOWN_WITHOUT_CHANGE,  // The control key is depressed, and the edit's
359c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                          // contents/selection have not changed since it was
360c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                          // depressed.  This is the only state in which we
361c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                          // do the "ctrl-enter" behavior when the user hits
362c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                          // enter.
363c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    DOWN_WITH_CHANGE,     // The control key is depressed, and the edit's
364c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                          // contents/selection have changed since it was
365c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                          // depressed.  If the user now hits enter, we assume
366c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                          // he simply hasn't released the key, rather than that
367c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                          // he intended to hit "ctrl-enter".
368c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  };
369c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
370dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // AutocompleteControllerDelegate:
371dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  virtual void OnResultChanged(bool default_match_changed);
372dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
373dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Returns true if a query to an autocomplete provider is currently
374dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // in progress.  This logic should in the future live in
375dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // AutocompleteController but resides here for now.  This method is used by
376dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // AutomationProvider::AutocompleteEditIsQueryInProgress.
377dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  bool query_in_progress() const;
378c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
379c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called whenever user_text_ should change.
38072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void InternalSetUserText(const string16& text);
381c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
3823345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Returns true if a keyword is selected.
3833345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool KeywordIsSelected() const;
3843345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
385c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Conversion between user text and display text. User text is the text the
386c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // user has input. Display text is the text being shown in the edit. The
387c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // two are different if a keyword is selected.
38872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  string16 DisplayTextFromUserText(const string16& text) const;
38972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  string16 UserTextFromDisplayText(const string16& text) const;
390c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
391dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Copies the selected match into |match|.  If an update is in progress,
392dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // "selected" means "default in the latest matches".  If there are no matches,
393dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // does not update |match|.
394dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  //
395dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // If |alternate_nav_url| is non-NULL, it will be set to the alternate
396dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // navigation URL for |url| if one exists, or left unchanged otherwise.  See
397dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // comments on AutocompleteResult::GetAlternateNavURL().
398dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  //
399dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // TODO(pkasting): When manually_selected_match_ moves to the controller, this
400dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // can move too.
401dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  void InfoForCurrentSelection(AutocompleteMatch* match,
402dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen                               GURL* alternate_nav_url) const;
403dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
404c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the default match for the current text, as well as the alternate
405c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // nav URL, if |alternate_nav_url| is non-NULL and there is such a URL.
406c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void GetInfoForCurrentText(AutocompleteMatch* match,
407c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                             GURL* alternate_nav_url) const;
408c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
40921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  // Returns true if |text| (which is display text in the current context)
41021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  // parses as a URL, and in that case sets |url| to the calculated URL.
41121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  // Subtle note: This ignores the desired_tld_ (unlike GetDataForURLExport()
41221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  // and CurrentTextIsURL()).  The view needs this because it calls this
41321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  // function during copy handling, when the control key is down to trigger the
41421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  // copy.
41572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool GetURLForText(const string16& text, GURL* url) const;
41672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
41772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Reverts the edit box from a temporary text back to the original user text.
41872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // If |revert_popup| is true then the popup will be reverted as well.
41972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void RevertTemporaryText(bool revert_popup);
42021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen
42172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Accepts current keyword if the user only typed a space at the end of
42272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // |new_user_text| comparing to the |old_user_text|.
42372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Returns true if the current keyword is accepted.
42472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool MaybeAcceptKeywordBySpace(const string16& old_user_text,
42572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                                 const string16& new_user_text);
42672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
427ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Checks if |allow_exact_keyword_match_| should be set to true according to
428ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // the old and new user text and the current caret position. It does not take
429ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // other factors into account, e.g. if the view is ready to change the keyword
430ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // ui or not. This is only for the case of inserting a space character in the
431ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // middle of the text. See the comment of |allow_exact_keyword_match_| below.
432ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool ShouldAllowExactKeywordMatch(const string16& old_user_text,
433ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                    const string16& new_user_text,
434ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                    size_t caret_position);
435ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
43672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Checks if a given character is a valid space character for accepting
43772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // keyword.
43872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  static bool IsSpaceCharForAcceptingKeyword(wchar_t c);
439513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
440dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  scoped_ptr<AutocompleteController> autocomplete_controller_;
441dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
442c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompleteEditView* view_;
443c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
444c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompletePopupModel* popup_;
445c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
446c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompleteEditController* controller_;
447c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
448c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Whether the edit has focus.
449c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool has_focus_;
450c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
451c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The URL of the currently displayed page.
45272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  string16 permanent_text_;
453c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
454c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // This flag is true when the user has modified the contents of the edit, but
455c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // not yet accepted them.  We use this to determine when we need to save
456c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // state (on switching tabs) and whether changes to the page URL should be
457c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // immediately displayed.
458c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // This flag will be true in a superset of the cases where the popup is open.
459c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool user_input_in_progress_;
460c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
461c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The text that the user has entered.  This does not include inline
462c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // autocomplete text that has not yet been accepted.
46372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  string16 user_text_;
464c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
465c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // When the user closes the popup, we need to remember the URL for their
466c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // desired choice, so that if they hit enter without reopening the popup we
467c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // know where to go.  We could simply rerun autocomplete in this case, but
468c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // we'd need to either wait for all results to come in (unacceptably slow) or
469c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // do the wrong thing when the user had chosen some provider whose results
470c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // were not returned instantaneously.
471c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
472c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // This variable is only valid when user_input_in_progress_ is true, since
473c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // when it is false the user has either never input anything (so there won't
474c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // be a value here anyway) or has canceled their input, which should be
475c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // treated the same way.  Also, since this is for preserving a desired URL
476c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // after the popup has been closed, we ignore this if the popup is open, and
477c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // simply ask the popup for the desired URL directly.  As a result, the
478c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // contents of this variable only need to be updated when the popup is closed
479c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // but user_input_in_progress_ is not being cleared.
48072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  string16 url_for_remembered_user_selection_;
481c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
482c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Inline autocomplete is allowed if the user has not just deleted text, and
483c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // no temporary text is showing.  In this case, inline_autocomplete_text_ is
484c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // appended to the user_text_ and displayed selected (at least initially).
485c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
486c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // NOTE: When the popup is closed there should never be inline autocomplete
487c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // text (actions that close the popup should either accept the text, convert
488c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // it to a normal selection, or change the edit entirely).
489c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool just_deleted_text_;
49072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  string16 inline_autocomplete_text_;
491c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
492c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Used by OnPopupDataChanged to keep track of whether there is currently a
493c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // temporary text.
494c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
495c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Example of use: If the user types "goog", then arrows down in the
496c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // autocomplete popup until, say, "google.com" appears in the edit box, then
497c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the user_text_ is still "goog", and "google.com" is "temporary text".
498c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // When the user hits <esc>, the edit box reverts to "goog".  Hit <esc> again
499c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // and the popup is closed and "goog" is replaced by the permanent_text_,
500c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // which is the URL of the current page.
501c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
502c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // original_url_ is only valid when there is temporary text, and is used as
503c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the unique identifier of the originally selected item.  Thus, if the user
504c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // arrows to a different item with the same text, we can still distinguish
505c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // them and not revert all the way to the permanent_text_.
506c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool has_temporary_text_;
507c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  GURL original_url_;
508c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
50972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // When the user's last action was to paste, we disallow inline autocomplete
51072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // (on the theory that the user is trying to paste in a new URL or part of
51172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // one, and in either case inline autocomplete would get in the way).
512c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  PasteState paste_state_;
513c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
514c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Whether the control key is depressed.  We track this to avoid calling
515c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // UpdatePopup() repeatedly if the user holds down the key, and to know
516c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // whether to trigger "ctrl-enter" behavior.
517c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ControlKeyState control_key_state_;
518c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
519c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The keyword associated with the current match.  The user may have an actual
520c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // selected keyword, or just some input text that looks like a keyword (so we
521c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // can show a hint to press <tab>).  This is the keyword in either case;
522c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // is_keyword_hint_ (below) distinguishes the two cases.
52372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  string16 keyword_;
524c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
525c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // True if the keyword associated with this match is merely a hint, i.e. the
526c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // user hasn't actually selected a keyword yet.  When this is true, we can use
527c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // keyword_ to show a "Press <tab> to search" sort of hint.
528c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool is_keyword_hint_;
529c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
530c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Paste And Go-related state.  See CanPasteAndGo().
531c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  mutable GURL paste_and_go_url_;
532c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  mutable PageTransition::Type paste_and_go_transition_;
533c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  mutable GURL paste_and_go_alternate_nav_url_;
534c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
535c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Profile* profile_;
536c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
537dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Should instant be updated? This is needed as prior to accepting the current
538dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // text the model is reverted, which triggers resetting instant. We don't want
539dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // to update instant in this case, so we use the flag to determine if this is
540dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // happening.
541dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  bool update_instant_;
542dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
543ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Indicates if the upcoming autocomplete search is allowed to be treated as
544ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // an exact keyword match. If it's true then keyword mode will be triggered
545ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // automatically if the input is "<keyword> <search string>". We only allow
546ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // such trigger when:
547ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // 1.A single space character is added at the end of a keyword, such as:
548ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  //   (assume "foo" is a keyword, | is the input caret)
549ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  //   foo| -> foo |
550ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  //   foo[bar] -> foo |  ([bar] indicates a selected text "bar")
551ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // 2.A single space character is inserted after a keyword when the caret is
552ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  //   not at the end of the line, such as:
553ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  //   foo|bar -> foo |bar
554ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  //
555ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // It has no effect if a keyword is already selected.
556ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool allow_exact_keyword_match_;
557ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
558ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Last value of InstantCompleteBehavior supplied to |SetSuggestedText|.
559ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  InstantCompleteBehavior instant_complete_behavior_;
560ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
561c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DISALLOW_COPY_AND_ASSIGN(AutocompleteEditModel);
562c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
563c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
564c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_
565