OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/search/instant_page.h" | 5 #include "chrome/browser/ui/search/instant_page.h" |
6 | 6 |
7 #include "apps/app_launcher.h" | 7 #include "apps/app_launcher.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/history/most_visited_tiles_experiment.h" |
| 11 #include "chrome/browser/history/top_sites.h" |
10 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/search/instant_service.h" | 13 #include "chrome/browser/search/instant_service.h" |
12 #include "chrome/browser/search/instant_service_factory.h" | 14 #include "chrome/browser/search/instant_service_factory.h" |
13 #include "chrome/browser/search/search.h" | 15 #include "chrome/browser/search/search.h" |
| 16 #include "chrome/browser/ui/browser_finder.h" |
14 #include "chrome/browser/ui/search/instant_tab.h" | 17 #include "chrome/browser/ui/search/instant_tab.h" |
15 #include "chrome/browser/ui/search/search_model.h" | 18 #include "chrome/browser/ui/search/search_model.h" |
16 #include "chrome/browser/ui/search/search_tab_helper.h" | 19 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
17 #include "chrome/common/render_messages.h" | 21 #include "chrome/common/render_messages.h" |
18 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
19 #include "content/public/browser/navigation_controller.h" | 23 #include "content/public/browser/navigation_controller.h" |
20 #include "content/public/browser/navigation_details.h" | 24 #include "content/public/browser/navigation_details.h" |
21 #include "content/public/browser/navigation_entry.h" | 25 #include "content/public/browser/navigation_entry.h" |
22 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
23 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
24 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/frame_navigate_params.h" | 29 #include "content/public/common/frame_navigate_params.h" |
26 #include "ui/base/resource/resource_bundle.h" | 30 #include "ui/base/resource/resource_bundle.h" |
27 #include "ui/gfx/font.h" | 31 #include "ui/gfx/font.h" |
28 | 32 |
| 33 namespace { |
| 34 |
| 35 // Creates a set containing the canonical URLs of the currently open tabs. |
| 36 void GetOpenUrls(const TabStripModel& tabs, |
| 37 const history::TopSites& top_sites, |
| 38 std::set<std::string>* urls) { |
| 39 for (int i = 0; i < tabs.count(); ++i) { |
| 40 content::WebContents* web_contents = tabs.GetWebContentsAt(i); |
| 41 if (web_contents) |
| 42 urls->insert(top_sites.GetCanonicalURLString(web_contents->GetURL())); |
| 43 } |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
29 InstantPage::Delegate::~Delegate() { | 48 InstantPage::Delegate::~Delegate() { |
30 } | 49 } |
31 | 50 |
32 InstantPage::~InstantPage() { | 51 InstantPage::~InstantPage() { |
33 if (contents()) | 52 if (contents()) |
34 SearchTabHelper::FromWebContents(contents())->model()->RemoveObserver(this); | 53 SearchTabHelper::FromWebContents(contents())->model()->RemoveObserver(this); |
35 | 54 |
36 // |profile_| may be NULL during unit tests. | 55 // |profile_| may be NULL during unit tests. |
37 if (profile_) { | 56 if (profile_) { |
38 InstantService* instant_service = | 57 InstantService* instant_service = |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 if (is_main_frame) | 199 if (is_main_frame) |
181 delegate_->InstantPageLoadFailed(contents()); | 200 delegate_->InstantPageLoadFailed(contents()); |
182 } | 201 } |
183 | 202 |
184 void InstantPage::ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) { | 203 void InstantPage::ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) { |
185 sender()->SendThemeBackgroundInfo(theme_info); | 204 sender()->SendThemeBackgroundInfo(theme_info); |
186 } | 205 } |
187 | 206 |
188 void InstantPage::MostVisitedItemsChanged( | 207 void InstantPage::MostVisitedItemsChanged( |
189 const std::vector<InstantMostVisitedItem>& items) { | 208 const std::vector<InstantMostVisitedItem>& items) { |
190 sender()->SendMostVisitedItems(items); | 209 std::vector<InstantMostVisitedItem> items_copy(items); |
| 210 MaybeRemoveMostVisitedItems(&items_copy); |
| 211 |
| 212 sender()->SendMostVisitedItems(items_copy); |
191 | 213 |
192 content::NotificationService::current()->Notify( | 214 content::NotificationService::current()->Notify( |
193 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, | 215 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, |
194 content::Source<InstantPage>(this), | 216 content::Source<InstantPage>(this), |
195 content::NotificationService::NoDetails()); | 217 content::NotificationService::NoDetails()); |
196 } | 218 } |
197 | 219 |
198 void InstantPage::ModelChanged(const SearchModel::State& old_state, | 220 void InstantPage::ModelChanged(const SearchModel::State& old_state, |
199 const SearchModel::State& new_state) { | 221 const SearchModel::State& new_state) { |
200 if (old_state.instant_support != new_state.instant_support) | 222 if (old_state.instant_support != new_state.instant_support) |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 delegate_->UndoAllMostVisitedDeletions(); | 298 delegate_->UndoAllMostVisitedDeletions(); |
277 } | 299 } |
278 | 300 |
279 void InstantPage::ClearContents() { | 301 void InstantPage::ClearContents() { |
280 if (contents()) | 302 if (contents()) |
281 SearchTabHelper::FromWebContents(contents())->model()->RemoveObserver(this); | 303 SearchTabHelper::FromWebContents(contents())->model()->RemoveObserver(this); |
282 | 304 |
283 sender()->SetContents(NULL); | 305 sender()->SetContents(NULL); |
284 Observe(NULL); | 306 Observe(NULL); |
285 } | 307 } |
| 308 |
| 309 void InstantPage::MaybeRemoveMostVisitedItems( |
| 310 std::vector<InstantMostVisitedItem>* items) { |
| 311 // The following #if is due to the facts that tabstripmodel cannot be accessed |
| 312 // in the same way, that chrome::FindBrowserWithProfile is undefined in Android |
| 313 // and, moreover, that this experiment is not designed to run on Android devices |
| 314 // due to different NTP presentation. |
| 315 #if !defined(OS_ANDROID) |
| 316 if (!history::MostVisitedTilesExperiment::IsDontShowOpenURLsEnabled()) { |
| 317 return; |
| 318 } |
| 319 |
| 320 TabStripModel* tab_strip_model = chrome::FindBrowserWithProfile( |
| 321 profile_, |
| 322 chrome::GetActiveDesktop())->tab_strip_model(); |
| 323 history::TopSites* top_sites = profile_->GetTopSites(); |
| 324 if (!tab_strip_model || !top_sites) { |
| 325 NOTREACHED(); |
| 326 return; |
| 327 } |
| 328 |
| 329 std::set<std::string> open_urls; |
| 330 GetOpenUrls(*tab_strip_model, *top_sites, &open_urls); |
| 331 history::MostVisitedTilesExperiment::RemoveItemsMatchingOpenTabs( |
| 332 open_urls, items); |
| 333 |
| 334 #endif |
| 335 } |
OLD | NEW |