searchbox.h revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
1// Copyright 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#ifndef CHROME_RENDERER_SEARCHBOX_SEARCHBOX_H_
6#define CHROME_RENDERER_SEARCHBOX_SEARCHBOX_H_
7
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/string16.h"
12#include "chrome/common/instant_restricted_id_cache.h"
13#include "chrome/common/instant_types.h"
14#include "chrome/common/search_types.h"
15#include "content/public/common/page_transition_types.h"
16#include "content/public/renderer/render_view_observer.h"
17#include "content/public/renderer/render_view_observer_tracker.h"
18#include "ui/base/window_open_disposition.h"
19#include "ui/gfx/rect.h"
20
21namespace content {
22class RenderView;
23}
24
25class SearchBox : public content::RenderViewObserver,
26                  public content::RenderViewObserverTracker<SearchBox> {
27 public:
28  explicit SearchBox(content::RenderView* render_view);
29  virtual ~SearchBox();
30
31  // Sends ChromeViewHostMsg_SetSuggestion to the browser.
32  void SetSuggestions(const std::vector<InstantSuggestion>& suggestions);
33
34  // Marks the current query text as restricted, to make sure that it does
35  // not get communicated to the page.  The restricted status lasts until the
36  // query is next changed or cleared.
37  void MarkQueryAsRestricted();
38
39  // Sends ChromeViewHostMsg_ShowInstantOverlay to the browser.
40  void ShowInstantOverlay(int height, InstantSizeUnits units);
41
42  // Sends ChromeViewHostMsg_FocusOmnibox to the browser.
43  void FocusOmnibox();
44
45  // Sends ChromeViewHostMsg_StartCapturingKeyStrokes to the browser.
46  void StartCapturingKeyStrokes();
47
48  // Sends ChromeViewHostMsg_StopCapturingKeyStrokes to the browser.
49  void StopCapturingKeyStrokes();
50
51  // Sends ChromeViewHostMsg_SearchBoxNavigate to the browser.
52  void NavigateToURL(const GURL& url,
53                     content::PageTransition transition,
54                     WindowOpenDisposition disposition);
55
56  // Shows any attached bars.
57  void ShowBars();
58
59  // Hides any attached bars.  When the bars are hidden, the "onbarshidden"
60  // event is fired to notify the page.
61  void HideBars();
62
63  const string16& query() const { return query_; }
64  bool verbatim() const { return verbatim_; }
65  bool query_is_restricted() const { return query_is_restricted_; }
66  size_t selection_start() const { return selection_start_; }
67  size_t selection_end() const { return selection_end_; }
68  bool is_key_capture_enabled() const { return is_key_capture_enabled_; }
69  bool display_instant_results() const { return display_instant_results_; }
70  const string16& omnibox_font() const { return omnibox_font_; }
71  size_t omnibox_font_size() const { return omnibox_font_size_; }
72
73  // In extended Instant, returns the start-edge margin of the location bar in
74  // screen pixels.
75  int GetStartMargin() const;
76
77  // Returns the bounds of the omnibox popup in screen coordinates.
78  gfx::Rect GetPopupBounds() const;
79
80  const ThemeBackgroundInfo& GetThemeBackgroundInfo();
81
82  // --- Autocomplete result APIs.
83
84  // Returns the most recent InstantAutocompleteResults sent by the browser.
85  void GetAutocompleteResults(
86      std::vector<InstantAutocompleteResultIDPair>* results) const;
87
88  // If the |autocomplete_result_id| is found in the cache, sets |item| to it
89  // and returns true.
90  bool GetAutocompleteResultWithID(InstantRestrictedID autocomplete_result_id,
91                                   InstantAutocompleteResult* result) const;
92
93  // --- Most Visited items APIs.
94
95  // Returns the latest most visited items sent by the browser.
96  void GetMostVisitedItems(
97      std::vector<InstantMostVisitedItemIDPair>* items) const;
98
99  // If the |most_visited_item_id| is found in the cache, sets |item| to it
100  // and returns true.
101  bool GetMostVisitedItemWithID(InstantRestrictedID most_visited_item_id,
102                                InstantMostVisitedItem* item) const;
103
104  // Sends ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem to the browser.
105  void DeleteMostVisitedItem(InstantRestrictedID most_visited_item_id);
106
107  // Sends ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion to the browser.
108  void UndoMostVisitedDeletion(InstantRestrictedID most_visited_item_id);
109
110  // Sends ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions to the
111  // browser.
112  void UndoAllMostVisitedDeletions();
113
114 private:
115  // Overridden from content::RenderViewObserver:
116  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
117  virtual void DidClearWindowObject(WebKit::WebFrame* frame) OVERRIDE;
118
119  void OnChange(const string16& query,
120                bool verbatim,
121                size_t selection_start,
122                size_t selection_end);
123  void OnSubmit(const string16& query);
124  void OnCancel(const string16& query);
125  void OnPopupResize(const gfx::Rect& bounds);
126  void OnMarginChange(int margin, int width);
127  void OnBarsHidden();
128  void OnDetermineIfPageSupportsInstant();
129  void OnAutocompleteResults(
130      const std::vector<InstantAutocompleteResult>& results);
131  void OnUpOrDownKeyPressed(int count);
132  void OnEscKeyPressed();
133  void OnCancelSelection(const string16& query,
134                         bool verbatim,
135                         size_t selection_start,
136                         size_t selection_end);
137  void OnKeyCaptureChange(bool is_key_capture_enabled);
138  void OnSetDisplayInstantResults(bool display_instant_results);
139  void OnThemeChanged(const ThemeBackgroundInfo& theme_info);
140  void OnThemeAreaHeightChanged(int height);
141  void OnFontInformationReceived(const string16& omnibox_font,
142                                 size_t omnibox_font_size);
143  void OnMostVisitedChanged(
144      const std::vector<InstantMostVisitedItemIDPair>& items);
145
146  // Returns the current zoom factor of the render view or 1 on failure.
147  double GetZoom() const;
148
149  // Sets the searchbox values to their initial value.
150  void Reset();
151
152  // Sets the query to a new value.
153  void SetQuery(const string16& query, bool verbatim);
154
155  string16 query_;
156  bool verbatim_;
157  bool query_is_restricted_;
158  size_t selection_start_;
159  size_t selection_end_;
160  int start_margin_;
161  gfx::Rect popup_bounds_;
162  bool is_key_capture_enabled_;
163  ThemeBackgroundInfo theme_info_;
164  bool display_instant_results_;
165  string16 omnibox_font_;
166  size_t omnibox_font_size_;
167  InstantRestrictedIDCache<InstantAutocompleteResult>
168      autocomplete_results_cache_;
169  InstantRestrictedIDCache<InstantMostVisitedItem> most_visited_items_cache_;
170
171  DISALLOW_COPY_AND_ASSIGN(SearchBox);
172};
173
174#endif  // CHROME_RENDERER_SEARCHBOX_SEARCHBOX_H_
175