OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/content/dom_distiller_viewer_source.h" | 5 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "components/dom_distiller/core/task_tracker.h" | 14 #include "components/dom_distiller/core/task_tracker.h" |
15 #include "components/dom_distiller/core/url_constants.h" | 15 #include "components/dom_distiller/core/url_constants.h" |
16 #include "components/dom_distiller/core/viewer.h" | 16 #include "components/dom_distiller/core/viewer.h" |
17 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
18 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
19 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
20 | 20 |
21 namespace dom_distiller { | 21 namespace dom_distiller { |
22 | 22 |
23 // Handles receiving data asynchronously for a specific entry, and passing | 23 // Handles receiving data asynchronously for a specific entry, and passing |
24 // it along to the data callback for the data source. | 24 // it along to the data callback for the data source. |
25 class DomDistillerViewerSource::RequestViewerHandle | 25 class DomDistillerViewerSource::RequestViewerHandle |
26 : public ViewRequestDelegate { | 26 : public ViewRequestDelegate { |
27 public: | 27 public: |
28 explicit RequestViewerHandle( | 28 explicit RequestViewerHandle( |
29 int render_process_id, | |
30 int render_frame_id, | |
29 const content::URLDataSource::GotDataCallback& callback); | 31 const content::URLDataSource::GotDataCallback& callback); |
30 virtual ~RequestViewerHandle(); | 32 virtual ~RequestViewerHandle(); |
31 | 33 |
32 // ViewRequestDelegate implementation. | 34 // ViewRequestDelegate implementation. |
33 virtual void OnArticleReady( | 35 virtual void OnArticleReady( |
34 const DistilledArticleProto* article_proto) OVERRIDE; | 36 const DistilledArticleProto* article_proto) OVERRIDE; |
35 | 37 |
36 virtual void OnArticleUpdated( | 38 virtual void OnArticleUpdated( |
37 ArticleDistillationUpdate article_update) OVERRIDE; | 39 ArticleDistillationUpdate article_update) OVERRIDE; |
38 | 40 |
39 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); | 41 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); |
40 | 42 |
41 private: | 43 private: |
42 // The handle to the view request towards the DomDistillerService. It | 44 // The handle to the view request towards the DomDistillerService. It |
43 // needs to be kept around to ensure the distillation request finishes. | 45 // needs to be kept around to ensure the distillation request finishes. |
44 scoped_ptr<ViewerHandle> viewer_handle_; | 46 scoped_ptr<ViewerHandle> viewer_handle_; |
45 | 47 |
48 // Ids associated with the viewer's render process. | |
49 int render_process_id_; | |
50 int render_frame_id_; | |
51 | |
46 // This holds the callback to where the data retrieved is sent back. | 52 // This holds the callback to where the data retrieved is sent back. |
47 content::URLDataSource::GotDataCallback callback_; | 53 content::URLDataSource::GotDataCallback callback_; |
54 | |
55 // Number of pages that have been rendered by the viewer. | |
56 int page_count_; | |
48 }; | 57 }; |
49 | 58 |
50 DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( | 59 DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( |
60 int render_process_id, | |
61 int render_frame_id, | |
51 const content::URLDataSource::GotDataCallback& callback) | 62 const content::URLDataSource::GotDataCallback& callback) |
52 : callback_(callback) { | 63 : render_process_id_(render_process_id), |
64 render_frame_id_(render_frame_id), | |
65 callback_(callback), | |
66 page_count_(0) { | |
53 } | 67 } |
54 | 68 |
55 DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { | 69 DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { |
56 } | 70 } |
57 | 71 |
58 void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( | 72 void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( |
59 const DistilledArticleProto* article_proto) { | 73 const DistilledArticleProto* article_proto) { |
60 std::string unsafe_page_html = viewer::GetUnsafeHtml(article_proto); | 74 content::RenderFrameHost* render_frame_host = |
61 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | 75 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); |
76 if (page_count_ == 0) { | |
77 std::string unsafe_page_html = viewer::GetUnsafeArticleHtml(article_proto); | |
nyquist
2014/04/29 23:58:37
Add a comment. Maybe something like: "This is a si
Yaron
2014/05/07 05:04:29
Done.
| |
78 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | |
79 } else if (page_count_ == article_proto->pages_size()) { | |
80 // We may still be showing the "Loading" indicator. | |
81 if (render_frame_host) { | |
82 render_frame_host->ExecuteJavaScript( | |
83 viewer::GetToggleLoadingIndicatorJs(true)); | |
84 } | |
85 } else { | |
86 if (!render_frame_host) { | |
87 return; | |
88 } | |
89 for (;page_count_ < article_proto->pages_size(); | |
nyquist
2014/04/29 23:58:37
Add a comment saying something like: "Got OnArticl
Yaron
2014/05/07 05:04:29
Done-ish.
| |
90 page_count_++) { | |
91 const DistilledPageProto& page = article_proto->pages(page_count_); | |
92 render_frame_host->ExecuteJavaScript( | |
93 viewer::GetUnsafeIncrementalDistilledPageJs( | |
94 &page, | |
95 page_count_ == article_proto->pages_size())); | |
96 } | |
97 } | |
62 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 98 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
63 } | 99 } |
64 | 100 |
65 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( | 101 void DomDistillerViewerSource::RequestViewerHandle::OnArticleUpdated( |
66 ArticleDistillationUpdate article_update) { | 102 ArticleDistillationUpdate article_update) { |
67 // TODO(nyquist): Add support for displaying pages incrementally. | 103 content::RenderFrameHost* render_frame_host = |
104 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); | |
105 for (;page_count_ < static_cast<int>(article_update.GetPagesSize()); | |
106 page_count_++) { | |
107 const DistilledPageProto& page = article_update.GetDistilledPage(page_count_ ); | |
108 if (page_count_ == 0) { | |
109 std::string unsafe_page_html = viewer::GetUnsafePartialArticleHtml(&page); | |
110 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | |
111 } else { | |
112 if (render_frame_host) { | |
113 render_frame_host->ExecuteJavaScript( | |
114 viewer::GetUnsafeIncrementalDistilledPageJs(&page, false)); | |
115 } | |
116 } | |
117 } | |
68 } | 118 } |
69 | 119 |
70 void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( | 120 void DomDistillerViewerSource::RequestViewerHandle::TakeViewerHandle( |
71 scoped_ptr<ViewerHandle> viewer_handle) { | 121 scoped_ptr<ViewerHandle> viewer_handle) { |
72 viewer_handle_ = viewer_handle.Pass(); | 122 viewer_handle_ = viewer_handle.Pass(); |
73 } | 123 } |
74 | 124 |
75 DomDistillerViewerSource::DomDistillerViewerSource( | 125 DomDistillerViewerSource::DomDistillerViewerSource( |
76 DomDistillerServiceInterface* dom_distiller_service, | 126 DomDistillerServiceInterface* dom_distiller_service, |
77 const std::string& scheme) | 127 const std::string& scheme) |
(...skipping 17 matching lines...) Expand all Loading... | |
95 DCHECK(render_frame_host); | 145 DCHECK(render_frame_host); |
96 content::RenderViewHost* render_view_host = | 146 content::RenderViewHost* render_view_host = |
97 render_frame_host->GetRenderViewHost(); | 147 render_frame_host->GetRenderViewHost(); |
98 DCHECK(render_view_host); | 148 DCHECK(render_view_host); |
99 CHECK_EQ(0, render_view_host->GetEnabledBindings()); | 149 CHECK_EQ(0, render_view_host->GetEnabledBindings()); |
100 | 150 |
101 if (kCssPath == path) { | 151 if (kCssPath == path) { |
102 std::string css = viewer::GetCss(); | 152 std::string css = viewer::GetCss(); |
103 callback.Run(base::RefCountedString::TakeString(&css)); | 153 callback.Run(base::RefCountedString::TakeString(&css)); |
104 return; | 154 return; |
155 } else if (kViewerJsPath == path) { | |
nyquist
2014/04/29 23:58:37
I think you also should add JS to the mime type ha
Yaron
2014/05/07 05:04:29
Done.
| |
156 std::string js = viewer::GetJavaScript(); | |
157 callback.Run(base::RefCountedString::TakeString(&js)); | |
158 return; | |
105 } | 159 } |
106 | 160 |
107 RequestViewerHandle* request_viewer_handle = | 161 RequestViewerHandle* request_viewer_handle = |
108 new RequestViewerHandle(callback); | 162 new RequestViewerHandle(render_process_id, render_frame_id, callback); |
109 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( | 163 scoped_ptr<ViewerHandle> viewer_handle = viewer::CreateViewRequest( |
110 dom_distiller_service_, path, request_viewer_handle); | 164 dom_distiller_service_, path, request_viewer_handle); |
111 | 165 |
112 if (viewer_handle) { | 166 if (viewer_handle) { |
113 // The service returned a |ViewerHandle| and guarantees it will call | 167 // The service returned a |ViewerHandle| and guarantees it will call |
114 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | 168 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
115 // request is not cancelled. The |RequestViewerHandle| will delete itself | 169 // request is not cancelled. The |RequestViewerHandle| will delete itself |
116 // after receiving the callback. | 170 // after receiving the callback. |
117 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | 171 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); |
118 } else { | 172 } else { |
(...skipping 23 matching lines...) Expand all Loading... | |
142 const net::URLRequest* request, | 196 const net::URLRequest* request, |
143 std::string* path) const { | 197 std::string* path) const { |
144 } | 198 } |
145 | 199 |
146 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 200 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
147 const { | 201 const { |
148 return "object-src 'none'; style-src 'self';"; | 202 return "object-src 'none'; style-src 'self';"; |
149 } | 203 } |
150 | 204 |
151 } // namespace dom_distiller | 205 } // namespace dom_distiller |
OLD | NEW |