OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_INSTANT_INSTANT_PAGE_H_ | 5 #ifndef CHROME_BROWSER_INSTANT_INSTANT_PAGE_H_ |
6 #define CHROME_BROWSER_INSTANT_INSTANT_PAGE_H_ | 6 #define CHROME_BROWSER_INSTANT_INSTANT_PAGE_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/string16.h" | 13 #include "base/string16.h" |
13 #include "chrome/common/instant_types.h" | |
14 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
15 #include "content/public/common/page_transition_types.h" | 15 #include "content/public/common/page_transition_types.h" |
| 16 #include "ui/base/window_open_disposition.h" |
16 | 17 |
17 class GURL; | 18 class GURL; |
| 19 struct InstantAutocompleteResult; |
| 20 class InstantService; |
| 21 struct InstantSuggestion; |
| 22 struct MostVisitedItem; |
| 23 struct ThemeBackgroundInfo; |
18 | 24 |
19 namespace content { | 25 namespace content { |
20 class WebContents; | 26 class WebContents; |
21 } | 27 } |
22 | 28 |
23 namespace gfx { | 29 namespace gfx { |
24 class Rect; | 30 class Rect; |
25 } | 31 } |
26 | 32 |
| 33 namespace IPC { |
| 34 class Message; |
| 35 } |
| 36 |
27 // InstantPage is used to exchange messages with a page that implements the | 37 // InstantPage is used to exchange messages with a page that implements the |
28 // Instant/Embedded Search API (http://dev.chromium.org/embeddedsearch). | 38 // Instant/Embedded Search API (http://dev.chromium.org/embeddedsearch). |
29 // InstantPage is not used directly but via one of its derived classes: | |
30 // InstantOverlay, InstantNTP and InstantTab. | |
31 class InstantPage : public content::WebContentsObserver { | 39 class InstantPage : public content::WebContentsObserver { |
32 public: | 40 public: |
33 // InstantPage calls its delegate in response to messages received from the | 41 // InstantPage calls its delegate in response to messages received from the |
34 // page. Each method is called with the |contents| corresponding to the page | 42 // page. Each method is called with the |contents| corresponding to the page |
35 // we are observing. | 43 // we are observing. |
36 class Delegate { | 44 class Delegate { |
37 public: | 45 public: |
38 // Called when a RenderView is created, so that state can be initialized. | 46 // Called when the underlying RenderView crashed. |
39 virtual void InstantPageRenderViewCreated( | 47 virtual void RenderViewGone(const content::WebContents* contents) = 0; |
| 48 |
| 49 // Called to request the delegate to initialize searchbox state, such as |
| 50 // theme, omnibox font, size, width, margin, Instant preference, etc. |
| 51 virtual void InitSearchBox(const content::WebContents* contents) = 0; |
| 52 |
| 53 // Called whem messages are received from the page. See instant_messages.h |
| 54 // for details. |
| 55 virtual void InstantSupportDetermined( |
40 const content::WebContents* contents) = 0; | 56 const content::WebContents* contents) = 0; |
41 | 57 virtual void SetSuggestion(const content::WebContents* contents, |
42 // Called upon determination of Instant API support. Either in response to | 58 const InstantSuggestion& suggestion) = 0; |
43 // the page loading or because we received some other message. | |
44 virtual void InstantSupportDetermined(const content::WebContents* contents, | |
45 bool supports_instant) = 0; | |
46 | |
47 // Called when the underlying RenderView crashed. | |
48 virtual void InstantPageRenderViewGone( | |
49 const content::WebContents* contents) = 0; | |
50 | |
51 // Called when the page is about to navigate to |url|. | |
52 virtual void InstantPageAboutToNavigateMainFrame( | |
53 const content::WebContents* contents, | |
54 const GURL& url) = 0; | |
55 | |
56 // Called when the page has suggestions. Usually in response to Update(), | |
57 // SendAutocompleteResults() or UpOrDownKeyPressed(). | |
58 virtual void SetSuggestions( | |
59 const content::WebContents* contents, | |
60 const std::vector<InstantSuggestion>& suggestions) = 0; | |
61 | |
62 // Called when the page wants to be shown. Usually in response to Update() | |
63 // or SendAutocompleteResults(). | |
64 virtual void ShowInstantOverlay(const content::WebContents* contents, | |
65 InstantShownReason reason, | |
66 int height, | |
67 InstantSizeUnits units) = 0; | |
68 | |
69 // Called when the page wants the omnibox to start capturing user key | |
70 // strokes. If this call is processed successfully, the omnibox will not | |
71 // look focused visibly but any user key strokes will go to the omnibox. | |
72 // Currently, this is implemented by focusing the omnibox invisibly. | |
73 virtual void StartCapturingKeyStrokes( | |
74 const content::WebContents* contents) = 0; | |
75 | |
76 // Called when the page wants the omnibox to stop capturing user key | |
77 // strokes. | |
78 virtual void StopCapturingKeyStrokes(content::WebContents* contents) = 0; | |
79 | |
80 // Called when the page wants to navigate to |url|. Usually used by the | |
81 // page to navigate to privileged destinations (e.g. chrome:// URLs) or to | |
82 // navigate to URLs that are hidden from the page using Restricted IDs (rid | |
83 // in the API). | |
84 virtual void NavigateToURL(const content::WebContents* contents, | 59 virtual void NavigateToURL(const content::WebContents* contents, |
85 const GURL& url, | 60 const GURL& url, |
86 content::PageTransition transition, | 61 content::PageTransition transition, |
87 WindowOpenDisposition disposition) = 0; | 62 WindowOpenDisposition disposition) = 0; |
88 | 63 virtual void ShowOverlay(const content::WebContents* contents, |
89 // Called when the SearchBox wants to delete a Most Visited item. | 64 int height, |
90 virtual void DeleteMostVisitedItem(const GURL& url) = 0; | 65 bool is_height_in_pixels) = 0; |
91 | 66 virtual void StartKeyCapture(const content::WebContents* contents) = 0; |
92 // Called when the SearchBox wants to undo a Most Visited deletion. | 67 virtual void StopKeyCapture(const content::WebContents* contents) = 0; |
93 virtual void UndoMostVisitedDeletion(const GURL& url) = 0; | 68 virtual void DeleteMostVisitedItem(const content::WebContents* contents, |
94 | 69 const GURL& url) = 0; |
95 // Called when the SearchBox wants to undo all Most Visited deletions. | 70 virtual void UndoMostVisitedItemDeletion( |
96 virtual void UndoAllMostVisitedDeletions() = 0; | 71 const content::WebContents* contents, |
| 72 const GURL& url) = 0; |
| 73 virtual void UndoAllMostVisitedItemDeletions( |
| 74 const content::WebContents* contents) = 0; |
97 | 75 |
98 protected: | 76 protected: |
99 virtual ~Delegate(); | 77 virtual ~Delegate(); |
100 }; | 78 }; |
101 | 79 |
| 80 InstantPage(Delegate* delegate, InstantService* service); |
102 virtual ~InstantPage(); | 81 virtual ~InstantPage(); |
103 | 82 |
104 // The WebContents corresponding to the page we're talking to. May be NULL. | 83 // The WebContents corresponding to the page we're talking to. May be NULL. |
105 content::WebContents* contents() const { return web_contents(); } | 84 content::WebContents* contents() const { return web_contents(); } |
106 | 85 |
| 86 // Starts observing / communicating with |contents|. If |contents| is NULL, |
| 87 // effectively stops all communication. |
| 88 void SetContents(content::WebContents* contents); |
| 89 |
107 // Returns true if the page is known to support the Instant API. This starts | 90 // Returns true if the page is known to support the Instant API. This starts |
108 // out false, and is set to true whenever we get any message from the page. | 91 // out false, and is set to true whenever we get any message from the page. |
109 // Once true, it never becomes false (the page isn't expected to drop API | 92 // Once true, it never becomes false as long as the page doesn't navigate (the |
110 // support suddenly). | 93 // page isn't expected to drop API support suddenly). |
111 bool supports_instant() const { return supports_instant_; } | 94 bool supports_instant() const { return supports_instant_; } |
| 95 void set_supports_instant(bool supports_instant) { |
| 96 supports_instant_ = supports_instant; |
| 97 } |
112 | 98 |
113 // Tells the page that the user typed |text| into the omnibox. If |verbatim| | 99 // Returns true if the page has navigated since the last call to Change(). |
114 // is false, the page predicts the query the user means to type and fetches | 100 bool navigated_after_change() const { return navigated_after_change_; } |
115 // results for the prediction. If |verbatim| is true, |text| is taken as the | |
116 // exact query (no prediction is made). | |
117 virtual void Update(const string16& text, | |
118 size_t selection_start, | |
119 size_t selection_end, | |
120 bool verbatim); | |
121 | 101 |
122 // Tells the page that the user pressed Enter in the omnibox. | 102 // Sends messages to the page. See instant_messages.h for details. |
123 void Submit(const string16& text); | 103 void DetermineInstantSupport(); |
124 | 104 void Change(const string16& query, |
125 // Tells the page that the user clicked on it. Nothing is being cancelled; the | 105 bool verbatim, |
126 // poor choice of name merely reflects the IPC of the same (poor) name. | 106 size_t selection_start, |
127 void Cancel(const string16& text); | 107 size_t selection_end); |
128 | 108 void Submit(const string16& query); |
129 // Tells the page the bounds of the omnibox dropdown (in screen coordinates). | 109 void AutocompleteResults( |
130 // This is used by the page to offset the results to avoid them being covered | |
131 // by the omnibox dropdown. | |
132 void SetPopupBounds(const gfx::Rect& bounds); | |
133 | |
134 // Tells the page the bounds of the omnibox (in screen coordinates). This is | |
135 // used by the page to align text or assets properly with the omnibox. | |
136 void SetOmniboxBounds(const gfx::Rect& bounds); | |
137 | |
138 // Tells the page about the font information. | |
139 void InitializeFonts(); | |
140 | |
141 // Tells the renderer to determine if the page supports the Instant API, which | |
142 // results in a call to InstantSupportDetermined() when the reply is received. | |
143 void DetermineIfPageSupportsInstant(); | |
144 | |
145 // Tells the page about the available autocomplete results. | |
146 void SendAutocompleteResults( | |
147 const std::vector<InstantAutocompleteResult>& results); | 110 const std::vector<InstantAutocompleteResult>& results); |
148 | 111 void Select(int count); |
149 // Tells the page that the user pressed Up or Down in the omnibox. |count| is | 112 void Cancel(const string16& query); |
150 // a repeat count, negative for moving up, positive for moving down. | 113 void Blur(const string16& query); |
151 void UpOrDownKeyPressed(int count); | 114 void PopupBounds(const gfx::Rect& bounds); |
152 | 115 void OmniboxBounds(const gfx::Rect& bounds); |
153 // Tells the page that the user pressed Esc in the omnibox after having | |
154 // arrowed down in the suggestions. The page should reset the selection to | |
155 // the first suggestion. |user_text| is what the omnibox has been reset to. | |
156 void CancelSelection(const string16& user_text); | |
157 | |
158 // Tells the page about the current theme background. | |
159 void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info); | |
160 | |
161 // Tells the page whether it is allowed to display Instant results. | |
162 void SetDisplayInstantResults(bool display_instant_results); | |
163 | |
164 // Tells the page whether the browser is capturing user key strokes. | |
165 void KeyCaptureChanged(bool is_key_capture_enabled); | 116 void KeyCaptureChanged(bool is_key_capture_enabled); |
166 | 117 void DisplayInstantResults(bool display_instant_results); |
167 // Tells the page about new Most Visited data. | 118 void ThemeChanged(const ThemeBackgroundInfo& theme_info); |
168 void SendMostVisitedItems(const std::vector<MostVisitedItem>& items); | 119 void FontChanged(const string16& font, size_t font_size); |
169 | 120 void MostVisitedItems(const std::vector<MostVisitedItem>& items); |
170 protected: | |
171 explicit InstantPage(Delegate* delegate); | |
172 | |
173 // Sets |contents| as the page to communicate with. |contents| may be NULL, | |
174 // which effectively stops all communication. | |
175 void SetContents(content::WebContents* contents); | |
176 | |
177 Delegate* delegate() const { return delegate_; } | |
178 | |
179 // These functions are called before processing messages received from the | |
180 // page. By default, all messages are handled, but any derived classes may | |
181 // choose to ingore some or all of the received messages by overriding these | |
182 // methods. | |
183 virtual bool ShouldProcessRenderViewCreated(); | |
184 virtual bool ShouldProcessRenderViewGone(); | |
185 virtual bool ShouldProcessAboutToNavigateMainFrame(); | |
186 virtual bool ShouldProcessSetSuggestions(); | |
187 virtual bool ShouldProcessShowInstantOverlay(); | |
188 virtual bool ShouldProcessStartCapturingKeyStrokes(); | |
189 virtual bool ShouldProcessStopCapturingKeyStrokes(); | |
190 virtual bool ShouldProcessNavigateToURL(); | |
191 | 121 |
192 private: | 122 private: |
193 // Overridden from content::WebContentsObserver: | 123 // Overridden from content::WebContentsObserver: |
194 virtual void RenderViewCreated( | 124 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE; |
195 content::RenderViewHost* render_view_host) OVERRIDE; | 125 virtual void DidNavigateMainFrame( |
| 126 const content::LoadCommittedDetails& details, |
| 127 const content::FrameNavigateParams& params) OVERRIDE; |
196 virtual void DidFinishLoad( | 128 virtual void DidFinishLoad( |
197 int64 frame_id, | 129 int64 frame_id, |
198 const GURL& validated_url, | 130 const GURL& validated_url, |
199 bool is_main_frame, | 131 bool is_main_frame, |
200 content::RenderViewHost* render_view_host) OVERRIDE; | 132 content::RenderViewHost* render_view_host) OVERRIDE; |
201 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 133 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
202 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE; | |
203 virtual void DidCommitProvisionalLoadForFrame( | |
204 int64 frame_id, | |
205 bool is_main_frame, | |
206 const GURL& url, | |
207 content::PageTransition transition_type, | |
208 content::RenderViewHost* render_view_host) OVERRIDE; | |
209 | 134 |
210 void OnSetSuggestions(int page_id, | 135 void SendMessage(scoped_ptr<IPC::Message> message); |
211 const std::vector<InstantSuggestion>& suggestions); | 136 |
212 void OnInstantSupportDetermined(int page_id, bool supports_instant); | 137 void OnInstantSupportDetermined(int page_id, bool supports_instant); |
213 void OnShowInstantOverlay(int page_id, | 138 void OnSetSuggestion(int page_id, const InstantSuggestion& suggestion); |
214 InstantShownReason reason, | 139 void OnNavigateToURL(int page_id, |
215 int height, | 140 const GURL& url, |
216 InstantSizeUnits units); | 141 content::PageTransition transition, |
217 void OnStartCapturingKeyStrokes(int page_id); | 142 WindowOpenDisposition disposition); |
218 void OnStopCapturingKeyStrokes(int page_id); | 143 void OnShowOverlay(int page_id, int height, bool is_height_in_pixels); |
219 void OnSearchBoxNavigate(int page_id, | 144 void OnStartKeyCapture(int page_id); |
220 const GURL& url, | 145 void OnStopKeyCapture(int page_id); |
221 content::PageTransition transition, | 146 void OnDeleteMostVisitedItem(int page_id, const GURL& url); |
222 WindowOpenDisposition disposition); | 147 void OnUndoMostVisitedItemDeletion(int page_id, const GURL& url); |
223 void OnDeleteMostVisitedItem(const GURL& url); | 148 void OnUndoAllMostVisitedItemDeletions(int page_id); |
224 void OnUndoMostVisitedDeletion(const GURL& url); | |
225 void OnUndoAllMostVisitedDeletions(); | |
226 | 149 |
227 Delegate* const delegate_; | 150 Delegate* const delegate_; |
| 151 InstantService* const service_; |
| 152 int routing_id_; |
228 bool supports_instant_; | 153 bool supports_instant_; |
| 154 bool navigated_after_change_; |
229 | 155 |
230 DISALLOW_COPY_AND_ASSIGN(InstantPage); | 156 DISALLOW_COPY_AND_ASSIGN(InstantPage); |
231 }; | 157 }; |
232 | 158 |
233 #endif // CHROME_BROWSER_INSTANT_INSTANT_PAGE_H_ | 159 #endif // CHROME_BROWSER_INSTANT_INSTANT_PAGE_H_ |
OLD | NEW |