Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / top_sites / top_sites_api.cc
blob6f76cb2df1cfce5af7c926f6e49ab08e57817e83
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.
5 #include "chrome/browser/extensions/api/top_sites/top_sites_api.h"
7 #include "base/bind.h"
8 #include "base/values.h"
9 #include "chrome/browser/history/top_sites_factory.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
12 #include "components/history/core/browser/top_sites.h"
14 namespace extensions {
16 TopSitesGetFunction::TopSitesGetFunction()
17 : weak_ptr_factory_(this) {}
19 TopSitesGetFunction::~TopSitesGetFunction() {}
21 bool TopSitesGetFunction::RunAsync() {
22 scoped_refptr<history::TopSites> ts =
23 TopSitesFactory::GetForProfile(GetProfile());
24 if (!ts)
25 return false;
27 ts->GetMostVisitedURLs(
28 base::Bind(&TopSitesGetFunction::OnMostVisitedURLsAvailable,
29 weak_ptr_factory_.GetWeakPtr()), false);
30 return true;
33 void TopSitesGetFunction::OnMostVisitedURLsAvailable(
34 const history::MostVisitedURLList& data) {
35 scoped_ptr<base::ListValue> pages_value(new base::ListValue);
36 for (size_t i = 0; i < data.size(); i++) {
37 const history::MostVisitedURL& url = data[i];
38 if (!url.url.is_empty()) {
39 base::DictionaryValue* page_value = new base::DictionaryValue();
40 page_value->SetString("url", url.url.spec());
41 if (url.title.empty())
42 page_value->SetString("title", url.url.spec());
43 else
44 page_value->SetString("title", url.title);
45 pages_value->Append(page_value);
49 SetResult(pages_value.release());
50 SendResponse(true);
53 } // namespace extensions