| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/sha1.h" | 8 #include "base/sha1.h" |
| 9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "chrome/browser/bookmarks/bookmark_codec.h" | 11 #include "chrome/browser/bookmarks/bookmark_codec.h" |
| 12 #include "chrome/browser/bookmarks/bookmark_html_writer.h" | 12 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
| 13 #include "chrome/browser/bookmarks/bookmark_model.h" | 13 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 14 #include "chrome/browser/bookmarks/bookmark_utils.h" | 14 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 15 #include "chrome/browser/browser_list.h" | 15 #include "chrome/browser/browser_list.h" |
| 16 #include "chrome/browser/extensions/extension_bookmark_helpers.h" | 16 #include "chrome/browser/extensions/extension_bookmark_helpers.h" |
| 17 #include "chrome/browser/extensions/extension_bookmarks_module_constants.h" | 17 #include "chrome/browser/extensions/extension_bookmarks_module_constants.h" |
| 18 #include "chrome/browser/extensions/extension_message_service.h" | 18 #include "chrome/browser/extensions/extension_message_service.h" |
| 19 #include "chrome/browser/extensions/extensions_quota_service.h" | 19 #include "chrome/browser/extensions/extensions_quota_service.h" |
| 20 #include "chrome/browser/importer/importer.h" | 20 #include "chrome/browser/importer/importer.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 49 NotificationService::current()->Notify( | 49 NotificationService::current()->Notify( |
| 50 NotificationType::EXTENSION_BOOKMARKS_API_INVOKED, | 50 NotificationType::EXTENSION_BOOKMARKS_API_INVOKED, |
| 51 Source<const Extension>(GetExtension()), | 51 Source<const Extension>(GetExtension()), |
| 52 Details<const BookmarksFunction>(this)); | 52 Details<const BookmarksFunction>(this)); |
| 53 } | 53 } |
| 54 SendResponse(success); | 54 SendResponse(success); |
| 55 } | 55 } |
| 56 | 56 |
| 57 bool BookmarksFunction::GetBookmarkIdAsInt64( | 57 bool BookmarksFunction::GetBookmarkIdAsInt64( |
| 58 const std::string& id_string, int64* id) { | 58 const std::string& id_string, int64* id) { |
| 59 if (StringToInt64(id_string, id)) | 59 if (base::StringToInt64(id_string, id)) |
| 60 return true; | 60 return true; |
| 61 | 61 |
| 62 error_ = keys::kInvalidIdError; | 62 error_ = keys::kInvalidIdError; |
| 63 return false; | 63 return false; |
| 64 } | 64 } |
| 65 | 65 |
| 66 void BookmarksFunction::Observe(NotificationType type, | 66 void BookmarksFunction::Observe(NotificationType type, |
| 67 const NotificationSource& source, | 67 const NotificationSource& source, |
| 68 const NotificationDetails& details) { | 68 const NotificationDetails& details) { |
| 69 DCHECK(type == NotificationType::BOOKMARK_MODEL_LOADED); | 69 DCHECK(type == NotificationType::BOOKMARK_MODEL_LOADED); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } | 105 } |
| 106 | 106 |
| 107 void ExtensionBookmarkEventRouter::BookmarkNodeMoved( | 107 void ExtensionBookmarkEventRouter::BookmarkNodeMoved( |
| 108 BookmarkModel* model, | 108 BookmarkModel* model, |
| 109 const BookmarkNode* old_parent, | 109 const BookmarkNode* old_parent, |
| 110 int old_index, | 110 int old_index, |
| 111 const BookmarkNode* new_parent, | 111 const BookmarkNode* new_parent, |
| 112 int new_index) { | 112 int new_index) { |
| 113 ListValue args; | 113 ListValue args; |
| 114 const BookmarkNode* node = new_parent->GetChild(new_index); | 114 const BookmarkNode* node = new_parent->GetChild(new_index); |
| 115 args.Append(new StringValue(Int64ToString(node->id()))); | 115 args.Append(new StringValue(base::Int64ToString(node->id()))); |
| 116 DictionaryValue* object_args = new DictionaryValue(); | 116 DictionaryValue* object_args = new DictionaryValue(); |
| 117 object_args->SetString(keys::kParentIdKey, Int64ToString(new_parent->id())); | 117 object_args->SetString(keys::kParentIdKey, |
| 118 base::Int64ToString(new_parent->id())); |
| 118 object_args->SetInteger(keys::kIndexKey, new_index); | 119 object_args->SetInteger(keys::kIndexKey, new_index); |
| 119 object_args->SetString(keys::kOldParentIdKey, | 120 object_args->SetString(keys::kOldParentIdKey, |
| 120 Int64ToString(old_parent->id())); | 121 base::Int64ToString(old_parent->id())); |
| 121 object_args->SetInteger(keys::kOldIndexKey, old_index); | 122 object_args->SetInteger(keys::kOldIndexKey, old_index); |
| 122 args.Append(object_args); | 123 args.Append(object_args); |
| 123 | 124 |
| 124 std::string json_args; | 125 std::string json_args; |
| 125 base::JSONWriter::Write(&args, false, &json_args); | 126 base::JSONWriter::Write(&args, false, &json_args); |
| 126 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args); | 127 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args); |
| 127 } | 128 } |
| 128 | 129 |
| 129 void ExtensionBookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, | 130 void ExtensionBookmarkEventRouter::BookmarkNodeAdded(BookmarkModel* model, |
| 130 const BookmarkNode* parent, | 131 const BookmarkNode* parent, |
| 131 int index) { | 132 int index) { |
| 132 ListValue args; | 133 ListValue args; |
| 133 const BookmarkNode* node = parent->GetChild(index); | 134 const BookmarkNode* node = parent->GetChild(index); |
| 134 args.Append(new StringValue(Int64ToString(node->id()))); | 135 args.Append(new StringValue(base::Int64ToString(node->id()))); |
| 135 DictionaryValue* obj = | 136 DictionaryValue* obj = |
| 136 extension_bookmark_helpers::GetNodeDictionary(node, false, false); | 137 extension_bookmark_helpers::GetNodeDictionary(node, false, false); |
| 137 args.Append(obj); | 138 args.Append(obj); |
| 138 | 139 |
| 139 std::string json_args; | 140 std::string json_args; |
| 140 base::JSONWriter::Write(&args, false, &json_args); | 141 base::JSONWriter::Write(&args, false, &json_args); |
| 141 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, json_args); | 142 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, json_args); |
| 142 } | 143 } |
| 143 | 144 |
| 144 void ExtensionBookmarkEventRouter::BookmarkNodeRemoved( | 145 void ExtensionBookmarkEventRouter::BookmarkNodeRemoved( |
| 145 BookmarkModel* model, | 146 BookmarkModel* model, |
| 146 const BookmarkNode* parent, | 147 const BookmarkNode* parent, |
| 147 int index, | 148 int index, |
| 148 const BookmarkNode* node) { | 149 const BookmarkNode* node) { |
| 149 ListValue args; | 150 ListValue args; |
| 150 args.Append(new StringValue(Int64ToString(node->id()))); | 151 args.Append(new StringValue(base::Int64ToString(node->id()))); |
| 151 DictionaryValue* object_args = new DictionaryValue(); | 152 DictionaryValue* object_args = new DictionaryValue(); |
| 152 object_args->SetString(keys::kParentIdKey, Int64ToString(parent->id())); | 153 object_args->SetString(keys::kParentIdKey, |
| 154 base::Int64ToString(parent->id())); |
| 153 object_args->SetInteger(keys::kIndexKey, index); | 155 object_args->SetInteger(keys::kIndexKey, index); |
| 154 args.Append(object_args); | 156 args.Append(object_args); |
| 155 | 157 |
| 156 std::string json_args; | 158 std::string json_args; |
| 157 base::JSONWriter::Write(&args, false, &json_args); | 159 base::JSONWriter::Write(&args, false, &json_args); |
| 158 DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, json_args); | 160 DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, json_args); |
| 159 } | 161 } |
| 160 | 162 |
| 161 void ExtensionBookmarkEventRouter::BookmarkNodeChanged( | 163 void ExtensionBookmarkEventRouter::BookmarkNodeChanged( |
| 162 BookmarkModel* model, const BookmarkNode* node) { | 164 BookmarkModel* model, const BookmarkNode* node) { |
| 163 ListValue args; | 165 ListValue args; |
| 164 args.Append(new StringValue(Int64ToString(node->id()))); | 166 args.Append(new StringValue(base::Int64ToString(node->id()))); |
| 165 | 167 |
| 166 // TODO(erikkay) The only three things that BookmarkModel sends this | 168 // TODO(erikkay) The only three things that BookmarkModel sends this |
| 167 // notification for are title, url and favicon. Since we're currently | 169 // notification for are title, url and favicon. Since we're currently |
| 168 // ignoring favicon and since the notification doesn't say which one anyway, | 170 // ignoring favicon and since the notification doesn't say which one anyway, |
| 169 // for now we only include title and url. The ideal thing would be to change | 171 // for now we only include title and url. The ideal thing would be to change |
| 170 // BookmarkModel to indicate what changed. | 172 // BookmarkModel to indicate what changed. |
| 171 DictionaryValue* object_args = new DictionaryValue(); | 173 DictionaryValue* object_args = new DictionaryValue(); |
| 172 object_args->SetString(keys::kTitleKey, node->GetTitle()); | 174 object_args->SetString(keys::kTitleKey, node->GetTitle()); |
| 173 if (node->is_url()) | 175 if (node->is_url()) |
| 174 object_args->SetString(keys::kUrlKey, node->GetURL().spec()); | 176 object_args->SetString(keys::kUrlKey, node->GetURL().spec()); |
| 175 args.Append(object_args); | 177 args.Append(object_args); |
| 176 | 178 |
| 177 std::string json_args; | 179 std::string json_args; |
| 178 base::JSONWriter::Write(&args, false, &json_args); | 180 base::JSONWriter::Write(&args, false, &json_args); |
| 179 DispatchEvent(model->profile(), keys::kOnBookmarkChanged, json_args); | 181 DispatchEvent(model->profile(), keys::kOnBookmarkChanged, json_args); |
| 180 } | 182 } |
| 181 | 183 |
| 182 void ExtensionBookmarkEventRouter::BookmarkNodeFavIconLoaded( | 184 void ExtensionBookmarkEventRouter::BookmarkNodeFavIconLoaded( |
| 183 BookmarkModel* model, const BookmarkNode* node) { | 185 BookmarkModel* model, const BookmarkNode* node) { |
| 184 // TODO(erikkay) anything we should do here? | 186 // TODO(erikkay) anything we should do here? |
| 185 } | 187 } |
| 186 | 188 |
| 187 void ExtensionBookmarkEventRouter::BookmarkNodeChildrenReordered( | 189 void ExtensionBookmarkEventRouter::BookmarkNodeChildrenReordered( |
| 188 BookmarkModel* model, const BookmarkNode* node) { | 190 BookmarkModel* model, const BookmarkNode* node) { |
| 189 ListValue args; | 191 ListValue args; |
| 190 args.Append(new StringValue(Int64ToString(node->id()))); | 192 args.Append(new StringValue(base::Int64ToString(node->id()))); |
| 191 int childCount = node->GetChildCount(); | 193 int childCount = node->GetChildCount(); |
| 192 ListValue* children = new ListValue(); | 194 ListValue* children = new ListValue(); |
| 193 for (int i = 0; i < childCount; ++i) { | 195 for (int i = 0; i < childCount; ++i) { |
| 194 const BookmarkNode* child = node->GetChild(i); | 196 const BookmarkNode* child = node->GetChild(i); |
| 195 Value* child_id = new StringValue(Int64ToString(child->id())); | 197 Value* child_id = new StringValue(base::Int64ToString(child->id())); |
| 196 children->Append(child_id); | 198 children->Append(child_id); |
| 197 } | 199 } |
| 198 DictionaryValue* reorder_info = new DictionaryValue(); | 200 DictionaryValue* reorder_info = new DictionaryValue(); |
| 199 reorder_info->Set(keys::kChildIdsKey, children); | 201 reorder_info->Set(keys::kChildIdsKey, children); |
| 200 args.Append(reorder_info); | 202 args.Append(reorder_info); |
| 201 | 203 |
| 202 std::string json_args; | 204 std::string json_args; |
| 203 base::JSONWriter::Write(&args, false, &json_args); | 205 base::JSONWriter::Write(&args, false, &json_args); |
| 204 DispatchEvent(model->profile(), | 206 DispatchEvent(model->profile(), |
| 205 keys::kOnBookmarkChildrenReordered, | 207 keys::kOnBookmarkChildrenReordered, |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 } | 342 } |
| 341 | 343 |
| 342 // static | 344 // static |
| 343 bool RemoveBookmarkFunction::ExtractIds(const ListValue* args, | 345 bool RemoveBookmarkFunction::ExtractIds(const ListValue* args, |
| 344 std::list<int64>* ids, | 346 std::list<int64>* ids, |
| 345 bool* invalid_id) { | 347 bool* invalid_id) { |
| 346 std::string id_string; | 348 std::string id_string; |
| 347 if (!args->GetString(0, &id_string)) | 349 if (!args->GetString(0, &id_string)) |
| 348 return false; | 350 return false; |
| 349 int64 id; | 351 int64 id; |
| 350 if (StringToInt64(id_string, &id)) | 352 if (base::StringToInt64(id_string, &id)) |
| 351 ids->push_back(id); | 353 ids->push_back(id); |
| 352 else | 354 else |
| 353 *invalid_id = true; | 355 *invalid_id = true; |
| 354 return true; | 356 return true; |
| 355 } | 357 } |
| 356 | 358 |
| 357 bool RemoveBookmarkFunction::RunImpl() { | 359 bool RemoveBookmarkFunction::RunImpl() { |
| 358 std::list<int64> ids; | 360 std::list<int64> ids; |
| 359 bool invalid_id = false; | 361 bool invalid_id = false; |
| 360 EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id)); | 362 EXTENSION_FUNCTION_VALIDATE(ExtractIds(args_.get(), &ids, &invalid_id)); |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 DictionaryValue* json; | 613 DictionaryValue* json; |
| 612 if (!args->GetDictionary(0, &json)) | 614 if (!args->GetDictionary(0, &json)) |
| 613 return; | 615 return; |
| 614 | 616 |
| 615 std::string parent_id; | 617 std::string parent_id; |
| 616 if (json->HasKey(keys::kParentIdKey)) { | 618 if (json->HasKey(keys::kParentIdKey)) { |
| 617 if (!json->GetString(keys::kParentIdKey, &parent_id)) | 619 if (!json->GetString(keys::kParentIdKey, &parent_id)) |
| 618 return; | 620 return; |
| 619 } | 621 } |
| 620 BookmarkModel* model = profile_->GetBookmarkModel(); | 622 BookmarkModel* model = profile_->GetBookmarkModel(); |
| 621 const BookmarkNode* parent = model->GetNodeByID(StringToInt64(parent_id)); | 623 |
| 624 int64 parent_id_int64; |
| 625 base::StringToInt64(parent_id, &parent_id_int64); |
| 626 const BookmarkNode* parent = model->GetNodeByID(parent_id_int64); |
| 622 if (!parent) | 627 if (!parent) |
| 623 return; | 628 return; |
| 624 | 629 |
| 625 std::string bucket_id = WideToUTF8(parent->GetTitle()); | 630 std::string bucket_id = WideToUTF8(parent->GetTitle()); |
| 626 std::wstring title; | 631 std::wstring title; |
| 627 json->GetString(keys::kTitleKey, &title); | 632 json->GetString(keys::kTitleKey, &title); |
| 628 std::string url_string; | 633 std::string url_string; |
| 629 json->GetString(keys::kUrlKey, &url_string); | 634 json->GetString(keys::kUrlKey, &url_string); |
| 630 | 635 |
| 631 bucket_id += WideToUTF8(title); | 636 bucket_id += WideToUTF8(title); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE); | 817 SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE); |
| 813 return true; | 818 return true; |
| 814 } | 819 } |
| 815 | 820 |
| 816 void ExportBookmarksFunction::FileSelected(const FilePath& path, | 821 void ExportBookmarksFunction::FileSelected(const FilePath& path, |
| 817 int index, | 822 int index, |
| 818 void* params) { | 823 void* params) { |
| 819 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); | 824 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); |
| 820 Release(); // Balanced in BookmarksIOFunction::SelectFile() | 825 Release(); // Balanced in BookmarksIOFunction::SelectFile() |
| 821 } | 826 } |
| OLD | NEW |