summaryrefslogtreecommitdiffstats
path: root/chrome/browser/history/top_sites_extension_api.cc
blob: 6a8c4b2f69deb44f3b4a843a9f4b7d12b42fae7d (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
52
53
// Copyright (c) 2011 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/history/top_sites_extension_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"

GetTopSitesFunction::GetTopSitesFunction() {
}

GetTopSitesFunction::~GetTopSitesFunction() {
}

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

  ts->GetMostVisitedURLs(
      &topsites_consumer_,
      base::Bind(&GetTopSitesFunction::OnMostVisitedURLsAvailable, this));
  return true;
}

void GetTopSitesFunction::OnMostVisitedURLsAvailable(
    const history::MostVisitedURLList& data) {
  // Code is a direct rip from most_visited_handler.cc TODO(estade): unfork.
  scoped_ptr<base::ListValue> pages_value(new ListValue);
  for (size_t i = 0; i < data.size(); i++) {
    const history::MostVisitedURL& url = data[i];
    DictionaryValue* page_value = new DictionaryValue();
    if (url.url.is_empty()) {
      page_value->SetBoolean("filler", true);
      pages_value->Append(page_value);
      continue;
    }

    NewTabUI::SetURLTitleAndDirection(page_value,
                                      url.title,
                                      url.url);

    pages_value->Append(page_value);
  }
  // End copied code. ----------------------------------------------------------

  result_.reset(pages_value.release());
  SendResponse(true);
}