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.
5 #include "components/dom_distiller/webui/dom_distiller_handler.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 "components/dom_distiller/core/url_utils.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_ui.h"
16 #include "net/base/escape.h"
19 namespace dom_distiller
{
23 GURL
GetViewUrlFromArgs(const std::string
& scheme
,
24 const base::ListValue
* args
) {
26 if (args
->GetString(0, &url
)) {
28 if (url_utils::IsUrlDistillable(gurl
)) {
29 return url_utils::GetDistillerViewUrlFromUrl(scheme
, gurl
);
37 DomDistillerHandler::DomDistillerHandler(DomDistillerService
* service
,
38 const std::string
& scheme
)
39 : service_(service
), article_scheme_(scheme
), weak_ptr_factory_(this) {}
41 DomDistillerHandler::~DomDistillerHandler() {}
43 void DomDistillerHandler::RegisterMessages() {
44 web_ui()->RegisterMessageCallback(
46 base::Bind(&DomDistillerHandler::HandleRequestEntries
,
47 base::Unretained(this)));
48 web_ui()->RegisterMessageCallback(
50 base::Bind(&DomDistillerHandler::HandleAddArticle
,
51 base::Unretained(this)));
52 web_ui()->RegisterMessageCallback(
54 base::Bind(&DomDistillerHandler::HandleSelectArticle
,
55 base::Unretained(this)));
56 web_ui()->RegisterMessageCallback(
58 base::Bind(&DomDistillerHandler::HandleViewUrl
, base::Unretained(this)));
61 void DomDistillerHandler::HandleAddArticle(const base::ListValue
* args
) {
63 args
->GetString(0, &url
);
65 if (gurl
.is_valid()) {
68 service_
->CreateDefaultDistillerPage(),
69 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded
,
70 base::Unretained(this))));
72 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed");
76 void DomDistillerHandler::HandleViewUrl(const base::ListValue
* args
) {
77 GURL view_url
= GetViewUrlFromArgs(article_scheme_
, args
);
78 if (view_url
.is_valid()) {
79 web_ui()->GetWebContents()->GetController().LoadURL(
82 content::PAGE_TRANSITION_GENERATED
,
85 web_ui()->CallJavascriptFunction("domDistiller.onViewUrlFailed");
89 void DomDistillerHandler::HandleSelectArticle(const base::ListValue
* args
) {
91 args
->GetString(0, &entry_id
);
93 url_utils::GetDistillerViewUrlFromEntryId(article_scheme_
, entry_id
);
94 DCHECK(url
.is_valid());
95 web_ui()->GetWebContents()->GetController().LoadURL(
98 content::PAGE_TRANSITION_GENERATED
,
102 void DomDistillerHandler::HandleRequestEntries(const base::ListValue
* args
) {
103 base::ListValue entries
;
104 const std::vector
<ArticleEntry
>& entries_specifics
= service_
->GetEntries();
105 for (std::vector
<ArticleEntry
>::const_iterator it
= entries_specifics
.begin();
106 it
!= entries_specifics
.end();
108 const ArticleEntry
& article
= *it
;
109 DCHECK(IsEntryValid(article
));
110 scoped_ptr
<base::DictionaryValue
> entry(new base::DictionaryValue());
111 entry
->SetString("entry_id", article
.entry_id());
112 std::string title
= (!article
.has_title() || article
.title().empty())
115 entry
->SetString("title", net::EscapeForHTML(title
));
116 entries
.Append(entry
.release());
118 // TODO(nyquist): Write a test that ensures we sanitize the data we send.
119 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries
);
122 void DomDistillerHandler::OnArticleAdded(bool article_available
) {
123 // TODO(nyquist): Update this function.
124 if (article_available
) {
125 HandleRequestEntries(NULL
);
127 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed");
131 } // namespace dom_distiller