mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
Add PDF extension support (issue #1565)
This commit is contained in:
21
libcef/browser/extensions/api/api_registration.gyp
Normal file
21
libcef/browser/extensions/api/api_registration.gyp
Normal file
@@ -0,0 +1,21 @@
|
||||
# Copyright 2014 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.
|
||||
|
||||
{
|
||||
'targets': [
|
||||
{
|
||||
'target_name': 'cef_api_registration',
|
||||
'type': 'static_library',
|
||||
# TODO(jschuh): http://crbug.com/167187 size_t -> int
|
||||
'msvs_disabled_warnings': [ 4267 ],
|
||||
'includes': [
|
||||
'../../../../../build/json_schema_bundle_registration_compile.gypi',
|
||||
'../../../common/extensions/api/schemas.gypi',
|
||||
],
|
||||
'dependencies': [
|
||||
'../../../common/extensions/api/api.gyp:cef_api',
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
@@ -0,0 +1,175 @@
|
||||
// Copyright (c) 2012 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 "libcef/browser/extensions/api/streams_private/streams_private_api.h"
|
||||
|
||||
#include "base/lazy_instance.h"
|
||||
#include "base/values.h"
|
||||
#include "cef/libcef/common/extensions/api/streams_private.h"
|
||||
#include "content/public/browser/stream_handle.h"
|
||||
#include "content/public/browser/stream_info.h"
|
||||
#include "extensions/browser/event_router.h"
|
||||
#include "extensions/browser/extension_function_registry.h"
|
||||
#include "extensions/browser/extension_registry.h"
|
||||
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_stream_manager.h"
|
||||
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
|
||||
#include "extensions/common/manifest_handlers/mime_types_handler.h"
|
||||
#include "net/http/http_response_headers.h"
|
||||
|
||||
namespace extensions {
|
||||
namespace {
|
||||
|
||||
void CreateResponseHeadersDictionary(const net::HttpResponseHeaders* headers,
|
||||
base::DictionaryValue* result) {
|
||||
if (!headers)
|
||||
return;
|
||||
|
||||
void* iter = NULL;
|
||||
std::string header_name;
|
||||
std::string header_value;
|
||||
while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) {
|
||||
base::Value* existing_value = NULL;
|
||||
if (result->Get(header_name, &existing_value)) {
|
||||
base::StringValue* existing_string_value =
|
||||
static_cast<base::StringValue*>(existing_value);
|
||||
existing_string_value->GetString()->append(", ").append(header_value);
|
||||
} else {
|
||||
result->SetString(header_name, header_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace streams_private = api::streams_private;
|
||||
|
||||
// static
|
||||
StreamsPrivateAPI* StreamsPrivateAPI::Get(content::BrowserContext* context) {
|
||||
return GetFactoryInstance()->Get(context);
|
||||
}
|
||||
|
||||
StreamsPrivateAPI::StreamsPrivateAPI(content::BrowserContext* context)
|
||||
: browser_context_(context),
|
||||
extension_registry_observer_(this),
|
||||
weak_ptr_factory_(this) {
|
||||
extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
|
||||
}
|
||||
|
||||
StreamsPrivateAPI::~StreamsPrivateAPI() {
|
||||
}
|
||||
|
||||
void StreamsPrivateAPI::ExecuteMimeTypeHandler(
|
||||
const std::string& extension_id,
|
||||
int tab_id,
|
||||
scoped_ptr<content::StreamInfo> stream,
|
||||
const std::string& view_id,
|
||||
int64 expected_content_size,
|
||||
bool embedded,
|
||||
int render_process_id,
|
||||
int render_frame_id) {
|
||||
const Extension* extension = ExtensionRegistry::Get(browser_context_)
|
||||
->enabled_extensions()
|
||||
.GetByID(extension_id);
|
||||
if (!extension)
|
||||
return;
|
||||
|
||||
MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
|
||||
// If the mime handler uses MimeHandlerViewGuest, the MimeHandlerViewGuest
|
||||
// will take ownership of the stream. Otherwise, store the stream handle in
|
||||
// |streams_| and fire an event notifying the extension.
|
||||
if (!handler->handler_url().empty()) {
|
||||
GURL handler_url(Extension::GetBaseURLFromExtensionId(extension_id).spec() +
|
||||
handler->handler_url());
|
||||
scoped_ptr<StreamContainer> stream_container(new StreamContainer(
|
||||
stream.Pass(), tab_id, embedded, handler_url, extension_id));
|
||||
MimeHandlerStreamManager::Get(browser_context_)
|
||||
->AddStream(view_id, stream_container.Pass(), render_process_id,
|
||||
render_frame_id);
|
||||
return;
|
||||
}
|
||||
// Create the event's arguments value.
|
||||
streams_private::StreamInfo info;
|
||||
info.mime_type = stream->mime_type;
|
||||
info.original_url = stream->original_url.spec();
|
||||
info.stream_url = stream->handle->GetURL().spec();
|
||||
info.tab_id = tab_id;
|
||||
info.embedded = embedded;
|
||||
|
||||
if (!view_id.empty()) {
|
||||
info.view_id.reset(new std::string(view_id));
|
||||
}
|
||||
|
||||
int size = -1;
|
||||
if (expected_content_size <= INT_MAX)
|
||||
size = expected_content_size;
|
||||
info.expected_content_size = size;
|
||||
|
||||
CreateResponseHeadersDictionary(stream->response_headers.get(),
|
||||
&info.response_headers.additional_properties);
|
||||
|
||||
scoped_ptr<Event> event(
|
||||
new Event(streams_private::OnExecuteMimeTypeHandler::kEventName,
|
||||
streams_private::OnExecuteMimeTypeHandler::Create(info)));
|
||||
|
||||
EventRouter::Get(browser_context_)
|
||||
->DispatchEventToExtension(extension_id, event.Pass());
|
||||
|
||||
GURL url = stream->handle->GetURL();
|
||||
streams_[extension_id][url] = make_linked_ptr(stream->handle.release());
|
||||
}
|
||||
|
||||
void StreamsPrivateAPI::AbortStream(const std::string& extension_id,
|
||||
const GURL& stream_url,
|
||||
const base::Closure& callback) {
|
||||
StreamMap::iterator extension_it = streams_.find(extension_id);
|
||||
if (extension_it == streams_.end()) {
|
||||
callback.Run();
|
||||
return;
|
||||
}
|
||||
|
||||
StreamMap::mapped_type* url_map = &extension_it->second;
|
||||
StreamMap::mapped_type::iterator url_it = url_map->find(stream_url);
|
||||
if (url_it == url_map->end()) {
|
||||
callback.Run();
|
||||
return;
|
||||
}
|
||||
|
||||
url_it->second->AddCloseListener(callback);
|
||||
url_map->erase(url_it);
|
||||
}
|
||||
|
||||
void StreamsPrivateAPI::OnExtensionUnloaded(
|
||||
content::BrowserContext* browser_context,
|
||||
const Extension* extension,
|
||||
UnloadedExtensionInfo::Reason reason) {
|
||||
streams_.erase(extension->id());
|
||||
}
|
||||
|
||||
StreamsPrivateAbortFunction::StreamsPrivateAbortFunction() {
|
||||
}
|
||||
|
||||
ExtensionFunction::ResponseAction StreamsPrivateAbortFunction::Run() {
|
||||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
||||
EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &stream_url_));
|
||||
StreamsPrivateAPI::Get(browser_context())->AbortStream(
|
||||
extension_id(), GURL(stream_url_), base::Bind(
|
||||
&StreamsPrivateAbortFunction::OnClose, this));
|
||||
return RespondLater();
|
||||
}
|
||||
|
||||
void StreamsPrivateAbortFunction::OnClose() {
|
||||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
||||
Respond(NoArguments());
|
||||
}
|
||||
|
||||
static base::LazyInstance<BrowserContextKeyedAPIFactory<StreamsPrivateAPI> >
|
||||
g_factory = LAZY_INSTANCE_INITIALIZER;
|
||||
|
||||
// static
|
||||
BrowserContextKeyedAPIFactory<StreamsPrivateAPI>*
|
||||
StreamsPrivateAPI::GetFactoryInstance() {
|
||||
return g_factory.Pointer();
|
||||
}
|
||||
|
||||
} // namespace extensions
|
@@ -0,0 +1,104 @@
|
||||
// Copyright (c) 2012 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.
|
||||
|
||||
#ifndef CEF_LIBCEF_BROWSER_EXTENSIONS_API_STREAMS_PRIVATE_STREAMS_PRIVATE_API_H_
|
||||
#define CEF_LIBCEF_BROWSER_EXTENSIONS_API_STREAMS_PRIVATE_STREAMS_PRIVATE_API_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "base/scoped_observer.h"
|
||||
#include "extensions/browser/browser_context_keyed_api_factory.h"
|
||||
#include "extensions/browser/extension_function.h"
|
||||
#include "extensions/browser/extension_registry_observer.h"
|
||||
|
||||
namespace content {
|
||||
class BrowserContext;
|
||||
class StreamHandle;
|
||||
struct StreamInfo;
|
||||
}
|
||||
|
||||
namespace extensions {
|
||||
class ExtensionRegistry;
|
||||
|
||||
class StreamsPrivateAPI : public BrowserContextKeyedAPI,
|
||||
public ExtensionRegistryObserver {
|
||||
public:
|
||||
// Convenience method to get the StreamsPrivateAPI for a BrowserContext.
|
||||
static StreamsPrivateAPI* Get(content::BrowserContext* context);
|
||||
|
||||
explicit StreamsPrivateAPI(content::BrowserContext* context);
|
||||
~StreamsPrivateAPI() override;
|
||||
|
||||
// Send the onExecuteMimeTypeHandler event to |extension_id|.
|
||||
// |tab_id| is used to determine the tabId where the document is being
|
||||
// opened. The data for the document will be readable from |stream|, and
|
||||
// should be |expected_content_size| bytes long. If the viewer is being opened
|
||||
// in a BrowserPlugin, specify a non-empty |view_id| of the plugin. |embedded|
|
||||
// should be set to whether the document is embedded within another document.
|
||||
void ExecuteMimeTypeHandler(const std::string& extension_id,
|
||||
int tab_id,
|
||||
scoped_ptr<content::StreamInfo> stream,
|
||||
const std::string& view_id,
|
||||
int64 expected_content_size,
|
||||
bool embedded,
|
||||
int render_process_id,
|
||||
int render_frame_id);
|
||||
|
||||
void AbortStream(const std::string& extension_id,
|
||||
const GURL& stream_url,
|
||||
const base::Closure& callback);
|
||||
|
||||
// BrowserContextKeyedAPI implementation.
|
||||
static BrowserContextKeyedAPIFactory<StreamsPrivateAPI>* GetFactoryInstance();
|
||||
|
||||
private:
|
||||
friend class BrowserContextKeyedAPIFactory<StreamsPrivateAPI>;
|
||||
typedef std::map<std::string,
|
||||
std::map<GURL,
|
||||
linked_ptr<content::StreamHandle> > > StreamMap;
|
||||
|
||||
// ExtensionRegistryObserver implementation.
|
||||
void OnExtensionUnloaded(content::BrowserContext* browser_context,
|
||||
const Extension* extension,
|
||||
UnloadedExtensionInfo::Reason reason) override;
|
||||
|
||||
// BrowserContextKeyedAPI implementation.
|
||||
static const char* service_name() {
|
||||
return "StreamsPrivateAPI";
|
||||
}
|
||||
static const bool kServiceIsNULLWhileTesting = true;
|
||||
static const bool kServiceRedirectedInIncognito = true;
|
||||
|
||||
content::BrowserContext* const browser_context_;
|
||||
StreamMap streams_;
|
||||
|
||||
// Listen to extension unloaded notifications.
|
||||
ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
|
||||
extension_registry_observer_;
|
||||
|
||||
base::WeakPtrFactory<StreamsPrivateAPI> weak_ptr_factory_;
|
||||
|
||||
};
|
||||
|
||||
class StreamsPrivateAbortFunction : public UIThreadExtensionFunction {
|
||||
public:
|
||||
StreamsPrivateAbortFunction();
|
||||
DECLARE_EXTENSION_FUNCTION("streamsPrivate.abort", STREAMSPRIVATE_ABORT)
|
||||
|
||||
protected:
|
||||
~StreamsPrivateAbortFunction() override {}
|
||||
|
||||
// ExtensionFunction:
|
||||
ExtensionFunction::ResponseAction Run() override;
|
||||
|
||||
private:
|
||||
void OnClose();
|
||||
|
||||
std::string stream_url_;
|
||||
};
|
||||
|
||||
} // namespace extensions
|
||||
|
||||
#endif // CEF_LIBCEF_BROWSER_EXTENSIONS_API_STREAMS_PRIVATE_STREAMS_PRIVATE_API_H_
|
Reference in New Issue
Block a user