blob: bb58c1a8553d7cc87d7488cef11f1a7306336d74 [file] [log] [blame]
// 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.
#include <map>
#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "mojo/application_manager/application_loader.h"
#include "mojo/application_manager/application_manager_export.h"
#include "mojo/public/interfaces/application/service_provider.mojom.h"
#include "url/gurl.h"
namespace mojo {
// API for testing.
explicit TestAPI(ApplicationManager* manager);
// Returns true if the shared instance has been created.
static bool HasCreatedInstance();
// Returns true if there is a ShellImpl for this URL.
bool HasFactoryForURL(const GURL& url) const;
ApplicationManager* manager_;
// Interface class for debugging only.
class Interceptor {
virtual ~Interceptor() {}
// Called when ApplicationManager::Connect is called.
virtual ServiceProviderPtr OnConnectToClient(
const GURL& url,
ServiceProviderPtr service_provider) = 0;
// Returns a shared instance, creating it if necessary.
static ApplicationManager* GetInstance();
// Loads a service if necessary and establishes a new client connection.
void ConnectToApplication(const GURL& application_url,
const GURL& requestor_url,
ServiceProviderPtr service_provider);
template <typename Interface>
inline void ConnectToService(const GURL& application_url,
InterfacePtr<Interface>* ptr) {
ScopedMessagePipeHandle service_handle =
ConnectToServiceByName(application_url, Interface::Name_);
ScopedMessagePipeHandle ConnectToServiceByName(
const GURL& application_url,
const std::string& interface_name);
// Sets the default Loader to be used if not overridden by SetLoaderForURL()
// or SetLoaderForScheme().
void set_default_loader(scoped_ptr<ApplicationLoader> loader) {
default_loader_ = loader.Pass();
// Sets a Loader to be used for a specific url.
void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url);
// Sets a Loader to be used for a specific url scheme.
void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader,
const std::string& scheme);
// Allows to interpose a debugger to service connections.
void SetInterceptor(Interceptor* interceptor);
// Destroys all Shell-ends of connections established with Applications.
// Applications connected by this ApplicationManager will observe pipe errors
// and have a chance to shutdown.
void TerminateShellConnections();
struct ContentHandlerConnection;
class LoadCallbacksImpl;
class ShellImpl;
typedef std::map<std::string, ApplicationLoader*> SchemeToLoaderMap;
typedef std::map<GURL, ApplicationLoader*> URLToLoaderMap;
typedef std::map<GURL, ShellImpl*> URLToShellImplMap;
typedef std::map<GURL, ContentHandlerConnection*> URLToContentHandlerMap;
void ConnectToClient(ShellImpl* shell_impl,
const GURL& url,
const GURL& requestor_url,
ServiceProviderPtr service_provider);
void RegisterLoadedApplication(const GURL& service_url,
const GURL& requestor_url,
ServiceProviderPtr service_provider,
ScopedMessagePipeHandle* shell_handle);
void LoadWithContentHandler(const GURL& content_url,
const GURL& requestor_url,
const GURL& content_handler_url,
URLResponsePtr content,
ServiceProviderPtr service_provider);
// Returns the Loader to use for a url (using default if not overridden.)
// The preference is to use a loader that's been specified for an url first,
// then one that's been specified for a scheme, then the default.
ApplicationLoader* GetLoaderForURL(const GURL& url);
// Removes a ShellImpl when it encounters an error.
void OnShellImplError(ShellImpl* shell_impl);
// Loader management.
URLToLoaderMap url_to_loader_;
SchemeToLoaderMap scheme_to_loader_;
scoped_ptr<ApplicationLoader> default_loader_;
Interceptor* interceptor_;
URLToShellImplMap url_to_shell_impl_;
URLToContentHandlerMap url_to_content_handler_;
base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
} // namespace mojo