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 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 ArticleDistillationUpdate article_update) OVERRIDE; | 48 ArticleDistillationUpdate article_update) OVERRIDE; |
49 | 49 |
50 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); | 50 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); |
51 | 51 |
52 // WebContentsObserver: | 52 // WebContentsObserver: |
53 virtual void DidNavigateMainFrame( | 53 virtual void DidNavigateMainFrame( |
54 const content::LoadCommittedDetails& details, | 54 const content::LoadCommittedDetails& details, |
55 const content::FrameNavigateParams& params) OVERRIDE; | 55 const content::FrameNavigateParams& params) OVERRIDE; |
56 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; | 56 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; |
57 virtual void WebContentsDestroyed() OVERRIDE; | 57 virtual void WebContentsDestroyed() OVERRIDE; |
58 virtual void DidFinishLoad( | 58 virtual void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
59 int64 frame_id, | 59 const GURL& validated_url) OVERRIDE; |
60 const GURL& validated_url, | |
61 bool is_main_frame, | |
62 content::RenderViewHost* render_view_host) OVERRIDE; | |
63 | 60 |
64 private: | 61 private: |
65 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't | 62 // Sends JavaScript to the attached Viewer, buffering data if the viewer isn't |
66 // ready. | 63 // ready. |
67 void SendJavaScript(const std::string& buffer); | 64 void SendJavaScript(const std::string& buffer); |
68 | 65 |
69 // Cancels the current view request. Once called, no updates will be | 66 // Cancels the current view request. Once called, no updates will be |
70 // propagated to the view, and the request to DomDistillerService will be | 67 // propagated to the view, and the request to DomDistillerService will be |
71 // cancelled. | 68 // cancelled. |
72 void Cancel(); | 69 void Cancel(); |
73 | 70 |
74 // The handle to the view request towards the DomDistillerService. It | 71 // The handle to the view request towards the DomDistillerService. It |
75 // needs to be kept around to ensure the distillation request finishes. | 72 // needs to be kept around to ensure the distillation request finishes. |
76 scoped_ptr<ViewerHandle> viewer_handle_; | 73 scoped_ptr<ViewerHandle> viewer_handle_; |
77 | 74 |
78 // WebContents associated with the Viewer's render process. | |
79 content::WebContents* web_contents_; | |
80 | |
81 // The scheme hosting the current view request; | 75 // The scheme hosting the current view request; |
82 std::string expected_scheme_; | 76 std::string expected_scheme_; |
83 | 77 |
84 // The query path for the current view request. | 78 // The query path for the current view request. |
85 std::string expected_request_path_; | 79 std::string expected_request_path_; |
86 | 80 |
87 // Holds the callback to where the data retrieved is sent back. | 81 // Holds the callback to where the data retrieved is sent back. |
88 content::URLDataSource::GotDataCallback callback_; | 82 content::URLDataSource::GotDataCallback callback_; |
89 | 83 |
90 // Number of pages of the distilled article content that have been rendered by | 84 // Number of pages of the distilled article content that have been rendered by |
91 // the viewer. | 85 // the viewer. |
92 int page_count_; | 86 int page_count_; |
93 | 87 |
94 // Whether the page is sufficiently initialized to handle updates from the | 88 // Whether the page is sufficiently initialized to handle updates from the |
95 // distiller. | 89 // distiller. |
96 bool waiting_for_page_ready_; | 90 bool waiting_for_page_ready_; |
97 | 91 |
98 // Temporary store of pending JavaScript if the page isn't ready to receive | 92 // Temporary store of pending JavaScript if the page isn't ready to receive |
99 // data from distillation. | 93 // data from distillation. |
100 std::string buffer_; | 94 std::string buffer_; |
101 }; | 95 }; |
102 | 96 |
103 DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( | 97 DomDistillerViewerSource::RequestViewerHandle::RequestViewerHandle( |
104 content::WebContents* web_contents, | 98 content::WebContents* web_contents, |
105 const std::string& expected_scheme, | 99 const std::string& expected_scheme, |
106 const std::string& expected_request_path, | 100 const std::string& expected_request_path, |
107 const content::URLDataSource::GotDataCallback& callback) | 101 const content::URLDataSource::GotDataCallback& callback) |
108 : web_contents_(web_contents), | 102 : expected_scheme_(expected_scheme), |
109 expected_scheme_(expected_scheme), | |
110 expected_request_path_(expected_request_path), | 103 expected_request_path_(expected_request_path), |
111 callback_(callback), | 104 callback_(callback), |
112 page_count_(0), | 105 page_count_(0), |
113 waiting_for_page_ready_(true) { | 106 waiting_for_page_ready_(true) { |
114 content::WebContentsObserver::Observe(web_contents_); | 107 content::WebContentsObserver::Observe(web_contents); |
115 } | 108 } |
116 | 109 |
117 DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { | 110 DomDistillerViewerSource::RequestViewerHandle::~RequestViewerHandle() { |
118 // Balanced with constructor although can be a no-op if frame navigated away. | |
119 content::WebContentsObserver::Observe(NULL); | |
120 } | 111 } |
121 | 112 |
122 void DomDistillerViewerSource::RequestViewerHandle::SendJavaScript( | 113 void DomDistillerViewerSource::RequestViewerHandle::SendJavaScript( |
123 const std::string& buffer) { | 114 const std::string& buffer) { |
124 if (waiting_for_page_ready_) { | 115 if (waiting_for_page_ready_) { |
125 buffer_ += buffer; | 116 buffer_ += buffer; |
126 } else { | 117 } else { |
127 if (web_contents_) { | 118 if (web_contents()) { |
128 web_contents_->GetMainFrame()->ExecuteJavaScript( | 119 web_contents()->GetMainFrame()->ExecuteJavaScript( |
129 base::UTF8ToUTF16(buffer)); | 120 base::UTF8ToUTF16(buffer)); |
130 } | 121 } |
131 } | 122 } |
132 } | 123 } |
133 | 124 |
134 void DomDistillerViewerSource::RequestViewerHandle::DidNavigateMainFrame( | 125 void DomDistillerViewerSource::RequestViewerHandle::DidNavigateMainFrame( |
135 const content::LoadCommittedDetails& details, | 126 const content::LoadCommittedDetails& details, |
136 const content::FrameNavigateParams& params) { | 127 const content::FrameNavigateParams& params) { |
137 const GURL& navigation = details.entry->GetURL(); | 128 const GURL& navigation = details.entry->GetURL(); |
138 if (details.is_in_page || ( | 129 if (details.is_in_page || ( |
(...skipping 10 matching lines...) Expand all Loading... |
149 void DomDistillerViewerSource::RequestViewerHandle::RenderProcessGone( | 140 void DomDistillerViewerSource::RequestViewerHandle::RenderProcessGone( |
150 base::TerminationStatus status) { | 141 base::TerminationStatus status) { |
151 Cancel(); | 142 Cancel(); |
152 } | 143 } |
153 | 144 |
154 void DomDistillerViewerSource::RequestViewerHandle::WebContentsDestroyed() { | 145 void DomDistillerViewerSource::RequestViewerHandle::WebContentsDestroyed() { |
155 Cancel(); | 146 Cancel(); |
156 } | 147 } |
157 | 148 |
158 void DomDistillerViewerSource::RequestViewerHandle::Cancel() { | 149 void DomDistillerViewerSource::RequestViewerHandle::Cancel() { |
159 // Ensure we don't send any incremental updates to the Viewer. | |
160 web_contents_ = NULL; | |
161 | |
162 // No need to listen for notifications. | 150 // No need to listen for notifications. |
163 content::WebContentsObserver::Observe(NULL); | 151 content::WebContentsObserver::Observe(NULL); |
164 | 152 |
165 // Schedule the Viewer for deletion. Ensures distillation is cancelled, and | 153 // Schedule the Viewer for deletion. Ensures distillation is cancelled, and |
166 // any pending data stored in |buffer_| is released. | 154 // any pending data stored in |buffer_| is released. |
167 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 155 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
168 } | 156 } |
169 | 157 |
170 void DomDistillerViewerSource::RequestViewerHandle::DidFinishLoad( | 158 void DomDistillerViewerSource::RequestViewerHandle::DidFinishLoad( |
171 int64 frame_id, | 159 content::RenderFrameHost* render_frame_host, |
172 const GURL& validated_url, | 160 const GURL& validated_url) { |
173 bool is_main_frame, | 161 if (render_frame_host->GetParent()) { |
174 content::RenderViewHost* render_view_host) { | |
175 if (!is_main_frame || web_contents_ == NULL) { | |
176 return; | 162 return; |
177 } | 163 } |
178 waiting_for_page_ready_ = false; | 164 waiting_for_page_ready_ = false; |
179 if (buffer_.empty()) { | 165 if (buffer_.empty()) { |
180 return; | 166 return; |
181 } | 167 } |
182 if (web_contents_) { | 168 web_contents()->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(buffer_)); |
183 web_contents_->GetMainFrame()->ExecuteJavaScript( | |
184 base::UTF8ToUTF16(buffer_)); | |
185 } | |
186 buffer_.clear(); | 169 buffer_.clear(); |
187 } | 170 } |
188 | 171 |
189 void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( | 172 void DomDistillerViewerSource::RequestViewerHandle::OnArticleReady( |
190 const DistilledArticleProto* article_proto) { | 173 const DistilledArticleProto* article_proto) { |
191 if (page_count_ == 0) { | 174 if (page_count_ == 0) { |
192 // This is a single-page article. | 175 // This is a single-page article. |
193 std::string unsafe_page_html = viewer::GetUnsafeArticleHtml(article_proto); | 176 std::string unsafe_page_html = viewer::GetUnsafeArticleHtml(article_proto); |
194 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); | 177 callback_.Run(base::RefCountedString::TakeString(&unsafe_page_html)); |
195 } else if (page_count_ == article_proto->pages_size()) { | 178 } else if (page_count_ == article_proto->pages_size()) { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 const net::URLRequest* request, | 302 const net::URLRequest* request, |
320 std::string* path) const { | 303 std::string* path) const { |
321 } | 304 } |
322 | 305 |
323 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() | 306 std::string DomDistillerViewerSource::GetContentSecurityPolicyObjectSrc() |
324 const { | 307 const { |
325 return "object-src 'none'; style-src 'self';"; | 308 return "object-src 'none'; style-src 'self';"; |
326 } | 309 } |
327 | 310 |
328 } // namespace dom_distiller | 311 } // namespace dom_distiller |
OLD | NEW |