| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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> | |
| 10 | 9 |
| 11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/string16.h" | |
| 15 #include "chrome/browser/history/history_types.h" | 13 #include "chrome/browser/history/history_types.h" |
| 16 #include "chrome/browser/instant/instant_commit_type.h" | 14 #include "chrome/browser/instant/instant_client.h" |
| 15 #include "chrome/common/instant_types.h" |
| 17 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
| 19 | 18 |
| 20 struct InstantAutocompleteResult; | |
| 21 class InstantController; | 19 class InstantController; |
| 22 class TabContents; | 20 class TabContents; |
| 23 struct ThemeBackgroundInfo; | |
| 24 | 21 |
| 25 namespace chrome { | 22 namespace chrome { |
| 26 namespace search { | 23 namespace search { |
| 27 struct Mode; | 24 struct Mode; |
| 28 } | 25 } |
| 29 } | 26 } |
| 30 | 27 |
| 31 namespace content { | 28 namespace content { |
| 32 class WebContents; | 29 class WebContents; |
| 33 } | 30 } |
| 34 | 31 |
| 35 namespace gfx { | 32 namespace gfx { |
| 36 class Rect; | 33 class Rect; |
| 37 } | 34 } |
| 38 | 35 |
| 39 // InstantLoader is created with an "Instant URL". It loads the URL and tells | 36 // InstantLoader is an InstantClient that talks to a preview WebContents that it |
| 40 // the InstantController of all interesting events. For example, it determines | 37 // owns and that it has loaded the "Instant URL" into. Since this preview can |
| 41 // if the page supports the Instant API (http://dev.chromium.org/searchbox) and | 38 // appear and disappear at will, it supports a richer set of functionality than |
| 42 // forwards messages (such as queries and autocomplete suggestions) between the | 39 // the barebones InstantClient. |
| 43 // page and the controller. | 40 class InstantLoader : public InstantClient, |
| 44 class InstantLoader : public content::NotificationObserver { | 41 public content::NotificationObserver { |
| 45 public: | 42 public: |
| 46 // Returns the Instant loader for |web_contents| if it's used for Instant. | 43 // Returns the Instant loader for |web_contents| if it's used for Instant. |
| 47 static InstantLoader* FromWebContents(content::WebContents* web_contents); | 44 static InstantLoader* FromWebContents(content::WebContents* web_contents); |
| 48 | 45 |
| 49 // Creates a new empty WebContents. Use Init() to actually load |instant_url|. | 46 // Creates a new empty WebContents. Use Init() to actually load |instant_url|. |
| 50 // |tab_contents| is the page the preview will be shown on top of and | 47 // |tab_contents| is the page the preview will be shown on top of and |
| 51 // potentially replace. |instant_url| is typically the instant_url field of | 48 // potentially replace. |instant_url| is typically the instant_url field of |
| 52 // the default search engine's TemplateURL, with the "{searchTerms}" parameter | 49 // the default search engine's TemplateURL, with the "{searchTerms}" parameter |
| 53 // replaced with an empty string. | 50 // replaced with an empty string. |
| 54 InstantLoader(InstantController* controller, | 51 InstantLoader(InstantController* controller, |
| 55 const std::string& instant_url, | 52 const std::string& instant_url, |
| 56 const TabContents* tab_contents); | 53 const TabContents* tab_contents); |
| 57 virtual ~InstantLoader(); | 54 virtual ~InstantLoader(); |
| 58 | 55 |
| 59 // Initializes |preview_contents_| and loads |instant_url_|. | 56 // Initializes |preview_contents_| and loads |instant_url_|. |
| 60 void Init(); | 57 void Init(); |
| 61 | 58 |
| 62 // Tells the preview page that the user typed |user_text| into the omnibox. | 59 // Tells the page that the user clicked on it. |
| 63 // If |verbatim| is false, the page predicts the query the user means to type | 60 void Cancel(const string16& text); |
| 64 // and fetches results for the prediction. If |verbatim| is true, |user_text| | |
| 65 // is taken as the exact query (no prediction is made). | |
| 66 void Update(const string16& user_text, bool verbatim); | |
| 67 | 61 |
| 68 // Tells the preview page of the bounds of the omnibox dropdown (in screen | 62 // Tells the preview page of the bounds of the omnibox dropdown (in screen |
| 69 // coordinates). This is used by the page to offset the results to avoid them | 63 // coordinates). This is used by the page to offset the results to avoid them |
| 70 // being covered by the omnibox dropdown. | 64 // being covered by the omnibox dropdown. |
| 71 void SetOmniboxBounds(const gfx::Rect& bounds); | 65 void SetOmniboxBounds(const gfx::Rect& bounds); |
| 72 | 66 |
| 73 // Tells the preview page about the available autocomplete results. | |
| 74 void SendAutocompleteResults( | |
| 75 const std::vector<InstantAutocompleteResult>& results); | |
| 76 | |
| 77 // Tells the preview page about the current theme background. | 67 // Tells the preview page about the current theme background. |
| 78 void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info); | 68 void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info); |
| 79 | 69 |
| 80 // Tells the preview page about the current theme area height. | 70 // Tells the preview page about the current theme area height. |
| 81 void SendThemeAreaHeight(int height); | 71 void SendThemeAreaHeight(int height); |
| 82 | 72 |
| 83 // Tells the preview page that the user pressed the up or down key. |count| | |
| 84 // is a repeat count, negative for moving up, positive for moving down. | |
| 85 void OnUpOrDownKeyPressed(int count); | |
| 86 | |
| 87 // Tells the preview page that the active tab's search mode has changed. | 73 // Tells the preview page that the active tab's search mode has changed. |
| 88 void SearchModeChanged(const chrome::search::Mode& mode); | 74 void SearchModeChanged(const chrome::search::Mode& mode); |
| 89 | 75 |
| 90 // Called by the history tab helper with the information that it would have | 76 // Called by the history tab helper with the information that it would have |
| 91 // added to the history service had this web contents not been used for | 77 // added to the history service had this web contents not been used for |
| 92 // Instant. | 78 // Instant. |
| 93 void DidNavigate(const history::HistoryAddPageArgs& add_page_args); | 79 void DidNavigate(const history::HistoryAddPageArgs& add_page_args); |
| 94 | 80 |
| 81 // The preview TabContents. The loader retains ownership. This will be |
| 82 // non-NULL until release_preview_contents() is called. |
| 83 TabContents* preview_contents() const { return preview_contents_.get(); } |
| 84 |
| 95 // Releases the preview TabContents passing ownership to the caller. This | 85 // Releases the preview TabContents passing ownership to the caller. This |
| 96 // should be called when the preview is committed. Notifies the page but not | 86 // should be called when the preview is committed. NOTE: caller should destroy |
| 97 // the controller. |text| is the final omnibox text being committed. NOTE: The | 87 // this loader object right after this method, since none of the other methods |
| 98 // caller should destroy this loader object right after this method, since | 88 // will work once the preview has been released. |
| 99 // none of the other methods will work once the preview has been released. | 89 TabContents* release_preview_contents() WARN_UNUSED_RESULT { |
| 100 TabContents* ReleasePreviewContents(InstantCommitType type, | 90 return preview_contents_.release(); |
| 101 const string16& text) WARN_UNUSED_RESULT; | 91 } |
| 102 | 92 |
| 103 // Severs delegate and observer connections, resets popup blocking, etc., on | 93 // Severs delegate and observer connections, resets popup blocking, etc., on |
| 104 // the |preview_contents_|. | 94 // the |preview_contents_|. |
| 105 void CleanupPreviewContents(); | 95 void CleanupPreviewContents(); |
| 106 | 96 |
| 107 // The preview TabContents. The loader retains ownership. This will be | |
| 108 // non-NULL until ReleasePreviewContents() is called. | |
| 109 TabContents* preview_contents() const { return preview_contents_.get(); } | |
| 110 | |
| 111 // Returns true if the preview page is known to support the Instant API. This | |
| 112 // starts out false, and becomes true whenever we get any message from the | |
| 113 // page. Once true, it never becomes false (the page isn't expected to drop | |
| 114 // Instant API support suddenly). | |
| 115 bool supports_instant() const { return supports_instant_; } | |
| 116 | |
| 117 // Returns the URL that we're loading. | 97 // Returns the URL that we're loading. |
| 118 const std::string& instant_url() const { return instant_url_; } | 98 const std::string& instant_url() const { return instant_url_; } |
| 119 | 99 |
| 120 // Returns info about the last navigation by the Instant page. If the page | 100 // Returns info about the last navigation by the Instant page. If the page |
| 121 // hasn't navigated since the last Update(), the URL is empty. | 101 // hasn't navigated since the last Update(), the URL is empty. |
| 122 const history::HistoryAddPageArgs& last_navigation() const { | 102 const history::HistoryAddPageArgs& last_navigation() const { |
| 123 return last_navigation_; | 103 return last_navigation_; |
| 124 } | 104 } |
| 125 | 105 |
| 126 // Returns true if the mouse or a touch pointer is down due to activating the | 106 // Returns true if the mouse or a touch pointer is down due to activating the |
| 127 // preview content. | 107 // preview content. |
| 128 bool IsPointerDownFromActivate() const; | 108 bool IsPointerDownFromActivate() const; |
| 129 | 109 |
| 130 private: | 110 private: |
| 131 class WebContentsDelegateImpl; | 111 class WebContentsDelegateImpl; |
| 132 | 112 |
| 113 // Overridden from InstantClient: |
| 114 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 115 |
| 133 // Overridden from content::NotificationObserver: | 116 // Overridden from content::NotificationObserver: |
| 134 virtual void Observe(int type, | 117 virtual void Observe(int type, |
| 135 const content::NotificationSource& source, | 118 const content::NotificationSource& source, |
| 136 const content::NotificationDetails& details) OVERRIDE; | 119 const content::NotificationDetails& details) OVERRIDE; |
| 137 | 120 |
| 138 void SetupPreviewContents(); | 121 void SetupPreviewContents(); |
| 139 void ReplacePreviewContents(content::WebContents* old_contents, | 122 void ReplacePreviewContents(content::WebContents* old_contents, |
| 140 content::WebContents* new_contents); | 123 content::WebContents* new_contents); |
| 141 | 124 |
| 142 InstantController* const controller_; | 125 // Message from the renderer requesting the preview be shown. |
| 126 void OnShowInstantPreview(int page_id, |
| 127 InstantShownReason reason, |
| 128 int height, |
| 129 InstantSizeUnits units); |
| 143 | 130 |
| 144 // Delegate of the preview WebContents. Used when the user does some gesture | 131 // Delegate of the preview WebContents. Used when the user does some gesture |
| 145 // on the WebContents and it needs to be activated. This MUST be defined above | 132 // on the WebContents and it needs to be activated. This MUST be defined above |
| 146 // |preview_contents_| so that the delegate can outlive the WebContents. | 133 // |preview_contents_| so that the delegate can outlive the WebContents. |
| 147 scoped_ptr<WebContentsDelegateImpl> preview_delegate_; | 134 scoped_ptr<WebContentsDelegateImpl> preview_delegate_; |
| 148 | 135 |
| 149 // See comments on the getter above. | 136 // See comments on the getter above. |
| 150 scoped_ptr<TabContents> preview_contents_; | 137 scoped_ptr<TabContents> preview_contents_; |
| 151 | 138 |
| 152 // See comments on the getter above. | 139 // See comments on the getter above. |
| 153 bool supports_instant_; | |
| 154 | |
| 155 // See comments on the getter above. | |
| 156 const std::string instant_url_; | 140 const std::string instant_url_; |
| 157 | 141 |
| 158 // Used to get notifications about renderers coming and going. | 142 // Used to get notifications about renderers coming and going. |
| 159 content::NotificationRegistrar registrar_; | 143 content::NotificationRegistrar registrar_; |
| 160 | 144 |
| 161 // See comments on the getter above. | 145 // See comments on the getter above. |
| 162 history::HistoryAddPageArgs last_navigation_; | 146 history::HistoryAddPageArgs last_navigation_; |
| 163 | 147 |
| 164 DISALLOW_COPY_AND_ASSIGN(InstantLoader); | 148 DISALLOW_COPY_AND_ASSIGN(InstantLoader); |
| 165 }; | 149 }; |
| 166 | 150 |
| 167 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ | 151 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ |
| OLD | NEW |