OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/instant/instant_loader.h" | |
6 | |
7 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | |
8 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" | |
9 #include "chrome/browser/favicon/favicon_tab_helper.h" | |
10 #include "chrome/browser/history/history_tab_helper.h" | |
11 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h" | |
12 #include "chrome/browser/tab_contents/tab_util.h" | |
13 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" | |
14 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" | |
15 #include "chrome/browser/ui/search/search_tab_helper.h" | |
16 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" | |
17 #include "content/public/browser/navigation_entry.h" | |
18 #include "content/public/browser/notification_source.h" | |
19 #include "content/public/browser/notification_types.h" | |
20 #include "content/public/browser/render_widget_host_view.h" | |
21 #include "content/public/browser/site_instance.h" | |
22 #include "content/public/browser/web_contents_view.h" | |
23 | |
24 namespace { | |
25 | |
26 const int kStalePageTimeoutMS = 3 * 3600 * 1000; // 3 hours | |
27 | |
28 // This HTTP header and value are set on loads that originate from Instant. | |
29 const char kInstantHeader[] = "X-Purpose: Instant"; | |
30 | |
31 } // namespace | |
32 | |
33 InstantLoader::Delegate::~Delegate() { | |
34 } | |
35 | |
36 InstantLoader::InstantLoader(Delegate* delegate) | |
37 : delegate_(delegate), | |
38 contents_(NULL), | |
39 stale_page_timer_(false, false) { | |
40 } | |
41 | |
42 InstantLoader::~InstantLoader() { | |
43 } | |
44 | |
45 void InstantLoader::Init(const GURL& instant_url, | |
46 Profile* profile, | |
47 const content::WebContents* active_tab, | |
48 const base::Closure& on_stale_callback) { | |
49 content::WebContents::CreateParams create_params(profile); | |
50 create_params.site_instance = content::SiteInstance::CreateForURL( | |
51 profile, instant_url); | |
52 SetContents(scoped_ptr<content::WebContents>( | |
53 content::WebContents::Create(create_params))); | |
54 instant_url_ = instant_url; | |
55 on_stale_callback_ = on_stale_callback; | |
56 } | |
57 | |
58 void InstantLoader::Load() { | |
59 DVLOG(1) << "LoadURL: " << instant_url_; | |
60 contents_->GetController().LoadURL( | |
61 instant_url_, content::Referrer(), | |
62 content::PAGE_TRANSITION_GENERATED, kInstantHeader); | |
63 contents_->WasHidden(); | |
64 stale_page_timer_.Start( | |
65 FROM_HERE, | |
66 base::TimeDelta::FromMilliseconds(kStalePageTimeoutMS), | |
67 on_stale_callback_); | |
68 } | |
69 | |
70 void InstantLoader::SetContents(scoped_ptr<content::WebContents> new_contents) { | |
71 contents_.reset(new_contents.release()); | |
72 contents_->SetDelegate(this); | |
73 | |
74 // Set up various tab helpers. The rest will get attached when (if) the | |
75 // contents is added to the tab strip. | |
76 | |
77 // Tab helpers to control popups. | |
78 BlockedContentTabHelper::CreateForWebContents(contents()); | |
79 BlockedContentTabHelper::FromWebContents(contents())-> | |
80 SetAllContentsBlocked(true); | |
81 TabSpecificContentSettings::CreateForWebContents(contents()); | |
82 TabSpecificContentSettings::FromWebContents(contents())-> | |
83 SetPopupsBlocked(true); | |
84 | |
85 // Bookmarks (Users can bookmark the Instant NTP. This ensures the bookmarked | |
86 // state is correctly set when the contents are swapped into a tab.) | |
87 BookmarkTabHelper::CreateForWebContents(contents()); | |
88 | |
89 // A tab helper to catch prerender content swapping shenanigans. | |
90 CoreTabHelper::CreateForWebContents(contents()); | |
91 CoreTabHelper::FromWebContents(contents())->set_delegate(this); | |
92 | |
93 // Tab helpers used when committing an overlay. | |
94 chrome::search::SearchTabHelper::CreateForWebContents(contents()); | |
95 HistoryTabHelper::CreateForWebContents(contents()); | |
96 | |
97 // Observers. | |
98 extensions::WebNavigationTabObserver::CreateForWebContents(contents()); | |
99 | |
100 // Favicons, required by the Task Manager. | |
101 FaviconTabHelper::CreateForWebContents(contents()); | |
102 | |
103 // And some flat-out paranoia. | |
104 safe_browsing::SafeBrowsingTabObserver::CreateForWebContents(contents()); | |
105 | |
106 #if defined(OS_MACOSX) | |
107 // If |contents_| doesn't yet have a RWHV, SetTakesFocusOnlyOnMouseDown() will | |
108 // be called later, when NOTIFICATION_RENDER_VIEW_HOST_CHANGED is received. | |
109 if (content::RenderWidgetHostView* rwhv = | |
110 contents_->GetRenderWidgetHostView()) | |
111 rwhv->SetTakesFocusOnlyOnMouseDown(true); | |
112 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, | |
113 content::Source<content::NavigationController>( | |
114 &contents_->GetController())); | |
115 #endif | |
116 } | |
117 | |
118 scoped_ptr<content::WebContents> InstantLoader::ReleaseContents() { | |
119 contents_->SetDelegate(NULL); | |
120 | |
121 // Undo tab helper work done in SetContents(). | |
122 | |
123 BlockedContentTabHelper::FromWebContents(contents())-> | |
124 SetAllContentsBlocked(false); | |
125 TabSpecificContentSettings::FromWebContents(contents())-> | |
126 SetPopupsBlocked(false); | |
127 | |
128 CoreTabHelper::FromWebContents(contents())->set_delegate(NULL); | |
129 | |
130 #if defined(OS_MACOSX) | |
131 if (content::RenderWidgetHostView* rwhv = | |
132 contents_->GetRenderWidgetHostView()) | |
133 rwhv->SetTakesFocusOnlyOnMouseDown(false); | |
134 registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, | |
135 content::Source<content::NavigationController>( | |
136 &contents_->GetController())); | |
137 #endif | |
138 | |
139 return contents_.Pass(); | |
140 } | |
141 | |
142 void InstantLoader::Observe(int type, | |
143 const content::NotificationSource& /* source */, | |
144 const content::NotificationDetails& /* details */) { | |
145 #if defined(OS_MACOSX) | |
146 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) { | |
147 if (content::RenderWidgetHostView* rwhv = | |
148 contents_->GetRenderWidgetHostView()) | |
149 rwhv->SetTakesFocusOnlyOnMouseDown(true); | |
150 return; | |
151 } | |
152 NOTREACHED(); | |
153 #endif | |
154 } | |
155 | |
156 void InstantLoader::SwapTabContents(content::WebContents* old_contents, | |
157 content::WebContents* new_contents) { | |
158 DCHECK_EQ(old_contents, contents()); | |
159 // We release here without deleting since the caller has the responsibility | |
160 // for deleting the old WebContents. | |
161 ignore_result(ReleaseContents().release()); | |
162 SetContents(scoped_ptr<content::WebContents>(new_contents)); | |
163 delegate_->OnSwappedContents(); | |
164 } | |
165 | |
166 bool InstantLoader::ShouldSuppressDialogs() { | |
167 // Messages shown during Instant cancel Instant, so we suppress them. | |
168 return true; | |
169 } | |
170 | |
171 bool InstantLoader::ShouldFocusPageAfterCrash() { | |
172 return false; | |
173 } | |
174 | |
175 void InstantLoader::LostCapture() { | |
176 delegate_->OnMouseUp(); | |
177 } | |
178 | |
179 void InstantLoader::WebContentsFocused(content::WebContents* /* contents */) { | |
180 delegate_->OnFocus(); | |
181 } | |
182 | |
183 bool InstantLoader::CanDownload(content::RenderViewHost* /* render_view_host */, | |
184 int /* request_id */, | |
185 const std::string& /* request_method */) { | |
186 // Downloads are disabled. | |
187 return false; | |
188 } | |
189 | |
190 void InstantLoader::HandleMouseDown() { | |
191 delegate_->OnMouseDown(); | |
192 } | |
193 | |
194 void InstantLoader::HandleMouseUp() { | |
195 delegate_->OnMouseUp(); | |
196 } | |
197 | |
198 void InstantLoader::HandlePointerActivate() { | |
199 delegate_->OnMouseDown(); | |
200 } | |
201 | |
202 void InstantLoader::HandleGestureEnd() { | |
203 delegate_->OnMouseUp(); | |
204 } | |
205 | |
206 void InstantLoader::DragEnded() { | |
207 // If the user drags, we won't get a mouse up (at least on Linux). Commit | |
208 // the Instant result when the drag ends, so that during the drag the page | |
209 // won't move around. | |
210 delegate_->OnMouseUp(); | |
211 } | |
212 | |
213 bool InstantLoader::OnGoToEntryOffset(int /* offset */) { | |
214 return false; | |
215 } | |
216 | |
217 content::WebContents* InstantLoader::OpenURLFromTab( | |
218 content::WebContents* source, | |
219 const content::OpenURLParams& params) { | |
220 return delegate_->OpenURLFromTab(source, params); | |
221 } | |
OLD | NEW |