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. 4 5 #include "chrome/browser/extensions/api/reading_list_private/reading_list_private_api.h" 6 7 #include <string> 8 #include <vector> 9 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" 16 #include "content/public/browser/web_contents.h" 17 18 namespace extensions { 19 20 namespace AddEntry = api::reading_list_private::AddEntry; 21 namespace RemoveEntry = api::reading_list_private::RemoveEntry; 22 namespace GetEntries = api::reading_list_private::GetEntries; 23 24 using api::reading_list_private::Entry; 25 using dom_distiller::ArticleEntry; 26 using dom_distiller::DomDistillerService; 27 using dom_distiller::DomDistillerServiceFactory; 28 29 bool ReadingListPrivateAddEntryFunction::RunAsync() { 30 scoped_ptr<AddEntry::Params> params(AddEntry::Params::Create(*args_)); 31 EXTENSION_FUNCTION_VALIDATE(params); 32 GURL url_to_add(params->entry.url); 33 if (!url_to_add.is_valid()) { 34 error_ = "Invalid url specified."; 35 SendResponse(false); 36 return false; 37 } 38 39 DomDistillerService* service = 40 DomDistillerServiceFactory::GetForBrowserContext(GetProfile()); 41 gfx::Size render_view_size; 42 content::WebContents* web_contents = GetAssociatedWebContents(); 43 if (web_contents) 44 render_view_size = web_contents->GetContainerBounds().size(); 45 const std::string& id = service->AddToList( 46 url_to_add, 47 service->CreateDefaultDistillerPage(render_view_size).Pass(), 48 base::Bind(&ReadingListPrivateAddEntryFunction::SendResponse, this)); 49 Entry new_entry; 50 new_entry.id = id; 51 results_ = AddEntry::Results::Create(new_entry); 52 return true; 53 } 54 55 bool ReadingListPrivateRemoveEntryFunction::RunSync() { 56 scoped_ptr<RemoveEntry::Params> params(RemoveEntry::Params::Create(*args_)); 57 EXTENSION_FUNCTION_VALIDATE(params); 58 DomDistillerService* service = 59 DomDistillerServiceFactory::GetForBrowserContext(GetProfile()); 60 scoped_ptr<ArticleEntry> entry(service->RemoveEntry(params->id)); 61 if (entry == NULL) { 62 results_ = make_scoped_ptr(new base::ListValue()); 63 } else { 64 Entry removed_entry; 65 removed_entry.id = entry->entry_id(); 66 results_ = RemoveEntry::Results::Create(removed_entry); 67 } 68 return true; 69 } 70 71 bool ReadingListPrivateGetEntriesFunction::RunSync() { 72 DomDistillerService* service = 73 DomDistillerServiceFactory::GetForBrowserContext(GetProfile()); 74 const std::vector<ArticleEntry>& entries = service->GetEntries(); 75 std::vector<linked_ptr<Entry> > result; 76 for (std::vector<ArticleEntry>::const_iterator i = entries.begin(); 77 i != entries.end(); 78 ++i) { 79 linked_ptr<Entry> e(new Entry); 80 e->id = i->entry_id(); 81 result.push_back(e); 82 } 83 results_ = GetEntries::Results::Create(result); 84 return true; 85 } 86 87 } // namespace extensions 88