1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/reading_list_private/reading_list_private_api.h"
10 #include "base/memory/linked_ptr.h"
11 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/extensions/api/reading_list_private.h"
14 #include "components/dom_distiller/core/article_entry.h"
15 #include "components/dom_distiller/core/dom_distiller_service.h"
17 namespace extensions {
19 namespace AddEntry = api::reading_list_private::AddEntry;
20 namespace RemoveEntry = api::reading_list_private::RemoveEntry;
21 namespace GetEntries = api::reading_list_private::GetEntries;
23 using api::reading_list_private::Entry;
24 using dom_distiller::ArticleEntry;
25 using dom_distiller::DomDistillerService;
26 using dom_distiller::DomDistillerServiceFactory;
28 bool ReadingListPrivateAddEntryFunction::RunAsync() {
29 scoped_ptr<AddEntry::Params> params(AddEntry::Params::Create(*args_));
30 EXTENSION_FUNCTION_VALIDATE(params);
31 GURL url_to_add(params->entry.url);
32 if (!url_to_add.is_valid()) {
33 error_ = "Invalid url specified.";
38 DomDistillerService* service =
39 DomDistillerServiceFactory::GetForBrowserContext(GetProfile());
40 const std::string& id = service->AddToList(
42 service->CreateDefaultDistillerPage().Pass(),
43 base::Bind(&ReadingListPrivateAddEntryFunction::SendResponse, this));
46 results_ = AddEntry::Results::Create(new_entry);
50 bool ReadingListPrivateRemoveEntryFunction::RunSync() {
51 scoped_ptr<RemoveEntry::Params> params(RemoveEntry::Params::Create(*args_));
52 EXTENSION_FUNCTION_VALIDATE(params);
53 DomDistillerService* service =
54 DomDistillerServiceFactory::GetForBrowserContext(GetProfile());
55 scoped_ptr<ArticleEntry> entry(service->RemoveEntry(params->id));
57 results_ = make_scoped_ptr(new base::ListValue());
60 removed_entry.id = entry->entry_id();
61 results_ = RemoveEntry::Results::Create(removed_entry);
66 bool ReadingListPrivateGetEntriesFunction::RunSync() {
67 DomDistillerService* service =
68 DomDistillerServiceFactory::GetForBrowserContext(GetProfile());
69 const std::vector<ArticleEntry>& entries = service->GetEntries();
70 std::vector<linked_ptr<Entry> > result;
71 for (std::vector<ArticleEntry>::const_iterator i = entries.begin();
74 linked_ptr<Entry> e(new Entry);
75 e->id = i->entry_id();
78 results_ = GetEntries::Results::Create(result);
82 } // namespace extensions