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/strings/utf_string_conversions.h" | |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "ios/public/provider/web/web_controller_provider.h" | 18 #include "components/favicon/ios/web_favicon_driver.h" |
18 #include "ios/public/provider/web/web_controller_provider_factory.h" | |
19 #include "ios/web/public/browser_state.h" | 19 #include "ios/web/public/browser_state.h" |
20 #import "ios/web/public/navigation_manager.h" | |
21 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" | |
22 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | |
23 #import "ios/web/public/web_state/web_state.h" | |
20 | 24 |
21 namespace { | 25 namespace { |
22 | 26 |
23 // This is duplicated here from ios/web/web_state/ui/web_view_js_utils.mm in | 27 // 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 | 28 // 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. | 29 // generated JSON deserializer does not accept doubles in the place of ints. |
26 // However WKWebView only returns "numbers." However, here the proto expects | 30 // 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 | 31 // 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 | 32 // 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. | 33 // limit the risk of broken JS calls. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 DCHECK(distiller_page_); | 122 DCHECK(distiller_page_); |
119 } | 123 } |
120 | 124 |
121 void DistillerWebStateObserver::PageLoaded( | 125 void DistillerWebStateObserver::PageLoaded( |
122 web::PageLoadCompletionStatus load_completion_status) { | 126 web::PageLoadCompletionStatus load_completion_status) { |
123 distiller_page_->OnLoadURLDone(load_completion_status); | 127 distiller_page_->OnLoadURLDone(load_completion_status); |
124 } | 128 } |
125 | 129 |
126 #pragma mark - | 130 #pragma mark - |
127 | 131 |
128 DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state) | 132 DistillerPageIOS::DistillerPageIOS(WebStatePool* web_state_pool) |
129 : browser_state_(browser_state), weak_ptr_factory_(this) { | 133 : web_state_pool_(web_state_pool), weak_ptr_factory_(this) {} |
130 } | |
131 | 134 |
132 DistillerPageIOS::~DistillerPageIOS() { | 135 DistillerPageIOS::~DistillerPageIOS() { |
133 } | 136 } |
134 | 137 |
135 bool DistillerPageIOS::StringifyOutput() { | 138 bool DistillerPageIOS::StringifyOutput() { |
136 return false; | 139 return false; |
137 } | 140 } |
138 | 141 |
139 void DistillerPageIOS::DistillPageImpl(const GURL& url, | 142 void DistillerPageIOS::DistillPageImpl(const GURL& url, |
140 const std::string& script) { | 143 const std::string& script) { |
141 if (!url.is_valid() || !script.length()) | 144 if (!url.is_valid() || !script.length()) |
142 return; | 145 return; |
143 url_ = url; | 146 url_ = url; |
144 script_ = script; | 147 script_ = script; |
145 | 148 |
146 // Lazily create provider. | 149 web_state_pool_->RequestWebState(this); |
147 if (!provider_) { | 150 } |
148 if (ios::GetWebControllerProviderFactory()) { | 151 |
149 provider_ = | 152 void DistillerPageIOS::OnWebStateAvailable(web::WebState* web_state) { |
150 ios::GetWebControllerProviderFactory()->CreateWebControllerProvider( | 153 web_state_ = web_state; |
151 browser_state_); | 154 web_state_observer_ = |
152 web_state_observer_.reset( | 155 base::MakeUnique<DistillerWebStateObserver>(web_state, this); |
153 new DistillerWebStateObserver(provider_->GetWebState(), this)); | 156 |
154 } | 157 if (web_state_) { |
158 // Load page using WebState. | |
159 favicon::WebFaviconDriver* favicon_driver = | |
160 favicon::WebFaviconDriver::FromWebState(web_state_); | |
161 favicon_driver->FetchFavicon(url_); | |
162 | |
163 web::NavigationManager::WebLoadParams params(url_); | |
164 web_state_->SetWebUsageEnabled(true); | |
165 web_state_->GetNavigationManager()->LoadURLWithParams(params); | |
166 // GetView is needed because the view is not created (but needed) when | |
167 // loading the page. | |
168 web_state_->GetView(); | |
169 } else { | |
170 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE); | |
155 } | 171 } |
156 | |
157 // Load page using provider. | |
158 if (provider_) | |
159 provider_->LoadURL(url_); | |
160 else | |
161 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE); | |
162 } | 172 } |
163 | 173 |
164 void DistillerPageIOS::OnLoadURLDone( | 174 void DistillerPageIOS::OnLoadURLDone( |
165 web::PageLoadCompletionStatus load_completion_status) { | 175 web::PageLoadCompletionStatus load_completion_status) { |
166 // Don't attempt to distill if the page load failed or if there is no | 176 // Don't attempt to distill if the page load failed or if there is no |
167 // provider. | 177 // WebState. |
168 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || | 178 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || |
169 !provider_) { | 179 !web_state_) { |
170 HandleJavaScriptResult(nil); | 180 HandleJavaScriptResult(nil); |
171 return; | 181 return; |
172 } | 182 } |
173 | 183 |
174 // Inject the script. | 184 // Inject the script. |
175 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); | 185 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); |
176 provider_->InjectScript(script_, ^(id result, NSError* error) { | 186 |
177 DistillerPageIOS* distiller_page = weak_this.get(); | 187 [[web_state_->GetJSInjectionReceiver() |
gambard
2016/12/12 15:04:26
Used to make sure I can use the script to parse th
| |
178 if (distiller_page) | 188 instanceOfClass:[CRWJSInjectionManager class]] |
179 distiller_page->HandleJavaScriptResult(result); | 189 executeJavaScript:base::SysUTF8ToNSString(script_) |
180 }); | 190 completionHandler:^(id result, NSError* error) { |
191 DistillerPageIOS* distiller_page = weak_this.get(); | |
192 if (distiller_page) | |
193 distiller_page->HandleJavaScriptResult(result); | |
194 }]; | |
181 } | 195 } |
182 | 196 |
183 void DistillerPageIOS::HandleJavaScriptResult(id result) { | 197 void DistillerPageIOS::HandleJavaScriptResult(id result) { |
198 web_state_pool_->ReturnWebState(web_state_); | |
199 web_state_ = nullptr; | |
184 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); | 200 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); |
185 if (result) { | 201 if (result) { |
186 resultValue = ValueResultFromScriptResult(result); | 202 resultValue = ValueResultFromScriptResult(result); |
187 } | 203 } |
188 OnDistillationDone(url_, resultValue.get()); | 204 OnDistillationDone(url_, resultValue.get()); |
189 } | 205 } |
190 | 206 |
191 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( | 207 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( |
192 id wk_result) { | 208 id wk_result) { |
193 return ::ValueResultFromScriptResult(wk_result, | 209 return ::ValueResultFromScriptResult(wk_result, |
194 kMaximumParsingRecursionDepth); | 210 kMaximumParsingRecursionDepth); |
195 } | 211 } |
196 } // namespace dom_distiller | 212 } // namespace dom_distiller |
OLD | NEW |