| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 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/history/top_sites_extension_api.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chrome/browser/history/top_sites.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | |
| 12 | |
| 13 GetTopSitesFunction::GetTopSitesFunction() | |
| 14 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} | |
| 15 | |
| 16 GetTopSitesFunction::~GetTopSitesFunction() {} | |
| 17 | |
| 18 bool GetTopSitesFunction::RunImpl() { | |
| 19 history::TopSites* ts = profile()->GetTopSites(); | |
| 20 if (!ts) | |
| 21 return false; | |
| 22 | |
| 23 ts->GetMostVisitedURLs( | |
| 24 base::Bind(&GetTopSitesFunction::OnMostVisitedURLsAvailable, | |
| 25 weak_ptr_factory_.GetWeakPtr())); | |
| 26 return true; | |
| 27 } | |
| 28 | |
| 29 void GetTopSitesFunction::OnMostVisitedURLsAvailable( | |
| 30 const history::MostVisitedURLList& data) { | |
| 31 scoped_ptr<base::ListValue> pages_value(new ListValue); | |
| 32 for (size_t i = 0; i < data.size(); i++) { | |
| 33 const history::MostVisitedURL& url = data[i]; | |
| 34 if (!url.url.is_empty()) { | |
| 35 DictionaryValue* page_value = new DictionaryValue(); | |
| 36 page_value->SetString("url", url.url.spec()); | |
| 37 if (url.title.empty()) | |
| 38 page_value->SetString("title", url.url.spec()); | |
| 39 else | |
| 40 page_value->SetString("title", url.title); | |
| 41 pages_value->Append(page_value); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 SetResult(pages_value.release()); | |
| 46 SendResponse(true); | |
| 47 } | |
| OLD | NEW |