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 #include "ios/web/public/web_state/web_state.h" | |
Eugene But (OOO till 7-30)
2016/12/08 15:34:53
s/include/import
kkhorimoto
2016/12/08 22:15:24
More of a comment for Eugene than Gauthier, but it
Eugene But (OOO till 7-30)
2016/12/08 22:45:34
Header has Objective-C code: https://cs.chromium.o
gambard
2016/12/12 15:04:26
Done.
| |
22 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | |
23 #import "ios/web/public/web_state/js/crw_js_injection_manager.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(web::WebState* web_state) |
129 : browser_state_(browser_state), weak_ptr_factory_(this) { | 133 : web_state_(web_state), weak_ptr_factory_(this) { |
134 web_state_observer_.reset(new DistillerWebStateObserver(web_state_, this)); | |
kkhorimoto
2016/12/08 22:15:24
DCHECK(web_state) before this line?
| |
130 } | 135 } |
131 | 136 |
132 DistillerPageIOS::~DistillerPageIOS() { | 137 DistillerPageIOS::~DistillerPageIOS() { |
133 } | 138 } |
134 | 139 |
135 bool DistillerPageIOS::StringifyOutput() { | 140 bool DistillerPageIOS::StringifyOutput() { |
136 return false; | 141 return false; |
137 } | 142 } |
138 | 143 |
139 void DistillerPageIOS::DistillPageImpl(const GURL& url, | 144 void DistillerPageIOS::DistillPageImpl(const GURL& url, |
140 const std::string& script) { | 145 const std::string& script) { |
141 if (!url.is_valid() || !script.length()) | 146 if (!url.is_valid() || !script.length()) |
142 return; | 147 return; |
143 url_ = url; | 148 url_ = url; |
144 script_ = script; | 149 script_ = script; |
145 | 150 |
146 // Lazily create provider. | 151 // Load page using WebState. |
147 if (!provider_) { | 152 if (web_state_) { |
148 if (ios::GetWebControllerProviderFactory()) { | 153 favicon::WebFaviconDriver* favicon_driver = |
149 provider_ = | 154 favicon::WebFaviconDriver::FromWebState(web_state_); |
150 ios::GetWebControllerProviderFactory()->CreateWebControllerProvider( | 155 favicon_driver->FetchFavicon(url); |
Olivier
2016/12/08 18:28:02
favicon_driver->AddObserver(this);
| |
151 browser_state_); | |
152 web_state_observer_.reset( | |
153 new DistillerWebStateObserver(provider_->GetWebState(), this)); | |
154 } | |
155 } | |
156 | 156 |
157 // Load page using provider. | 157 web::NavigationManager::WebLoadParams params(url); |
158 if (provider_) | 158 web_state_->SetWebUsageEnabled(true); |
159 provider_->LoadURL(url_); | 159 web_state_->GetNavigationManager()->LoadURLWithParams(params); |
160 else | 160 web_state_->GetView(); |
Eugene But (OOO till 7-30)
2016/12/08 15:34:53
nit: It worth commenting why you have to call this
gambard
2016/12/12 15:04:26
Done.
| |
161 } else | |
Eugene But (OOO till 7-30)
2016/12/08 15:34:53
nit: Needs braces, because previous block has them
gambard
2016/12/12 15:04:26
Done.
| |
161 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE); | 162 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE); |
162 } | 163 } |
163 | 164 |
164 void DistillerPageIOS::OnLoadURLDone( | 165 void DistillerPageIOS::OnLoadURLDone( |
165 web::PageLoadCompletionStatus load_completion_status) { | 166 web::PageLoadCompletionStatus load_completion_status) { |
166 // Don't attempt to distill if the page load failed or if there is no | 167 // Don't attempt to distill if the page load failed or if there is no |
167 // provider. | 168 // WebState. |
168 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || | 169 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || |
169 !provider_) { | 170 !web_state_) { |
170 HandleJavaScriptResult(nil); | 171 HandleJavaScriptResult(nil); |
171 return; | 172 return; |
172 } | 173 } |
173 | 174 |
174 // Inject the script. | 175 // Inject the script. |
175 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); | 176 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); |
176 provider_->InjectScript(script_, ^(id result, NSError* error) { | 177 |
177 DistillerPageIOS* distiller_page = weak_this.get(); | 178 [[web_state_->GetJSInjectionReceiver() |
Eugene But (OOO till 7-30)
2016/12/08 15:34:53
Ideally you want to use WebState::ExecuteJavaScrip
gambard
2016/12/12 15:04:26
I can use it because WebState::ExecuteJavaScript r
| |
178 if (distiller_page) | 179 instanceOfClass:[CRWJSInjectionManager class]] |
179 distiller_page->HandleJavaScriptResult(result); | 180 executeJavaScript:base::SysUTF8ToNSString(script_) |
180 }); | 181 completionHandler:^(id result, NSError* error) { |
182 DistillerPageIOS* distiller_page = weak_this.get(); | |
183 if (distiller_page) | |
184 distiller_page->HandleJavaScriptResult(result); | |
185 }]; | |
181 } | 186 } |
182 | 187 |
183 void DistillerPageIOS::HandleJavaScriptResult(id result) { | 188 void DistillerPageIOS::HandleJavaScriptResult(id result) { |
184 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); | 189 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); |
185 if (result) { | 190 if (result) { |
186 resultValue = ValueResultFromScriptResult(result); | 191 resultValue = ValueResultFromScriptResult(result); |
187 } | 192 } |
188 OnDistillationDone(url_, resultValue.get()); | 193 OnDistillationDone(url_, resultValue.get()); |
Olivier
2016/12/08 18:28:02
Wait for OnFaviconUpdated to be called.
| |
189 } | 194 } |
190 | 195 |
191 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( | 196 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( |
192 id wk_result) { | 197 id wk_result) { |
193 return ::ValueResultFromScriptResult(wk_result, | 198 return ::ValueResultFromScriptResult(wk_result, |
194 kMaximumParsingRecursionDepth); | 199 kMaximumParsingRecursionDepth); |
195 } | 200 } |
196 } // namespace dom_distiller | 201 } // namespace dom_distiller |
OLD | NEW |