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