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

Unified Diff: chrome/browser/extensions/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: Synced 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/extension_suggested_links_manager.cc
diff --git a/chrome/browser/extensions/extension_suggested_links_manager.cc b/chrome/browser/extensions/extension_suggested_links_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9eef9ca3970b4467c9e26f63ed1f41d6d50480d8
--- /dev/null
+++ b/chrome/browser/extensions/extension_suggested_links_manager.cc
@@ -0,0 +1,84 @@
+// 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/extension_suggested_links_manager.h"
+
+#include <algorithm>
+
+using content::BrowserThread;
+
+namespace {
+
+// A functor that mathes an ExtensionSuggestedLink having a given URL. Needs an
+// implicit copy constructor to be used with std::find.
+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() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+}
+
+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.
+ 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