instant_loader.h revision 201ade2fbba22bfb27ae029f4d23fca6ded109a0
1// Copyright (c) 2010 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_BROWSER_INSTANT_INSTANT_LOADER_H_ 6#define CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ 7#pragma once 8 9#include "base/basictypes.h" 10#include "base/scoped_ptr.h" 11#include "base/string16.h" 12#include "base/timer.h" 13#include "chrome/browser/instant/instant_commit_type.h" 14#include "chrome/browser/search_engines/template_url_id.h" 15#include "chrome/common/notification_observer.h" 16#include "chrome/common/notification_registrar.h" 17#include "chrome/common/page_transition_types.h" 18#include "gfx/rect.h" 19#include "googleurl/src/gurl.h" 20 21class InstantLoaderDelegate; 22class InstantLoaderManagerTest; 23class TabContents; 24class TabContentsWrapper; 25class TemplateURL; 26 27// InstantLoader does the loading of a particular URL for InstantController. 28// InstantLoader notifies its delegate, which is typically InstantController, of 29// all interesting events. 30// 31// InstantLoader is created with a TemplateURLID. If non-zero InstantLoader 32// first determines if the site actually supports instant. If it doesn't, the 33// delegate is notified by way of |InstantLoaderDoesntSupportInstant|. 34// 35// If the TemplateURLID supplied to the constructor is zero, then the url is 36// loaded as is. 37class InstantLoader : public NotificationObserver { 38 public: 39 InstantLoader(InstantLoaderDelegate* delegate, TemplateURLID id); 40 virtual ~InstantLoader(); 41 42 // Invoked to load a URL. |tab_contents| is the TabContents the preview is 43 // going to be shown on top of and potentially replace. 44 void Update(TabContentsWrapper* tab_contents, 45 const TemplateURL* template_url, 46 const GURL& url, 47 PageTransition::Type transition_type, 48 const string16& user_text, 49 bool verbatim, 50 string16* suggested_text); 51 52 // Sets the bounds of the omnibox (in screen coordinates). The bounds are 53 // remembered until the preview is committed or destroyed. This is only used 54 // when showing results for a search provider that supports instant. 55 void SetOmniboxBounds(const gfx::Rect& bounds); 56 57 // Returns true if the mouse is down as the result of activating the preview 58 // content. 59 bool IsMouseDownFromActivate(); 60 61 // Releases the preview TabContents passing ownership to the caller. This is 62 // intended to be called when the preview TabContents is committed. This does 63 // not notify the delegate. 64 TabContentsWrapper* ReleasePreviewContents(InstantCommitType type); 65 66 // Calls through to method of same name on delegate. 67 bool ShouldCommitInstantOnMouseUp(); 68 void CommitInstantLoader(); 69 70 virtual void Observe(NotificationType type, 71 const NotificationSource& source, 72 const NotificationDetails& details); 73 74 // The preview TabContents; may be null. 75 TabContentsWrapper* preview_contents() const { 76 return preview_contents_.get(); 77 } 78 79 // Returns true if the preview TabContents is ready to be shown. 80 bool ready() const { return ready_; } 81 82 const GURL& url() const { return url_; } 83 84 bool verbatim() const { return verbatim_; } 85 86 // Are we showing instant results? 87 bool is_showing_instant() const { return template_url_id_ != 0; } 88 89 // If we're showing instant this returns non-zero. 90 TemplateURLID template_url_id() const { return template_url_id_; } 91 92 // See description above field. 93 const string16& user_text() const { return user_text_; } 94 95 private: 96 friend class InstantLoaderManagerTest; 97 class FrameLoadObserver; 98 class PaintObserverImpl; 99 class TabContentsDelegateImpl; 100 101 // Invoked when the page wants to update the suggested text. If |user_text_| 102 // starts with |suggested_text|, then the delegate is notified of the change, 103 // which results in updating the omnibox. 104 void SetCompleteSuggestedText(const string16& suggested_text); 105 106 // Invoked when the page paints. 107 void PreviewPainted(); 108 109 // Invoked to show the preview. This is invoked in two possible cases: when 110 // the renderer paints, or when an auth dialog is shown. This notifies the 111 // delegate the preview is ready to be shown. 112 void ShowPreview(); 113 114 // Invoked once the page has finished loading and the script has been sent. 115 void PageFinishedLoading(); 116 117 // Returns the bounds of the omnibox in terms of the preview tab contents. 118 gfx::Rect GetOmniboxBoundsInTermsOfPreview(); 119 120 // Are we waiting for the preview page to finish loading? 121 bool is_waiting_for_load() const { 122 return frame_load_observer_.get() != NULL; 123 } 124 125 // Invoked if it the page doesn't really support instant when we thought it 126 // did. If |needs_reload| is true, the text changed since the first load and 127 // the page needs to be reloaded. 128 void PageDoesntSupportInstant(bool needs_reload); 129 130 // Invokes |SetBoundsToPage(false)|. This is called from the timer. 131 void ProcessBoundsChange(); 132 133 // Notifes the page of the omnibox bounds. If |force_if_loading| is true the 134 // bounds are sent down even if we're waiting on the load, otherwise if we're 135 // waiting on the load and |force_if_loading| is false this does nothing. 136 void SendBoundsToPage(bool force_if_loading); 137 138 // Creates and sets the preview TabContentsWrapper. 139 void CreatePreviewContents(TabContentsWrapper* tab_contents); 140 141 InstantLoaderDelegate* delegate_; 142 143 // If we're showing instant results this is the ID of the TemplateURL driving 144 // the results. A value of 0 means there is no TemplateURL. 145 const TemplateURLID template_url_id_; 146 147 // The url we're displaying. 148 GURL url_; 149 150 // Delegate of the preview TabContents. Used to detect when the user does some 151 // gesture on the TabContents and the preview needs to be activated. 152 scoped_ptr<TabContentsDelegateImpl> preview_tab_contents_delegate_; 153 154 // The preview TabContents; may be null. 155 scoped_ptr<TabContentsWrapper> preview_contents_; 156 157 // Is the preview_contents ready to be shown? 158 bool ready_; 159 160 // The text the user typed in the omnibox, stripped of the leading ?, if any. 161 string16 user_text_; 162 163 // The latest suggestion from the page. 164 string16 complete_suggested_text_; 165 166 // The latest suggestion (suggested text less the user text). 167 string16 last_suggestion_; 168 169 // See description above setter. 170 gfx::Rect omnibox_bounds_; 171 172 // Last bounds passed to the page. 173 gfx::Rect last_omnibox_bounds_; 174 175 scoped_ptr<FrameLoadObserver> frame_load_observer_; 176 177 // Transition type of the match last passed to Update. 178 PageTransition::Type last_transition_type_; 179 180 // Timer used to update the bounds of the omnibox. 181 base::OneShotTimer<InstantLoader> update_bounds_timer_; 182 183 // Used to get notifications about renderers coming and going. 184 NotificationRegistrar registrar_; 185 186 // Last value of verbatim passed to |Update|. 187 bool verbatim_; 188 189 DISALLOW_COPY_AND_ASSIGN(InstantLoader); 190}; 191 192#endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ 193