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/guid.h" | |
|
shashi
2014/02/27 04:29:08
nit: is this import needed?
shashi
2014/02/27 04:29:08
nit: is this import needed?
nyquist
2014/02/27 19:50:19
Done.
| |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "components/dom_distiller/core/dom_distiller_service.h" | 12 #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/proto/distilled_page.pb.h" |
| 14 #include "components/dom_distiller/core/url_constants.h" | |
|
shashi
2014/02/27 04:29:08
nit: is this import needed?
shashi
2014/02/27 04:29:08
nit: is this import needed?
nyquist
2014/02/27 19:50:19
Done.
| |
| 15 #include "components/dom_distiller/core/url_utils.h" | |
| 13 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/browser/web_ui.h" | 17 #include "content/public/browser/web_ui.h" |
| 18 #include "net/base/escape.h" | |
| 15 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 16 | 20 |
| 17 namespace dom_distiller { | 21 namespace dom_distiller { |
| 18 | 22 |
| 19 DomDistillerHandler::DomDistillerHandler(DomDistillerService* service, | 23 DomDistillerHandler::DomDistillerHandler(DomDistillerService* service, |
| 20 const std::string& scheme) | 24 const std::string& scheme) |
| 21 : service_(service), article_scheme_(scheme), weak_ptr_factory_(this) {} | 25 : service_(service), article_scheme_(scheme), weak_ptr_factory_(this) {} |
| 22 | 26 |
| 23 DomDistillerHandler::~DomDistillerHandler() {} | 27 DomDistillerHandler::~DomDistillerHandler() {} |
| 24 | 28 |
| 25 void DomDistillerHandler::RegisterMessages() { | 29 void DomDistillerHandler::RegisterMessages() { |
| 26 web_ui()->RegisterMessageCallback( | 30 web_ui()->RegisterMessageCallback( |
| 27 "requestEntries", | 31 "requestEntries", |
| 28 base::Bind(&DomDistillerHandler::HandleRequestEntries, | 32 base::Bind(&DomDistillerHandler::HandleRequestEntries, |
| 29 base::Unretained(this))); | 33 base::Unretained(this))); |
| 30 web_ui()->RegisterMessageCallback( | 34 web_ui()->RegisterMessageCallback( |
| 31 "addArticle", | 35 "addArticle", |
| 32 base::Bind(&DomDistillerHandler::HandleAddArticle, | 36 base::Bind(&DomDistillerHandler::HandleAddArticle, |
| 33 base::Unretained(this))); | 37 base::Unretained(this))); |
| 34 web_ui()->RegisterMessageCallback( | 38 web_ui()->RegisterMessageCallback( |
| 35 "selectArticle", | 39 "selectArticle", |
| 36 base::Bind(&DomDistillerHandler::HandleSelectArticle, | 40 base::Bind(&DomDistillerHandler::HandleSelectArticle, |
| 37 base::Unretained(this))); | 41 base::Unretained(this))); |
| 42 web_ui()->RegisterMessageCallback( | |
| 43 "viewUrl", | |
| 44 base::Bind(&DomDistillerHandler::HandleViewUrl, base::Unretained(this))); | |
| 38 } | 45 } |
| 39 | 46 |
| 40 void DomDistillerHandler::HandleAddArticle(const base::ListValue* args) { | 47 void DomDistillerHandler::HandleAddArticle(const base::ListValue* args) { |
| 41 std::string url; | 48 std::string url; |
| 42 args->GetString(0, &url); | 49 args->GetString(0, &url); |
| 43 GURL gurl(url); | 50 GURL gurl(url); |
| 44 if (gurl.is_valid()) { | 51 if (gurl.is_valid()) { |
| 45 service_->AddToList( | 52 service_->AddToList( |
| 46 gurl, | 53 gurl, |
| 47 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded, | 54 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded, |
| 48 base::Unretained(this)))); | 55 base::Unretained(this)))); |
| 49 } else { | 56 } else { |
| 50 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); | 57 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); |
| 51 } | 58 } |
| 52 } | 59 } |
| 53 | 60 |
| 61 void DomDistillerHandler::HandleViewUrl(const base::ListValue* args) { | |
| 62 std::string url; | |
| 63 args->GetString(0, &url); | |
| 64 const GURL gurl(url); | |
| 65 if (gurl.is_valid()) { | |
| 66 GURL view_url = | |
| 67 url_utils::GetDistillerViewUrlFromUrl(article_scheme_, gurl); | |
| 68 DCHECK(view_url.is_valid()); | |
|
shashi
2014/02/27 04:29:08
nit: should it be DCHECK(url_utils::IsDistillableU
nyquist
2014/02/27 19:50:19
It should be that on the check of gurl above. This
| |
| 69 web_ui()->GetWebContents()->GetController().LoadURL( | |
| 70 view_url, | |
| 71 content::Referrer(), | |
| 72 content::PAGE_TRANSITION_GENERATED, | |
| 73 std::string()); | |
| 74 } else { | |
| 75 web_ui()->CallJavascriptFunction("domDistiller.onViewUrlFailed"); | |
| 76 } | |
| 77 } | |
| 78 | |
| 54 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) { | 79 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) { |
| 55 std::string entry_id; | 80 std::string entry_id; |
| 56 args->GetString(0, &entry_id); | 81 args->GetString(0, &entry_id); |
| 57 GURL url(article_scheme_ + std::string("://") + entry_id); | 82 GURL url = |
| 83 url_utils::GetDistillerViewUrlFromEntryId(article_scheme_, entry_id); | |
| 58 DCHECK(url.is_valid()); | 84 DCHECK(url.is_valid()); |
| 59 web_ui()->GetWebContents()->GetController().LoadURL(url, | 85 web_ui()->GetWebContents()->GetController().LoadURL( |
| 60 content::Referrer(), content::PAGE_TRANSITION_GENERATED, | 86 url, |
| 87 content::Referrer(), | |
| 88 content::PAGE_TRANSITION_GENERATED, | |
| 61 std::string()); | 89 std::string()); |
| 62 } | 90 } |
| 63 | 91 |
| 64 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) { | 92 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) { |
| 65 base::ListValue entries; | 93 base::ListValue entries; |
| 66 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); | 94 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); |
| 67 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); | 95 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); |
| 68 it != entries_specifics.end(); | 96 it != entries_specifics.end(); |
| 69 ++it) { | 97 ++it) { |
| 70 const ArticleEntry& article = *it; | 98 const ArticleEntry& article = *it; |
| 71 DCHECK(IsEntryValid(article)); | 99 DCHECK(IsEntryValid(article)); |
| 72 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); | 100 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); |
| 73 entry->SetString("entry_id", article.entry_id()); | 101 entry->SetString("entry_id", article.entry_id()); |
| 74 std::string title = (!article.has_title() || article.title().empty()) | 102 std::string title = (!article.has_title() || article.title().empty()) |
| 75 ? article.entry_id() | 103 ? article.entry_id() |
| 76 : article.title(); | 104 : article.title(); |
| 77 entry->SetString("title", title); | 105 entry->SetString("title", net::EscapeForHTML(title)); |
|
shashi
2014/02/27 04:29:08
Is there a unit test somewhere to test all values
nyquist
2014/02/27 19:50:19
Good suggestion. I'll add a TODO for that.
| |
| 78 entries.Append(entry.release()); | 106 entries.Append(entry.release()); |
| 79 } | 107 } |
| 80 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries); | 108 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries); |
| 81 } | 109 } |
| 82 | 110 |
| 83 void DomDistillerHandler::OnArticleAdded(bool article_available) { | 111 void DomDistillerHandler::OnArticleAdded(bool article_available) { |
| 84 // TODO(nyquist): Update this function. | 112 // TODO(nyquist): Update this function. |
| 85 if (article_available) { | 113 if (article_available) { |
| 86 HandleRequestEntries(NULL); | 114 HandleRequestEntries(NULL); |
| 87 } else { | 115 } else { |
| 88 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); | 116 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); |
| 89 } | 117 } |
| 90 } | 118 } |
| 91 | 119 |
| 92 } // namespace dom_distiller | 120 } // namespace dom_distiller |
| OLD | NEW |