1 // Copyright 2013 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 "components/dom_distiller/webui/dom_distiller_handler.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/values.h"
11 #include "components/dom_distiller/core/dom_distiller_service.h"
12 #include "components/dom_distiller/core/proto/distilled_page.pb.h"
13 #include "content/public/browser/web_ui.h"
14 #include "url/gurl.h"
15
16 namespace dom_distiller {
17
DomDistillerHandler(DomDistillerService * service,const std::string & scheme)18 DomDistillerHandler::DomDistillerHandler(DomDistillerService* service,
19 const std::string& scheme)
20 : weak_ptr_factory_(this),
21 service_(service) {
22 article_scheme_ = scheme;
23 }
24
~DomDistillerHandler()25 DomDistillerHandler::~DomDistillerHandler() {}
26
RegisterMessages()27 void DomDistillerHandler::RegisterMessages() {
28 web_ui()->RegisterMessageCallback(
29 "requestEntries",
30 base::Bind(&DomDistillerHandler::HandleRequestEntries,
31 base::Unretained(this)));
32 web_ui()->RegisterMessageCallback(
33 "addArticle",
34 base::Bind(&DomDistillerHandler::HandleAddArticle,
35 base::Unretained(this)));
36 web_ui()->RegisterMessageCallback(
37 "selectArticle",
38 base::Bind(&DomDistillerHandler::HandleSelectArticle,
39 base::Unretained(this)));
40 }
41
HandleAddArticle(const ListValue * args)42 void DomDistillerHandler::HandleAddArticle(const ListValue* args) {
43 std::string url;
44 args->GetString(0, &url);
45 GURL gurl(url);
46 if (gurl.is_valid())
47 service_->AddToList(gurl);
48 else
49 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed");
50 }
51
HandleSelectArticle(const ListValue * args)52 void DomDistillerHandler::HandleSelectArticle(const ListValue* args) {
53 std::string entry_id;
54 args->GetString(0, &entry_id);
55
56 // TODO(nyquist): Do something here.
57 }
58
HandleRequestEntries(const ListValue * args)59 void DomDistillerHandler::HandleRequestEntries(const ListValue* args) {
60 base::ListValue entries;
61 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries();
62 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin();
63 it != entries_specifics.end();
64 ++it) {
65 const ArticleEntry& article = *it;
66 DCHECK(IsEntryValid(article));
67 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue());
68 entry->SetString("entry_id", article.entry_id());
69 std::string title = (!article.has_title() || article.title().empty()) ?
70 article.entry_id() : article.title();
71 entry->SetString("title", title);
72 entries.Append(entry.release());
73 }
74 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries);
75 }
76
77 } // namespace dom_distiller
78