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 whether it is allowed to display Instant results. | |
84 void SetDisplayInstantResults(bool display_instant_results); | |
85 | |
86 // Tells the preview page that the user pressed the up or down key. |count| | |
87 // is a repeat count, negative for moving up, positive for moving down. | |
88 void OnUpOrDownKeyPressed(int count); | |
89 | |
90 // Tells the preview page that the active tab's search mode has changed. | |
91 void SearchModeChanged(const chrome::search::Mode& mode); | |
92 | |
93 // Called by the history tab helper with the information that it would have | |
94 // added to the history service had this web contents not been used for | |
95 // Instant. | |
96 void DidNavigate(const history::HistoryAddPageArgs& add_page_args); | |
97 | |
98 // Releases the preview TabContents passing ownership to the caller. This | |
99 // should be called when the preview is committed. Notifies the page but not | |
100 // the controller. |text| is the final omnibox text being committed. NOTE: The | |
101 // caller should destroy this loader object right after this method, since | |
102 // none of the other methods will work once the preview has been released. | |
103 TabContents* ReleasePreviewContents(InstantCommitType type, | |
104 const string16& text) WARN_UNUSED_RESULT; | |
105 | |
106 // Severs delegate and observer connections, resets popup blocking, etc., on | |
107 // the |preview_contents_|. | |
108 void CleanupPreviewContents(); | |
109 | |
110 // The preview TabContents. The loader retains ownership. This will be | |
111 // non-NULL until ReleasePreviewContents() is called. | |
112 TabContents* preview_contents() const { return preview_contents_.get(); } | |
113 | |
114 // Returns true if the preview page is known to support the Instant API. This | |
115 // 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 |
116 // 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 |
117 // Instant API support suddenly). | 70 // Instant API support suddenly). |
118 bool supports_instant() const { return supports_instant_; } | 71 bool supports_instant() const { return supports_instant_; } |
119 | 72 |
120 // Returns the URL that we're loading. | 73 // Returns true if the mouse or a touch pointer is down due to activating the |
121 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 } |
122 | 78 |
123 // 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 |
124 // hasn't navigated since the last Update(), the URL is empty. | 80 // hasn't navigated since the last Update(), the URL is empty. |
125 const history::HistoryAddPageArgs& last_navigation() const { | 81 const history::HistoryAddPageArgs& last_navigation() const { |
126 return last_navigation_; | 82 return last_navigation_; |
127 } | 83 } |
128 | 84 |
129 // 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 |
130 // preview content. | 86 // to the history service had this WebContents not been used for Instant. |
131 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, |
| 91 size_t selection_start, |
| 92 size_t selection_end, |
| 93 bool verbatim); |
| 94 void Submit(const string16& text); |
| 95 void Cancel(const string16& text); |
| 96 void SetOmniboxBounds(const gfx::Rect& bounds); |
| 97 void SendAutocompleteResults( |
| 98 const std::vector<InstantAutocompleteResult>& results); |
| 99 void UpOrDownKeyPressed(int count); |
| 100 void SearchModeChanged(const chrome::search::Mode& mode); |
| 101 void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info); |
| 102 void SendThemeAreaHeight(int height); |
| 103 void SetDisplayInstantResults(bool display_instant_results); |
132 | 104 |
133 private: | 105 private: |
134 class WebContentsDelegateImpl; | 106 class WebContentsDelegateImpl; |
135 | 107 |
| 108 // Overridden from InstantClient::Delegate: |
| 109 virtual void SetSuggestions( |
| 110 const std::vector<InstantSuggestion>& suggestions) OVERRIDE; |
| 111 virtual void InstantSupportDetermined(bool supports_instant) OVERRIDE; |
| 112 virtual void ShowInstantPreview(InstantShownReason reason, |
| 113 int height, |
| 114 InstantSizeUnits units) OVERRIDE; |
| 115 |
136 // Overridden from content::NotificationObserver: | 116 // Overridden from content::NotificationObserver: |
137 virtual void Observe(int type, | 117 virtual void Observe(int type, |
138 const content::NotificationSource& source, | 118 const content::NotificationSource& source, |
139 const content::NotificationDetails& details) OVERRIDE; | 119 const content::NotificationDetails& details) OVERRIDE; |
140 | 120 |
141 void SetupPreviewContents(); | 121 void SetupPreviewContents(); |
| 122 void CleanupPreviewContents(); |
142 void ReplacePreviewContents(content::WebContents* old_contents, | 123 void ReplacePreviewContents(content::WebContents* old_contents, |
143 content::WebContents* new_contents); | 124 content::WebContents* new_contents); |
144 | 125 |
| 126 InstantClient client_; |
145 InstantController* const controller_; | 127 InstantController* const controller_; |
146 | 128 |
147 // Delegate of the preview WebContents. Used when the user does some gesture | 129 // Delegate of the preview WebContents. Used when the user does some gesture |
148 // on the WebContents and it needs to be activated. This MUST be defined above | 130 // on the preview and it needs to be activated. |
149 // |preview_contents_| so that the delegate can outlive the WebContents. | 131 scoped_ptr<WebContentsDelegateImpl> delegate_; |
150 scoped_ptr<WebContentsDelegateImpl> preview_delegate_; | 132 scoped_ptr<content::WebContents> contents_; |
151 | 133 |
152 // See comments on the getter above. | 134 const std::string instant_url_; |
153 scoped_ptr<TabContents> preview_contents_; | |
154 | |
155 // See comments on the getter above. | |
156 bool supports_instant_; | 135 bool supports_instant_; |
157 | 136 bool is_pointer_down_from_activate_; |
158 // See comments on the getter above. | 137 history::HistoryAddPageArgs last_navigation_; |
159 const std::string instant_url_; | |
160 | 138 |
161 // Used to get notifications about renderers coming and going. | 139 // Used to get notifications about renderers coming and going. |
162 content::NotificationRegistrar registrar_; | 140 content::NotificationRegistrar registrar_; |
163 | 141 |
164 // See comments on the getter above. | |
165 history::HistoryAddPageArgs last_navigation_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(InstantLoader); | 142 DISALLOW_COPY_AND_ASSIGN(InstantLoader); |
168 }; | 143 }; |
169 | 144 |
170 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ | 145 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ |
OLD | NEW |