Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: chrome/browser/extensions/extension_bookmarks_module.cc

Issue 151032: a few minor tweaks the bookmarks API:... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/extension_bookmarks_module.h" 5 #include "chrome/browser/extensions/extension_bookmarks_module.h"
6 6
7 #include "base/json_writer.h" 7 #include "base/json_writer.h"
8 #include "chrome/browser/bookmarks/bookmark_codec.h" 8 #include "chrome/browser/bookmarks/bookmark_codec.h"
9 #include "chrome/browser/bookmarks/bookmark_model.h" 9 #include "chrome/browser/bookmarks/bookmark_model.h"
10 #include "chrome/browser/bookmarks/bookmark_utils.h" 10 #include "chrome/browser/bookmarks/bookmark_utils.h"
(...skipping 13 matching lines...) Expand all
24 // Convert |node| into a JSON value 24 // Convert |node| into a JSON value
25 static DictionaryValue* GetNodeDictionary(const BookmarkNode* node, 25 static DictionaryValue* GetNodeDictionary(const BookmarkNode* node,
26 bool recurse) { 26 bool recurse) {
27 DictionaryValue* dict = new DictionaryValue(); 27 DictionaryValue* dict = new DictionaryValue();
28 dict->SetInteger(keys::kIdKey, node->id()); 28 dict->SetInteger(keys::kIdKey, node->id());
29 29
30 const BookmarkNode* parent = node->GetParent(); 30 const BookmarkNode* parent = node->GetParent();
31 if (parent) 31 if (parent)
32 dict->SetInteger(keys::kParentIdKey, parent->id()); 32 dict->SetInteger(keys::kParentIdKey, parent->id());
33 33
34 if (!node->is_folder()) 34 if (!node->is_folder()) {
35 dict->SetString(keys::kUrlKey, node->GetURL().spec()); 35 dict->SetString(keys::kUrlKey, node->GetURL().spec());
36 } else {
37 // Javascript Date wants milliseconds since the epoch, ToDoubleT is
38 // seconds.
39 base::Time t = node->date_group_modified();
40 if (!t.is_null())
41 dict->SetReal(keys::kDateGroupModifiedKey, floor(t.ToDoubleT() * 1000));
42 }
36 43
37 dict->SetString(keys::kTitleKey, node->GetTitle()); 44 dict->SetString(keys::kTitleKey, node->GetTitle());
45 if (!node->date_added().is_null()) {
46 // Javascript Date wants milliseconds since the epoch, ToDoubleT is
47 // seconds.
48 dict->SetReal(keys::kDateAddedKey,
49 floor(node->date_added().ToDoubleT() * 1000));
50 }
38 51
39 int childCount = node->GetChildCount(); 52 int childCount = node->GetChildCount();
40 ListValue* children = new ListValue(); 53 ListValue* children = new ListValue();
41 for (int i = 0; i < childCount; ++i) { 54 for (int i = 0; i < childCount; ++i) {
42 const BookmarkNode* child = node->GetChild(i); 55 const BookmarkNode* child = node->GetChild(i);
43 if (recurse) { 56 if (recurse) {
44 DictionaryValue* dict = GetNodeDictionary(child, true); 57 DictionaryValue* dict = GetNodeDictionary(child, true);
45 children->Append(dict); 58 children->Append(dict);
46 } 59 }
47 } 60 }
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 JSONWriter::Write(&args, false, &json_args); 174 JSONWriter::Write(&args, false, &json_args);
162 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args); 175 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args);
163 } 176 }
164 177
165 void ExtensionBookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, 178 void ExtensionBookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model,
166 const BookmarkNode* parent, 179 const BookmarkNode* parent,
167 int index) { 180 int index) {
168 ListValue args; 181 ListValue args;
169 const BookmarkNode* node = parent->GetChild(index); 182 const BookmarkNode* node = parent->GetChild(index);
170 args.Append(new FundamentalValue(node->id())); 183 args.Append(new FundamentalValue(node->id()));
171 DictionaryValue* object_args = new DictionaryValue(); 184 DictionaryValue* obj = ExtensionBookmarks::GetNodeDictionary(node, false);
172 object_args->SetString(keys::kTitleKey, node->GetTitle()); 185
173 object_args->SetString(keys::kUrlKey, node->GetURL().spec()); 186 // Remove id since it's already being passed as the first argument.
174 object_args->SetInteger(keys::kParentIdKey, parent->id()); 187 obj->Remove(keys::kIdKey, NULL);
175 object_args->SetInteger(keys::kIndexKey, index); 188 args.Append(obj);
176 args.Append(object_args);
177 189
178 std::string json_args; 190 std::string json_args;
179 JSONWriter::Write(&args, false, &json_args); 191 JSONWriter::Write(&args, false, &json_args);
180 DispatchEvent(model->profile(), keys::kOnBookmarkAdded, json_args); 192 DispatchEvent(model->profile(), keys::kOnBookmarkAdded, json_args);
181 } 193 }
182 194
183 void ExtensionBookmarkEventRouter::BookmarkNodeRemoved( 195 void ExtensionBookmarkEventRouter::BookmarkNodeRemoved(
184 BookmarkModel* model, 196 BookmarkModel* model,
185 const BookmarkNode* parent, 197 const BookmarkNode* parent,
186 int index) { 198 int index) {
199 // TODO(erikkay) can this version ever be called?
200 NOTREACHED();
201 }
202
203 void ExtensionBookmarkEventRouter::BookmarkNodeRemoved(
204 BookmarkModel* model,
205 const BookmarkNode* parent,
206 int index,
207 const BookmarkNode* node) {
187 ListValue args; 208 ListValue args;
209 args.Append(new FundamentalValue(node->id()));
188 DictionaryValue* object_args = new DictionaryValue(); 210 DictionaryValue* object_args = new DictionaryValue();
189 object_args->SetInteger(keys::kParentIdKey, parent->id()); 211 object_args->SetInteger(keys::kParentIdKey, parent->id());
190 object_args->SetInteger(keys::kIndexKey, index); 212 object_args->SetInteger(keys::kIndexKey, index);
191 args.Append(object_args); 213 args.Append(object_args);
192 214
193 std::string json_args; 215 std::string json_args;
194 JSONWriter::Write(&args, false, &json_args); 216 JSONWriter::Write(&args, false, &json_args);
195 DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, json_args); 217 DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, json_args);
196 } 218 }
197 219
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 } 503 }
482 if (node == model->root_node() || 504 if (node == model->root_node() ||
483 node == model->other_node() || 505 node == model->other_node() ||
484 node == model->GetBookmarkBarNode()) { 506 node == model->GetBookmarkBarNode()) {
485 error_ = keys::kModifySpecialError; 507 error_ = keys::kModifySpecialError;
486 return false; 508 return false;
487 } 509 }
488 model->SetTitle(node, title); 510 model->SetTitle(node, title);
489 return true; 511 return true;
490 } 512 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698