summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/top_sites/top_sites_api.cc
blob: 77d5a93c63b3c5b046481c4b4a0b06ac841492fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/top_sites/top_sites_api.h"

#include "base/bind.h"
#include "base/values.h"
#include "chrome/browser/history/top_sites.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"

namespace extensions {

TopSitesGetFunction::TopSitesGetFunction()
    : weak_ptr_factory_(this) {}

TopSitesGetFunction::~TopSitesGetFunction() {}

bool TopSitesGetFunction::RunImpl() {
  history::TopSites* ts = profile()->GetTopSites();
  if (!ts)
    return false;

  ts->GetMostVisitedURLs(
      base::Bind(&TopSitesGetFunction::OnMostVisitedURLsAvailable,
                 weak_ptr_factory_.GetWeakPtr()));
  return true;
}

void TopSitesGetFunction::OnMostVisitedURLsAvailable(
    const history::MostVisitedURLList& data) {
  scoped_ptr<base::ListValue> pages_value(new base::ListValue);
  for (size_t i = 0; i < data.size(); i++) {
    const history::MostVisitedURL& url = data[i];
    if (!url.url.is_empty()) {
      base::DictionaryValue* page_value = new base::DictionaryValue();
      page_value->SetString("url", url.url.spec());
      if (url.title.empty())
        page_value->SetString("title", url.url.spec());
      else
        page_value->SetString("title", url.title);
      pages_value->Append(page_value);
    }
  }

  SetResult(pages_value.release());
  SendResponse(true);
}

}  // namespace extensions