1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/prerender/prerender_tab_helper.h"
#include "base/bind.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/password_manager/password_manager.h"
#include "chrome/browser/predictors/logged_in_predictor_table.h"
#include "chrome/browser/prerender/prerender_histograms.h"
#include "chrome/browser/prerender/prerender_local_predictor.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/prerender/prerender_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/common/frame_navigate_params.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/rect.h"
using content::WebContents;
DEFINE_WEB_CONTENTS_USER_DATA_KEY(prerender::PrerenderTabHelper);
namespace prerender {
namespace {
void ReportTabHelperURLSeenToLocalPredictor(
PrerenderManager* prerender_manager,
const GURL& url,
WebContents* web_contents) {
if (!prerender_manager)
return;
PrerenderLocalPredictor* local_predictor =
prerender_manager->local_predictor();
if (!local_predictor)
return;
local_predictor->OnTabHelperURLSeen(url, web_contents);
}
} // namespace
// Helper class to compute pixel-based stats on the paint progress
// between when a prerendered page is swapped in and when the onload event
// fires.
class PrerenderTabHelper::PixelStats {
public:
explicit PixelStats(PrerenderTabHelper* tab_helper) :
bitmap_web_contents_(NULL),
weak_factory_(this),
tab_helper_(tab_helper) {
}
// Reasons why we need to fetch bitmaps: either a prerender was swapped in,
// or a prerendered page has finished loading.
enum BitmapType {
BITMAP_SWAP_IN,
BITMAP_ON_LOAD
};
void GetBitmap(BitmapType bitmap_type, WebContents* web_contents) {
if (bitmap_type == BITMAP_SWAP_IN) {
bitmap_.reset();
bitmap_web_contents_ = web_contents;
}
if (bitmap_type == BITMAP_ON_LOAD && bitmap_web_contents_ != web_contents)
return;
if (!web_contents || !web_contents->GetView() ||
!web_contents->GetRenderViewHost()) {
return;
}
web_contents->GetRenderViewHost()->CopyFromBackingStore(
gfx::Rect(),
gfx::Size(),
base::Bind(&PrerenderTabHelper::PixelStats::HandleBitmapResult,
weak_factory_.GetWeakPtr(),
bitmap_type,
web_contents));
}
private:
void HandleBitmapResult(BitmapType bitmap_type,
WebContents* web_contents,
bool succeeded,
const SkBitmap& canvas_bitmap) {
scoped_ptr<SkBitmap> bitmap;
if (succeeded) {
// TODO(nick): This copy may now be unnecessary.
bitmap.reset(new SkBitmap());
canvas_bitmap.copyTo(bitmap.get(), SkBitmap::kARGB_8888_Config);
}
if (bitmap_web_contents_ != web_contents)
return;
if (bitmap_type == BITMAP_SWAP_IN)
bitmap_.swap(bitmap);
if (bitmap_type == BITMAP_ON_LOAD) {
PrerenderManager* prerender_manager =
tab_helper_->MaybeGetPrerenderManager();
if (prerender_manager) {
prerender_manager->RecordFractionPixelsFinalAtSwapin(
web_contents, CompareBitmaps(bitmap_.get(), bitmap.get()));
}
bitmap_.reset();
bitmap_web_contents_ = NULL;
}
}
// Helper comparing two bitmaps of identical size.
// Returns a value < 0.0 if there is an error, and otherwise, a double in
// [0, 1] indicating the fraction of pixels that are the same.
double CompareBitmaps(SkBitmap* bitmap1, SkBitmap* bitmap2) {
if (!bitmap1 || !bitmap2) {
return -2.0;
}
if (bitmap1->width() != bitmap2->width() ||
bitmap1->height() != bitmap2->height()) {
return -1.0;
}
int pixels = bitmap1->width() * bitmap1->height();
int same_pixels = 0;
for (int y = 0; y < bitmap1->height(); ++y) {
for (int x = 0; x < bitmap1->width(); ++x) {
if (bitmap1->getColor(x, y) == bitmap2->getColor(x, y))
same_pixels++;
}
}
return static_cast<double>(same_pixels) / static_cast<double>(pixels);
}
// Bitmap of what the last swapped in prerendered tab looked like at swapin,
// and the WebContents that it was swapped into.
scoped_ptr<SkBitmap> bitmap_;
WebContents* bitmap_web_contents_;
base::WeakPtrFactory<PixelStats> weak_factory_;
PrerenderTabHelper* tab_helper_;
};
// static
void PrerenderTabHelper::CreateForWebContentsWithPasswordManager(
content::WebContents* web_contents,
PasswordManager* password_manager) {
if (!FromWebContents(web_contents)) {
web_contents->SetUserData(UserDataKey(),
new PrerenderTabHelper(web_contents,
password_manager));
}
}
PrerenderTabHelper::PrerenderTabHelper(content::WebContents* web_contents,
PasswordManager* password_manager)
: content::WebContentsObserver(web_contents),
weak_factory_(this) {
password_manager->AddSubmissionCallback(
base::Bind(&PrerenderTabHelper::PasswordSubmitted,
weak_factory_.GetWeakPtr()));
}
PrerenderTabHelper::~PrerenderTabHelper() {
}
void PrerenderTabHelper::ProvisionalChangeToMainFrameUrl(
const GURL& url,
content::RenderViewHost* render_view_host) {
url_ = url;
RecordEvent(EVENT_MAINFRAME_CHANGE);
RecordEventIfLoggedInURL(EVENT_MAINFRAME_CHANGE_DOMAIN_LOGGED_IN, url);
PrerenderManager* prerender_manager = MaybeGetPrerenderManager();
if (!prerender_manager)
return;
if (prerender_manager->IsWebContentsPrerendering(web_contents(), NULL))
return;
prerender_manager->MarkWebContentsAsNotPrerendered(web_contents());
ReportTabHelperURLSeenToLocalPredictor(prerender_manager, url,
web_contents());
}
void PrerenderTabHelper::DidCommitProvisionalLoadForFrame(
int64 frame_id,
bool is_main_frame,
const GURL& validated_url,
content::PageTransition transition_type,
content::RenderViewHost* render_view_host) {
if (!is_main_frame)
return;
RecordEvent(EVENT_MAINFRAME_COMMIT);
RecordEventIfLoggedInURL(EVENT_MAINFRAME_COMMIT_DOMAIN_LOGGED_IN,
validated_url);
url_ = validated_url;
PrerenderManager* prerender_manager = MaybeGetPrerenderManager();
if (!prerender_manager)
return;
if (prerender_manager->IsWebContentsPrerendering(web_contents(), NULL))
return;
prerender_manager->RecordNavigation(validated_url);
ReportTabHelperURLSeenToLocalPredictor(prerender_manager, validated_url,
web_contents());
}
void PrerenderTabHelper::DidStopLoading(
content::RenderViewHost* render_view_host) {
// Compute the PPLT metric and report it in a histogram, if needed.
// We include pages that are still prerendering and have just finished
// loading -- PrerenderManager will sort this out and handle it correctly
// (putting those times into a separate histogram).
if (!pplt_load_start_.is_null()) {
double fraction_elapsed_at_swapin = -1.0;
base::TimeTicks now = base::TimeTicks::Now();
if (!actual_load_start_.is_null()) {
double plt = (now - actual_load_start_).InMillisecondsF();
if (plt > 0.0) {
fraction_elapsed_at_swapin = 1.0 -
(now - pplt_load_start_).InMillisecondsF() / plt;
} else {
fraction_elapsed_at_swapin = 1.0;
}
DCHECK_GE(fraction_elapsed_at_swapin, 0.0);
DCHECK_LE(fraction_elapsed_at_swapin, 1.0);
}
PrerenderManager::RecordPerceivedPageLoadTime(
now - pplt_load_start_, fraction_elapsed_at_swapin, web_contents(),
url_);
if (IsPrerendered() && pixel_stats_.get())
pixel_stats_->GetBitmap(PixelStats::BITMAP_ON_LOAD, web_contents());
}
// Reset the PPLT metric.
pplt_load_start_ = base::TimeTicks();
actual_load_start_ = base::TimeTicks();
}
void PrerenderTabHelper::DidStartProvisionalLoadForFrame(
int64 frame_id,
int64 parent_frame_id,
bool is_main_frame,
const GURL& validated_url,
bool is_error_page,
bool is_iframe_srcdoc,
content::RenderViewHost* render_view_host) {
if (is_main_frame) {
// Record the beginning of a new PPLT navigation.
pplt_load_start_ = base::TimeTicks::Now();
actual_load_start_ = base::TimeTicks();
}
}
void PrerenderTabHelper::PasswordSubmitted(const content::PasswordForm& form) {
PrerenderManager* prerender_manager = MaybeGetPrerenderManager();
if (prerender_manager) {
prerender_manager->RecordLikelyLoginOnURL(form.origin);
RecordEvent(EVENT_LOGIN_ACTION_ADDED);
if (form.password_value.empty())
RecordEvent(EVENT_LOGIN_ACTION_ADDED_PW_EMPTY);
}
}
PrerenderManager* PrerenderTabHelper::MaybeGetPrerenderManager() const {
return PrerenderManagerFactory::GetForProfile(
Profile::FromBrowserContext(web_contents()->GetBrowserContext()));
}
bool PrerenderTabHelper::IsPrerendering() {
PrerenderManager* prerender_manager = MaybeGetPrerenderManager();
if (!prerender_manager)
return false;
return prerender_manager->IsWebContentsPrerendering(web_contents(), NULL);
}
bool PrerenderTabHelper::IsPrerendered() {
PrerenderManager* prerender_manager = MaybeGetPrerenderManager();
if (!prerender_manager)
return false;
return prerender_manager->IsWebContentsPrerendered(web_contents(), NULL);
}
void PrerenderTabHelper::PrerenderSwappedIn() {
// Ensure we are not prerendering any more.
DCHECK(!IsPrerendering());
if (pplt_load_start_.is_null()) {
// If we have already finished loading, report a 0 PPLT.
PrerenderManager::RecordPerceivedPageLoadTime(base::TimeDelta(), 1.0,
web_contents(), url_);
PrerenderManager* prerender_manager = MaybeGetPrerenderManager();
if (prerender_manager)
prerender_manager->RecordFractionPixelsFinalAtSwapin(web_contents(), 1.0);
} else {
// If we have not finished loading yet, record the actual load start, and
// rebase the start time to now.
actual_load_start_ = pplt_load_start_;
pplt_load_start_ = base::TimeTicks::Now();
if (pixel_stats_.get())
pixel_stats_->GetBitmap(PixelStats::BITMAP_SWAP_IN, web_contents());
}
}
void PrerenderTabHelper::RecordEvent(PrerenderTabHelper::Event event) const {
UMA_HISTOGRAM_ENUMERATION("Prerender.TabHelperEvent",
event, PrerenderTabHelper::EVENT_MAX_VALUE);
}
void PrerenderTabHelper::RecordEventIfLoggedInURL(
PrerenderTabHelper::Event event, const GURL& url) {
PrerenderManager* prerender_manager = MaybeGetPrerenderManager();
if (!prerender_manager)
return;
scoped_ptr<bool> is_present(new bool);
scoped_ptr<bool> lookup_succeeded(new bool);
bool* is_present_ptr = is_present.get();
bool* lookup_succeeded_ptr = lookup_succeeded.get();
prerender_manager->CheckIfLikelyLoggedInOnURL(
url,
is_present_ptr,
lookup_succeeded_ptr,
base::Bind(&PrerenderTabHelper::RecordEventIfLoggedInURLResult,
weak_factory_.GetWeakPtr(),
event,
base::Passed(&is_present),
base::Passed(&lookup_succeeded)));
}
void PrerenderTabHelper::RecordEventIfLoggedInURLResult(
PrerenderTabHelper::Event event,
scoped_ptr<bool> is_present,
scoped_ptr<bool> lookup_succeeded) {
if (*lookup_succeeded && *is_present)
RecordEvent(event);
}
} // namespace prerender
|