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

Unified Diff: chrome/browser/extensions/api/discovery/extension_suggested_links_manager.cc

Issue 10391034: Scaffolding for an experimental discovery API letting users inject links in the recommended pane of… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Now using IDL and a ProfileKeyedService. Created 8 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/discovery/extension_suggested_links_manager.cc
diff --git a/chrome/browser/extensions/api/discovery/extension_suggested_links_manager.cc b/chrome/browser/extensions/api/discovery/extension_suggested_links_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..262e0bedcdac462269a73bd17942aedddbe0cde1
--- /dev/null
+++ b/chrome/browser/extensions/api/discovery/extension_suggested_links_manager.cc
@@ -0,0 +1,81 @@
+// Copyright (c) 2012 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/api/discovery/extension_suggested_links_manager.h"
+
+#include <algorithm>
+
+namespace {
+
+// A functor that mathes an ExtensionSuggestedLink having a given URL. Needs an
Yoyo Zhou 2012/05/15 00:38:04 typo: matches
beaudoin 2012/05/16 16:56:16 Ack. (Gone.)
+// implicit copy constructor to be used with std::find.
Aaron Boodman 2012/05/15 00:07:43 Blech, std::find. Not worth the trouble. Just loop
beaudoin 2012/05/16 16:56:16 Done.
+class ExtensionSuggestedLinkMatchUrlPred {
+ public:
+ explicit ExtensionSuggestedLinkMatchUrlPred(const std::string* link_url) :
+ link_url_(link_url) {}
+
+ bool operator() (linked_ptr<ExtensionSuggestedLink> suggested_link) const {
+ return suggested_link->link_url().compare(*link_url_) == 0;
+ }
+
+ private:
+ const std::string* link_url_;
+};
+
+void RemoveLinkFromList(const std::string& link_url,
+ ExtensionSuggestedLinkList* list) {
+ ExtensionSuggestedLinkMatchUrlPred pred(&link_url);
+ ExtensionSuggestedLinkList::iterator found = std::find_if(list->begin(),
+ list->end(), pred);
+ if (found != list->end())
+ list->erase(found);
+}
+
+}
+
+ExtensionSuggestedLinksManager::ExtensionSuggestedLinksManager() {}
+
+ExtensionSuggestedLinksManager::~ExtensionSuggestedLinksManager() {
+}
+
+void ExtensionSuggestedLinksManager::Add(const std::string& extension_id,
+ ExtensionSuggestedLink* item) {
+ // Do this first since we own the incoming item and hence want to delete
+ // it in error paths.
+ linked_ptr<ExtensionSuggestedLink> linked_item(item);
+
+ ExtensionSuggestedLinkList& list = GetAllInternal(extension_id);
+ list.push_back(linked_item);
+}
+
+const ExtensionSuggestedLinkList* ExtensionSuggestedLinksManager::GetAll(
+ const std::string& extension_id) const {
+ SuggestedLinksMap::const_iterator found = suggested_links_.find(extension_id);
+ if (found != suggested_links_.end())
+ return &found->second;
+ return NULL;
+}
+
+void ExtensionSuggestedLinksManager::Clear(const std::string& extension_id,
+ const std::string& link_url) {
+ SuggestedLinksMap::iterator found = suggested_links_.find(extension_id);
+ if (found != suggested_links_.end())
+ RemoveLinkFromList(link_url, &found->second);
+}
+
+void ExtensionSuggestedLinksManager::ClearAll(const std::string& extension_id) {
+ SuggestedLinksMap::iterator found = suggested_links_.find(extension_id);
+ if (found != suggested_links_.end())
+ suggested_links_.erase(found);
+}
+
+ExtensionSuggestedLinkList& ExtensionSuggestedLinksManager::GetAllInternal(
+ const std::string& extension_id) {
+ // |insert| returns the element if it's already in the map.
Aaron Boodman 2012/05/15 00:07:43 Cool trick, never seen that one.
beaudoin 2012/05/16 16:56:16 Ack.
+ SuggestedLinksMap::iterator found = suggested_links_.insert(
+ SuggestedLinksMap::value_type(extension_id,
+ ExtensionSuggestedLinkList())).first;
+ CHECK(found != suggested_links_.end());
+ return found->second;
+}

Powered by Google App Engine
This is Rietveld 408576698