Home | History | Annotate | Download | only in prerender
      1 // Copyright (c) 2011 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/prerender/prerender_observer.h"
      6 
      7 #include "base/time.h"
      8 #include "chrome/browser/prerender/prerender_manager.h"
      9 #include "chrome/browser/profiles/profile.h"
     10 #include "content/browser/tab_contents/tab_contents.h"
     11 #include "content/common/view_messages.h"
     12 
     13 namespace prerender {
     14 
     15 PrerenderObserver::PrerenderObserver(TabContents* tab_contents)
     16     : TabContentsObserver(tab_contents),
     17       pplt_load_start_() {
     18 }
     19 
     20 PrerenderObserver::~PrerenderObserver() {
     21 }
     22 
     23 void PrerenderObserver::ProvisionalChangeToMainFrameUrl(const GURL& url) {
     24   PrerenderManager* pm = MaybeGetPrerenderManager();
     25   if (pm)
     26     pm->MarkTabContentsAsNotPrerendered(tab_contents());
     27   MaybeUsePreloadedPage(url);
     28 }
     29 
     30 bool PrerenderObserver::OnMessageReceived(const IPC::Message& message) {
     31   IPC_BEGIN_MESSAGE_MAP(PrerenderObserver, message)
     32     IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
     33                         OnDidStartProvisionalLoadForFrame)
     34   IPC_END_MESSAGE_MAP()
     35   return false;
     36 }
     37 
     38 void PrerenderObserver::OnDidStartProvisionalLoadForFrame(int64 frame_id,
     39                                                           bool is_main_frame,
     40                                                           const GURL& url) {
     41   if (is_main_frame) {
     42     // Record the beginning of a new PPLT navigation.
     43     pplt_load_start_ = base::TimeTicks::Now();
     44   }
     45 }
     46 
     47 void PrerenderObserver::DidStopLoading() {
     48   // Compute the PPLT metric and report it in a histogram, if needed.
     49   if (!pplt_load_start_.is_null()) {
     50     PrerenderManager::RecordPerceivedPageLoadTime(
     51         base::TimeTicks::Now() - pplt_load_start_, tab_contents());
     52   }
     53 
     54   // Reset the PPLT metric.
     55   pplt_load_start_ = base::TimeTicks();
     56 }
     57 
     58 PrerenderManager* PrerenderObserver::MaybeGetPrerenderManager() {
     59   return tab_contents()->profile()->GetPrerenderManager();
     60 }
     61 
     62 bool PrerenderObserver::MaybeUsePreloadedPage(const GURL& url) {
     63   PrerenderManager* pm = MaybeGetPrerenderManager();
     64   if (pm && pm->MaybeUsePreloadedPage(tab_contents(), url))
     65     return true;
     66   return false;
     67 }
     68 
     69 }  // namespace prerender
     70