| 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/ios/distiller_page_ios.h" | 5 #include "components/dom_distiller/ios/distiller_page_ios.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/mac/foundation_util.h" | 13 #include "base/mac/foundation_util.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "components/dom_distiller/ios/distiller_favicon_ios.h" |
| 17 #include "ios/public/provider/web/web_controller_provider.h" | 18 #include "ios/public/provider/web/web_controller_provider.h" |
| 18 #include "ios/public/provider/web/web_controller_provider_factory.h" | 19 #include "ios/public/provider/web/web_controller_provider_factory.h" |
| 19 #include "ios/web/public/browser_state.h" | 20 #include "ios/web/public/browser_state.h" |
| 21 #include "ios/web/public/favicon_url.h" |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 // This is duplicated here from ios/web/web_state/ui/web_view_js_utils.mm in | 25 // This is duplicated here from ios/web/web_state/ui/web_view_js_utils.mm in |
| 24 // order to handle numbers. The dom distiller proto expects integers and the | 26 // order to handle numbers. The dom distiller proto expects integers and the |
| 25 // generated JSON deserializer does not accept doubles in the place of ints. | 27 // generated JSON deserializer does not accept doubles in the place of ints. |
| 26 // However WKWebView only returns "numbers." However, here the proto expects | 28 // However WKWebView only returns "numbers." However, here the proto expects |
| 27 // integers and doubles, which is done by checking if the number has a fraction | 29 // integers and doubles, which is done by checking if the number has a fraction |
| 28 // or not; since this is a hacky method it's isolated to this file so as to | 30 // or not; since this is a hacky method it's isolated to this file so as to |
| 29 // limit the risk of broken JS calls. | 31 // limit the risk of broken JS calls. |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 DCHECK(distiller_page_); | 120 DCHECK(distiller_page_); |
| 119 } | 121 } |
| 120 | 122 |
| 121 void DistillerWebStateObserver::PageLoaded( | 123 void DistillerWebStateObserver::PageLoaded( |
| 122 web::PageLoadCompletionStatus load_completion_status) { | 124 web::PageLoadCompletionStatus load_completion_status) { |
| 123 distiller_page_->OnLoadURLDone(load_completion_status); | 125 distiller_page_->OnLoadURLDone(load_completion_status); |
| 124 } | 126 } |
| 125 | 127 |
| 126 #pragma mark - | 128 #pragma mark - |
| 127 | 129 |
| 128 DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state) | 130 DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state, |
| 129 : browser_state_(browser_state), weak_ptr_factory_(this) { | 131 DistillerFaviconIOS* distiller_favicon) |
| 130 } | 132 : distiller_favicon_(distiller_favicon), weak_ptr_factory_(this) {} |
| 131 | 133 |
| 132 DistillerPageIOS::~DistillerPageIOS() { | 134 DistillerPageIOS::~DistillerPageIOS() { |
| 133 } | 135 } |
| 134 | 136 |
| 135 bool DistillerPageIOS::StringifyOutput() { | 137 bool DistillerPageIOS::StringifyOutput() { |
| 136 return false; | 138 return false; |
| 137 } | 139 } |
| 138 | 140 |
| 139 void DistillerPageIOS::DistillPageImpl(const GURL& url, | 141 void DistillerPageIOS::DistillPageImpl(const GURL& url, |
| 140 const std::string& script) { | 142 const std::string& script) { |
| 141 if (!url.is_valid() || !script.length()) | 143 if (!url.is_valid() || !script.length()) |
| 142 return; | 144 return; |
| 143 url_ = url; | 145 url_ = url; |
| 144 script_ = script; | 146 script_ = script; |
| 145 | 147 |
| 146 // Lazily create provider. | 148 provider_ = distiller_favicon_->CreateProvider(url); |
| 147 if (!provider_) { | 149 |
| 148 if (ios::GetWebControllerProviderFactory()) { | 150 // Lazily create provider. |
| 149 provider_ = | 151 // if (!provider_) { |
| 150 ios::GetWebControllerProviderFactory()->CreateWebControllerProvider( | 152 // if (ios::GetWebControllerProviderFactory()) { |
| 151 browser_state_); | 153 // provider_ = |
| 152 web_state_observer_.reset( | 154 // ios::GetWebControllerProviderFactory()->CreateWebControllerProvider( |
| 153 new DistillerWebStateObserver(provider_->GetWebState(), this)); | 155 // browser_state_); |
| 154 } | 156 // web_state_observer_.reset( |
| 155 } | 157 // new DistillerWebStateObserver(provider_->GetWebState(), this)); |
| 158 // } |
| 159 // } |
| 156 | 160 |
| 157 // Load page using provider. | 161 // Load page using provider. |
| 158 if (provider_) | 162 if (provider_) |
| 159 provider_->LoadURL(url_); | 163 provider_->LoadURL(url_); |
| 160 else | 164 else |
| 161 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE); | 165 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE); |
| 162 } | 166 } |
| 163 | 167 |
| 164 void DistillerPageIOS::OnLoadURLDone( | 168 void DistillerPageIOS::OnLoadURLDone( |
| 165 web::PageLoadCompletionStatus load_completion_status) { | 169 web::PageLoadCompletionStatus load_completion_status) { |
| 166 // Don't attempt to distill if the page load failed or if there is no | 170 // Don't attempt to distill if the page load failed or if there is no |
| 167 // provider. | 171 // provider. |
| 168 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || | 172 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || |
| 169 !provider_) { | 173 !provider_) { |
| 170 HandleJavaScriptResult(nil); | 174 HandleJavaScriptResult(nil); |
| 171 return; | 175 return; |
| 172 } | 176 } |
| 173 | 177 |
| 174 // Inject the script. | 178 // Inject the script. |
| 175 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); | 179 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); |
| 176 provider_->InjectScript(script_, ^(id result, NSError* error) { | 180 provider_->InjectScript(script_, ^(id result, NSError* error) { |
| 177 DistillerPageIOS* distiller_page = weak_this.get(); | 181 DistillerPageIOS* distiller_page = weak_this.get(); |
| 178 if (distiller_page) | 182 |
| 183 if (distiller_page) { |
| 179 distiller_page->HandleJavaScriptResult(result); | 184 distiller_page->HandleJavaScriptResult(result); |
| 185 |
| 186 // Get the favicons. |
| 187 // provider_->InjectScript(favicon_script_, ^(id result, NSError* |
| 188 // error) { |
| 189 // DistillerPageIOS* distiller_page_favicon = weak_this.get(); |
| 190 // if (distiller_page_favicon) |
| 191 // distiller_page_favicon->HandleFaviconResult(result); |
| 192 // }); |
| 193 } |
| 180 }); | 194 }); |
| 181 } | 195 } |
| 182 | 196 |
| 183 void DistillerPageIOS::HandleJavaScriptResult(id result) { | 197 void DistillerPageIOS::HandleJavaScriptResult(id result) { |
| 184 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); | 198 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); |
| 185 if (result) { | 199 if (result) { |
| 186 resultValue = ValueResultFromScriptResult(result); | 200 resultValue = ValueResultFromScriptResult(result); |
| 187 } | 201 } |
| 188 OnDistillationDone(url_, resultValue.get()); | 202 OnDistillationDone(url_, resultValue.get()); |
| 189 } | 203 } |
| 190 | 204 |
| 191 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( | 205 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( |
| 192 id wk_result) { | 206 id wk_result) { |
| 193 return ::ValueResultFromScriptResult(wk_result, | 207 return ::ValueResultFromScriptResult(wk_result, |
| 194 kMaximumParsingRecursionDepth); | 208 kMaximumParsingRecursionDepth); |
| 195 } | 209 } |
| 196 } // namespace dom_distiller | 210 } // namespace dom_distiller |
| OLD | NEW |