Chromium Code Reviews| 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 |
| 19 DomDistillerHandler::DomDistillerHandler(DomDistillerService* service, | 21 DomDistillerHandler::DomDistillerHandler(DomDistillerService* service, |
| 20 const std::string& scheme) | 22 const std::string& scheme) |
| 21 : service_(service), article_scheme_(scheme), weak_ptr_factory_(this) {} | 23 : service_(service), article_scheme_(scheme), weak_ptr_factory_(this) {} |
| 22 | 24 |
| 23 DomDistillerHandler::~DomDistillerHandler() {} | 25 DomDistillerHandler::~DomDistillerHandler() {} |
| 24 | 26 |
| 25 void DomDistillerHandler::RegisterMessages() { | 27 void DomDistillerHandler::RegisterMessages() { |
| 26 web_ui()->RegisterMessageCallback( | 28 web_ui()->RegisterMessageCallback( |
| 27 "requestEntries", | 29 "requestEntries", |
| 28 base::Bind(&DomDistillerHandler::HandleRequestEntries, | 30 base::Bind(&DomDistillerHandler::HandleRequestEntries, |
| 29 base::Unretained(this))); | 31 base::Unretained(this))); |
| 30 web_ui()->RegisterMessageCallback( | 32 web_ui()->RegisterMessageCallback( |
| 31 "addArticle", | 33 "addArticle", |
| 32 base::Bind(&DomDistillerHandler::HandleAddArticle, | 34 base::Bind(&DomDistillerHandler::HandleAddArticle, |
| 33 base::Unretained(this))); | 35 base::Unretained(this))); |
| 34 web_ui()->RegisterMessageCallback( | 36 web_ui()->RegisterMessageCallback( |
| 35 "selectArticle", | 37 "selectArticle", |
| 36 base::Bind(&DomDistillerHandler::HandleSelectArticle, | 38 base::Bind(&DomDistillerHandler::HandleSelectArticle, |
| 37 base::Unretained(this))); | 39 base::Unretained(this))); |
| 40 web_ui()->RegisterMessageCallback( | |
| 41 "viewUrl", | |
| 42 base::Bind(&DomDistillerHandler::HandleViewUrl, base::Unretained(this))); | |
| 38 } | 43 } |
| 39 | 44 |
| 40 void DomDistillerHandler::HandleAddArticle(const base::ListValue* args) { | 45 void DomDistillerHandler::HandleAddArticle(const base::ListValue* args) { |
| 41 std::string url; | 46 std::string url; |
| 42 args->GetString(0, &url); | 47 args->GetString(0, &url); |
| 43 GURL gurl(url); | 48 GURL gurl(url); |
| 44 if (gurl.is_valid()) { | 49 if (gurl.is_valid()) { |
| 45 service_->AddToList( | 50 service_->AddToList( |
| 46 gurl, | 51 gurl, |
| 47 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded, | 52 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded, |
| 48 base::Unretained(this)))); | 53 base::Unretained(this)))); |
| 49 } else { | 54 } else { |
| 50 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); | 55 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); |
| 51 } | 56 } |
| 52 } | 57 } |
| 53 | 58 |
| 59 void DomDistillerHandler::HandleViewUrl(const base::ListValue* args) { | |
| 60 std::string url; | |
| 61 args->GetString(0, &url); | |
|
nasko
2014/02/27 22:40:44
nit: You aren't checking the success of GetString.
nyquist
2014/02/28 16:46:51
Done.
| |
| 62 const GURL gurl(url); | |
| 63 if (url_utils::IsDistillableUrl(gurl)) { | |
|
nasko
2014/02/27 22:40:44
nit: Just a personal preference, but it reads bett
nyquist
2014/02/28 16:46:51
I couldn't find a pretty way to write the method,
| |
| 64 GURL view_url = | |
| 65 url_utils::GetDistillerViewUrlFromUrl(article_scheme_, gurl); | |
| 66 DCHECK(view_url.is_valid()); | |
|
nasko
2014/02/27 22:40:44
DCHECKs should almost always be followed by an if
nyquist
2014/02/28 16:46:51
Removed DCHECK, and instead handle an error. The s
| |
| 67 web_ui()->GetWebContents()->GetController().LoadURL( | |
| 68 view_url, | |
| 69 content::Referrer(), | |
| 70 content::PAGE_TRANSITION_GENERATED, | |
| 71 std::string()); | |
| 72 } else { | |
| 73 web_ui()->CallJavascriptFunction("domDistiller.onViewUrlFailed"); | |
| 74 } | |
| 75 } | |
| 76 | |
| 54 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) { | 77 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) { |
| 55 std::string entry_id; | 78 std::string entry_id; |
| 56 args->GetString(0, &entry_id); | 79 args->GetString(0, &entry_id); |
| 57 GURL url(article_scheme_ + std::string("://") + entry_id); | 80 GURL url = |
| 81 url_utils::GetDistillerViewUrlFromEntryId(article_scheme_, entry_id); | |
| 58 DCHECK(url.is_valid()); | 82 DCHECK(url.is_valid()); |
| 59 web_ui()->GetWebContents()->GetController().LoadURL(url, | 83 web_ui()->GetWebContents()->GetController().LoadURL( |
| 60 content::Referrer(), content::PAGE_TRANSITION_GENERATED, | 84 url, |
| 85 content::Referrer(), | |
| 86 content::PAGE_TRANSITION_GENERATED, | |
| 61 std::string()); | 87 std::string()); |
| 62 } | 88 } |
| 63 | 89 |
| 64 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) { | 90 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) { |
| 65 base::ListValue entries; | 91 base::ListValue entries; |
| 66 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); | 92 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); |
| 67 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); | 93 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); |
| 68 it != entries_specifics.end(); | 94 it != entries_specifics.end(); |
| 69 ++it) { | 95 ++it) { |
| 70 const ArticleEntry& article = *it; | 96 const ArticleEntry& article = *it; |
| 71 DCHECK(IsEntryValid(article)); | 97 DCHECK(IsEntryValid(article)); |
| 72 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); | 98 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); |
| 73 entry->SetString("entry_id", article.entry_id()); | 99 entry->SetString("entry_id", article.entry_id()); |
| 74 std::string title = (!article.has_title() || article.title().empty()) | 100 std::string title = (!article.has_title() || article.title().empty()) |
| 75 ? article.entry_id() | 101 ? article.entry_id() |
| 76 : article.title(); | 102 : article.title(); |
| 77 entry->SetString("title", title); | 103 entry->SetString("title", net::EscapeForHTML(title)); |
| 78 entries.Append(entry.release()); | 104 entries.Append(entry.release()); |
| 79 } | 105 } |
| 106 // TODO(nyquist): Write a test that ensures we sanitize the data we send. | |
| 80 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries); | 107 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries); |
| 81 } | 108 } |
| 82 | 109 |
| 83 void DomDistillerHandler::OnArticleAdded(bool article_available) { | 110 void DomDistillerHandler::OnArticleAdded(bool article_available) { |
| 84 // TODO(nyquist): Update this function. | 111 // TODO(nyquist): Update this function. |
| 85 if (article_available) { | 112 if (article_available) { |
| 86 HandleRequestEntries(NULL); | 113 HandleRequestEntries(NULL); |
| 87 } else { | 114 } else { |
| 88 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); | 115 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); |
| 89 } | 116 } |
| 90 } | 117 } |
| 91 | 118 |
| 92 } // namespace dom_distiller | 119 } // namespace dom_distiller |
| OLD | NEW |