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

Side by Side Diff: mojo/examples/html_viewer/html_document_view.cc

Issue 393983004: Move mojo/examples/html_viewer to mojo/services/html_viewer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sort Created 6 years, 5 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 | Annotate | Revision Log
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 "mojo/examples/html_viewer/html_document_view.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "mojo/examples/html_viewer/blink_input_events_type_converters.h"
12 #include "mojo/examples/html_viewer/webstoragenamespace_impl.h"
13 #include "mojo/examples/html_viewer/weburlloader_impl.h"
14 #include "mojo/services/public/cpp/view_manager/node.h"
15 #include "mojo/services/public/cpp/view_manager/view.h"
16 #include "mojo/services/public/cpp/view_manager/view_observer.h"
17 #include "skia/ext/refptr.h"
18 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
19 #include "third_party/WebKit/public/web/WebDocument.h"
20 #include "third_party/WebKit/public/web/WebElement.h"
21 #include "third_party/WebKit/public/web/WebInputEvent.h"
22 #include "third_party/WebKit/public/web/WebLocalFrame.h"
23 #include "third_party/WebKit/public/web/WebScriptSource.h"
24 #include "third_party/WebKit/public/web/WebSettings.h"
25 #include "third_party/WebKit/public/web/WebView.h"
26 #include "third_party/skia/include/core/SkCanvas.h"
27 #include "third_party/skia/include/core/SkColor.h"
28 #include "third_party/skia/include/core/SkDevice.h"
29
30 namespace mojo {
31 namespace examples {
32 namespace {
33
34 void ConfigureSettings(blink::WebSettings* settings) {
35 settings->setAcceleratedCompositingEnabled(false);
36 settings->setDefaultFixedFontSize(13);
37 settings->setDefaultFontSize(16);
38 settings->setLoadsImagesAutomatically(true);
39 settings->setJavaScriptEnabled(true);
40 }
41
42 navigation::Target WebNavigationPolicyToNavigationTarget(
43 blink::WebNavigationPolicy policy) {
44 switch (policy) {
45 case blink::WebNavigationPolicyCurrentTab:
46 return navigation::SOURCE_NODE;
47 case blink::WebNavigationPolicyNewBackgroundTab:
48 case blink::WebNavigationPolicyNewForegroundTab:
49 case blink::WebNavigationPolicyNewWindow:
50 case blink::WebNavigationPolicyNewPopup:
51 return navigation::NEW_NODE;
52 default:
53 return navigation::DEFAULT;
54 }
55 }
56
57 bool CanNavigateLocally(blink::WebFrame* frame,
58 const blink::WebURLRequest& request) {
59 // For now, we just load child frames locally.
60 // TODO(aa): In the future, this should use embedding to connect to a
61 // different instance of Blink if the frame is cross-origin.
62 if (frame->parent())
63 return true;
64
65 // If we have extraData() it means we already have the url response
66 // (presumably because we are being called via Navigate()). In that case we
67 // can go ahead and navigate locally.
68 if (request.extraData())
69 return true;
70
71 // Otherwise we don't know if we're the right app to handle this request. Ask
72 // host to do the navigation for us.
73 return false;
74 }
75
76 } // namespace
77
78 HTMLDocumentView::HTMLDocumentView(ServiceProvider* service_provider,
79 view_manager::ViewManager* view_manager)
80 : view_manager_(view_manager),
81 view_(view_manager::View::Create(view_manager_)),
82 web_view_(NULL),
83 repaint_pending_(false),
84 navigator_host_(service_provider),
85 weak_factory_(this) {
86 view_->AddObserver(this);
87 }
88
89 HTMLDocumentView::~HTMLDocumentView() {
90 view_->RemoveObserver(this);
91
92 if (web_view_)
93 web_view_->close();
94 }
95
96 void HTMLDocumentView::AttachToNode(view_manager::Node* node) {
97 node->SetActiveView(view_);
98 view_->SetColor(SK_ColorCYAN); // Dummy background color.
99
100 web_view_ = blink::WebView::create(this);
101 ConfigureSettings(web_view_->settings());
102 web_view_->setMainFrame(blink::WebLocalFrame::create(this));
103
104 web_view_->resize(gfx::Size(node->bounds().size()));
105 }
106
107 void HTMLDocumentView::Load(URLResponsePtr response) {
108 DCHECK(web_view_);
109
110 GURL url(response->url);
111
112 WebURLRequestExtraData* extra_data = new WebURLRequestExtraData;
113 extra_data->synthetic_response = response.Pass();
114
115 blink::WebURLRequest web_request;
116 web_request.initialize();
117 web_request.setURL(url);
118 web_request.setExtraData(extra_data);
119
120 web_view_->mainFrame()->loadRequest(web_request);
121 }
122
123 blink::WebStorageNamespace* HTMLDocumentView::createSessionStorageNamespace() {
124 return new WebStorageNamespaceImpl();
125 }
126
127 void HTMLDocumentView::didInvalidateRect(const blink::WebRect& rect) {
128 if (!repaint_pending_) {
129 repaint_pending_ = true;
130 base::ThreadTaskRunnerHandle::Get()->PostTask(
131 FROM_HERE,
132 base::Bind(&HTMLDocumentView::Repaint, weak_factory_.GetWeakPtr()));
133 }
134 }
135
136 bool HTMLDocumentView::allowsBrokenNullLayerTreeView() const {
137 // TODO(darin): Switch to using compositor bindings.
138 //
139 // NOTE: Note to Blink maintainers, feel free to just break this code if it
140 // is the last using compositor bindings and you want to delete the old path.
141 //
142 return true;
143 }
144
145 blink::WebNavigationPolicy HTMLDocumentView::decidePolicyForNavigation(
146 blink::WebLocalFrame* frame, blink::WebDataSource::ExtraData* data,
147 const blink::WebURLRequest& request, blink::WebNavigationType nav_type,
148 blink::WebNavigationPolicy default_policy, bool is_redirect) {
149 if (CanNavigateLocally(frame, request))
150 return default_policy;
151
152 navigation::NavigationDetailsPtr nav_details(
153 navigation::NavigationDetails::New());
154 nav_details->url = request.url().string().utf8();
155 navigator_host_->RequestNavigate(
156 view_->node()->id(),
157 WebNavigationPolicyToNavigationTarget(default_policy),
158 nav_details.Pass());
159
160 return blink::WebNavigationPolicyIgnore;
161 }
162
163 void HTMLDocumentView::didAddMessageToConsole(
164 const blink::WebConsoleMessage& message,
165 const blink::WebString& source_name,
166 unsigned source_line,
167 const blink::WebString& stack_trace) {
168 printf("### console: %s\n", std::string(message.text.utf8()).c_str());
169 }
170
171 void HTMLDocumentView::didNavigateWithinPage(
172 blink::WebLocalFrame* frame, const blink::WebHistoryItem& history_item,
173 blink::WebHistoryCommitType commit_type) {
174 navigator_host_->DidNavigateLocally(view_->node()->id(),
175 history_item.urlString().utf8());
176 }
177
178 void HTMLDocumentView::OnViewInputEvent(view_manager::View* view,
179 const EventPtr& event) {
180 scoped_ptr<blink::WebInputEvent> web_event =
181 TypeConverter<EventPtr, scoped_ptr<blink::WebInputEvent> >::ConvertTo(
182 event);
183 if (web_event)
184 web_view_->handleInputEvent(*web_event);
185 }
186
187 void HTMLDocumentView::Repaint() {
188 repaint_pending_ = false;
189
190 web_view_->animate(0.0);
191 web_view_->layout();
192
193 int width = web_view_->size().width;
194 int height = web_view_->size().height;
195
196 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(SkCanvas::NewRaster(
197 SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType)));
198
199 web_view_->paint(canvas.get(), gfx::Rect(0, 0, width, height));
200
201 view_->SetContents(canvas->getDevice()->accessBitmap(false));
202 }
203
204 } // namespace examples
205 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698