1// Copyright 2014 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/predictors/resource_prefetch_predictor_tab_helper.h"
6
7#include "chrome/browser/predictors/resource_prefetch_predictor.h"
8#include "chrome/browser/predictors/resource_prefetch_predictor_factory.h"
9#include "chrome/browser/profiles/profile.h"
10#include "content/public/browser/browser_thread.h"
11#include "content/public/browser/load_from_memory_cache_details.h"
12
13DEFINE_WEB_CONTENTS_USER_DATA_KEY(
14    predictors::ResourcePrefetchPredictorTabHelper);
15
16using content::BrowserThread;
17
18namespace predictors {
19
20ResourcePrefetchPredictorTabHelper::ResourcePrefetchPredictorTabHelper(
21    content::WebContents* web_contents)
22    : content::WebContentsObserver(web_contents) {
23}
24
25ResourcePrefetchPredictorTabHelper::~ResourcePrefetchPredictorTabHelper() {
26}
27
28void ResourcePrefetchPredictorTabHelper::DocumentOnLoadCompletedInMainFrame() {
29  DCHECK_CURRENTLY_ON(BrowserThread::UI);
30
31  ResourcePrefetchPredictor* predictor =
32      ResourcePrefetchPredictorFactory::GetForProfile(
33          web_contents()->GetBrowserContext());
34  if (!predictor)
35    return;
36
37  NavigationID navigation_id(web_contents());
38  predictor->RecordMainFrameLoadComplete(navigation_id);
39}
40
41void ResourcePrefetchPredictorTabHelper::DidLoadResourceFromMemoryCache(
42    const content::LoadFromMemoryCacheDetails& details) {
43  DCHECK_CURRENTLY_ON(BrowserThread::UI);
44
45  ResourcePrefetchPredictor* predictor =
46      ResourcePrefetchPredictorFactory::GetForProfile(
47          web_contents()->GetBrowserContext());
48  if (!predictor)
49    return;
50
51  ResourcePrefetchPredictor::URLRequestSummary summary;
52  summary.navigation_id = NavigationID(web_contents());
53  summary.resource_url = details.url;
54  summary.mime_type = details.mime_type;
55  summary.resource_type =
56      ResourcePrefetchPredictor::GetResourceTypeFromMimeType(
57          details.mime_type, details.resource_type);
58  summary.was_cached = true;
59  predictor->RecordURLResponse(summary);
60}
61
62}  // namespace predictors
63