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> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" |
12 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
13 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
14 #include "base/string16.h" | 12 #include "base/timer.h" |
15 #include "chrome/browser/history/history_types.h" | 13 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" |
16 #include "chrome/browser/instant/instant_client.h" | |
17 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
18 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
| 16 #include "content/public/browser/web_contents_delegate.h" |
19 | 17 |
20 struct InstantAutocompleteResult; | 18 class GURL; |
21 class InstantController; | 19 class Profile; |
22 struct ThemeBackgroundInfo; | |
23 | |
24 namespace chrome { | |
25 namespace search { | |
26 struct Mode; | |
27 } | |
28 } | |
29 | 20 |
30 namespace content { | 21 namespace content { |
| 22 struct OpenURLParams; |
31 class WebContents; | 23 class WebContents; |
32 } | 24 } |
33 | 25 |
34 namespace gfx { | 26 // InstantLoader is used to create and maintain a WebContents where we can |
35 class Rect; | 27 // preload a page into. It is used by InstantOverlay and InstantNTP to |
36 } | 28 // preload an Instant page. |
| 29 class InstantLoader : public content::NotificationObserver, |
| 30 public content::WebContentsDelegate, |
| 31 public CoreTabHelperDelegate { |
| 32 public: |
| 33 // InstantLoader calls these methods on its delegate in response to certain |
| 34 // changes in the underlying contents. |
| 35 class Delegate { |
| 36 public: |
| 37 // Called after someone has swapped in a different WebContents for ours. |
| 38 virtual void OnSwappedContents() = 0; |
37 | 39 |
38 // InstantLoader is used to communicate with a preview WebContents that it owns | 40 // Called when the underlying contents receive focus. |
39 // and loads the "Instant URL" into. This preview can appear and disappear at | 41 virtual void OnFocus() = 0; |
40 // will as the user types in the omnibox (compare: InstantTab, which talks to a | |
41 // committed tab on the tab strip). | |
42 class InstantLoader : public InstantClient::Delegate, | |
43 public content::NotificationObserver { | |
44 public: | |
45 // Returns the Instant loader for |contents| if it's used for Instant. | |
46 static InstantLoader* FromWebContents(const content::WebContents* contents); | |
47 | 42 |
48 // Doesn't take ownership of |controller|. | 43 // Called when the mouse pointer is down. |
49 InstantLoader(InstantController* controller, const std::string& instant_url); | 44 virtual void OnMouseDown() = 0; |
| 45 |
| 46 // Called when the mouse pointer is released (or a drag event ends). |
| 47 virtual void OnMouseUp() = 0; |
| 48 |
| 49 // Called to open a URL using the underlying contents (see |
| 50 // WebContentsDelegate::OpenURLFromTab). The Delegate should return the |
| 51 // WebContents the URL is opened in, or NULL if the URL wasn't opened |
| 52 // immediately. |
| 53 virtual content::WebContents* OpenURLFromTab( |
| 54 content::WebContents* source, |
| 55 const content::OpenURLParams& params) = 0; |
| 56 |
| 57 protected: |
| 58 ~Delegate(); |
| 59 }; |
| 60 |
| 61 explicit InstantLoader(Delegate* delegate); |
50 virtual ~InstantLoader(); | 62 virtual ~InstantLoader(); |
51 | 63 |
52 // The preview WebContents. InstantLoader retains ownership. This will be | 64 // Creates a new WebContents in the context of |profile| that will be used to |
53 // non-NULL after InitFromContents(), and until ReleaseContents() is called. | 65 // load |instant_url|. The page is not actually loaded until Load() is |
| 66 // called. Uses |active_contents|, if non-NULL, to initialize the size of the |
| 67 // new contents. |on_stale_callback| will be called after kStalePageTimeoutMS |
| 68 // has elapsed after Load() being called. |
| 69 void Init(const GURL& instant_url, |
| 70 Profile* profile, |
| 71 const content::WebContents* active_contents, |
| 72 const base::Closure& on_stale_callback); |
| 73 |
| 74 // Loads |instant_url_| in |contents_|. |
| 75 void Load(); |
| 76 |
| 77 // Returns the contents currently held. May be NULL. |
54 content::WebContents* contents() const { return contents_.get(); } | 78 content::WebContents* contents() const { return contents_.get(); } |
55 | 79 |
56 // Creates a new WebContents and loads |instant_url_| into it. |active_tab| is | 80 // Replaces the contents held with |contents|. Any existing contents is |
57 // the page the preview will be shown on top of and potentially replace. | 81 // deleted. The expiration timer is not restarted. |
58 void InitContents(const content::WebContents* active_tab); | 82 void SetContents(scoped_ptr<content::WebContents> contents); |
59 | 83 |
60 // Releases the preview WebContents passing ownership to the caller. This | 84 // Releases the contents currently held. Must only be called if contents() is |
61 // should be called when the preview is committed. | 85 // not NULL. |
62 content::WebContents* ReleaseContents() WARN_UNUSED_RESULT; | 86 scoped_ptr<content::WebContents> ReleaseContents() WARN_UNUSED_RESULT; |
63 | |
64 // Returns the URL that we're loading. | |
65 const std::string& instant_url() const { return instant_url_; } | |
66 | |
67 // Returns true if the preview is known to support the Instant API. This | |
68 // starts out false, and becomes true whenever we get any message from the | |
69 // page. Once true, it never becomes false (the page isn't expected to drop | |
70 // Instant API support suddenly). | |
71 bool supports_instant() const { return supports_instant_; } | |
72 | |
73 // Returns true if the mouse or a touch pointer is down due to activating the | |
74 // preview contents. | |
75 bool is_pointer_down_from_activate() const { | |
76 return is_pointer_down_from_activate_; | |
77 } | |
78 | |
79 // Returns info about the last navigation by the Instant page. If the page | |
80 // hasn't navigated since the last Update(), the URL is empty. | |
81 const history::HistoryAddPageArgs& last_navigation() const { | |
82 return last_navigation_; | |
83 } | |
84 | |
85 // Called by the history tab helper with information that it would have added | |
86 // to the history service had this WebContents not been used for Instant. | |
87 void DidNavigate(const history::HistoryAddPageArgs& add_page_args); | |
88 | |
89 // Returns true if the loader is using | |
90 // InstantController::kLocalOmniboxPopupURL as the |instant_url_|. | |
91 bool IsUsingLocalPreview() const; | |
92 | |
93 // Calls through to methods of the same name on InstantClient. | |
94 void Update(const string16& text, | |
95 size_t selection_start, | |
96 size_t selection_end, | |
97 bool verbatim); | |
98 void Submit(const string16& text); | |
99 void Cancel(const string16& text); | |
100 void SetPopupBounds(const gfx::Rect& bounds); | |
101 void SetMarginSize(int start, int end); | |
102 void InitializeFonts(); | |
103 void SendAutocompleteResults( | |
104 const std::vector<InstantAutocompleteResult>& results); | |
105 void UpOrDownKeyPressed(int count); | |
106 void SearchModeChanged(const chrome::search::Mode& mode); | |
107 void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info); | |
108 void SendThemeAreaHeight(int height); | |
109 void SetDisplayInstantResults(bool display_instant_results); | |
110 void KeyCaptureChanged(bool is_key_capture_enabled); | |
111 | 87 |
112 private: | 88 private: |
113 class WebContentsDelegateImpl; | |
114 | |
115 // Overridden from InstantClient::Delegate: | |
116 virtual void SetSuggestions( | |
117 const std::vector<InstantSuggestion>& suggestions) OVERRIDE; | |
118 virtual void InstantSupportDetermined(bool supports_instant) OVERRIDE; | |
119 virtual void ShowInstantPreview(InstantShownReason reason, | |
120 int height, | |
121 InstantSizeUnits units) OVERRIDE; | |
122 virtual void StartCapturingKeyStrokes() OVERRIDE; | |
123 virtual void StopCapturingKeyStrokes() OVERRIDE; | |
124 virtual void RenderViewGone() OVERRIDE; | |
125 virtual void AboutToNavigateMainFrame(const GURL& url) OVERRIDE; | |
126 virtual void NavigateToURL(const GURL& url, | |
127 content::PageTransition transition) OVERRIDE; | |
128 virtual void RenderViewCreated() OVERRIDE; | |
129 | |
130 // Overridden from content::NotificationObserver: | 89 // Overridden from content::NotificationObserver: |
131 virtual void Observe(int type, | 90 virtual void Observe(int type, |
132 const content::NotificationSource& source, | 91 const content::NotificationSource& source, |
133 const content::NotificationDetails& details) OVERRIDE; | 92 const content::NotificationDetails& details) OVERRIDE; |
134 | 93 |
135 void SetupPreviewContents(); | 94 // Overridden from CoreTabHelperDelegate: |
136 void CleanupPreviewContents(); | 95 virtual void SwapTabContents(content::WebContents* old_contents, |
137 void ReplacePreviewContents(content::WebContents* old_contents, | 96 content::WebContents* new_contents) OVERRIDE; |
138 content::WebContents* new_contents); | |
139 | 97 |
140 InstantClient client_; | 98 // Overridden from content::WebContentsDelegate: |
141 InstantController* const controller_; | 99 virtual bool ShouldSuppressDialogs() OVERRIDE; |
| 100 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; |
| 101 virtual void LostCapture() OVERRIDE; |
| 102 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE; |
| 103 virtual bool CanDownload(content::RenderViewHost* render_view_host, |
| 104 int request_id, |
| 105 const std::string& request_method) OVERRIDE; |
| 106 virtual void HandleMouseDown() OVERRIDE; |
| 107 virtual void HandleMouseUp() OVERRIDE; |
| 108 virtual void HandlePointerActivate() OVERRIDE; |
| 109 virtual void HandleGestureEnd() OVERRIDE; |
| 110 virtual void DragEnded() OVERRIDE; |
| 111 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; |
| 112 virtual content::WebContents* OpenURLFromTab( |
| 113 content::WebContents* source, |
| 114 const content::OpenURLParams& params) OVERRIDE; |
142 | 115 |
143 // Delegate of the preview WebContents. Used when the user does some gesture | 116 Delegate* const delegate_; |
144 // on the preview and it needs to be activated. | |
145 scoped_ptr<WebContentsDelegateImpl> delegate_; | |
146 scoped_ptr<content::WebContents> contents_; | 117 scoped_ptr<content::WebContents> contents_; |
147 | 118 |
148 const std::string instant_url_; | 119 // The URL we will be loading. |
149 bool supports_instant_; | 120 GURL instant_url_; |
150 bool is_pointer_down_from_activate_; | |
151 history::HistoryAddPageArgs last_navigation_; | |
152 | 121 |
153 // Used to get notifications about renderers coming and going. | 122 // Called when |stale_page_timer_| fires. |
| 123 base::Closure on_stale_callback_; |
| 124 |
| 125 // Used to mark when the page is stale. |
| 126 base::Timer stale_page_timer_; |
| 127 |
| 128 // Used to get notifications about renderers. |
154 content::NotificationRegistrar registrar_; | 129 content::NotificationRegistrar registrar_; |
155 | 130 |
156 DISALLOW_COPY_AND_ASSIGN(InstantLoader); | 131 DISALLOW_COPY_AND_ASSIGN(InstantLoader); |
157 }; | 132 }; |
158 | 133 |
159 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ | 134 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ |
OLD | NEW |