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_preloader.h" |
| 6 |
| 7 #include "base/stringprintf.h" |
| 8 #include "chrome/browser/instant/instant_service.h" |
| 9 #include "chrome/browser/instant/search.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "content/public/browser/site_instance.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 const int kStalePageTimeoutMS = 3 * 3600 * 1000; // 3 hours |
| 17 |
| 18 // This HTTP header and value are set on loads that originate from Instant. |
| 19 const char kInstantHeader[] = "X-Purpose: Instant"; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 InstantPreloader::InstantPreloader(InstantService* service) |
| 24 : InstantWebContentsContainer( |
| 25 ALLOW_THIS_IN_INITIALIZER_LIST(this), service), |
| 26 service_(service) { |
| 27 service_->AddObserver(this); |
| 28 } |
| 29 |
| 30 InstantPreloader::~InstantPreloader() { |
| 31 service_->RemoveObserver(this); |
| 32 } |
| 33 |
| 34 void InstantPreloader::InitContents() { |
| 35 if (contents_ && (supports_instant() || contents_->IsLoading())) |
| 36 return; |
| 37 |
| 38 GURL instant_url = chrome::search::GetInstantURL(service_->profile()); |
| 39 if (!instant_url.is_valid()) |
| 40 return; |
| 41 |
| 42 content::WebContents::CreateParams create_params(service_->profile(), |
| 43 content::SiteInstance::CreateForURL(service_->profile(), instant_url)); |
| 44 contents_.reset(content::WebContents::Create(create_params)); |
| 45 |
| 46 SetUpContents(); |
| 47 |
| 48 service_->LogDebugEvent(base::StringPrintf("%p LoadURL '%s'", |
| 49 this, |
| 50 instant_url.spec().c_str())); |
| 51 |
| 52 contents_->GetController().LoadURL(instant_url, content::Referrer(), |
| 53 content::PAGE_TRANSITION_GENERATED, kInstantHeader); |
| 54 contents_->WasHidden(); |
| 55 |
| 56 stale_page_timer_.Start(FROM_HERE, |
| 57 base::TimeDelta::FromMilliseconds(kStalePageTimeoutMS), |
| 58 this, &InstantPreloader::ReloadContents); |
| 59 } |
| 60 |
| 61 scoped_ptr<content::WebContents> InstantPreloader::ReleaseContents() { |
| 62 TearDownContents(); |
| 63 scoped_ptr<content::WebContents> old_contents(contents_.Pass()); |
| 64 InitContents(); |
| 65 return old_contents.Pass(); |
| 66 } |
| 67 |
| 68 void InstantPreloader::RenderViewGone( |
| 69 const content::WebContents* /* contents */) { |
| 70 DeleteContents(); |
| 71 } |
| 72 |
| 73 void InstantPreloader::InitSearchBox( |
| 74 const content::WebContents* /* contents */) { |
| 75 page_.DisplayInstantResults( |
| 76 chrome::search::IsInstantPrefEnabled(service_->profile())); |
| 77 page_.ThemeChanged(service_->theme_info()); |
| 78 page_.FontChanged(service_->omnibox_font_name(), |
| 79 service_->omnibox_font_size()); |
| 80 page_.MostVisitedItems(service_->most_visited_items()); |
| 81 } |
| 82 |
| 83 void InstantPreloader::InstantSupportDetermined( |
| 84 const content::WebContents* /* contents */) { |
| 85 if (!supports_instant()) |
| 86 DeleteContents(); |
| 87 service_->InstantSupportDetermined(); |
| 88 } |
| 89 |
| 90 void InstantPreloader::SetSuggestion( |
| 91 const content::WebContents* /* contents */, |
| 92 const InstantSuggestion& /* suggestion */) { |
| 93 } |
| 94 |
| 95 void InstantPreloader::NavigateToURL(const content::WebContents* /* contents */, |
| 96 const GURL& /* url */, |
| 97 content::PageTransition /* transition */, |
| 98 WindowOpenDisposition /* disposition */) { |
| 99 } |
| 100 |
| 101 void InstantPreloader::ShowOverlay(const content::WebContents* /* contents */, |
| 102 int /* height */, |
| 103 InstantSizeUnits /* height_units */) { |
| 104 } |
| 105 |
| 106 void InstantPreloader::SetFocusState(const content::WebContents* /* contents */, |
| 107 OmniboxFocusState /* focus_state */) { |
| 108 } |
| 109 |
| 110 void InstantPreloader::DeleteMostVisitedItem( |
| 111 const content::WebContents* /* contents */, |
| 112 const GURL& /* url */) { |
| 113 } |
| 114 |
| 115 void InstantPreloader::UndoMostVisitedItemDeletion( |
| 116 const content::WebContents* /* contents */, |
| 117 const GURL& /* url */) { |
| 118 } |
| 119 |
| 120 void InstantPreloader::UndoAllMostVisitedItemDeletions( |
| 121 const content::WebContents* /* contents */) { |
| 122 } |
| 123 |
| 124 void InstantPreloader::InstantStatusChanged() { |
| 125 if (contents_) |
| 126 ReloadContents(); |
| 127 } |
| 128 |
| 129 void InstantPreloader::ThemeInfoChanged() { |
| 130 page_.ThemeChanged(service_->theme_info()); |
| 131 } |
| 132 |
| 133 void InstantPreloader::MostVisitedItemsChanged() { |
| 134 page_.MostVisitedItems(service_->most_visited_items()); |
| 135 } |
| 136 |
| 137 void InstantPreloader::CloseContents(content::WebContents* /* source */) { |
| 138 DeleteContents(); |
| 139 } |
| 140 |
| 141 void InstantPreloader::TearDownContents() { |
| 142 InstantWebContentsContainer::TearDownContents(); |
| 143 stale_page_timer_.Stop(); |
| 144 } |
| 145 |
| 146 void InstantPreloader::DeleteContents() { |
| 147 TearDownContents(); |
| 148 contents_.reset(); |
| 149 } |
| 150 |
| 151 void InstantPreloader::ReloadContents() { |
| 152 DeleteContents(); |
| 153 InitContents(); |
| 154 } |
OLD | NEW |