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

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

Issue 10694085: Refactor extension event distribution to use Values instead of JSON strings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase and review changes. Created 8 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/bookmarks/bookmark_extension_api.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"
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 BookmarkExtensionEventRouter::~BookmarkExtensionEventRouter() { 136 BookmarkExtensionEventRouter::~BookmarkExtensionEventRouter() {
137 if (model_) { 137 if (model_) {
138 model_->RemoveObserver(this); 138 model_->RemoveObserver(this);
139 } 139 }
140 } 140 }
141 141
142 void BookmarkExtensionEventRouter::Init() { 142 void BookmarkExtensionEventRouter::Init() {
143 model_->AddObserver(this); 143 model_->AddObserver(this);
144 } 144 }
145 145
146 void BookmarkExtensionEventRouter::DispatchEvent(Profile *profile, 146 void BookmarkExtensionEventRouter::DispatchEvent(Profile* profile,
miket_OOO 2012/07/10 22:33:19 Well done.
147 const char* event_name, 147 const char* event_name,
148 const std::string& json_args) { 148 ListValue* event_args) {
149 if (profile->GetExtensionEventRouter()) { 149 if (profile->GetExtensionEventRouter()) {
150 profile->GetExtensionEventRouter()->DispatchEventToRenderers( 150 profile->GetExtensionEventRouter()->DispatchEventToRenderers(
151 event_name, json_args, NULL, GURL(), extensions::EventFilteringInfo()); 151 event_name, event_args, NULL, GURL(), extensions::EventFilteringInfo());
152 } 152 }
153 } 153 }
154 154
155 void BookmarkExtensionEventRouter::Loaded(BookmarkModel* model, 155 void BookmarkExtensionEventRouter::Loaded(BookmarkModel* model,
156 bool ids_reassigned) { 156 bool ids_reassigned) {
157 // TODO(erikkay): Perhaps we should send this event down to the extension 157 // TODO(erikkay): Perhaps we should send this event down to the extension
158 // so they know when it's safe to use the API? 158 // so they know when it's safe to use the API?
159 } 159 }
160 160
161 void BookmarkExtensionEventRouter::BookmarkModelBeingDeleted( 161 void BookmarkExtensionEventRouter::BookmarkModelBeingDeleted(
162 BookmarkModel* model) { 162 BookmarkModel* model) {
163 model_ = NULL; 163 model_ = NULL;
164 } 164 }
165 165
166 void BookmarkExtensionEventRouter::BookmarkNodeMoved( 166 void BookmarkExtensionEventRouter::BookmarkNodeMoved(
167 BookmarkModel* model, 167 BookmarkModel* model,
168 const BookmarkNode* old_parent, 168 const BookmarkNode* old_parent,
169 int old_index, 169 int old_index,
170 const BookmarkNode* new_parent, 170 const BookmarkNode* new_parent,
171 int new_index) { 171 int new_index) {
172 ListValue args; 172 ListValue* args = new ListValue();
173 const BookmarkNode* node = new_parent->GetChild(new_index); 173 const BookmarkNode* node = new_parent->GetChild(new_index);
174 args.Append(new StringValue(base::Int64ToString(node->id()))); 174 args->Append(new StringValue(base::Int64ToString(node->id())));
175 DictionaryValue* object_args = new DictionaryValue(); 175 DictionaryValue* object_args = new DictionaryValue();
176 object_args->SetString(keys::kParentIdKey, 176 object_args->SetString(keys::kParentIdKey,
177 base::Int64ToString(new_parent->id())); 177 base::Int64ToString(new_parent->id()));
178 object_args->SetInteger(keys::kIndexKey, new_index); 178 object_args->SetInteger(keys::kIndexKey, new_index);
179 object_args->SetString(keys::kOldParentIdKey, 179 object_args->SetString(keys::kOldParentIdKey,
180 base::Int64ToString(old_parent->id())); 180 base::Int64ToString(old_parent->id()));
181 object_args->SetInteger(keys::kOldIndexKey, old_index); 181 object_args->SetInteger(keys::kOldIndexKey, old_index);
182 args.Append(object_args); 182 args->Append(object_args);
183 183
184 std::string json_args; 184 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, args);
185 base::JSONWriter::Write(&args, &json_args);
186 DispatchEvent(model->profile(), keys::kOnBookmarkMoved, json_args);
187 } 185 }
188 186
189 void BookmarkExtensionEventRouter::BookmarkNodeAdded(BookmarkModel* model, 187 void BookmarkExtensionEventRouter::BookmarkNodeAdded(BookmarkModel* model,
190 const BookmarkNode* parent, 188 const BookmarkNode* parent,
191 int index) { 189 int index) {
192 ListValue args; 190 ListValue* args = new ListValue();
193 const BookmarkNode* node = parent->GetChild(index); 191 const BookmarkNode* node = parent->GetChild(index);
194 args.Append(new StringValue(base::Int64ToString(node->id()))); 192 args->Append(new StringValue(base::Int64ToString(node->id())));
195 DictionaryValue* obj = 193 DictionaryValue* obj =
196 bookmark_extension_helpers::GetNodeDictionary(node, false, false); 194 bookmark_extension_helpers::GetNodeDictionary(node, false, false);
197 args.Append(obj); 195 args->Append(obj);
198 196
199 std::string json_args; 197 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, args);
200 base::JSONWriter::Write(&args, &json_args);
201 DispatchEvent(model->profile(), keys::kOnBookmarkCreated, json_args);
202 } 198 }
203 199
204 void BookmarkExtensionEventRouter::BookmarkNodeRemoved( 200 void BookmarkExtensionEventRouter::BookmarkNodeRemoved(
205 BookmarkModel* model, 201 BookmarkModel* model,
206 const BookmarkNode* parent, 202 const BookmarkNode* parent,
207 int index, 203 int index,
208 const BookmarkNode* node) { 204 const BookmarkNode* node) {
209 ListValue args; 205 ListValue* args = new ListValue();
210 args.Append(new StringValue(base::Int64ToString(node->id()))); 206 args->Append(new StringValue(base::Int64ToString(node->id())));
211 DictionaryValue* object_args = new DictionaryValue(); 207 DictionaryValue* object_args = new DictionaryValue();
212 object_args->SetString(keys::kParentIdKey, 208 object_args->SetString(keys::kParentIdKey,
213 base::Int64ToString(parent->id())); 209 base::Int64ToString(parent->id()));
214 object_args->SetInteger(keys::kIndexKey, index); 210 object_args->SetInteger(keys::kIndexKey, index);
215 args.Append(object_args); 211 args->Append(object_args);
216 212
217 std::string json_args; 213 DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, args);
218 base::JSONWriter::Write(&args, &json_args);
219 DispatchEvent(model->profile(), keys::kOnBookmarkRemoved, json_args);
220 } 214 }
221 215
222 void BookmarkExtensionEventRouter::BookmarkNodeChanged( 216 void BookmarkExtensionEventRouter::BookmarkNodeChanged(
223 BookmarkModel* model, const BookmarkNode* node) { 217 BookmarkModel* model, const BookmarkNode* node) {
224 ListValue args; 218 ListValue* args = new ListValue();
225 args.Append(new StringValue(base::Int64ToString(node->id()))); 219 args->Append(new StringValue(base::Int64ToString(node->id())));
226 220
227 // TODO(erikkay) The only three things that BookmarkModel sends this 221 // TODO(erikkay) The only three things that BookmarkModel sends this
228 // notification for are title, url and favicon. Since we're currently 222 // notification for are title, url and favicon. Since we're currently
229 // ignoring favicon and since the notification doesn't say which one anyway, 223 // ignoring favicon and since the notification doesn't say which one anyway,
230 // for now we only include title and url. The ideal thing would be to change 224 // for now we only include title and url. The ideal thing would be to change
231 // BookmarkModel to indicate what changed. 225 // BookmarkModel to indicate what changed.
232 DictionaryValue* object_args = new DictionaryValue(); 226 DictionaryValue* object_args = new DictionaryValue();
233 object_args->SetString(keys::kTitleKey, node->GetTitle()); 227 object_args->SetString(keys::kTitleKey, node->GetTitle());
234 if (node->is_url()) 228 if (node->is_url())
235 object_args->SetString(keys::kUrlKey, node->url().spec()); 229 object_args->SetString(keys::kUrlKey, node->url().spec());
236 args.Append(object_args); 230 args->Append(object_args);
237 231
238 std::string json_args; 232 DispatchEvent(model->profile(), keys::kOnBookmarkChanged, args);
239 base::JSONWriter::Write(&args, &json_args);
240 DispatchEvent(model->profile(), keys::kOnBookmarkChanged, json_args);
241 } 233 }
242 234
243 void BookmarkExtensionEventRouter::BookmarkNodeFaviconChanged( 235 void BookmarkExtensionEventRouter::BookmarkNodeFaviconChanged(
244 BookmarkModel* model, const BookmarkNode* node) { 236 BookmarkModel* model, const BookmarkNode* node) {
245 // TODO(erikkay) anything we should do here? 237 // TODO(erikkay) anything we should do here?
246 } 238 }
247 239
248 void BookmarkExtensionEventRouter::BookmarkNodeChildrenReordered( 240 void BookmarkExtensionEventRouter::BookmarkNodeChildrenReordered(
249 BookmarkModel* model, const BookmarkNode* node) { 241 BookmarkModel* model, const BookmarkNode* node) {
250 ListValue args; 242 ListValue* args = new ListValue();
251 args.Append(new StringValue(base::Int64ToString(node->id()))); 243 args->Append(new StringValue(base::Int64ToString(node->id())));
252 int childCount = node->child_count(); 244 int childCount = node->child_count();
253 ListValue* children = new ListValue(); 245 ListValue* children = new ListValue();
254 for (int i = 0; i < childCount; ++i) { 246 for (int i = 0; i < childCount; ++i) {
255 const BookmarkNode* child = node->GetChild(i); 247 const BookmarkNode* child = node->GetChild(i);
256 Value* child_id = new StringValue(base::Int64ToString(child->id())); 248 Value* child_id = new StringValue(base::Int64ToString(child->id()));
257 children->Append(child_id); 249 children->Append(child_id);
258 } 250 }
259 DictionaryValue* reorder_info = new DictionaryValue(); 251 DictionaryValue* reorder_info = new DictionaryValue();
260 reorder_info->Set(keys::kChildIdsKey, children); 252 reorder_info->Set(keys::kChildIdsKey, children);
261 args.Append(reorder_info); 253 args->Append(reorder_info);
262 254
263 std::string json_args; 255 DispatchEvent(model->profile(), keys::kOnBookmarkChildrenReordered, args);
264 base::JSONWriter::Write(&args, &json_args);
265 DispatchEvent(model->profile(),
266 keys::kOnBookmarkChildrenReordered,
267 json_args);
268 } 256 }
269 257
270 void BookmarkExtensionEventRouter:: 258 void BookmarkExtensionEventRouter::
271 ExtensiveBookmarkChangesBeginning(BookmarkModel* model) { 259 ExtensiveBookmarkChangesBeginning(BookmarkModel* model) {
272 ListValue args; 260 ListValue* args = new ListValue();
273 std::string json_args;
274 base::JSONWriter::Write(&args, &json_args);
275 DispatchEvent(model->profile(), 261 DispatchEvent(model->profile(),
276 keys::kOnBookmarkImportBegan, 262 keys::kOnBookmarkImportBegan,
277 json_args); 263 args);
278 } 264 }
279 265
280 void BookmarkExtensionEventRouter::ExtensiveBookmarkChangesEnded( 266 void BookmarkExtensionEventRouter::ExtensiveBookmarkChangesEnded(
281 BookmarkModel* model) { 267 BookmarkModel* model) {
282 ListValue args; 268 ListValue* args = new ListValue();
283 std::string json_args;
284 base::JSONWriter::Write(&args, &json_args);
285 DispatchEvent(model->profile(), 269 DispatchEvent(model->profile(),
286 keys::kOnBookmarkImportEnded, 270 keys::kOnBookmarkImportEnded,
287 json_args); 271 args);
288 } 272 }
289 273
290 bool GetBookmarksFunction::RunImpl() { 274 bool GetBookmarksFunction::RunImpl() {
291 BookmarkModel* model = profile()->GetBookmarkModel(); 275 BookmarkModel* model = profile()->GetBookmarkModel();
292 scoped_ptr<ListValue> json(new ListValue()); 276 scoped_ptr<ListValue> json(new ListValue());
293 Value* arg0; 277 Value* arg0;
294 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0)); 278 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &arg0));
295 if (arg0->IsType(Value::TYPE_LIST)) { 279 if (arg0->IsType(Value::TYPE_LIST)) {
296 const ListValue* ids = static_cast<const ListValue*>(arg0); 280 const ListValue* ids = static_cast<const ListValue*>(arg0);
297 size_t count = ids->GetSize(); 281 size_t count = ids->GetSize();
(...skipping 647 matching lines...) Expand 10 before | Expand all | Expand 10 after
945 int index, 929 int index,
946 void* params) { 930 void* params) {
947 #if !defined(OS_ANDROID) 931 #if !defined(OS_ANDROID)
948 // Android does not have support for the standard exporter. 932 // Android does not have support for the standard exporter.
949 // TODO(jgreenwald): remove ifdef once extensions are no longer built on 933 // TODO(jgreenwald): remove ifdef once extensions are no longer built on
950 // Android. 934 // Android.
951 bookmark_html_writer::WriteBookmarks(profile(), path, NULL); 935 bookmark_html_writer::WriteBookmarks(profile(), path, NULL);
952 #endif 936 #endif
953 Release(); // Balanced in BookmarksIOFunction::SelectFile() 937 Release(); // Balanced in BookmarksIOFunction::SelectFile()
954 } 938 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698