OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/dom_distiller/content/browser/distillability_driver.h" |
| 6 #include "components/dom_distiller/content/common/distiller_messages.h" |
| 7 |
| 8 #include "content/public/browser/render_frame_host.h" |
| 9 #include "content/public/browser/web_contents_observer.h" |
| 10 #include "content/public/browser/web_contents_user_data.h" |
| 11 |
| 12 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 13 dom_distiller::WebContentsDistillabilityDriver); |
| 14 |
| 15 namespace dom_distiller { |
| 16 |
| 17 WebContentsDistillabilityDriver::WebContentsDistillabilityDriver( |
| 18 content::WebContents* web_contents) |
| 19 : content::WebContentsObserver(web_contents), |
| 20 callback_id_(0) { |
| 21 } |
| 22 |
| 23 WebContentsDistillabilityDriver::~WebContentsDistillabilityDriver() { |
| 24 CleanUp(); |
| 25 } |
| 26 |
| 27 bool WebContentsDistillabilityDriver::OnMessageReceived(const IPC::Message &msg, |
| 28 content::RenderFrameHost* render_frame_host) { |
| 29 bool handled = true; |
| 30 IPC_BEGIN_MESSAGE_MAP(WebContentsDistillabilityDriver, msg) |
| 31 IPC_MESSAGE_HANDLER(FrameHostMsg_ExtractFeatureResponse, |
| 32 OnExtractFeatureResponse) |
| 33 IPC_MESSAGE_UNHANDLED(handled = false) |
| 34 IPC_END_MESSAGE_MAP() |
| 35 return handled; |
| 36 } |
| 37 |
| 38 void WebContentsDistillabilityDriver::OnExtractFeatureResponse(int id, |
| 39 FrameHostMsg_ExtractFeatureResponse_Params results) { |
| 40 std::map<int, ExtractFeatureCallback>::iterator it = |
| 41 callbacks_.find(id); |
| 42 if (it != callbacks_.end()) { |
| 43 it->second.Run( |
| 44 results.isOGArticle, |
| 45 results.url, |
| 46 results.numElements, |
| 47 results.numAnchors, |
| 48 results.numForms, |
| 49 results.innerText, |
| 50 results.textContent, |
| 51 results.innerHTML); |
| 52 callbacks_.erase(it); |
| 53 } else { |
| 54 NOTREACHED() << "Received ExtractFeature response for unknown request"; |
| 55 } |
| 56 } |
| 57 |
| 58 void WebContentsDistillabilityDriver::RenderProcessGone( |
| 59 base::TerminationStatus status) { |
| 60 CleanUp(); |
| 61 } |
| 62 |
| 63 void WebContentsDistillabilityDriver::ExtractFeatures( |
| 64 const ExtractFeatureCallback& callback) { |
| 65 int key = callback_id_++; |
| 66 callbacks_.insert(std::make_pair(key, callback)); |
| 67 |
| 68 content::RenderFrameHost* rfh = web_contents()->GetMainFrame(); |
| 69 rfh->Send(new FrameMsg_ExtractFeatureRequest(rfh->GetRoutingID(), key)); |
| 70 } |
| 71 |
| 72 void WebContentsDistillabilityDriver::CleanUp() { |
| 73 content::WebContentsObserver::Observe(NULL); |
| 74 } |
| 75 |
| 76 } // namespace dom_distiller |
OLD | NEW |