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

Unified Diff: components/dom_distiller/content/dom_distiller_viewer_source.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 side-by-side diff with in-line comments
Download patch
Index: components/dom_distiller/content/dom_distiller_viewer_source.cc
diff --git a/components/dom_distiller/content/dom_distiller_viewer_source.cc b/components/dom_distiller/content/dom_distiller_viewer_source.cc
index 375a542c77aade81760c01823c9f37f4d46cf2fe..b529d5745936fe45b0aa6d02c46201676cd8dfea 100644
--- a/components/dom_distiller/content/dom_distiller_viewer_source.cc
+++ b/components/dom_distiller/content/dom_distiller_viewer_source.cc
@@ -16,19 +16,23 @@
#include "components/dom_distiller/core/proto/distilled_article.pb.h"
#include "components/dom_distiller/core/proto/distilled_page.pb.h"
#include "components/dom_distiller/core/task_tracker.h"
+#include "components/dom_distiller/core/url_constants.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "grit/component_resources.h"
#include "grit/component_strings.h"
#include "net/base/escape.h"
+#include "net/base/url_util.h"
#include "net/url_request/url_request.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"
+namespace dom_distiller {
+
namespace {
-const char kCssPath[] = "readability.css";
+const char kInternalScheme[] = "chrome-distiller-internal";
std::string ReplaceHtmlTemplateValues(std::string title, std::string content) {
base::StringPiece html_template =
@@ -44,11 +48,10 @@ std::string ReplaceHtmlTemplateValues(std::string title, std::string content) {
} // namespace
-namespace dom_distiller {
-
// Handles receiving data asynchronously for a specific entry, and passing
// it along to the data callback for the data source.
-class RequestViewerHandle : public ViewRequestDelegate {
+class DomDistillerViewerSource::RequestViewerHandle
+ : public ViewRequestDelegate {
public:
explicit RequestViewerHandle(
const content::URLDataSource::GotDataCallback& callback);
@@ -69,13 +72,13 @@ class RequestViewerHandle : public ViewRequestDelegate {
content::URLDataSource::GotDataCallback callback_;
};
-RequestViewerHandle::RequestViewerHandle(
+DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle(
const content::URLDataSource::GotDataCallback& callback)
: callback_(callback) {}
-RequestViewerHandle::~RequestViewerHandle() {}
+DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() {}
-void RequestViewerHandle::OnArticleReady(
+void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady(
const DistilledArticleProto* article_proto) {
DCHECK(article_proto);
std::string title;
@@ -101,13 +104,13 @@ void RequestViewerHandle::OnArticleReady(
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
-void RequestViewerHandle::TakeViewerHandle(
+void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle(
scoped_ptr<ViewerHandle> viewer_handle) {
viewer_handle_ = viewer_handle.Pass();
}
DomDistillerViewerSource::DomDistillerViewerSource(
- DomDistillerService* dom_distiller_service,
+ DomDistillerServiceInterface* dom_distiller_service,
const std::string& scheme)
: scheme_(scheme), dom_distiller_service_(dom_distiller_service) {}
@@ -140,9 +143,9 @@ void DomDistillerViewerSource::StartDataRequest(
RequestViewerHandle* request_viewer_handle =
new RequestViewerHandle(callback);
- std::string entry_id = StringToUpperASCII(path);
scoped_ptr<ViewerHandle> viewer_handle =
- dom_distiller_service_->ViewEntry(request_viewer_handle, entry_id);
+ CreateViewRequest(path, request_viewer_handle);
+
if (viewer_handle) {
// The service returned a |ViewerHandle| and guarantees it will call
// the |RequestViewerHandle|, so passing ownership to it, to ensure the
@@ -163,8 +166,8 @@ void DomDistillerViewerSource::StartDataRequest(
}
};
-std::string DomDistillerViewerSource::GetMimeType(const std::string& path)
- const {
+std::string DomDistillerViewerSource::GetMimeType(
+ const std::string& path) const {
if (path == kCssPath)
return "text/css";
return "text/html";
@@ -175,19 +178,52 @@ bool DomDistillerViewerSource::ShouldServiceRequest(
return request->url().SchemeIs(scheme_.c_str());
}
+// TODO(nyquist): Start tracking requests using this method.
void DomDistillerViewerSource::WillServiceRequest(
const net::URLRequest* request,
- std::string* path) const {
- if (*path != kCssPath) {
- // Since the full request is not available to StartDataRequest, replace the
- // path to contain the data needed.
- *path = request->url().host();
- }
-};
+ std::string* path) const {};
std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc()
const {
- return "object-src 'none'; style-src 'self'";
+ return "object-src 'none'; style-src 'self';";
+}
+
+scoped_ptr<ViewerHandle> DomDistillerViewerSource::CreateViewRequest(
+ const std::string& path,
+ ViewRequestDelegate* view_request_delegate) {
+ std::string entry_id = GetValueForKeyInURLPathQuery(path, kEntryIdKey);
+ bool has_entry_id = !entry_id.empty();
+ entry_id = StringToUpperASCII(entry_id);
+
+ std::string requested_url_str = GetValueForKeyInURLPathQuery(path, kUrlKey);
+ GURL requested_url(requested_url_str);
+ bool has_url = !requested_url_str.empty() && requested_url.is_valid();
shashi 2014/02/25 21:48:08 nit is requested_url.is_valid() sufficient here? h
nyquist 2014/02/27 00:07:37 Done.
+
+ if (has_entry_id && has_url) {
+ // It is invalid to specify a query param for both |kEntryIdKey| and
+ // |kUrlKey|.
+ return scoped_ptr<ViewerHandle>();
+ }
+
+ if (has_entry_id) {
+ return dom_distiller_service_->ViewEntry(view_request_delegate, entry_id)
+ .Pass();
+ } else if (has_url) {
+ return dom_distiller_service_->ViewUrl(view_request_delegate, requested_url)
+ .Pass();
+ }
+
+ // It is invalid to not specify a query param for |kEntryIdKey| or |kUrlKey|.
+ return scoped_ptr<ViewerHandle>();
+}
+
+std::string DomDistillerViewerSource::GetValueForKeyInURLPathQuery(
+ const std::string& path,
+ const std::string& key) {
+ GURL dummy_url(std::string(kInternalScheme) + "://dummy/" + path);
shashi 2014/02/25 21:48:08 It will be nice to have a comment here, that using
nyquist 2014/02/27 00:07:37 Done. Also moved more of this into the constant, w
+ std::string value;
+ net::GetValueForKeyInQuery(dummy_url, key, &value);
+ return value;
}
} // namespace dom_distiller

Powered by Google App Engine
This is Rietveld 408576698