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.
5 #ifndef CHROME_RENDERER_SEARCHBOX_SEARCHBOX_H_
6 #define CHROME_RENDERER_SEARCHBOX_SEARCHBOX_H_
11 #include "base/basictypes.h"
12 #include "base/strings/string16.h"
13 #include "chrome/common/instant_types.h"
14 #include "chrome/common/ntp_logging_events.h"
15 #include "chrome/common/omnibox_focus_state.h"
16 #include "chrome/renderer/instant_restricted_id_cache.h"
17 #include "content/public/renderer/render_view_observer.h"
18 #include "content/public/renderer/render_view_observer_tracker.h"
19 #include "ui/base/window_open_disposition.h"
26 class SearchBox
: public content::RenderViewObserver
,
27 public content::RenderViewObserverTracker
<SearchBox
> {
29 enum ImageSourceType
{
37 // Helper class for GenerateImageURLFromTransientURL() to adapt SearchBox's
38 // instance, thereby allow mocking for unit tests.
42 virtual ~IconURLHelper();
43 // Retruns view id for validating icon URL.
44 virtual int GetViewID() const = 0;
45 // Returns the page URL string for |rid|, or empty string for invalid |rid|.
46 virtual std::string
GetURLStringFromRestrictedID(InstantRestrictedID rid
)
50 explicit SearchBox(content::RenderView
* render_view
);
51 ~SearchBox() override
;
53 // Sends ChromeViewHostMsg_LogEvent to the browser.
54 void LogEvent(NTPLoggingEventType event
);
56 // Sends ChromeViewHostMsg_LogMostVisitedImpression to the browser.
57 void LogMostVisitedImpression(int position
, const base::string16
& provider
);
59 // Sends ChromeViewHostMsg_LogMostVisitedNavigation to the browser.
60 void LogMostVisitedNavigation(int position
, const base::string16
& provider
);
62 // Sends ChromeViewHostMsg_ChromeIdentityCheck to the browser.
63 void CheckIsUserSignedInToChromeAs(const base::string16
& identity
);
65 // Sends ChromeViewHostMsg_HistorySyncCheck to the browser.
66 void CheckIsUserSyncingHistory();
68 // Sends ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem to the browser.
69 void DeleteMostVisitedItem(InstantRestrictedID most_visited_item_id
);
71 // Generates the image URL of |type| for the most visited item specified in
72 // |transient_url|. If |transient_url| is valid, |url| with a translated URL
73 // and returns true. Otherwise it depends on |type|:
74 // - FAVICON: Returns true and renders an URL to display the default favicon.
75 // - LARGE_ICON and FALLBACK_ICON: Returns false.
77 // For |type| == FAVICON, valid forms of |transient_url|:
78 // chrome-search://favicon/<view_id>/<restricted_id>
79 // chrome-search://favicon/<favicon_parameters>/<view_id>/<restricted_id>
81 // For |type| == LARGE_ICON, valid form of |transient_url|:
82 // chrome-search://large-icon/<size>/<view_id>/<restricted_id>
84 // For |type| == FALLBACK_ICON, valid form of |transient_url|:
85 // chrome-search://fallback-icon/<icon specs>/<view_id>/<restricted_id>
87 // For |type| == THUMB, valid form of |transient_url|:
88 // chrome-search://thumb/<render_view_id>/<most_visited_item_id>
90 // We do this to prevent search providers from abusing image URLs and deduce
91 // whether the user has visited a particular page. For example, if
92 // "chrome-search://favicon/http://www.secretsite.com" is accessible, then
93 // the search provider can use its return code to determine whether the user
94 // has visited "http://www.secretsite.com". Therefore we require search
95 // providers to specify URL by "<view_id>/<restricted_id>". We then translate
96 // this to the original |url|, and pass the request to the proper endpoint.
97 bool GenerateImageURLFromTransientURL(const GURL
& transient_url
,
101 // Returns the latest most visited items sent by the browser.
102 void GetMostVisitedItems(
103 std::vector
<InstantMostVisitedItemIDPair
>* items
) const;
105 // If the |most_visited_item_id| is found in the cache, sets |item| to it
107 bool GetMostVisitedItemWithID(InstantRestrictedID most_visited_item_id
,
108 InstantMostVisitedItem
* item
) const;
110 // Sends ChromeViewHostMsg_FocusOmnibox to the browser.
113 // Sends ChromeViewHostMsg_SearchBoxNavigate to the browser.
114 void NavigateToURL(const GURL
& url
,
115 WindowOpenDisposition disposition
,
116 bool is_most_visited_item_url
);
118 // Sends ChromeViewHostMsg_SearchBoxPaste to the browser.
119 void Paste(const base::string16
& text
);
121 const ThemeBackgroundInfo
& GetThemeBackgroundInfo();
122 const EmbeddedSearchRequestParams
& GetEmbeddedSearchRequestParams();
124 // Sends ChromeViewHostMsg_SetVoiceSearchSupported to the browser.
125 void SetVoiceSearchSupported(bool supported
);
127 // Sends ChromeViewHostMsg_StartCapturingKeyStrokes to the browser.
128 void StartCapturingKeyStrokes();
130 // Sends ChromeViewHostMsg_StopCapturingKeyStrokes to the browser.
131 void StopCapturingKeyStrokes();
133 // Sends ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions to the
135 void UndoAllMostVisitedDeletions();
137 // Sends ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion to the browser.
138 void UndoMostVisitedDeletion(InstantRestrictedID most_visited_item_id
);
140 bool app_launcher_enabled() const { return app_launcher_enabled_
; }
141 bool is_focused() const { return is_focused_
; }
142 bool is_input_in_progress() const { return is_input_in_progress_
; }
143 bool is_key_capture_enabled() const { return is_key_capture_enabled_
; }
144 bool display_instant_results() const { return display_instant_results_
; }
145 const base::string16
& query() const { return query_
; }
146 int start_margin() const { return start_margin_
; }
147 const InstantSuggestion
& suggestion() const { return suggestion_
; }
150 // Overridden from content::RenderViewObserver:
151 bool OnMessageReceived(const IPC::Message
& message
) override
;
153 void OnSetPageSequenceNumber(int page_seq_no
);
154 void OnChromeIdentityCheckResult(const base::string16
& identity
,
155 bool identity_match
);
156 void OnDetermineIfPageSupportsInstant();
157 void OnFocusChanged(OmniboxFocusState new_focus_state
,
158 OmniboxFocusChangeReason reason
);
159 void OnHistorySyncCheckResult(bool sync_history
);
160 void OnMarginChange(int margin
);
161 void OnMostVisitedChanged(
162 const std::vector
<InstantMostVisitedItem
>& items
);
163 void OnPromoInformationReceived(bool is_app_launcher_enabled
);
164 void OnSetDisplayInstantResults(bool display_instant_results
);
165 void OnSetInputInProgress(bool input_in_progress
);
166 void OnSetSuggestionToPrefetch(const InstantSuggestion
& suggestion
);
167 void OnSubmit(const base::string16
& query
,
168 const EmbeddedSearchRequestParams
& params
);
169 void OnThemeChanged(const ThemeBackgroundInfo
& theme_info
);
170 void OnToggleVoiceSearch();
172 // Returns the current zoom factor of the render view or 1 on failure.
173 double GetZoom() const;
175 // Sets the searchbox values to their initial value.
178 // Returns the URL of the Most Visited item specified by the |item_id|.
179 GURL
GetURLForMostVisitedItem(InstantRestrictedID item_id
) const;
182 bool app_launcher_enabled_
;
184 bool is_input_in_progress_
;
185 bool is_key_capture_enabled_
;
186 bool display_instant_results_
;
187 InstantRestrictedIDCache
<InstantMostVisitedItem
> most_visited_items_cache_
;
188 ThemeBackgroundInfo theme_info_
;
189 base::string16 query_
;
190 EmbeddedSearchRequestParams embedded_search_request_params_
;
192 InstantSuggestion suggestion_
;
194 DISALLOW_COPY_AND_ASSIGN(SearchBox
);
197 #endif // CHROME_RENDERER_SEARCHBOX_SEARCHBOX_H_