Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Side by Side Diff: chrome/browser/instant/instant_loader.h

Issue 10732002: Upstream rewrite of Instant. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge test Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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_LOADER_H_ 5 #ifndef CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_
6 #define CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ 6 #define CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector>
9 10
10 #include "base/basictypes.h" 11 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h" 12 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "base/string16.h" 14 #include "base/string16.h"
14 #include "base/timer.h"
15 #include "chrome/browser/instant/instant_commit_type.h" 15 #include "chrome/browser/instant/instant_commit_type.h"
16 #include "chrome/browser/search_engines/template_url_id.h"
17 #include "chrome/common/instant_types.h" 16 #include "chrome/common/instant_types.h"
17 #include "googleurl/src/gurl.h"
18
19 #if defined(OS_MACOSX)
18 #include "content/public/browser/notification_observer.h" 20 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h" 21 #include "content/public/browser/notification_registrar.h"
20 #include "content/public/common/page_transition_types.h" 22 #endif
21 #include "googleurl/src/gurl.h"
22 #include "ui/gfx/rect.h"
23 23
24 class InstantLoaderDelegate; 24 class InstantLoaderDelegate;
25 class InstantLoaderManagerTest;
26 class TabContents; 25 class TabContents;
27 class TemplateURL;
28 26
29 namespace content { 27 namespace gfx {
30 class SessionStorageNamespace; 28 class Rect;
31 } 29 }
32 30
33 // InstantLoader does the loading of a particular URL for InstantController. 31 // |InstantLoader| is created with an "Instant URL". It loads the URL and tells
34 // InstantLoader notifies its delegate, which is typically InstantController, of 32 // its delegate (usually |InstantController|) of all interesting events. For
35 // all interesting events. 33 // example, it determines if the page actually supports the Instant API
36 // 34 // (http://dev.chromium.org/searchbox) and forwards messages (such as queries
37 // InstantLoader is created with a TemplateURLID. If non-zero InstantLoader 35 // and autocomplete suggestions) between the page and the delegate.
38 // first determines if the site actually supports instant. If it doesn't, the 36 class InstantLoader
39 // delegate is notified by way of |InstantLoaderDoesntSupportInstant|. 37 #if defined(OS_MACOSX)
sky 2012/07/17 18:21:42 All the mac specific ifdefs make the code hard to
Jered 2012/07/18 17:38:58 Done.
40 // 38 : public content::NotificationObserver
41 // If the TemplateURLID supplied to the constructor is zero, then the url is 39 #endif
42 // loaded as is. 40 {
43 class InstantLoader : public content::NotificationObserver {
44 public: 41 public:
45 // Header and value set on loads that originate from instant. 42 // Creates a new WebContents and loads |instant_url| into it. |tab_contents|
46 static const char* const kInstantHeader; 43 // is the page the preview will be shown on top of and potentially replace.
sky 2012/07/17 18:21:42 Document loader_id.
Jered 2012/07/18 17:38:58 Done.
47 static const char* const kInstantHeaderValue;
48
49 // |group| is an identifier suffixed to histograms to distinguish field trial
50 // statistics from regular operation; can be a blank string.
51 InstantLoader(InstantLoaderDelegate* delegate, 44 InstantLoader(InstantLoaderDelegate* delegate,
52 TemplateURLID id, 45 const string16& loader_id,
53 const std::string& group); 46 const GURL& instant_url,
47 const TabContents* tab_contents);
54 virtual ~InstantLoader(); 48 virtual ~InstantLoader();
55 49
56 // Invoked to load a URL. |tab_contents| is the TabContents the preview 50 // Tells the preview page that the user typed |user_text| into the omnibox.
57 // is going to be shown on top of and potentially replace. Returns true if the 51 // If |verbatim| is false, the page predicts the query the user means to type
58 // arguments differ from the last call to |Update|. 52 // and fetches results for the prediction. If |verbatim| is true, |user_text|
59 bool Update(TabContents* tab_contents, 53 // is taken as the exact query (no prediction is made).
60 const TemplateURL* template_url, 54 void Update(const std::string& user_text, bool verbatim);
61 const GURL& url,
62 content::PageTransition transition_type,
63 const string16& user_text,
64 bool verbatim,
65 string16* suggested_text);
66 55
67 // Sets the bounds of the omnibox (in screen coordinates). The bounds are 56 // Tells the preview page of the bounds of the omnibox dropdown (in screen
68 // remembered until the preview is committed or destroyed. This is only used 57 // coordinates). This is used by the page to offset the results to avoid them
69 // when showing results for a search provider that supports instant. 58 // being covered by the omnibox dropdown.
70 void SetOmniboxBounds(const gfx::Rect& bounds); 59 void SetOmniboxBounds(const gfx::Rect& bounds);
71 60
72 // Returns true if the mouse is down as the result of activating the preview 61 // Releases the preview TabContents passing ownership to the caller. This
73 // content. 62 // should be called when the preview is committed. Notifies the page but not
74 bool IsMouseDownFromActivate(); 63 // the delegate. |text| is the final omnibox text being committed. NOTE: The
64 // caller should destroy this loader object right after this method, since
65 // none of the other methods will work once the preview has been released.
66 TabContents* ReleasePreviewContents(
67 InstantCommitType type,
68 const std::string& text) WARN_UNUSED_RESULT;
75 69
76 // Releases the preview TabContents passing ownership to the caller. 70 // Returns the ID this loader was created with.
77 // This is intended to be called when the preview TabContents is 71 string16 loader_id() const { return loader_id_; }
sky 2012/07/17 18:21:42 const string16&
Jered 2012/07/18 17:38:58 Done.
78 // committed. This does not notify the delegate. |tab_contents| is the
79 // underlying tab onto which the preview will be committed. It can be NULL
80 // when the underlying tab is irrelevant, for example when |type| is
81 // INSTANT_COMMIT_DESTROY.
82 TabContents* ReleasePreviewContents(InstantCommitType type,
83 TabContents* tab_contents);
84 72
85 // Calls through to method of same name on delegate. 73 // The preview TabContents. The loader retains ownership. This will be
86 bool ShouldCommitInstantOnMouseUp(); 74 // non-NULL until |ReleasePreviewContents| is called.
sky 2012/07/17 18:21:42 |'s are only used for fields/arguments. Use () for
Jered 2012/07/18 17:38:58 Done.
87 void CommitInstantLoader(); 75 TabContents* preview_contents() const { return preview_contents_.get(); }
88 76
89 // Preload |template_url|'s instant URL, if the loader doesn't already have 77 // Returns true if the preview page is known to support the Instant API. This
90 // a |preview_contents()| for it. 78 // starts out false, and becomes true whenever we get any message from the
91 void MaybeLoadInstantURL(TabContents* tab_contents, 79 // page. Once true, it never becomes false (the page isn't expected to drop
92 const TemplateURL* template_url); 80 // Instant API support suddenly).
81 bool supports_instant() const { return supports_instant_; }
93 82
94 // Returns true if the preview NavigationController's WebContents has a 83 // Returns the URL that we're loading.
95 // pending NavigationEntry. 84 const GURL& instant_url() const { return instant_url_; }
96 bool IsNavigationPending() const;
97 85
86 // Returns true if the mouse is down due to activating the preview content.
87 bool IsMouseDownFromActivate() const;
88
89 #if defined(OS_MACOSX)
98 // content::NotificationObserver: 90 // content::NotificationObserver:
99 virtual void Observe(int type, 91 virtual void Observe(int type,
100 const content::NotificationSource& source, 92 const content::NotificationSource& source,
101 const content::NotificationDetails& details) OVERRIDE; 93 const content::NotificationDetails& details) OVERRIDE;
102 94 #endif
103 // The preview TabContents; may be null.
104 TabContents* preview_contents() const {
105 return preview_contents_.get();
106 }
107
108 // Returns true if the preview TabContents is ready to be shown. A
109 // non-instant loader is ready once the renderer paints, otherwise it isn't
110 // ready until we get a response back from the page.
111 bool ready() const { return ready_; }
112
113 // Returns true if the current load returned a 200.
114 bool http_status_ok() const { return http_status_ok_; }
115
116 // Returns true if the url needs to be reloaded. This is set to true for
117 // downloads.
118 bool needs_reload() const { return needs_reload_; }
119
120 const GURL& url() const { return url_; }
121
122 bool verbatim() const { return verbatim_; }
123
124 // Are we showing instant results?
125 bool is_showing_instant() const { return template_url_id_ != 0; }
126
127 // If we're showing instant this returns non-zero.
128 TemplateURLID template_url_id() const { return template_url_id_; }
129
130 // See description above field.
131 const string16& user_text() const { return user_text_; }
132
133 // Are we waiting for the preview page to finish loading and to determine if
134 // it supports instant?
135 bool is_determining_if_page_supports_instant() const {
136 return frame_load_observer_.get() != NULL;
137 }
138 95
139 private: 96 private:
140 friend class InstantLoaderManagerTest;
141 friend class InstantTest;
142 class FrameLoadObserver;
143 class PaintObserverImpl;
144 class WebContentsDelegateImpl; 97 class WebContentsDelegateImpl;
145 98
146 // Invoked when the page wants to update the suggested text. If |user_text_| 99 void SetupPreviewContents();
147 // starts with |suggested_text|, then the delegate is notified of the change, 100 void CleanupPreviewContents();
148 // which results in updating the omnibox. 101 void ReplacePreviewContents(TabContents* old_tc, TabContents* new_tc);
149 void SetCompleteSuggestedText(const string16& suggested_text,
150 InstantCompleteBehavior behavior);
151 102
152 // Invoked when the page paints. 103 InstantLoaderDelegate* const loader_delegate_;
153 void PreviewPainted();
154 104
155 // Invoked when the http status code changes. This may notify the delegate. 105 // See comments on the getter above.
156 void SetHTTPStatusOK(bool is_ok); 106 const string16 loader_id_;
157 107
158 // Invoked to show the preview. This is invoked in two possible cases: when 108 // See comments on the getter above.
159 // the renderer paints, or when an auth dialog is shown. This notifies the 109 scoped_ptr<TabContents> preview_contents_;
160 // delegate the preview is ready to be shown.
161 void ShowPreview();
162
163 // Invoked once the page has finished loading and the script has been sent.
164 void PageFinishedLoading();
165
166 // Returns the bounds of the omnibox in terms of the preview tab contents.
167 gfx::Rect GetOmniboxBoundsInTermsOfPreview();
168
169 // Invoked if it the page doesn't really support instant when we thought it
170 // did. If |needs_reload| is true, the text changed since the first load and
171 // the page needs to be reloaded.
172 void PageDoesntSupportInstant(bool needs_reload);
173
174 // Invokes |SetBoundsToPage(false)|. This is called from the timer.
175 void ProcessBoundsChange();
176
177 // Notifes the page of the omnibox bounds. If |force_if_loading| is true the
178 // bounds are sent down even if we're waiting on the load, otherwise if we're
179 // waiting on the load and |force_if_loading| is false this does nothing.
180 void SendBoundsToPage(bool force_if_loading);
181
182 // Called when the TabContentsDelegate wants to swap a new TabContents
183 // into our |preview_contents_|.
184 void ReplacePreviewContents(TabContents* old_tc,
185 TabContents* new_tc);
186
187 // Called to set up the |preview_contents_| based on |tab_contents| when it is
188 // created or replaced.
189 void SetupPreviewContents(TabContents* tab_contents);
190
191 // Creates and sets the preview TabContents.
192 void CreatePreviewContents(TabContents* tab_contents);
193
194 // Creates and loads the |template_url|'s instant URL.
195 void LoadInstantURL(const TemplateURL* template_url,
196 content::PageTransition transition_type,
197 const string16& user_text,
198 bool verbatim,
199 bool override_user_agent);
200
201 InstantLoaderDelegate* delegate_;
202
203 // If we're showing instant results this is the ID of the TemplateURL driving
204 // the results. A value of 0 means there is no TemplateURL.
205 const TemplateURLID template_url_id_;
206
207 // The url we're displaying.
208 GURL url_;
209 110
210 // Delegate of the preview WebContents. Used to detect when the user does some 111 // Delegate of the preview WebContents. Used to detect when the user does some
211 // gesture on the WebContents and the preview needs to be activated. 112 // gesture on the WebContents and the preview needs to be activated.
212 scoped_ptr<WebContentsDelegateImpl> preview_tab_contents_delegate_; 113 scoped_ptr<WebContentsDelegateImpl> preview_delegate_;
213 114
214 // The preview TabContents; may be null. 115 // See comments on the getter above.
215 scoped_ptr<TabContents> preview_contents_; 116 bool supports_instant_;
216 117
217 // Is the preview_contents ready to be shown? 118 // The URL that we're loading.
218 bool ready_; 119 const GURL instant_url_;
219 120
220 // Was the last status code a 200? 121 #if defined(OS_MACOSX)
221 bool http_status_ok_;
222
223 // The text the user typed in the omnibox, stripped of the leading ?, if any.
224 string16 user_text_;
225
226 // The latest suggestion from the page.
227 string16 complete_suggested_text_;
228
229 // The latest suggestion (suggested text less the user text).
230 string16 last_suggestion_;
231
232 // See description above setter.
233 gfx::Rect omnibox_bounds_;
234
235 // Last bounds passed to the page.
236 gfx::Rect last_omnibox_bounds_;
237
238 scoped_ptr<FrameLoadObserver> frame_load_observer_;
239
240 // Transition type of the match last passed to Update.
241 content::PageTransition last_transition_type_;
242
243 // Timer used to update the bounds of the omnibox.
244 base::OneShotTimer<InstantLoader> update_bounds_timer_;
245
246 // Used to get notifications about renderers coming and going. 122 // Used to get notifications about renderers coming and going.
247 content::NotificationRegistrar registrar_; 123 content::NotificationRegistrar registrar_;
248 124 #endif
249 // Last value of verbatim passed to |Update|.
250 bool verbatim_;
251
252 // True if the page needs to be reloaded.
253 bool needs_reload_;
254
255 // See description above constructor.
256 std::string group_;
257
258 // The session storage namespace identifier of the original tab contents that
259 // the preview_contents_ was based upon.
260 scoped_refptr<content::SessionStorageNamespace> session_storage_namespace_;
261 125
262 DISALLOW_COPY_AND_ASSIGN(InstantLoader); 126 DISALLOW_COPY_AND_ASSIGN(InstantLoader);
263 }; 127 };
264 128
265 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ 129 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698