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" | |
| 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" | |
| 13 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/browser/web_ui.h" | 16 #include "content/public/browser/web_ui.h" |
| 17 #include "net/base/escape.h" | |
| 15 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 16 | 19 |
| 17 namespace dom_distiller { | 20 namespace dom_distiller { |
| 18 | 21 |
| 19 DomDistillerHandler::DomDistillerHandler(DomDistillerService* service, | 22 DomDistillerHandler::DomDistillerHandler(DomDistillerService* service, |
| 20 const std::string& scheme) | 23 const std::string& scheme) |
| 21 : service_(service), article_scheme_(scheme), weak_ptr_factory_(this) {} | 24 : service_(service), article_scheme_(scheme), weak_ptr_factory_(this) {} |
| 22 | 25 |
| 23 DomDistillerHandler::~DomDistillerHandler() {} | 26 DomDistillerHandler::~DomDistillerHandler() {} |
| 24 | 27 |
| 25 void DomDistillerHandler::RegisterMessages() { | 28 void DomDistillerHandler::RegisterMessages() { |
| 26 web_ui()->RegisterMessageCallback( | 29 web_ui()->RegisterMessageCallback( |
| 27 "requestEntries", | 30 "requestEntries", |
| 28 base::Bind(&DomDistillerHandler::HandleRequestEntries, | 31 base::Bind(&DomDistillerHandler::HandleRequestEntries, |
| 29 base::Unretained(this))); | 32 base::Unretained(this))); |
| 30 web_ui()->RegisterMessageCallback( | 33 web_ui()->RegisterMessageCallback( |
| 31 "addArticle", | 34 "addArticle", |
| 32 base::Bind(&DomDistillerHandler::HandleAddArticle, | 35 base::Bind(&DomDistillerHandler::HandleAddArticle, |
| 33 base::Unretained(this))); | 36 base::Unretained(this))); |
| 34 web_ui()->RegisterMessageCallback( | 37 web_ui()->RegisterMessageCallback( |
| 35 "selectArticle", | 38 "selectArticle", |
| 36 base::Bind(&DomDistillerHandler::HandleSelectArticle, | 39 base::Bind(&DomDistillerHandler::HandleSelectArticle, |
| 37 base::Unretained(this))); | 40 base::Unretained(this))); |
| 41 web_ui()->RegisterMessageCallback( | |
| 42 "viewUrl", | |
| 43 base::Bind(&DomDistillerHandler::HandleViewUrl, base::Unretained(this))); | |
| 38 } | 44 } |
| 39 | 45 |
| 40 void DomDistillerHandler::HandleAddArticle(const base::ListValue* args) { | 46 void DomDistillerHandler::HandleAddArticle(const base::ListValue* args) { |
| 41 std::string url; | 47 std::string url; |
| 42 args->GetString(0, &url); | 48 args->GetString(0, &url); |
| 43 GURL gurl(url); | 49 GURL gurl(url); |
| 44 if (gurl.is_valid()) { | 50 if (gurl.is_valid()) { |
| 45 service_->AddToList( | 51 service_->AddToList( |
| 46 gurl, | 52 gurl, |
| 47 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded, | 53 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded, |
| 48 base::Unretained(this)))); | 54 base::Unretained(this)))); |
| 49 } else { | 55 } else { |
| 50 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); | 56 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); |
| 51 } | 57 } |
| 52 } | 58 } |
| 53 | 59 |
| 60 void DomDistillerHandler::HandleViewUrl(const base::ListValue* args) { | |
| 61 std::string url; | |
| 62 args->GetString(0, &url); | |
| 63 GURL gurl(url); | |
| 64 if (gurl.is_valid()) { | |
| 65 GURL view_url(article_scheme_ + "://" + base::GenerateGUID() + "/?" + | |
| 66 std::string(kUrlKey) + "=" + | |
|
shashi
2014/02/05 22:28:08
nit: can be refactored and maybe use: net::AppendO
nyquist
2014/02/25 20:30:24
Done.
shashi
2014/02/25 21:48:08
Is this refactored.
On 2014/02/25 20:30:24, nyquis
| |
| 67 net::EscapeQueryParamValue(gurl.spec(), true)); | |
| 68 web_ui()->GetWebContents()->GetController().LoadURL( | |
| 69 view_url, | |
| 70 content::Referrer(), | |
| 71 content::PAGE_TRANSITION_GENERATED, | |
| 72 std::string()); | |
| 73 } else { | |
| 74 web_ui()->CallJavascriptFunction("domDistiller.onViewUrlFailed"); | |
| 75 } | |
| 76 } | |
| 77 | |
| 54 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) { | 78 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) { |
| 55 std::string entry_id; | 79 std::string entry_id; |
| 56 args->GetString(0, &entry_id); | 80 args->GetString(0, &entry_id); |
| 57 GURL url(article_scheme_ + std::string("://") + entry_id); | 81 GURL url(article_scheme_ + "://" + base::GenerateGUID() + "/?" + |
| 58 DCHECK(url.is_valid()); | 82 std::string(kEntryIdKey) + "=" + |
|
shashi
2014/02/05 22:28:08
why remove the DCHECK?
nyquist
2014/02/25 20:30:24
Done.
| |
| 59 web_ui()->GetWebContents()->GetController().LoadURL(url, | 83 net::EscapeQueryParamValue(entry_id, true)); |
|
shashi
2014/02/05 22:28:08
nit: can be refactored and maybe use: net::AppendO
nyquist
2014/02/25 20:30:24
Done.
shashi
2014/02/25 21:48:08
Is this refactored?
On 2014/02/25 20:30:24, nyquis
| |
| 60 content::Referrer(), content::PAGE_TRANSITION_GENERATED, | 84 web_ui()->GetWebContents()->GetController().LoadURL( |
| 85 url, | |
| 86 content::Referrer(), | |
| 87 content::PAGE_TRANSITION_GENERATED, | |
| 61 std::string()); | 88 std::string()); |
| 62 } | 89 } |
| 63 | 90 |
| 64 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) { | 91 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) { |
| 65 base::ListValue entries; | 92 base::ListValue entries; |
| 66 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); | 93 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); |
| 67 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); | 94 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); |
| 68 it != entries_specifics.end(); | 95 it != entries_specifics.end(); |
| 69 ++it) { | 96 ++it) { |
| 70 const ArticleEntry& article = *it; | 97 const ArticleEntry& article = *it; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 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 |