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

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 blank lines 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 "ios/public/provider/web/web_controller_provider.h"
14 #include "ios/web/public/browser_state.h"
15
16 namespace dom_distiller {
17
18 // Helper class for observing the loading of URLs to distill.
19 class DistillerWebStateObserver : public web::WebStateObserver {
20 public:
21 DistillerWebStateObserver(web::WebState* web_state,
22 DistillerPageIOS* distiller_page);
23
24 // WebStateObserver implementation:
25 void PageLoaded(
26 web::PageLoadCompletionStatus load_completion_status) override;
27
28 private:
29 DistillerPageIOS* distiller_page_; // weak, owns this object.
30 };
31
32 DistillerWebStateObserver::DistillerWebStateObserver(
33 web::WebState* web_state,
34 DistillerPageIOS* distiller_page)
35 : web::WebStateObserver(web_state), distiller_page_(distiller_page) {
36 DCHECK(web_state);
37 DCHECK(distiller_page_);
38 }
39
40 void DistillerWebStateObserver::PageLoaded(
41 web::PageLoadCompletionStatus load_completion_status) {
42 distiller_page_->OnLoadURLDone(load_completion_status);
43 }
44
45 #pragma mark -
46
47 DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state)
48 : browser_state_(browser_state), weak_ptr_factory_(this) {
49 }
50
51 DistillerPageIOS::~DistillerPageIOS() {
52 }
53
54 void DistillerPageIOS::DistillPageImpl(const GURL& url,
55 const std::string& script) {
56 if (!url.is_valid() || !script.length())
57 return;
58 url_ = url;
59 script_ = script;
60
61 // Lazily create provider.
62 if (!provider_) {
63 if (ios::GetWebControllerProviderFactory()) {
64 provider_ =
65 ios::GetWebControllerProviderFactory()->CreateWebControllerProvider(
66 browser_state_);
67 web_state_observer_.reset(
68 new DistillerWebStateObserver(provider_->GetWebState(), this));
69 }
70 }
71
72 // Load page using provider.
73 if (provider_)
74 provider_->LoadURL(url_);
75 else
76 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE);
77 }
78
79 void DistillerPageIOS::OnLoadURLDone(
80 web::PageLoadCompletionStatus load_completion_status) {
81 // Don't attempt to distill if the page load failed or if there is no
82 // provider.
83 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE ||
84 !provider_) {
85 HandleJavaScriptResultString(@"");
86 return;
87 }
88
89 // Inject the script.
90 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr();
91 provider_->InjectScript(script_, ^(NSString* string, NSError* error) {
92 DistillerPageIOS* distiller_page = weak_this.get();
93 if (distiller_page)
94 distiller_page->HandleJavaScriptResultString(string);
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::Value> dictionaryValue(
102 base::JSONReader::Read(base::SysNSStringToUTF8(result)));
103 if (dictionaryValue &&
104 dictionaryValue->IsType(base::Value::TYPE_DICTIONARY)) {
105 resultValue = dictionaryValue.Pass();
106 }
107 }
108 OnDistillationDone(url_, resultValue.get());
109 }
110
111 } // namespace dom_distiller
OLDNEW
« no previous file with comments | « components/dom_distiller/ios/distiller_page_ios.h ('k') | components/dom_distiller/ios/javascript/domdistiller.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698