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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 102 |
99 // Helper class for observing the loading of URLs to distill. | 103 // Helper class for observing the loading of URLs to distill. |
100 class DistillerWebStateObserver : public web::WebStateObserver { | 104 class DistillerWebStateObserver : public web::WebStateObserver { |
101 public: | 105 public: |
102 DistillerWebStateObserver(web::WebState* web_state, | 106 DistillerWebStateObserver(web::WebState* web_state, |
103 DistillerPageIOS* distiller_page); | 107 DistillerPageIOS* distiller_page); |
104 | 108 |
105 // WebStateObserver implementation: | 109 // WebStateObserver implementation: |
106 void PageLoaded( | 110 void PageLoaded( |
107 web::PageLoadCompletionStatus load_completion_status) override; | 111 web::PageLoadCompletionStatus load_completion_status) override; |
| 112 void WebStateDestroyed() override; |
108 | 113 |
109 private: | 114 private: |
110 DistillerPageIOS* distiller_page_; // weak, owns this object. | 115 DistillerPageIOS* distiller_page_; // weak, owns this object. |
111 }; | 116 }; |
112 | 117 |
113 DistillerWebStateObserver::DistillerWebStateObserver( | 118 DistillerWebStateObserver::DistillerWebStateObserver( |
114 web::WebState* web_state, | 119 web::WebState* web_state, |
115 DistillerPageIOS* distiller_page) | 120 DistillerPageIOS* distiller_page) |
116 : web::WebStateObserver(web_state), distiller_page_(distiller_page) { | 121 : web::WebStateObserver(web_state), distiller_page_(distiller_page) { |
117 DCHECK(web_state); | 122 DCHECK(web_state); |
118 DCHECK(distiller_page_); | 123 DCHECK(distiller_page_); |
119 } | 124 } |
120 | 125 |
121 void DistillerWebStateObserver::PageLoaded( | 126 void DistillerWebStateObserver::PageLoaded( |
122 web::PageLoadCompletionStatus load_completion_status) { | 127 web::PageLoadCompletionStatus load_completion_status) { |
123 distiller_page_->OnLoadURLDone(load_completion_status); | 128 distiller_page_->OnLoadURLDone(load_completion_status); |
124 } | 129 } |
125 | 130 |
| 131 void DistillerWebStateObserver::WebStateDestroyed() { |
| 132 distiller_page_->web_state_ = nullptr; |
| 133 } |
| 134 |
126 #pragma mark - | 135 #pragma mark - |
127 | 136 |
128 DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state) | 137 DistillerPageIOS::DistillerPageIOS( |
129 : browser_state_(browser_state), weak_ptr_factory_(this) { | 138 FaviconWebStateDispatcher* web_state_dispatcher) |
130 } | 139 : web_state_dispatcher_(web_state_dispatcher), weak_ptr_factory_(this) {} |
131 | 140 |
132 DistillerPageIOS::~DistillerPageIOS() { | 141 DistillerPageIOS::~DistillerPageIOS() { |
133 } | 142 } |
134 | 143 |
135 bool DistillerPageIOS::StringifyOutput() { | 144 bool DistillerPageIOS::StringifyOutput() { |
136 return false; | 145 return false; |
137 } | 146 } |
138 | 147 |
139 void DistillerPageIOS::DistillPageImpl(const GURL& url, | 148 void DistillerPageIOS::DistillPageImpl(const GURL& url, |
140 const std::string& script) { | 149 const std::string& script) { |
141 if (!url.is_valid() || !script.length()) | 150 if (!url.is_valid() || !script.length()) |
142 return; | 151 return; |
143 url_ = url; | 152 url_ = url; |
144 script_ = script; | 153 script_ = script; |
145 | 154 |
146 // Lazily create provider. | 155 web_state_ = web_state_dispatcher_->RequestWebState(); |
147 if (!provider_) { | 156 |
148 if (ios::GetWebControllerProviderFactory()) { | 157 if (!web_state_) { |
149 provider_ = | 158 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE); |
150 ios::GetWebControllerProviderFactory()->CreateWebControllerProvider( | 159 return; |
151 browser_state_); | |
152 web_state_observer_.reset( | |
153 new DistillerWebStateObserver(provider_->GetWebState(), this)); | |
154 } | |
155 } | 160 } |
156 | 161 |
157 // Load page using provider. | 162 web_state_observer_ = |
158 if (provider_) | 163 base::MakeUnique<DistillerWebStateObserver>(web_state_, this); |
159 provider_->LoadURL(url_); | 164 |
160 else | 165 // The favicon driver needs to know which URL is currently fetched. |
161 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE); | 166 favicon::WebFaviconDriver* favicon_driver = |
| 167 favicon::WebFaviconDriver::FromWebState(web_state_); |
| 168 favicon_driver->FetchFavicon(url_); |
| 169 |
| 170 // Load page using WebState. |
| 171 web::NavigationManager::WebLoadParams params(url_); |
| 172 web_state_->SetWebUsageEnabled(true); |
| 173 web_state_->GetNavigationManager()->LoadURLWithParams(params); |
| 174 // GetView is needed because the view is not created (but needed) when |
| 175 // loading the page. |
| 176 web_state_->GetView(); |
162 } | 177 } |
163 | 178 |
164 void DistillerPageIOS::OnLoadURLDone( | 179 void DistillerPageIOS::OnLoadURLDone( |
165 web::PageLoadCompletionStatus load_completion_status) { | 180 web::PageLoadCompletionStatus load_completion_status) { |
166 // Don't attempt to distill if the page load failed or if there is no | 181 // Don't attempt to distill if the page load failed or if there is no |
167 // provider. | 182 // WebState. |
168 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || | 183 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || |
169 !provider_) { | 184 !web_state_) { |
170 HandleJavaScriptResult(nil); | 185 HandleJavaScriptResult(nil); |
171 return; | 186 return; |
172 } | 187 } |
173 | 188 |
174 // Inject the script. | 189 // Inject the script. |
175 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); | 190 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); |
176 provider_->InjectScript(script_, ^(id result, NSError* error) { | 191 |
177 DistillerPageIOS* distiller_page = weak_this.get(); | 192 [[web_state_->GetJSInjectionReceiver() |
178 if (distiller_page) | 193 instanceOfClass:[CRWJSInjectionManager class]] |
179 distiller_page->HandleJavaScriptResult(result); | 194 executeJavaScript:base::SysUTF8ToNSString(script_) |
180 }); | 195 completionHandler:^(id result, NSError* error) { |
| 196 DistillerPageIOS* distiller_page = weak_this.get(); |
| 197 if (distiller_page) |
| 198 distiller_page->HandleJavaScriptResult(result); |
| 199 }]; |
181 } | 200 } |
182 | 201 |
183 void DistillerPageIOS::HandleJavaScriptResult(id result) { | 202 void DistillerPageIOS::HandleJavaScriptResult(id result) { |
| 203 web_state_dispatcher_->ReturnWebState(web_state_); |
| 204 web_state_ = nullptr; |
184 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); | 205 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); |
185 if (result) { | 206 if (result) { |
186 resultValue = ValueResultFromScriptResult(result); | 207 resultValue = ValueResultFromScriptResult(result); |
187 } | 208 } |
188 OnDistillationDone(url_, resultValue.get()); | 209 OnDistillationDone(url_, resultValue.get()); |
189 } | 210 } |
190 | 211 |
191 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( | 212 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( |
192 id wk_result) { | 213 id wk_result) { |
193 return ::ValueResultFromScriptResult(wk_result, | 214 return ::ValueResultFromScriptResult(wk_result, |
194 kMaximumParsingRecursionDepth); | 215 kMaximumParsingRecursionDepth); |
195 } | 216 } |
196 } // namespace dom_distiller | 217 } // namespace dom_distiller |
OLD | NEW |