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

Side by Side Diff: chrome/browser/bookmarks/bookmark_extension_api.cc

Issue 8374023: Move bookmarks extension api implementation to bookmarks dir (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix deps, rebase Created 9 years, 1 month 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/bookmarks/bookmark_extension_api.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/i18n/file_util_icu.h" 9 #include "base/i18n/file_util_icu.h"
10 #include "base/i18n/time_formatting.h" 10 #include "base/i18n/time_formatting.h"
11 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/sha1.h" 13 #include "base/sha1.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/string16.h" 15 #include "base/string16.h"
16 #include "base/string_number_conversions.h" 16 #include "base/string_number_conversions.h"
17 #include "base/string_util.h" 17 #include "base/string_util.h"
18 #include "base/time.h" 18 #include "base/time.h"
19 #include "base/utf_string_conversions.h" 19 #include "base/utf_string_conversions.h"
20 #include "chrome/browser/bookmarks/bookmark_codec.h" 20 #include "chrome/browser/bookmarks/bookmark_codec.h"
21 #include "chrome/browser/bookmarks/bookmark_extension_api_constants.h"
22 #include "chrome/browser/bookmarks/bookmark_extension_helpers.h"
21 #include "chrome/browser/bookmarks/bookmark_html_writer.h" 23 #include "chrome/browser/bookmarks/bookmark_html_writer.h"
22 #include "chrome/browser/bookmarks/bookmark_model.h" 24 #include "chrome/browser/bookmarks/bookmark_model.h"
23 #include "chrome/browser/bookmarks/bookmark_utils.h" 25 #include "chrome/browser/bookmarks/bookmark_utils.h"
24 #include "chrome/browser/extensions/extension_bookmark_helpers.h"
25 #include "chrome/browser/extensions/extension_bookmarks_module_constants.h"
26 #include "chrome/browser/extensions/extension_event_router.h" 26 #include "chrome/browser/extensions/extension_event_router.h"
27 #include "chrome/browser/extensions/extension_function_dispatcher.h" 27 #include "chrome/browser/extensions/extension_function_dispatcher.h"
28 #include "chrome/browser/extensions/extensions_quota_service.h" 28 #include "chrome/browser/extensions/extensions_quota_service.h"
29 #include "chrome/browser/importer/importer_data_types.h" 29 #include "chrome/browser/importer/importer_data_types.h"
30 #include "chrome/browser/importer/importer_host.h" 30 #include "chrome/browser/importer/importer_host.h"
31 #include "chrome/browser/prefs/pref_service.h" 31 #include "chrome/browser/prefs/pref_service.h"
32 #include "chrome/browser/profiles/profile.h" 32 #include "chrome/browser/profiles/profile.h"
33 #include "chrome/browser/ui/browser_list.h" 33 #include "chrome/browser/ui/browser_list.h"
34 #include "chrome/common/chrome_notification_types.h" 34 #include "chrome/common/chrome_notification_types.h"
35 #include "chrome/common/chrome_paths.h" 35 #include "chrome/common/chrome_paths.h"
36 #include "chrome/common/pref_names.h" 36 #include "chrome/common/pref_names.h"
37 #include "content/public/browser/notification_service.h" 37 #include "content/public/browser/notification_service.h"
38 #include "grit/generated_resources.h" 38 #include "grit/generated_resources.h"
39 #include "ui/base/l10n/l10n_util.h" 39 #include "ui/base/l10n/l10n_util.h"
40 40
41 namespace keys = extension_bookmarks_module_constants; 41 namespace keys = bookmark_extension_api_constants;
42 42
43 using base::TimeDelta; 43 using base::TimeDelta;
44 typedef QuotaLimitHeuristic::Bucket Bucket; 44 typedef QuotaLimitHeuristic::Bucket Bucket;
45 typedef QuotaLimitHeuristic::Config Config; 45 typedef QuotaLimitHeuristic::Config Config;
46 typedef QuotaLimitHeuristic::BucketList BucketList; 46 typedef QuotaLimitHeuristic::BucketList BucketList;
47 typedef ExtensionsQuotaService::TimedLimit TimedLimit; 47 typedef ExtensionsQuotaService::TimedLimit TimedLimit;
48 typedef ExtensionsQuotaService::SustainedLimit SustainedLimit; 48 typedef ExtensionsQuotaService::SustainedLimit SustainedLimit;
49 typedef QuotaLimitHeuristic::BucketMapper BucketMapper; 49 typedef QuotaLimitHeuristic::BucketMapper BucketMapper;
50 50
51 namespace { 51 namespace {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 DCHECK(type == chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED); 118 DCHECK(type == chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED);
119 Profile* source_profile = content::Source<Profile>(source).ptr(); 119 Profile* source_profile = content::Source<Profile>(source).ptr();
120 if (!source_profile || !source_profile->IsSameProfile(profile())) 120 if (!source_profile || !source_profile->IsSameProfile(profile()))
121 return; 121 return;
122 122
123 DCHECK(profile()->GetBookmarkModel()->IsLoaded()); 123 DCHECK(profile()->GetBookmarkModel()->IsLoaded());
124 Run(); 124 Run();
125 Release(); // Balanced in Run(). 125 Release(); // Balanced in Run().
126 } 126 }
127 127
128 ExtensionBookmarkEventRouter::ExtensionBookmarkEventRouter( 128 BookmarkExtensionEventRouter::BookmarkExtensionEventRouter(
129 BookmarkModel* model) : model_(model) { 129 BookmarkModel* model) : model_(model) {
130 } 130 }
131 131
132 ExtensionBookmarkEventRouter::~ExtensionBookmarkEventRouter() { 132 BookmarkExtensionEventRouter::~BookmarkExtensionEventRouter() {
133 if (model_) { 133 if (model_) {
134 model_->RemoveObserver(this); 134 model_->RemoveObserver(this);
135 } 135 }
136 } 136 }
137 137
138 void ExtensionBookmarkEventRouter::Init() { 138 void BookmarkExtensionEventRouter::Init() {
139 model_->AddObserver(this); 139 model_->AddObserver(this);
140 } 140 }
141 141
142 void ExtensionBookmarkEventRouter::DispatchEvent(Profile *profile, 142 void BookmarkExtensionEventRouter::DispatchEvent(Profile *profile,
143 const char* event_name, 143 const char* event_name,
144 const std::string& json_args) { 144 const std::string& json_args) {
145 if (profile->GetExtensionEventRouter()) { 145 if (profile->GetExtensionEventRouter()) {
146 profile->GetExtensionEventRouter()->DispatchEventToRenderers( 146 profile->GetExtensionEventRouter()->DispatchEventToRenderers(
147 event_name, json_args, NULL, GURL()); 147 event_name, json_args, NULL, GURL());
148 } 148 }
149 } 149 }
150 150
151 void ExtensionBookmarkEventRouter::Loaded(BookmarkModel* model, 151 void BookmarkExtensionEventRouter::Loaded(BookmarkModel* model,
152 bool ids_reassigned) { 152 bool ids_reassigned) {
153 // TODO(erikkay): Perhaps we should send this event down to the extension 153 // TODO(erikkay): Perhaps we should send this event down to the extension
154 // so they know when it's safe to use the API? 154 // so they know when it's safe to use the API?
155 } 155 }
156 156
157 void ExtensionBookmarkEventRouter::BookmarkModelBeingDeleted( 157 void BookmarkExtensionEventRouter::BookmarkModelBeingDeleted(
158 BookmarkModel* model) { 158 BookmarkModel* model) {
159 model_ = NULL; 159 model_ = NULL;
160 } 160 }
161 161
162 void ExtensionBookmarkEventRouter::BookmarkNodeMoved( 162 void BookmarkExtensionEventRouter::BookmarkNodeMoved(
163 BookmarkModel* model, 163 BookmarkModel* model,
164 const BookmarkNode* old_parent, 164 const BookmarkNode* old_parent,
165 int old_index, 165 int old_index,
166 const BookmarkNode* new_parent, 166 const BookmarkNode* new_parent,
167 int new_index) { 167 int new_index) {
168 ListValue args; 168 ListValue args;
169 const BookmarkNode* node = new_parent->GetChild(new_index); 169 const BookmarkNode* node = new_parent->GetChild(new_index);
170 args.Append(new StringValue(base::Int64ToString(node->id()))); 170 args.Append(new StringValue(base::Int64ToString(node->id())));
171 DictionaryValue* object_args = new DictionaryValue(); 171 DictionaryValue* object_args = new DictionaryValue();
172 object_args->SetString(keys::kParentIdKey, 172 object_args->SetString(keys::kParentIdKey,
173 base::Int64ToString(new_parent->id())); 173 base::Int64ToString(new_parent->id()));
174 object_args->SetInteger(keys::kIndexKey, new_index); 174 object_args->SetInteger(keys::kIndexKey, new_index);
175 object_args->SetString(keys::kOldParentIdKey, 175 object_args->SetString(keys::kOldParentIdKey,
176 base::Int64ToString(old_parent->id())); 176 base::Int64ToString(old_parent->id()));
177 object_args->SetInteger(keys::kOldIndexKey, old_index); 177 object_args->SetInteger(keys::kOldIndexKey, old_index);
178 args.Append(object_args); 178 args.Append(object_args);
179 179
180 std::string json_args; 180 std::string json_args;
181 base::JSONWriter::Write(&args, false, &json_args); 181 base::JSONWriter::Write(&args, false, &json_args);
182 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args); 182 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args);
183 } 183 }
184 184
185 void ExtensionBookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, 185 void BookmarkExtensionEventRouter::BookmarkNodeAdded(BookmarkModel* model,
186 const BookmarkNode* parent, 186 const BookmarkNode* parent,
187 int index) { 187 int index) {
188 ListValue args; 188 ListValue args;
189 const BookmarkNode* node = parent->GetChild(index); 189 const BookmarkNode* node = parent->GetChild(index);
190 args.Append(new StringValue(base::Int64ToString(node->id()))); 190 args.Append(new StringValue(base::Int64ToString(node->id())));
191 DictionaryValue* obj = 191 DictionaryValue* obj =
192 extension_bookmark_helpers::GetNodeDictionary(node, false, false); 192 bookmark_extension_helpers::GetNodeDictionary(node, false, false);
193 args.Append(obj); 193 args.Append(obj);
194 194
195 std::string json_args; 195 std::string json_args;
196 base::JSONWriter::Write(&args, false, &json_args); 196 base::JSONWriter::Write(&args, false, &json_args);
197 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, json_args); 197 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, json_args);
198 } 198 }
199 199
200 void ExtensionBookmarkEventRouter::BookmarkNodeRemoved( 200 void BookmarkExtensionEventRouter::BookmarkNodeRemoved(
201 BookmarkModel* model, 201 BookmarkModel* model,
202 const BookmarkNode* parent, 202 const BookmarkNode* parent,
203 int index, 203 int index,
204 const BookmarkNode* node) { 204 const BookmarkNode* node) {
205 ListValue args; 205 ListValue args;
206 args.Append(new StringValue(base::Int64ToString(node->id()))); 206 args.Append(new StringValue(base::Int64ToString(node->id())));
207 DictionaryValue* object_args = new DictionaryValue(); 207 DictionaryValue* object_args = new DictionaryValue();
208 object_args->SetString(keys::kParentIdKey, 208 object_args->SetString(keys::kParentIdKey,
209 base::Int64ToString(parent->id())); 209 base::Int64ToString(parent->id()));
210 object_args->SetInteger(keys::kIndexKey, index); 210 object_args->SetInteger(keys::kIndexKey, index);
211 args.Append(object_args); 211 args.Append(object_args);
212 212
213 std::string json_args; 213 std::string json_args;
214 base::JSONWriter::Write(&args, false, &json_args); 214 base::JSONWriter::Write(&args, false, &json_args);
215 DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, json_args); 215 DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, json_args);
216 } 216 }
217 217
218 void ExtensionBookmarkEventRouter::BookmarkNodeChanged( 218 void BookmarkExtensionEventRouter::BookmarkNodeChanged(
219 BookmarkModel* model, const BookmarkNode* node) { 219 BookmarkModel* model, const BookmarkNode* node) {
220 ListValue args; 220 ListValue args;
221 args.Append(new StringValue(base::Int64ToString(node->id()))); 221 args.Append(new StringValue(base::Int64ToString(node->id())));
222 222
223 // TODO(erikkay) The only three things that BookmarkModel sends this 223 // TODO(erikkay) The only three things that BookmarkModel sends this
224 // notification for are title, url and favicon. Since we're currently 224 // notification for are title, url and favicon. Since we're currently
225 // ignoring favicon and since the notification doesn't say which one anyway, 225 // ignoring favicon and since the notification doesn't say which one anyway,
226 // for now we only include title and url. The ideal thing would be to change 226 // for now we only include title and url. The ideal thing would be to change
227 // BookmarkModel to indicate what changed. 227 // BookmarkModel to indicate what changed.
228 DictionaryValue* object_args = new DictionaryValue(); 228 DictionaryValue* object_args = new DictionaryValue();
229 object_args->SetString(keys::kTitleKey, node->GetTitle()); 229 object_args->SetString(keys::kTitleKey, node->GetTitle());
230 if (node->is_url()) 230 if (node->is_url())
231 object_args->SetString(keys::kUrlKey, node->url().spec()); 231 object_args->SetString(keys::kUrlKey, node->url().spec());
232 args.Append(object_args); 232 args.Append(object_args);
233 233
234 std::string json_args; 234 std::string json_args;
235 base::JSONWriter::Write(&args, false, &json_args); 235 base::JSONWriter::Write(&args, false, &json_args);
236 DispatchEvent(model->profile(), keys::kOnBookmarkChanged, json_args); 236 DispatchEvent(model->profile(), keys::kOnBookmarkChanged, json_args);
237 } 237 }
238 238
239 void ExtensionBookmarkEventRouter::BookmarkNodeFaviconChanged( 239 void BookmarkExtensionEventRouter::BookmarkNodeFaviconChanged(
240 BookmarkModel* model, const BookmarkNode* node) { 240 BookmarkModel* model, const BookmarkNode* node) {
241 // TODO(erikkay) anything we should do here? 241 // TODO(erikkay) anything we should do here?
242 } 242 }
243 243
244 void ExtensionBookmarkEventRouter::BookmarkNodeChildrenReordered( 244 void BookmarkExtensionEventRouter::BookmarkNodeChildrenReordered(
245 BookmarkModel* model, const BookmarkNode* node) { 245 BookmarkModel* model, const BookmarkNode* node) {
246 ListValue args; 246 ListValue args;
247 args.Append(new StringValue(base::Int64ToString(node->id()))); 247 args.Append(new StringValue(base::Int64ToString(node->id())));
248 int childCount = node->child_count(); 248 int childCount = node->child_count();
249 ListValue* children = new ListValue(); 249 ListValue* children = new ListValue();
250 for (int i = 0; i < childCount; ++i) { 250 for (int i = 0; i < childCount; ++i) {
251 const BookmarkNode* child = node->GetChild(i); 251 const BookmarkNode* child = node->GetChild(i);
252 Value* child_id = new StringValue(base::Int64ToString(child->id())); 252 Value* child_id = new StringValue(base::Int64ToString(child->id()));
253 children->Append(child_id); 253 children->Append(child_id);
254 } 254 }
255 DictionaryValue* reorder_info = new DictionaryValue(); 255 DictionaryValue* reorder_info = new DictionaryValue();
256 reorder_info->Set(keys::kChildIdsKey, children); 256 reorder_info->Set(keys::kChildIdsKey, children);
257 args.Append(reorder_info); 257 args.Append(reorder_info);
258 258
259 std::string json_args; 259 std::string json_args;
260 base::JSONWriter::Write(&args, false, &json_args); 260 base::JSONWriter::Write(&args, false, &json_args);
261 DispatchEvent(model->profile(), 261 DispatchEvent(model->profile(),
262 keys::kOnBookmarkChildrenReordered, 262 keys::kOnBookmarkChildrenReordered,
263 json_args); 263 json_args);
264 } 264 }
265 265
266 void ExtensionBookmarkEventRouter:: 266 void BookmarkExtensionEventRouter::
267 BookmarkImportBeginning(BookmarkModel* model) { 267 BookmarkImportBeginning(BookmarkModel* model) {
268 ListValue args; 268 ListValue args;
269 std::string json_args; 269 std::string json_args;
270 base::JSONWriter::Write(&args, false, &json_args); 270 base::JSONWriter::Write(&args, false, &json_args);
271 DispatchEvent(model->profile(), 271 DispatchEvent(model->profile(),
272 keys::kOnBookmarkImportBegan, 272 keys::kOnBookmarkImportBegan,
273 json_args); 273 json_args);
274 } 274 }
275 275
276 void ExtensionBookmarkEventRouter::BookmarkImportEnding(BookmarkModel* model) { 276 void BookmarkExtensionEventRouter::BookmarkImportEnding(BookmarkModel* model) {
277 ListValue args; 277 ListValue args;
278 std::string json_args; 278 std::string json_args;
279 base::JSONWriter::Write(&args, false, &json_args); 279 base::JSONWriter::Write(&args, false, &json_args);
280 DispatchEvent(model->profile(), 280 DispatchEvent(model->profile(),
281 keys::kOnBookmarkImportEnded, 281 keys::kOnBookmarkImportEnded,
282 json_args); 282 json_args);
283 } 283 }
284 284
285 bool GetBookmarksFunction::RunImpl() { 285 bool GetBookmarksFunction::RunImpl() {
286 BookmarkModel* model = profile()->GetBookmarkModel(); 286 BookmarkModel* model = profile()->GetBookmarkModel();
287 scoped_ptr<ListValue> json(new ListValue()); 287 scoped_ptr<ListValue> json(new ListValue());
288 Value* arg0; 288 Value* arg0;
289 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0)); 289 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0));
290 if (arg0->IsType(Value::TYPE_LIST)) { 290 if (arg0->IsType(Value::TYPE_LIST)) {
291 const ListValue* ids = static_cast<const ListValue*>(arg0); 291 const ListValue* ids = static_cast<const ListValue*>(arg0);
292 size_t count = ids->GetSize(); 292 size_t count = ids->GetSize();
293 EXTENSION_FUNCTION_VALIDATE(count > 0); 293 EXTENSION_FUNCTION_VALIDATE(count > 0);
294 for (size_t i = 0; i < count; ++i) { 294 for (size_t i = 0; i < count; ++i) {
295 int64 id; 295 int64 id;
296 std::string id_string; 296 std::string id_string;
297 EXTENSION_FUNCTION_VALIDATE(ids->GetString(i, &id_string)); 297 EXTENSION_FUNCTION_VALIDATE(ids->GetString(i, &id_string));
298 if (!GetBookmarkIdAsInt64(id_string, &id)) 298 if (!GetBookmarkIdAsInt64(id_string, &id))
299 return false; 299 return false;
300 const BookmarkNode* node = model->GetNodeByID(id); 300 const BookmarkNode* node = model->GetNodeByID(id);
301 if (!node) { 301 if (!node) {
302 error_ = keys::kNoNodeError; 302 error_ = keys::kNoNodeError;
303 return false; 303 return false;
304 } else { 304 } else {
305 extension_bookmark_helpers::AddNode(node, json.get(), false); 305 bookmark_extension_helpers::AddNode(node, json.get(), false);
306 } 306 }
307 } 307 }
308 } else { 308 } else {
309 int64 id; 309 int64 id;
310 std::string id_string; 310 std::string id_string;
311 EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string)); 311 EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string));
312 if (!GetBookmarkIdAsInt64(id_string, &id)) 312 if (!GetBookmarkIdAsInt64(id_string, &id))
313 return false; 313 return false;
314 const BookmarkNode* node = model->GetNodeByID(id); 314 const BookmarkNode* node = model->GetNodeByID(id);
315 if (!node) { 315 if (!node) {
316 error_ = keys::kNoNodeError; 316 error_ = keys::kNoNodeError;
317 return false; 317 return false;
318 } 318 }
319 extension_bookmark_helpers::AddNode(node, json.get(), false); 319 bookmark_extension_helpers::AddNode(node, json.get(), false);
320 } 320 }
321 321
322 result_.reset(json.release()); 322 result_.reset(json.release());
323 return true; 323 return true;
324 } 324 }
325 325
326 bool GetBookmarkChildrenFunction::RunImpl() { 326 bool GetBookmarkChildrenFunction::RunImpl() {
327 BookmarkModel* model = profile()->GetBookmarkModel(); 327 BookmarkModel* model = profile()->GetBookmarkModel();
328 int64 id; 328 int64 id;
329 std::string id_string; 329 std::string id_string;
330 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string)); 330 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &id_string));
331 if (!GetBookmarkIdAsInt64(id_string, &id)) 331 if (!GetBookmarkIdAsInt64(id_string, &id))
332 return false; 332 return false;
333 scoped_ptr<ListValue> json(new ListValue()); 333 scoped_ptr<ListValue> json(new ListValue());
334 const BookmarkNode* node = model->GetNodeByID(id); 334 const BookmarkNode* node = model->GetNodeByID(id);
335 if (!node) { 335 if (!node) {
336 error_ = keys::kNoNodeError; 336 error_ = keys::kNoNodeError;
337 return false; 337 return false;
338 } 338 }
339 int child_count = node->child_count(); 339 int child_count = node->child_count();
340 for (int i = 0; i < child_count; ++i) { 340 for (int i = 0; i < child_count; ++i) {
341 const BookmarkNode* child = node->GetChild(i); 341 const BookmarkNode* child = node->GetChild(i);
342 extension_bookmark_helpers::AddNode(child, json.get(), false); 342 bookmark_extension_helpers::AddNode(child, json.get(), false);
343 } 343 }
344 344
345 result_.reset(json.release()); 345 result_.reset(json.release());
346 return true; 346 return true;
347 } 347 }
348 348
349 bool GetBookmarkRecentFunction::RunImpl() { 349 bool GetBookmarkRecentFunction::RunImpl() {
350 int number_of_items; 350 int number_of_items;
351 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &number_of_items)); 351 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &number_of_items));
352 if (number_of_items < 1) 352 if (number_of_items < 1)
353 return false; 353 return false;
354 354
355 BookmarkModel* model = profile()->GetBookmarkModel(); 355 BookmarkModel* model = profile()->GetBookmarkModel();
356 ListValue* json = new ListValue(); 356 ListValue* json = new ListValue();
357 std::vector<const BookmarkNode*> nodes; 357 std::vector<const BookmarkNode*> nodes;
358 bookmark_utils::GetMostRecentlyAddedEntries(model, number_of_items, &nodes); 358 bookmark_utils::GetMostRecentlyAddedEntries(model, number_of_items, &nodes);
359 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); 359 std::vector<const BookmarkNode*>::iterator i = nodes.begin();
360 for (; i != nodes.end(); ++i) { 360 for (; i != nodes.end(); ++i) {
361 const BookmarkNode* node = *i; 361 const BookmarkNode* node = *i;
362 extension_bookmark_helpers::AddNode(node, json, false); 362 bookmark_extension_helpers::AddNode(node, json, false);
363 } 363 }
364 result_.reset(json); 364 result_.reset(json);
365 return true; 365 return true;
366 } 366 }
367 367
368 bool GetBookmarkTreeFunction::RunImpl() { 368 bool GetBookmarkTreeFunction::RunImpl() {
369 BookmarkModel* model = profile()->GetBookmarkModel(); 369 BookmarkModel* model = profile()->GetBookmarkModel();
370 scoped_ptr<ListValue> json(new ListValue()); 370 scoped_ptr<ListValue> json(new ListValue());
371 const BookmarkNode* node = model->root_node(); 371 const BookmarkNode* node = model->root_node();
372 extension_bookmark_helpers::AddNode(node, json.get(), true); 372 bookmark_extension_helpers::AddNode(node, json.get(), true);
373 result_.reset(json.release()); 373 result_.reset(json.release());
374 return true; 374 return true;
375 } 375 }
376 376
377 bool GetBookmarkSubTreeFunction::RunImpl() { 377 bool GetBookmarkSubTreeFunction::RunImpl() {
378 BookmarkModel* model = profile()->GetBookmarkModel(); 378 BookmarkModel* model = profile()->GetBookmarkModel();
379 scoped_ptr<ListValue> json(new ListValue()); 379 scoped_ptr<ListValue> json(new ListValue());
380 Value* arg0; 380 Value* arg0;
381 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0)); 381 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0));
382 int64 id; 382 int64 id;
383 std::string id_string; 383 std::string id_string;
384 EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string)); 384 EXTENSION_FUNCTION_VALIDATE(arg0->GetAsString(&id_string));
385 if (!GetBookmarkIdAsInt64(id_string, &id)) 385 if (!GetBookmarkIdAsInt64(id_string, &id))
386 return false; 386 return false;
387 const BookmarkNode* node = model->GetNodeByID(id); 387 const BookmarkNode* node = model->GetNodeByID(id);
388 if (!node) { 388 if (!node) {
389 error_ = keys::kNoNodeError; 389 error_ = keys::kNoNodeError;
390 return false; 390 return false;
391 } 391 }
392 extension_bookmark_helpers::AddNode(node, json.get(), true); 392 bookmark_extension_helpers::AddNode(node, json.get(), true);
393 result_.reset(json.release()); 393 result_.reset(json.release());
394 return true; 394 return true;
395 } 395 }
396 396
397 bool SearchBookmarksFunction::RunImpl() { 397 bool SearchBookmarksFunction::RunImpl() {
398 string16 query; 398 string16 query;
399 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &query)); 399 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &query));
400 400
401 BookmarkModel* model = profile()->GetBookmarkModel(); 401 BookmarkModel* model = profile()->GetBookmarkModel();
402 ListValue* json = new ListValue(); 402 ListValue* json = new ListValue();
403 std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); 403 std::string lang = profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
404 std::vector<const BookmarkNode*> nodes; 404 std::vector<const BookmarkNode*> nodes;
405 bookmark_utils::GetBookmarksContainingText(model, query, 405 bookmark_utils::GetBookmarksContainingText(model, query,
406 std::numeric_limits<int>::max(), 406 std::numeric_limits<int>::max(),
407 lang, &nodes); 407 lang, &nodes);
408 std::vector<const BookmarkNode*>::iterator i = nodes.begin(); 408 std::vector<const BookmarkNode*>::iterator i = nodes.begin();
409 for (; i != nodes.end(); ++i) { 409 for (; i != nodes.end(); ++i) {
410 const BookmarkNode* node = *i; 410 const BookmarkNode* node = *i;
411 extension_bookmark_helpers::AddNode(node, json, false); 411 bookmark_extension_helpers::AddNode(node, json, false);
412 } 412 }
413 413
414 result_.reset(json); 414 result_.reset(json);
415 return true; 415 return true;
416 } 416 }
417 417
418 // static 418 // static
419 bool RemoveBookmarkFunction::ExtractIds(const ListValue* args, 419 bool RemoveBookmarkFunction::ExtractIds(const ListValue* args,
420 std::list<int64>* ids, 420 std::list<int64>* ids,
421 bool* invalid_id) { 421 bool* invalid_id) {
(...skipping 19 matching lines...) Expand all
441 return false; 441 return false;
442 } 442 }
443 bool recursive = false; 443 bool recursive = false;
444 if (name() == RemoveTreeBookmarkFunction::function_name()) 444 if (name() == RemoveTreeBookmarkFunction::function_name())
445 recursive = true; 445 recursive = true;
446 446
447 BookmarkModel* model = profile()->GetBookmarkModel(); 447 BookmarkModel* model = profile()->GetBookmarkModel();
448 size_t count = ids.size(); 448 size_t count = ids.size();
449 EXTENSION_FUNCTION_VALIDATE(count > 0); 449 EXTENSION_FUNCTION_VALIDATE(count > 0);
450 for (std::list<int64>::iterator it = ids.begin(); it != ids.end(); ++it) { 450 for (std::list<int64>::iterator it = ids.begin(); it != ids.end(); ++it) {
451 if (!extension_bookmark_helpers::RemoveNode(model, *it, recursive, &error_)) 451 if (!bookmark_extension_helpers::RemoveNode(model, *it, recursive, &error_))
452 return false; 452 return false;
453 } 453 }
454 return true; 454 return true;
455 } 455 }
456 456
457 bool CreateBookmarkFunction::RunImpl() { 457 bool CreateBookmarkFunction::RunImpl() {
458 if (!EditBookmarksEnabled()) 458 if (!EditBookmarksEnabled())
459 return false; 459 return false;
460 DictionaryValue* json; 460 DictionaryValue* json;
461 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &json)); 461 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &json));
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 node = model->AddURL(parent, index, title, url); 509 node = model->AddURL(parent, index, title, url);
510 else 510 else
511 node = model->AddFolder(parent, index, title); 511 node = model->AddFolder(parent, index, title);
512 DCHECK(node); 512 DCHECK(node);
513 if (!node) { 513 if (!node) {
514 error_ = keys::kNoNodeError; 514 error_ = keys::kNoNodeError;
515 return false; 515 return false;
516 } 516 }
517 517
518 DictionaryValue* ret = 518 DictionaryValue* ret =
519 extension_bookmark_helpers::GetNodeDictionary(node, false, false); 519 bookmark_extension_helpers::GetNodeDictionary(node, false, false);
520 result_.reset(ret); 520 result_.reset(ret);
521 521
522 return true; 522 return true;
523 } 523 }
524 524
525 // static 525 // static
526 bool MoveBookmarkFunction::ExtractIds(const ListValue* args, 526 bool MoveBookmarkFunction::ExtractIds(const ListValue* args,
527 std::list<int64>* ids, 527 std::list<int64>* ids,
528 bool* invalid_id) { 528 bool* invalid_id) {
529 // For now, Move accepts ID parameters in the same way as an Update. 529 // For now, Move accepts ID parameters in the same way as an Update.
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 error_ = keys::kInvalidIndexError; 588 error_ = keys::kInvalidIndexError;
589 return false; 589 return false;
590 } 590 }
591 } else { 591 } else {
592 index = parent->child_count(); 592 index = parent->child_count();
593 } 593 }
594 594
595 model->Move(node, parent, index); 595 model->Move(node, parent, index);
596 596
597 DictionaryValue* ret = 597 DictionaryValue* ret =
598 extension_bookmark_helpers::GetNodeDictionary(node, false, false); 598 bookmark_extension_helpers::GetNodeDictionary(node, false, false);
599 result_.reset(ret); 599 result_.reset(ret);
600 600
601 return true; 601 return true;
602 } 602 }
603 603
604 // static 604 // static
605 bool UpdateBookmarkFunction::ExtractIds(const ListValue* args, 605 bool UpdateBookmarkFunction::ExtractIds(const ListValue* args,
606 std::list<int64>* ids, 606 std::list<int64>* ids,
607 bool* invalid_id) { 607 bool* invalid_id) {
608 // For now, Update accepts ID parameters in the same way as an Remove. 608 // For now, Update accepts ID parameters in the same way as an Remove.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 if (model->is_permanent_node(node)) { 646 if (model->is_permanent_node(node)) {
647 error_ = keys::kModifySpecialError; 647 error_ = keys::kModifySpecialError;
648 return false; 648 return false;
649 } 649 }
650 if (has_title) 650 if (has_title)
651 model->SetTitle(node, title); 651 model->SetTitle(node, title);
652 if (!url.is_empty()) 652 if (!url.is_empty())
653 model->SetURL(node, url); 653 model->SetURL(node, url);
654 654
655 DictionaryValue* ret = 655 DictionaryValue* ret =
656 extension_bookmark_helpers::GetNodeDictionary(node, false, false); 656 bookmark_extension_helpers::GetNodeDictionary(node, false, false);
657 result_.reset(ret); 657 result_.reset(ret);
658 658
659 return true; 659 return true;
660 } 660 }
661 661
662 // Mapper superclass for BookmarkFunctions. 662 // Mapper superclass for BookmarkFunctions.
663 template <typename BucketIdType> 663 template <typename BucketIdType>
664 class BookmarkBucketMapper : public BucketMapper { 664 class BookmarkBucketMapper : public BucketMapper {
665 public: 665 public:
666 virtual ~BookmarkBucketMapper() { STLDeleteValues(&buckets_); } 666 virtual ~BookmarkBucketMapper() { STLDeleteValues(&buckets_); }
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
934 SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE); 934 SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE);
935 return true; 935 return true;
936 } 936 }
937 937
938 void ExportBookmarksFunction::FileSelected(const FilePath& path, 938 void ExportBookmarksFunction::FileSelected(const FilePath& path,
939 int index, 939 int index,
940 void* params) { 940 void* params) {
941 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); 941 bookmark_html_writer::WriteBookmarks(profile(), path, NULL);
942 Release(); // Balanced in BookmarksIOFunction::SelectFile() 942 Release(); // Balanced in BookmarksIOFunction::SelectFile()
943 } 943 }
OLDNEW
« no previous file with comments | « chrome/browser/bookmarks/bookmark_extension_api.h ('k') | chrome/browser/bookmarks/bookmark_extension_api_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698