summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/ntp/bookmarks_handler.cc
blob: 5a2507232c7ffb5877d136dab8be457d538bf917 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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/ui/webui/ntp/bookmarks_handler.h"

#include "base/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/extensions/extension_bookmark_helpers.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"
#include "content/common/notification_service.h"

BookmarksHandler::BookmarksHandler() {
  // TODO(csilv): Register for bookmark model change notifications.
}

BookmarksHandler::~BookmarksHandler() {}

void BookmarksHandler::RegisterMessages() {
  web_ui_->RegisterMessageCallback("getBookmarksData",
      NewCallback(this, &BookmarksHandler::HandleGetBookmarksData));
}

void BookmarksHandler::Observe(int type,
                               const NotificationSource& source,
                               const NotificationDetails& details) {
  // TODO(csilv): Update UI based on changes to bookmark notifications.
}

void BookmarksHandler::HandleGetBookmarksData(const base::ListValue* args) {
  int64 id;
  std::string id_string;
  PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs();
  if (args->GetString(0, &id_string) && base::StringToInt64(id_string, &id)) {
    // A folder ID was requested, so persist this value.
    prefs->SetInt64(prefs::kNTPShownBookmarksFolder, id);
  } else {
    // No folder ID was requested, so get the default (persisted) value.
    id = prefs->GetInt64(prefs::kNTPShownBookmarksFolder);
  }

  BookmarkModel* model = Profile::FromWebUI(web_ui_)->GetBookmarkModel();
  const BookmarkNode* node = model->GetNodeByID(id);
  if (!node)
    return;

  base::ListValue* items = new base::ListValue();
  int child_count = node->child_count();
  for (int i = 0; i < child_count; ++i) {
    const BookmarkNode* child = node->GetChild(i);
    extension_bookmark_helpers::AddNode(child, items, false);
  }

  base::ListValue* navigation_items = new base::ListValue();
  while (node) {
    extension_bookmark_helpers::AddNode(node, navigation_items, false);
    node = node->parent();
  }

  base::DictionaryValue bookmarksData;
  bookmarksData.Set("items", items);
  bookmarksData.Set("navigationItems", navigation_items);
  web_ui_->CallJavascriptFunction("ntp4.setBookmarksData", bookmarksData);
}

// static
void BookmarksHandler::RegisterUserPrefs(PrefService* prefs) {
  // Default folder is the root node.
  // TODO(csilv): Should we default to the Bookmarks bar?
  // TODO(csilv): Should we sync this preference?
  prefs->RegisterInt64Pref(prefs::kNTPShownBookmarksFolder, 0,
                           PrefService::UNSYNCABLE_PREF);
}