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

Unified Diff: third_party/WebKit/Source/modules/mime_handler_view/MimeHandlerView.cpp

Issue 2719333002: second stage
Patch Set: Need to get around resource loading without web/ Created 3 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/mime_handler_view/MimeHandlerView.cpp
diff --git a/third_party/WebKit/Source/modules/mime_handler_view/MimeHandlerView.cpp b/third_party/WebKit/Source/modules/mime_handler_view/MimeHandlerView.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..8217810cf4d7a0e743d539e8987eb371c2ca543a
--- /dev/null
+++ b/third_party/WebKit/Source/modules/mime_handler_view/MimeHandlerView.cpp
@@ -0,0 +1,114 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "modules/mime_handler_view/MimeHandlerView.h"
+
+#include "Source/core/html/HTMLPlugInElement.h"
+#include "public/platform/InterfaceProvider.h"
+#include "public/web/WebAssociatedURLLoader.h"
+#include "public/web/WebAssociatedURLLoaderClient.h"
+#include "public/web/WebFrame.h"
+#include "public/web/WebFrameClient.h"
+#include "wtf/Functional.h"
+#include "wtf/text/StringBuilder.h"
+
+namespace blink {
+
+namespace {
+using ResourceLoaderCallback =
+ WTF::Function<void(const WebString&), WTF::SameThreadAffinity>;
+
+class ResourceLoadClient : public WebAssociatedURLLoaderClient {
+ public:
+ ResourceLoadClient(std::unique_ptr<ResourceLoaderCallback> callback)
+ : m_callback(std::move(callback)) {}
+
+ ~ResourceLoadClient() {}
+
+ void didReceiveData(const char* data, int dataLength) {
+ m_data.append(data, dataLength);
+ }
+
+ void didFinishLoading(double finishtTime) {
+ (*m_callback)(m_data.toString());
+ delete this;
+ }
+
+ private:
+ WTF::StringBuilder m_data;
+ std::unique_ptr<ResourceLoaderCallback> m_callback;
+};
+}
+
+// static
+MimeHandlerView* MimeHandlerView::create(HTMLPlugInElement* htmlPlugInElement,
+ const WebURL& resourceUrl,
+ const WebString& mimeType,
+ int32_t elementInstanceId) {
+ // TODO: Use the right constants here.
+ if (mimeType == "application/pdf" || mimeType == "text/pdf")
+ return new MimeHandlerView(htmlPlugInElement, resourceUrl, mimeType, elementInstanceId);
+ return nullptr;
+}
+
+MimeHandlerView::MimeHandlerView(HTMLPlugInElement* htmlPlugInElement,
+ const WebURL& resourceUrl,
+ const WebString& mimeType,
+ int32_t elementInstanceId)
+ : PluginClient(htmlPlugInElement), m_resourceUrl(resourceUrl), m_elementInstanceId(elementInstanceId) {
+ createWebUrlRequestForResource();
+}
+
+MimeHandlerView::~MimeHandlerView() {}
+
+void MimeHandlerView::close() {
+ delete this;
+}
+
+bool MimeHandlerView::initialize() {
+ return mimeHandlerViewService();
+}
+
+v8::Local<v8::Object> MimeHandlerView::scriptableObject(v8::Isolate* isolate) {
+ // TODO: Implement postMessage here.
+ return PluginClient::scriptableObject(isolate);
+}
+
+void MimeHandlerView::createWebUrlRequestForResource() {
+ WebAssociatedURLLoaderOptions options;
+ // The embedded plugin is allowed to be cross-origin and we should always
+ // send credentials/cookies with the request.
+ options.crossOriginRequestPolicy =
+ WebAssociatedURLLoaderOptions::CrossOriginRequestPolicyAllow;
+ options.allowCredentials = true;
+ DCHECK(!m_loader);
+ m_loader.reset(WebFrame::fromFrameOwnerElement(toHTMLFrameOwnerElement(embedderFrame()->owner()))
+ ->createAssociatedURLLoader(options));
+
+ WebURLRequest request(m_resourceUrl);
+ request.setRequestContext(WebURLRequest::RequestContextObject);
+ m_loader->loadAsynchronously(
+ request,
+ new ResourceLoadClient(WTF::bind(&MimeHandlerView::createMimeHandlerView,
+ wrapPersistent(this))));
+}
+
+void MimeHandlerView::createMimeHandlerView(const WebString& viewId) {
+ mimeHandlerViewService()->CreateMimeHandlerViewGuest(
+ m_elementInstanceId, viewId, m_mimeType, m_resourceUrl);
+}
+
+LocalFrame* MimeHandlerView::embedderFrame() {
+ return htmlPlugInElement()->document().frame();
+}
+
+mojom::blink::MimeHandlerViewServicePtr
+MimeHandlerView::mimeHandlerViewService() {
+ mojom::blink::MimeHandlerViewServicePtr service;
+ embedderFrame()->interfaceProvider()->getInterface(
+ mojo::MakeRequest(&service));
+ return service;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698