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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
// Copyright (c) 2009 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/extension_bookmarks_module.h"
#include "base/json_writer.h"
#include "chrome/browser/bookmarks/bookmark_codec.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_utils.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/extensions/extension_bookmarks_module_constants.h"
#include "chrome/browser/extensions/extension_message_service.h"
#include "chrome/browser/profile.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
namespace keys = extension_bookmarks_module_constants;
// Helper functions.
class ExtensionBookmarks {
public:
// Convert |node| into a JSON value
static DictionaryValue* GetNodeDictionary(BookmarkNode* node, bool recurse) {
DictionaryValue* dict = new DictionaryValue();
dict->SetInteger(keys::kIdKey, node->id());
BookmarkNode* parent = node->GetParent();
if (parent)
dict->SetInteger(keys::kParentIdKey, parent->id());
if (!node->is_folder())
dict->SetString(keys::kUrlKey, node->GetURL().spec());
dict->SetString(keys::kTitleKey, node->GetTitle());
int childCount = node->GetChildCount();
ListValue* children = new ListValue();
for (int i = 0; i < childCount; ++i) {
BookmarkNode* child = node->GetChild(i);
if (recurse) {
DictionaryValue* dict = GetNodeDictionary(child, true);
children->Append(dict);
}
}
if (recurse)
dict->Set(keys::kChildrenKey, children);
return dict;
}
// Add a JSON representation of |node| to the JSON |list|.
static void AddNode(BookmarkNode* node, ListValue* list, bool recurse) {
DictionaryValue* dict = GetNodeDictionary(node, recurse);
list->Append(dict);
}
static bool RemoveNode(BookmarkModel* model, int id, bool recursive,
std::string* error) {
BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
*error = keys::kNoNodeError;
return false;
}
if (node == model->root_node() ||
node == model->other_node() ||
node == model->GetBookmarkBarNode()) {
*error = keys::kModifySpecialError;
return false;
}
if (node->is_folder() && node->GetChildCount() > 0 && !recursive) {
*error = keys::kFolderNotEmptyError;
return false;
}
BookmarkNode* parent = node->GetParent();
int index = parent->IndexOfChild(node);
model->Remove(parent, index);
return true;
}
private:
ExtensionBookmarks();
};
void BookmarksFunction::Run() {
// TODO(erikkay) temporary hack until adding an event listener can notify the
// browser.
BookmarkModel* model = profile()->GetBookmarkModel();
if (!model->IsLoaded()) {
// Bookmarks are not ready yet. We'll wait.
registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
NotificationService::AllSources());
AddRef(); // balanced in Observe()
return;
}
ExtensionBookmarkEventRouter* event_router =
ExtensionBookmarkEventRouter::GetSingleton();
event_router->Observe(model);
SendResponse(RunImpl());
}
void BookmarksFunction::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(type == NotificationType::BOOKMARK_MODEL_LOADED);
DCHECK(profile()->GetBookmarkModel()->IsLoaded());
Run();
Release(); // balanced in Run()
}
// static
ExtensionBookmarkEventRouter* ExtensionBookmarkEventRouter::GetSingleton() {
return Singleton<ExtensionBookmarkEventRouter>::get();
}
ExtensionBookmarkEventRouter::ExtensionBookmarkEventRouter() {
}
ExtensionBookmarkEventRouter::~ExtensionBookmarkEventRouter() {
}
void ExtensionBookmarkEventRouter::Observe(BookmarkModel* model) {
if (models_.find(model) == models_.end()) {
model->AddObserver(this);
models_.insert(model);
}
}
void ExtensionBookmarkEventRouter::DispatchEvent(Profile *profile,
const char* event_name,
const std::string json_args) {
ExtensionMessageService::GetInstance(profile->GetRequestContext())->
DispatchEventToRenderers(event_name, json_args);
}
void ExtensionBookmarkEventRouter::Loaded(BookmarkModel* model) {
// TODO(erikkay): Perhaps we should send this event down to the extension
// so they know when it's safe to use the API?
}
void ExtensionBookmarkEventRouter::BookmarkNodeMoved(BookmarkModel* model,
BookmarkNode* old_parent,
int old_index,
BookmarkNode* new_parent,
int new_index) {
ListValue args;
BookmarkNode* node = new_parent->GetChild(new_index);
args.Append(new FundamentalValue(node->id()));
DictionaryValue* object_args = new DictionaryValue();
object_args->SetInteger(keys::kParentIdKey, new_parent->id());
object_args->SetInteger(keys::kIndexKey, new_index);
object_args->SetInteger(keys::kOldParentIdKey, old_parent->id());
object_args->SetInteger(keys::kOldIndexKey, old_index);
args.Append(object_args);
std::string json_args;
JSONWriter::Write(&args, false, &json_args);
DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args);
}
void ExtensionBookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model,
BookmarkNode* parent,
int index) {
ListValue args;
BookmarkNode* node = parent->GetChild(index);
args.Append(new FundamentalValue(node->id()));
DictionaryValue* object_args = new DictionaryValue();
object_args->SetString(keys::kTitleKey, node->GetTitle());
object_args->SetString(keys::kUrlKey, node->GetURL().spec());
object_args->SetInteger(keys::kParentIdKey, parent->id());
object_args->SetInteger(keys::kIndexKey, index);
args.Append(object_args);
std::string json_args;
JSONWriter::Write(&args, false, &json_args);
DispatchEvent(model->profile(), keys::kOnBookmarkAdded, json_args);
}
void ExtensionBookmarkEventRouter::BookmarkNodeRemoved(BookmarkModel* model,
BookmarkNode* parent,
int index) {
ListValue args;
DictionaryValue* object_args = new DictionaryValue();
object_args->SetInteger(keys::kParentIdKey, parent->id());
object_args->SetInteger(keys::kIndexKey, index);
args.Append(object_args);
std::string json_args;
JSONWriter::Write(&args, false, &json_args);
DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, json_args);
}
void ExtensionBookmarkEventRouter::BookmarkNodeChanged(BookmarkModel* model,
BookmarkNode* node) {
ListValue args;
args.Append(new FundamentalValue(node->id()));
// TODO(erikkay) The only two things that BookmarkModel sends this
// notification for are title and favicon. Since we're currently ignoring
// favicon and since the notification doesn't say which one anyway, for now
// we only include title. The ideal thing would be to change BookmarkModel
// to indicate what changed.
DictionaryValue* object_args = new DictionaryValue();
object_args->SetString(keys::kTitleKey, node->GetTitle());
args.Append(object_args);
std::string json_args;
JSONWriter::Write(&args, false, &json_args);
DispatchEvent(model->profile(), keys::kOnBookmarkChanged, json_args);
}
void ExtensionBookmarkEventRouter::BookmarkNodeFavIconLoaded(
BookmarkModel* model, BookmarkNode* node) {
// TODO(erikkay) anything we should do here?
}
void ExtensionBookmarkEventRouter::BookmarkNodeChildrenReordered(
BookmarkModel* model, BookmarkNode* node) {
ListValue args;
args.Append(new FundamentalValue(node->id()));
int childCount = node->GetChildCount();
ListValue* children = new ListValue();
for (int i = 0; i < childCount; ++i) {
BookmarkNode* child = node->GetChild(i);
Value* child_id = new FundamentalValue(child->id());
children->Append(child_id);
}
args.Append(children);
std::string json_args;
JSONWriter::Write(&args, false, &json_args);
DispatchEvent(model->profile(),
keys::kOnBookmarkChildrenReordered,
json_args);
}
bool GetBookmarksFunction::RunImpl() {
BookmarkModel* model = profile()->GetBookmarkModel();
scoped_ptr<ListValue> json(new ListValue());
if (args_->IsType(Value::TYPE_LIST)) {
ListValue* ids = static_cast<ListValue*>(args_);
size_t count = ids->GetSize();
EXTENSION_FUNCTION_VALIDATE(count > 0);
for (size_t i = 0; i < count; ++i) {
int id = 0;
EXTENSION_FUNCTION_VALIDATE(ids->GetInteger(i, &id));
BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
} else {
ExtensionBookmarks::AddNode(node, json.get(), false);
}
}
} else {
int id;
EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&id));
BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
}
ExtensionBookmarks::AddNode(node, json.get(), false);
}
result_.reset(json.release());
return true;
}
bool GetBookmarkChildrenFunction::RunImpl() {
BookmarkModel* model = profile()->GetBookmarkModel();
int id;
EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&id));
scoped_ptr<ListValue> json(new ListValue());
BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
}
int child_count = node->GetChildCount();
for (int i = 0; i < child_count; ++i) {
BookmarkNode* child = node->GetChild(i);
ExtensionBookmarks::AddNode(child, json.get(), false);
}
result_.reset(json.release());
return true;
}
bool GetBookmarkTreeFunction::RunImpl() {
BookmarkModel* model = profile()->GetBookmarkModel();
scoped_ptr<ListValue> json(new ListValue());
BookmarkNode* node = model->root_node();
ExtensionBookmarks::AddNode(node, json.get(), true);
result_.reset(json.release());
return true;
}
bool SearchBookmarksFunction::RunImpl() {
EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_STRING));
std::wstring query;
EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&query));
BookmarkModel* model = profile()->GetBookmarkModel();
ListValue* json = new ListValue();
std::wstring lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
std::vector<BookmarkNode*> nodes;
bookmark_utils::GetBookmarksContainingText(model, query, 50, lang, &nodes);
std::vector<BookmarkNode*>::iterator i = nodes.begin();
for (; i != nodes.end(); ++i) {
BookmarkNode* node = *i;
ExtensionBookmarks::AddNode(node, json, false);
}
result_.reset(json);
return true;
}
bool RemoveBookmarkFunction::RunImpl() {
EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST));
const ListValue* args = static_cast<const ListValue*>(args_);
bool recursive = false;
EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(1, &recursive));
BookmarkModel* model = profile()->GetBookmarkModel();
int id;
if (args->GetInteger(0, &id)) {
return ExtensionBookmarks::RemoveNode(model, id, recursive, &error_);
} else {
ListValue* ids;
EXTENSION_FUNCTION_VALIDATE(args->GetList(0, &ids));
size_t count = ids->GetSize();
EXTENSION_FUNCTION_VALIDATE(count > 0);
for (size_t i = 0; i < count; ++i) {
EXTENSION_FUNCTION_VALIDATE(ids->GetInteger(i, &id));
if (!ExtensionBookmarks::RemoveNode(model, id, recursive, &error_))
return false;
}
return true;
}
}
bool CreateBookmarkFunction::RunImpl() {
EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
DictionaryValue* json = static_cast<DictionaryValue*>(args_);
BookmarkModel* model = profile()->GetBookmarkModel();
int parentId;
if (!json->HasKey(keys::kParentIdKey)) {
// optional, default to "other bookmarks"
parentId = model->other_node()->id();
} else {
EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kParentIdKey,
&parentId));
}
BookmarkNode* parent = model->GetNodeByID(parentId);
if (!parent) {
error_ = keys::kNoParentError;
return false;
}
if (parent->GetParent() == NULL) { // can't create children of the root
error_ = keys::kNoParentError;
return false;
}
int index;
if (!json->HasKey(keys::kIndexKey)) { // optional (defaults to end)
index = parent->GetChildCount();
} else {
EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kIndexKey, &index));
if (index > parent->GetChildCount() || index < 0) {
error_ = keys::kInvalidIndexError;
return false;
}
}
std::wstring title;
json->GetString(keys::kTitleKey, &title); // optional
std::string url_string;
json->GetString(keys::kUrlKey, &url_string); // optional
GURL url(url_string);
if (!url.is_empty() && !url.is_valid()) {
error_ = keys::kInvalidUrlError;
return false;
}
BookmarkNode* node;
if (url_string.length())
node = model->AddURL(parent, index, title, url);
else
node = model->AddGroup(parent, index, title);
DCHECK(node);
if (!node) {
error_ = keys::kNoNodeError;
return false;
}
DictionaryValue* ret = ExtensionBookmarks::GetNodeDictionary(node, false);
result_.reset(ret);
return true;
}
bool MoveBookmarkFunction::RunImpl() {
EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST));
const ListValue* args = static_cast<const ListValue*>(args_);
int id;
EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &id));
DictionaryValue* destination;
EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &destination));
BookmarkModel* model = profile()->GetBookmarkModel();
BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
}
if (node == model->root_node() ||
node == model->other_node() ||
node == model->GetBookmarkBarNode()) {
error_ = keys::kModifySpecialError;
return false;
}
BookmarkNode* parent;
if (!destination->HasKey(keys::kParentIdKey)) {
// optional, defaults to current parent
parent = node->GetParent();
} else {
int parentId;
EXTENSION_FUNCTION_VALIDATE(destination->GetInteger(keys::kParentIdKey,
&parentId));
parent = model->GetNodeByID(parentId);
}
if (!parent) {
error_ = keys::kNoParentError;
// TODO(erikkay) return an error message
return false;
}
if (parent == model->root_node()) {
error_ = keys::kModifySpecialError;
return false;
}
int index;
if (destination->HasKey(keys::kIndexKey)) { // optional (defaults to end)
EXTENSION_FUNCTION_VALIDATE(destination->GetInteger(keys::kIndexKey,
&index));
if (index > parent->GetChildCount() || index < 0) {
error_ = keys::kInvalidIndexError;
return false;
}
} else {
index = parent->GetChildCount();
}
model->Move(node, parent, index);
return true;
}
bool SetBookmarkTitleFunction::RunImpl() {
EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
DictionaryValue* json = static_cast<DictionaryValue*>(args_);
std::wstring title;
json->GetString(keys::kTitleKey, &title); // optional (empty is clear)
BookmarkModel* model = profile()->GetBookmarkModel();
int id = 0;
EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kIdKey, &id));
BookmarkNode* node = model->GetNodeByID(id);
if (!node) {
error_ = keys::kNoNodeError;
return false;
}
if (node == model->root_node() ||
node == model->other_node() ||
node == model->GetBookmarkBarNode()) {
error_ = keys::kModifySpecialError;
return false;
}
model->SetTitle(node, title);
return true;
}
|