summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/ntp/bookmarks_handler.cc
blob: e91e644e707492ad5aa81891e16d84f62bdb807c (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// 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/auto_reset.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/bookmarks/bookmark_extension_api_constants.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"

// TODO(csilv):
// Much of this implementation is based on the classes defined in
// extension_bookmarks_module.cc.  Longer term we should consider migrating
// NTP into an embedded extension which would allow us to leverage the same
// bookmark APIs as the bookmark manager.

namespace keys = bookmark_extension_api_constants;

BookmarksHandler::BookmarksHandler() : model_(NULL),
                                       dom_ready_(false),
                                       from_current_page_(false) {
}

BookmarksHandler::~BookmarksHandler() {
  if (model_)
    model_->RemoveObserver(this);
}

WebUIMessageHandler* BookmarksHandler::Attach(WebUI* web_ui) {
  WebUIMessageHandler::Attach(web_ui);
  model_ = Profile::FromWebUI(web_ui)->GetBookmarkModel();
  if (model_)
    model_->AddObserver(this);
  return this;
}

void BookmarksHandler::RegisterMessages() {
  web_ui_->RegisterMessageCallback("createBookmark",
      base::Bind(&BookmarksHandler::HandleCreateBookmark,
                 base::Unretained(this)));
  web_ui_->RegisterMessageCallback("getBookmarksData",
      base::Bind(&BookmarksHandler::HandleGetBookmarksData,
                 base::Unretained(this)));
  web_ui_->RegisterMessageCallback("moveBookmark",
      base::Bind(&BookmarksHandler::HandleMoveBookmark,
                 base::Unretained(this)));
  web_ui_->RegisterMessageCallback("removeBookmark",
      base::Bind(&BookmarksHandler::HandleRemoveBookmark,
                 base::Unretained(this)));
}

void BookmarksHandler::Loaded(BookmarkModel* model, bool ids_reassigned) {
  if (dom_ready_)
    HandleGetBookmarksData(NULL);
}

void BookmarksHandler::BookmarkModelBeingDeleted(BookmarkModel* model) {
  // If this occurs it probably means that this tab will close shortly.
  // Discard our reference to the model so that we won't use it again.
  model_ = NULL;
}

void BookmarksHandler::BookmarkNodeMoved(BookmarkModel* model,
                                         const BookmarkNode* old_parent,
                                         int old_index,
                                         const BookmarkNode* new_parent,
                                         int new_index) {
  if (!dom_ready_) return;
  const BookmarkNode* node = new_parent->GetChild(new_index);
  base::StringValue id(base::Int64ToString(node->id()));
  base::DictionaryValue move_info;
  move_info.SetString(keys::kParentIdKey,
                      base::Int64ToString(new_parent->id()));
  move_info.SetInteger(keys::kIndexKey, new_index);
  move_info.SetString(keys::kOldParentIdKey,
                      base::Int64ToString(old_parent->id()));
  move_info.SetInteger(keys::kOldIndexKey, old_index);
  base::FundamentalValue from_page(from_current_page_);

  web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeMoved", id, move_info,
                                  from_page);
}

void BookmarksHandler::BookmarkNodeAdded(BookmarkModel* model,
                                         const BookmarkNode* parent,
                                         int index) {
  if (!dom_ready_) return;
  const BookmarkNode* node = parent->GetChild(index);
  base::StringValue id(base::Int64ToString(node->id()));
  scoped_ptr<base::DictionaryValue> node_info(GetNodeDictionary(node));
  base::FundamentalValue from_page(from_current_page_);

  web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeAdded", id, *node_info,
                                  from_page);
}

void BookmarksHandler::BookmarkNodeRemoved(BookmarkModel* model,
                                           const BookmarkNode* parent,
                                           int index,
                                           const BookmarkNode* node) {
  if (!dom_ready_) return;

  base::StringValue id(base::Int64ToString(node->id()));
  base::DictionaryValue remove_info;
  remove_info.SetString(keys::kParentIdKey, base::Int64ToString(parent->id()));
  remove_info.SetInteger(keys::kIndexKey, index);
  base::FundamentalValue from_page(from_current_page_);

  web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeRemoved", id, remove_info,
                                  from_page);
}

void BookmarksHandler::BookmarkNodeChanged(BookmarkModel* model,
                                           const BookmarkNode* node) {
  if (!dom_ready_) return;
  base::StringValue id(base::Int64ToString(node->id()));
  base::DictionaryValue change_info;
  change_info.SetString(keys::kTitleKey, node->GetTitle());
  if (node->is_url())
    change_info.SetString(keys::kUrlKey, node->url().spec());
  base::FundamentalValue from_page(from_current_page_);

  web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeChanged", id, change_info,
                                  from_page);
}

void BookmarksHandler::BookmarkNodeFaviconChanged(BookmarkModel* model,
                                                  const BookmarkNode* node) {
  // Favicons are handled by through use of the chrome://favicon protocol, so
  // there's nothing for us to do here (but we need to provide an
  // implementation).
}

void BookmarksHandler::BookmarkNodeChildrenReordered(BookmarkModel* model,
                                                     const BookmarkNode* node) {
  if (!dom_ready_) return;
  base::StringValue id(base::Int64ToString(node->id()));
  base::ListValue* children = new base::ListValue;
  for (int i = 0; i < node->child_count(); ++i) {
    const BookmarkNode* child = node->GetChild(i);
    Value* child_id = new StringValue(base::Int64ToString(child->id()));
    children->Append(child_id);
  }
  base::DictionaryValue reorder_info;
  reorder_info.Set(keys::kChildIdsKey, children);
  base::FundamentalValue from_page(from_current_page_);

  web_ui_->CallJavascriptFunction("ntp4.bookmarkNodeChildrenReordered", id,
                                  reorder_info, from_page);
}

void BookmarksHandler::BookmarkImportBeginning(BookmarkModel* model) {
  if (dom_ready_)
    web_ui_->CallJavascriptFunction("ntp4.bookmarkImportBegan");
}

void BookmarksHandler::BookmarkImportEnding(BookmarkModel* model) {
  if (dom_ready_)
    web_ui_->CallJavascriptFunction("ntp4.bookmarkImportEnded");
}

void BookmarksHandler::HandleCreateBookmark(const ListValue* args) {
  if (!model_) return;

  // This is the only required argument - a stringified int64 parent ID.
  std::string parent_id_string;
  CHECK(args->GetString(0, &parent_id_string));
  int64 parent_id;
  CHECK(base::StringToInt64(parent_id_string, &parent_id));

  const BookmarkNode* parent = model_->GetNodeByID(parent_id);
  if (!parent) return;

  double index;
  if (!args->GetDouble(1, &index) ||
      (index > parent->child_count() || index < 0)) {
    index = parent->child_count();
  }

  // If they didn't pass the argument, just use a blank string.
  string16 title;
  if (!args->GetString(2, &title))
    title = string16();

  // We'll be creating either a bookmark or a folder, so set this for both.
  AutoReset<bool> from_page(&from_current_page_, true);

  // According to the bookmarks API, "If url is NULL or missing, it will be a
  // folder.". Let's just follow the same behavior.
  std::string url_string;
  if (args->GetString(3, &url_string)) {
    GURL url(url_string);
    if (!url.is_empty() && url.is_valid()) {
      // Only valid case for a bookmark as opposed to folder.
      model_->AddURL(parent, static_cast<int>(index), title, url);
      return;
    }
  }

  // We didn't have all the valid parts for a bookmark, just make a folder.
  model_->AddFolder(parent, static_cast<int>(index), title);
}

void BookmarksHandler::HandleGetBookmarksData(const base::ListValue* args) {
  dom_ready_ = true;

  // At startup, Bookmarks may not be fully loaded. If this is the case,
  // we'll wait for the notification to arrive.
  Profile* profile = Profile::FromWebUI(web_ui_);
  BookmarkModel* model = profile->GetBookmarkModel();
  if (!model->IsLoaded()) return;

  int64 id;
  std::string id_string;
  PrefService* prefs = profile->GetPrefs();
  if (args && 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);
  }

  const BookmarkNode* node = model->GetNodeByID(id);
  if (!node)
    node = model->root_node();

  // We wish to merge the root node with the bookmarks bar node.
  if (model->is_root_node(node))
    node = model->bookmark_bar_node();

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

  base::ListValue* navigation_items = new base::ListValue;
  while (node) {
    if (node != model->bookmark_bar_node())
      AddNode(node, navigation_items);
    node = node->parent();
  }

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

void BookmarksHandler::HandleRemoveBookmark(const ListValue* args) {
  if (!model_) return;

  std::string id_string;
  CHECK(args->GetString(0, &id_string));
  int64 id;
  CHECK(base::StringToInt64(id_string, &id));

  const BookmarkNode* node = model_->GetNodeByID(id);
  if (!node) return;

  AutoReset<bool> from_page(&from_current_page_, true);
  model_->Remove(node->parent(), node->parent()->GetIndexOf(node));
}

void BookmarksHandler::HandleMoveBookmark(const ListValue* args) {
  if (!model_) return;

  std::string id_string;
  CHECK(args->GetString(0, &id_string));
  int64 id;
  CHECK(base::StringToInt64(id_string, &id));

  std::string parent_id_string;
  CHECK(args->GetString(1, &parent_id_string));
  int64 parent_id;
  CHECK(base::StringToInt64(parent_id_string, &parent_id));

  double index;
  args->GetDouble(2, &index);

  const BookmarkNode* parent = model_->GetNodeByID(parent_id);
  if (!parent) return;

  const BookmarkNode* node = model_->GetNodeByID(id);
  if (!node) return;

  AutoReset<bool> from_page(&from_current_page_, true);
  model_->Move(node, parent, static_cast<int>(index));
}

base::DictionaryValue* BookmarksHandler::GetNodeDictionary(
    const BookmarkNode* node) {
  base::DictionaryValue* dict = new base::DictionaryValue();
  dict->SetString(keys::kIdKey, base::Int64ToString(node->id()));

  const BookmarkNode* parent = node->parent();
  if (parent) {
    dict->SetString(keys::kParentIdKey, base::Int64ToString(parent->id()));
    dict->SetInteger(keys::kIndexKey, parent->GetIndexOf(node));
  }

  NewTabUI::SetURLTitleAndDirection(dict, node->GetTitle(), node->url());

  if (!node->is_folder())
    dict->SetString(keys::kUrlKey, node->url().spec());

  return dict;
}

void BookmarksHandler::AddNode(const BookmarkNode* node,
                               base::ListValue* list) {
  list->Append(GetNodeDictionary(node));
}

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