Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: chrome/browser/instant/instant_loader.cc

Issue 12520005: Move desktop-specific Instant bits to c/b/ui/search. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/instant/instant_loader.h ('k') | chrome/browser/instant/instant_ntp.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // When the WebContents finishes loading it should be checked to ensure that
118 // it is in the instant process.
119 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
120 content::Source<content::WebContents>(contents_.get()));
121 }
122
123 scoped_ptr<content::WebContents> InstantLoader::ReleaseContents() {
124 contents_->SetDelegate(NULL);
125
126 // Undo tab helper work done in SetContents().
127
128 BlockedContentTabHelper::FromWebContents(contents())->
129 SetAllContentsBlocked(false);
130 TabSpecificContentSettings::FromWebContents(contents())->
131 SetPopupsBlocked(false);
132
133 CoreTabHelper::FromWebContents(contents())->set_delegate(NULL);
134
135 #if defined(OS_MACOSX)
136 if (content::RenderWidgetHostView* rwhv =
137 contents_->GetRenderWidgetHostView())
138 rwhv->SetTakesFocusOnlyOnMouseDown(false);
139 registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
140 content::Source<content::NavigationController>(
141 &contents_->GetController()));
142 #endif
143 registrar_.Remove(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
144 content::Source<content::WebContents>(contents_.get()));
145
146 return contents_.Pass();
147 }
148
149 void InstantLoader::Observe(int type,
150 const content::NotificationSource& source,
151 const content::NotificationDetails& details) {
152 if (type == content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME) {
153 const content::WebContents* web_contents =
154 content::Source<content::WebContents>(source).ptr();
155 DCHECK_EQ(contents_.get(), web_contents);
156 delegate_->LoadCompletedMainFrame();
157 return;
158 }
159
160 #if defined(OS_MACOSX)
161 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) {
162 if (content::RenderWidgetHostView* rwhv =
163 contents_->GetRenderWidgetHostView())
164 rwhv->SetTakesFocusOnlyOnMouseDown(true);
165 return;
166 }
167 #endif
168 NOTREACHED();
169 }
170
171 void InstantLoader::SwapTabContents(content::WebContents* old_contents,
172 content::WebContents* new_contents) {
173 DCHECK_EQ(old_contents, contents());
174 // We release here without deleting since the caller has the responsibility
175 // for deleting the old WebContents.
176 ignore_result(ReleaseContents().release());
177 SetContents(scoped_ptr<content::WebContents>(new_contents));
178 delegate_->OnSwappedContents();
179 }
180
181 bool InstantLoader::ShouldSuppressDialogs() {
182 // Messages shown during Instant cancel Instant, so we suppress them.
183 return true;
184 }
185
186 bool InstantLoader::ShouldFocusPageAfterCrash() {
187 return false;
188 }
189
190 void InstantLoader::LostCapture() {
191 delegate_->OnMouseUp();
192 }
193
194 void InstantLoader::WebContentsFocused(content::WebContents* /* contents */) {
195 delegate_->OnFocus();
196 }
197
198 bool InstantLoader::CanDownload(content::RenderViewHost* /* render_view_host */,
199 int /* request_id */,
200 const std::string& /* request_method */) {
201 // Downloads are disabled.
202 return false;
203 }
204
205 void InstantLoader::HandleMouseDown() {
206 delegate_->OnMouseDown();
207 }
208
209 void InstantLoader::HandleMouseUp() {
210 delegate_->OnMouseUp();
211 }
212
213 void InstantLoader::HandlePointerActivate() {
214 delegate_->OnMouseDown();
215 }
216
217 void InstantLoader::HandleGestureEnd() {
218 delegate_->OnMouseUp();
219 }
220
221 void InstantLoader::DragEnded() {
222 // If the user drags, we won't get a mouse up (at least on Linux). Commit
223 // the Instant result when the drag ends, so that during the drag the page
224 // won't move around.
225 delegate_->OnMouseUp();
226 }
227
228 bool InstantLoader::OnGoToEntryOffset(int /* offset */) {
229 return false;
230 }
231
232 content::WebContents* InstantLoader::OpenURLFromTab(
233 content::WebContents* source,
234 const content::OpenURLParams& params) {
235 return delegate_->OpenURLFromTab(source, params);
236 }
OLDNEW
« no previous file with comments | « chrome/browser/instant/instant_loader.h ('k') | chrome/browser/instant/instant_ntp.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698