Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(505)

Side by Side Diff: components/dom_distiller/ios/distiller_page_ios.mm

Issue 936843002: Upstream iOS implementation of dom_distiller (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@web_controller_provider
Patch Set: Add missing (optional) semi-colon in the javascript Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/dom_distiller/ios/distiller_page_ios.h"
6
7 #import <UIKit/UIKit.h>
8
9 #include "base/json/json_reader.h"
10 #include "base/logging.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/values.h"
13 #include "grit/components_resources.h"
14 #include "ios/public/provider/web/web_controller_provider.h"
15 #include "ios/web/public/browser_state.h"
16
17 namespace dom_distiller {
18
19 // Helper class for observing the loading of URLs to distill.
20 class DistillerWebStateObserver : public web::WebStateObserver {
21 public:
22 DistillerWebStateObserver(web::WebState* web_state,
23 DistillerPageIOS* distiller_page);
24
25 // WebStateObserver implementation:
26 void PageLoaded(
27 web::PageLoadCompletionStatus load_completion_status) override;
28
29 private:
30 DistillerPageIOS* distiller_page_; // weak, owns this object.
31 };
32
33 DistillerWebStateObserver::DistillerWebStateObserver(
34 web::WebState* web_state,
35 DistillerPageIOS* distiller_page)
36 : web::WebStateObserver(web_state), distiller_page_(distiller_page) {
37 DCHECK(web_state);
38 DCHECK(distiller_page_);
39 }
40
41 void DistillerWebStateObserver::PageLoaded(
42 web::PageLoadCompletionStatus load_completion_status) {
43 distiller_page_->OnLoadURLDone(load_completion_status);
44 }
45
46 #pragma mark -
47
48 DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state)
49 : browser_state_(browser_state), weak_ptr_factory_(this) {
50 }
51
52 DistillerPageIOS::~DistillerPageIOS() {
53 }
54
55 void DistillerPageIOS::DistillPageImpl(const GURL& url,
56 const std::string& script) {
57 if (!url.is_valid() || !script.length())
58 return;
59 url_ = url;
60 script_ = script;
61
62 // Lazily create provider.
63 if (!provider_) {
64 if (ios::GetWebControllerProviderFactory()) {
65 provider_ =
66 ios::GetWebControllerProviderFactory()->CreateWebControllerProvider(
67 browser_state_);
68 web_state_observer_.reset(
69 new DistillerWebStateObserver(provider_->GetWebState(), this));
70 }
71 }
72
73 // Load page using provider.
74 if (provider_) {
75 provider_->LoadURL(url_);
76 } else {
77 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE);
78 }
noyau (Ping after 24h) 2015/02/19 09:15:43 Either put the brackets on one liner if, or don't.
sdefresne 2015/02/19 10:50:13 Done.
79 }
80
81 void DistillerPageIOS::OnLoadURLDone(
82 web::PageLoadCompletionStatus load_completion_status) {
83 // Don't attempt to distill if the page load failed or if there is no
84 // provider.
85 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE ||
86 !provider_) {
87 HandleJavaScriptResultString(@"");
88 return;
89 }
90
91 // Inject the script.
92 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr();
93 provider_->InjectScript(script_, ^(NSString* string, NSError* error) {
94 weak_this->HandleJavaScriptResultString(string);
droger 2015/02/19 09:38:49 You need to return early if weak_this is null, or
sdefresne 2015/02/19 10:50:13 Good catch, done.
95 });
96 }
97
98 void DistillerPageIOS::HandleJavaScriptResultString(NSString* result) {
99 scoped_ptr<base::Value> resultValue(base::Value::CreateNullValue());
100 if (result.length) {
101 scoped_ptr<base::DictionaryValue> dictionaryValue(
102 static_cast<base::DictionaryValue*>(
103 base::JSONReader::Read(base::SysNSStringToUTF8(result))));
104 if (dictionaryValue)
105 resultValue = dictionaryValue.Pass();
106 }
107 OnDistillationDone(url_, resultValue.get());
108 }
109
110 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698