Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: components/dom_distiller/webui/dom_distiller_handler.cc

Issue 151003006: Add support for distilling arbitrary URLs in DOM Distiller Viewer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments. added unit tests Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
18 #include "net/base/url_util.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 GURL gurl(url);
65 if (gurl.is_valid()) {
66 GURL view_url(article_scheme_ + "://" + base::GenerateGUID() + "/?" +
shashi 2014/02/25 21:48:08 Probably want to use the GetDistillerViewUrl* func
nyquist 2014/02/27 00:07:37 Done.
67 std::string(kUrlKey) + "=" +
68 net::EscapeQueryParamValue(gurl.spec(), true));
69 DCHECK(view_url.is_valid());
70 web_ui()->GetWebContents()->GetController().LoadURL(
71 view_url,
72 content::Referrer(),
73 content::PAGE_TRANSITION_GENERATED,
74 std::string());
75 } else {
76 web_ui()->CallJavascriptFunction("domDistiller.onViewUrlFailed");
77 }
78 }
79
54 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) { 80 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) {
55 std::string entry_id; 81 std::string entry_id;
56 args->GetString(0, &entry_id); 82 args->GetString(0, &entry_id);
57 GURL url(article_scheme_ + std::string("://") + entry_id); 83 GURL url(article_scheme_ + "://" + base::GenerateGUID() + "/?" +
shashi 2014/02/25 21:48:08 Probably want to use the GetDistillerViewUrl* func
nyquist 2014/02/27 00:07:37 Done.
84 std::string(kEntryIdKey) + "=" +
85 net::EscapeQueryParamValue(entry_id, true));
58 DCHECK(url.is_valid()); 86 DCHECK(url.is_valid());
59 web_ui()->GetWebContents()->GetController().LoadURL(url, 87 web_ui()->GetWebContents()->GetController().LoadURL(
60 content::Referrer(), content::PAGE_TRANSITION_GENERATED, 88 url,
89 content::Referrer(),
90 content::PAGE_TRANSITION_GENERATED,
61 std::string()); 91 std::string());
62 } 92 }
63 93
64 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) { 94 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) {
65 base::ListValue entries; 95 base::ListValue entries;
66 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); 96 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries();
67 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); 97 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin();
68 it != entries_specifics.end(); 98 it != entries_specifics.end();
69 ++it) { 99 ++it) {
70 const ArticleEntry& article = *it; 100 const ArticleEntry& article = *it;
(...skipping 11 matching lines...) Expand all
82 112
83 void DomDistillerHandler::OnArticleAdded(bool article_available) { 113 void DomDistillerHandler::OnArticleAdded(bool article_available) {
84 // TODO(nyquist): Update this function. 114 // TODO(nyquist): Update this function.
85 if (article_available) { 115 if (article_available) {
86 HandleRequestEntries(NULL); 116 HandleRequestEntries(NULL);
87 } else { 117 } else {
88 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); 118 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed");
89 } 119 }
90 } 120 }
91 121
122 const GURL DomDistillerHandler::GetDistillerViewUrlFromEntryId(
shashi 2014/02/25 21:48:08 Is this function used anywhere?
cjhopman 2014/02/25 22:25:14 Looks like this is also defined in the tests.
nyquist 2014/02/27 00:07:37 Done. Created dom_distiller::url_utils for this an
123 const std::string& entry_id) const {
124 GURL url(article_scheme_ + "://" + base::GenerateGUID());
125 return net::AppendOrReplaceQueryParameter(url, kEntryIdKey, entry_id);
126 }
127
128 const GURL DomDistillerHandler::GetDistillerViewUrlFromUrl(const GURL& view_url)
129 const {
130 GURL url(article_scheme_ + "://" + base::GenerateGUID());
shashi 2014/02/25 21:48:08 Same for this function.
nyquist 2014/02/27 00:07:37 Done.
131 return net::AppendOrReplaceQueryParameter(url, kUrlKey, view_url.spec());
132 }
133
92 } // namespace dom_distiller 134 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698