OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/dom_distiller/webui/dom_distiller_handler.h" | 5 #include "components/dom_distiller/webui/dom_distiller_handler.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "components/dom_distiller/core/dom_distiller_service.h" | 11 #include "components/dom_distiller/core/dom_distiller_service.h" |
12 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 12 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
| 13 #include "components/dom_distiller/core/url_utils.h" |
13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
14 #include "content/public/browser/web_ui.h" | 15 #include "content/public/browser/web_ui.h" |
| 16 #include "net/base/escape.h" |
15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
16 | 18 |
17 namespace dom_distiller { | 19 namespace dom_distiller { |
18 | 20 |
| 21 namespace { |
| 22 |
| 23 GURL GetViewUrlFromArgs(const std::string& scheme, |
| 24 const base::ListValue* args) { |
| 25 std::string url; |
| 26 if (args->GetString(0, &url)) { |
| 27 const GURL gurl(url); |
| 28 if (url_utils::IsUrlDistillable(gurl)) { |
| 29 return url_utils::GetDistillerViewUrlFromUrl(scheme, gurl); |
| 30 } |
| 31 } |
| 32 return GURL(); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
19 DomDistillerHandler::DomDistillerHandler(DomDistillerService* service, | 37 DomDistillerHandler::DomDistillerHandler(DomDistillerService* service, |
20 const std::string& scheme) | 38 const std::string& scheme) |
21 : service_(service), article_scheme_(scheme), weak_ptr_factory_(this) {} | 39 : service_(service), article_scheme_(scheme), weak_ptr_factory_(this) {} |
22 | 40 |
23 DomDistillerHandler::~DomDistillerHandler() {} | 41 DomDistillerHandler::~DomDistillerHandler() {} |
24 | 42 |
25 void DomDistillerHandler::RegisterMessages() { | 43 void DomDistillerHandler::RegisterMessages() { |
26 web_ui()->RegisterMessageCallback( | 44 web_ui()->RegisterMessageCallback( |
27 "requestEntries", | 45 "requestEntries", |
28 base::Bind(&DomDistillerHandler::HandleRequestEntries, | 46 base::Bind(&DomDistillerHandler::HandleRequestEntries, |
29 base::Unretained(this))); | 47 base::Unretained(this))); |
30 web_ui()->RegisterMessageCallback( | 48 web_ui()->RegisterMessageCallback( |
31 "addArticle", | 49 "addArticle", |
32 base::Bind(&DomDistillerHandler::HandleAddArticle, | 50 base::Bind(&DomDistillerHandler::HandleAddArticle, |
33 base::Unretained(this))); | 51 base::Unretained(this))); |
34 web_ui()->RegisterMessageCallback( | 52 web_ui()->RegisterMessageCallback( |
35 "selectArticle", | 53 "selectArticle", |
36 base::Bind(&DomDistillerHandler::HandleSelectArticle, | 54 base::Bind(&DomDistillerHandler::HandleSelectArticle, |
37 base::Unretained(this))); | 55 base::Unretained(this))); |
| 56 web_ui()->RegisterMessageCallback( |
| 57 "viewUrl", |
| 58 base::Bind(&DomDistillerHandler::HandleViewUrl, base::Unretained(this))); |
38 } | 59 } |
39 | 60 |
40 void DomDistillerHandler::HandleAddArticle(const base::ListValue* args) { | 61 void DomDistillerHandler::HandleAddArticle(const base::ListValue* args) { |
41 std::string url; | 62 std::string url; |
42 args->GetString(0, &url); | 63 args->GetString(0, &url); |
43 GURL gurl(url); | 64 GURL gurl(url); |
44 if (gurl.is_valid()) { | 65 if (gurl.is_valid()) { |
45 service_->AddToList( | 66 service_->AddToList( |
46 gurl, | 67 gurl, |
47 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded, | 68 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded, |
48 base::Unretained(this)))); | 69 base::Unretained(this)))); |
49 } else { | 70 } else { |
50 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); | 71 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); |
51 } | 72 } |
52 } | 73 } |
53 | 74 |
| 75 void DomDistillerHandler::HandleViewUrl(const base::ListValue* args) { |
| 76 GURL view_url = GetViewUrlFromArgs(article_scheme_, args); |
| 77 if (view_url.is_valid()) { |
| 78 web_ui()->GetWebContents()->GetController().LoadURL( |
| 79 view_url, |
| 80 content::Referrer(), |
| 81 content::PAGE_TRANSITION_GENERATED, |
| 82 std::string()); |
| 83 } else { |
| 84 web_ui()->CallJavascriptFunction("domDistiller.onViewUrlFailed"); |
| 85 } |
| 86 } |
| 87 |
54 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) { | 88 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) { |
55 std::string entry_id; | 89 std::string entry_id; |
56 args->GetString(0, &entry_id); | 90 args->GetString(0, &entry_id); |
57 GURL url(article_scheme_ + std::string("://") + entry_id); | 91 GURL url = |
| 92 url_utils::GetDistillerViewUrlFromEntryId(article_scheme_, entry_id); |
58 DCHECK(url.is_valid()); | 93 DCHECK(url.is_valid()); |
59 web_ui()->GetWebContents()->GetController().LoadURL(url, | 94 web_ui()->GetWebContents()->GetController().LoadURL( |
60 content::Referrer(), content::PAGE_TRANSITION_GENERATED, | 95 url, |
| 96 content::Referrer(), |
| 97 content::PAGE_TRANSITION_GENERATED, |
61 std::string()); | 98 std::string()); |
62 } | 99 } |
63 | 100 |
64 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) { | 101 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) { |
65 base::ListValue entries; | 102 base::ListValue entries; |
66 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); | 103 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); |
67 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); | 104 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); |
68 it != entries_specifics.end(); | 105 it != entries_specifics.end(); |
69 ++it) { | 106 ++it) { |
70 const ArticleEntry& article = *it; | 107 const ArticleEntry& article = *it; |
71 DCHECK(IsEntryValid(article)); | 108 DCHECK(IsEntryValid(article)); |
72 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); | 109 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); |
73 entry->SetString("entry_id", article.entry_id()); | 110 entry->SetString("entry_id", article.entry_id()); |
74 std::string title = (!article.has_title() || article.title().empty()) | 111 std::string title = (!article.has_title() || article.title().empty()) |
75 ? article.entry_id() | 112 ? article.entry_id() |
76 : article.title(); | 113 : article.title(); |
77 entry->SetString("title", title); | 114 entry->SetString("title", net::EscapeForHTML(title)); |
78 entries.Append(entry.release()); | 115 entries.Append(entry.release()); |
79 } | 116 } |
| 117 // TODO(nyquist): Write a test that ensures we sanitize the data we send. |
80 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries); | 118 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries); |
81 } | 119 } |
82 | 120 |
83 void DomDistillerHandler::OnArticleAdded(bool article_available) { | 121 void DomDistillerHandler::OnArticleAdded(bool article_available) { |
84 // TODO(nyquist): Update this function. | 122 // TODO(nyquist): Update this function. |
85 if (article_available) { | 123 if (article_available) { |
86 HandleRequestEntries(NULL); | 124 HandleRequestEntries(NULL); |
87 } else { | 125 } else { |
88 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); | 126 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); |
89 } | 127 } |
90 } | 128 } |
91 | 129 |
92 } // namespace dom_distiller | 130 } // namespace dom_distiller |
OLD | NEW |