Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / reading_list_private / reading_list_private_api.cc
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
17 namespace extensions {
18
19 namespace AddEntry = api::reading_list_private::AddEntry;
20 namespace RemoveEntry = api::reading_list_private::RemoveEntry;
21 namespace GetEntries = api::reading_list_private::GetEntries;
22
23 using api::reading_list_private::Entry;
24 using dom_distiller::ArticleEntry;
25 using dom_distiller::DomDistillerService;
26 using dom_distiller::DomDistillerServiceFactory;
27
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.";
34     SendResponse(false);
35     return false;
36   }
37
38   DomDistillerService* service =
39       DomDistillerServiceFactory::GetForBrowserContext(GetProfile());
40   const std::string& id = service->AddToList(
41       url_to_add,
42       service->CreateDefaultDistillerPage().Pass(),
43       base::Bind(&ReadingListPrivateAddEntryFunction::SendResponse, this));
44   Entry new_entry;
45   new_entry.id = id;
46   results_ = AddEntry::Results::Create(new_entry);
47   return true;
48 }
49
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));
56   if (entry == NULL) {
57     results_ = make_scoped_ptr(new base::ListValue());
58   } else {
59     Entry removed_entry;
60     removed_entry.id = entry->entry_id();
61     results_ = RemoveEntry::Results::Create(removed_entry);
62   }
63   return true;
64 }
65
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();
72        i != entries.end();
73        ++i) {
74     linked_ptr<Entry> e(new Entry);
75     e->id = i->entry_id();
76     result.push_back(e);
77   }
78   results_ = GetEntries::Results::Create(result);
79   return true;
80 }
81
82 }  // namespace extensions