blob: a4aee60d862cedb4944f6bb6f9856f8aafcaa06a [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 "extensions/shell/browser/shell_extensions_browser_client.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/pref_service_factory.h"
#include "base/prefs/testing_pref_store.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/generated_api_registration.h"
#include "extensions/browser/app_sorting.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_function_registry.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/null_app_sorting.h"
#include "extensions/browser/url_request_util.h"
#include "extensions/shell/browser/shell_extension_host_delegate.h"
#include "extensions/shell/browser/shell_extension_system_factory.h"
#include "extensions/shell/browser/shell_runtime_api_delegate.h"
using content::BrowserContext;
using content::BrowserThread;
namespace extensions {
namespace {
// See chrome::RegisterProfilePrefs() in chrome/browser/prefs/browser_prefs.cc
void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) {
ExtensionPrefs::RegisterProfilePrefs(registry);
}
} // namespace
ShellExtensionsBrowserClient::ShellExtensionsBrowserClient(
BrowserContext* context)
: browser_context_(context), api_client_(new ExtensionsAPIClient) {
// Set up the preferences service.
base::PrefServiceFactory factory;
factory.set_user_prefs(new TestingPrefStore);
factory.set_extension_prefs(new TestingPrefStore);
// app_shell should not require syncable preferences, but for now we need to
// recycle some of the RegisterProfilePrefs() code in Chrome.
// TODO(jamescook): Convert this to PrefRegistrySimple.
user_prefs::PrefRegistrySyncable* pref_registry =
new user_prefs::PrefRegistrySyncable;
// Prefs should be registered before the PrefService is created.
RegisterPrefs(pref_registry);
prefs_ = factory.Create(pref_registry).Pass();
user_prefs::UserPrefs::Set(browser_context_, prefs_.get());
}
ShellExtensionsBrowserClient::~ShellExtensionsBrowserClient() {
}
bool ShellExtensionsBrowserClient::IsShuttingDown() {
return false;
}
bool ShellExtensionsBrowserClient::AreExtensionsDisabled(
const base::CommandLine& command_line,
BrowserContext* context) {
return false;
}
bool ShellExtensionsBrowserClient::IsValidContext(BrowserContext* context) {
return context == browser_context_;
}
bool ShellExtensionsBrowserClient::IsSameContext(BrowserContext* first,
BrowserContext* second) {
return first == second;
}
bool ShellExtensionsBrowserClient::HasOffTheRecordContext(
BrowserContext* context) {
return false;
}
BrowserContext* ShellExtensionsBrowserClient::GetOffTheRecordContext(
BrowserContext* context) {
// app_shell only supports a single context.
return NULL;
}
BrowserContext* ShellExtensionsBrowserClient::GetOriginalContext(
BrowserContext* context) {
return context;
}
bool ShellExtensionsBrowserClient::IsGuestSession(
BrowserContext* context) const {
return false;
}
bool ShellExtensionsBrowserClient::IsExtensionIncognitoEnabled(
const std::string& extension_id,
content::BrowserContext* context) const {
return false;
}
bool ShellExtensionsBrowserClient::CanExtensionCrossIncognito(
const Extension* extension,
content::BrowserContext* context) const {
return false;
}
net::URLRequestJob*
ShellExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const base::FilePath& directory_path,
const std::string& content_security_policy,
bool send_cors_header) {
return NULL;
}
bool ShellExtensionsBrowserClient::AllowCrossRendererResourceLoad(
net::URLRequest* request,
bool is_incognito,
const Extension* extension,
InfoMap* extension_info_map) {
bool allowed = false;
if (url_request_util::AllowCrossRendererResourceLoad(
request, is_incognito, extension, extension_info_map, &allowed)) {
return allowed;
}
// Couldn't determine if resource is allowed. Block the load.
return false;
}
PrefService* ShellExtensionsBrowserClient::GetPrefServiceForContext(
BrowserContext* context) {
return prefs_.get();
}
void ShellExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
content::BrowserContext* context,
std::vector<ExtensionPrefsObserver*>* observers) const {
}
ProcessManagerDelegate*
ShellExtensionsBrowserClient::GetProcessManagerDelegate() const {
return NULL;
}
scoped_ptr<ExtensionHostDelegate>
ShellExtensionsBrowserClient::CreateExtensionHostDelegate() {
return scoped_ptr<ExtensionHostDelegate>(new ShellExtensionHostDelegate);
}
bool ShellExtensionsBrowserClient::DidVersionUpdate(BrowserContext* context) {
// TODO(jamescook): We might want to tell extensions when app_shell updates.
return false;
}
void ShellExtensionsBrowserClient::PermitExternalProtocolHandler() {
}
scoped_ptr<AppSorting> ShellExtensionsBrowserClient::CreateAppSorting() {
return scoped_ptr<AppSorting>(new NullAppSorting);
}
bool ShellExtensionsBrowserClient::IsRunningInForcedAppMode() {
return false;
}
ApiActivityMonitor* ShellExtensionsBrowserClient::GetApiActivityMonitor(
BrowserContext* context) {
// app_shell doesn't monitor API function calls or events.
return NULL;
}
ExtensionSystemProvider*
ShellExtensionsBrowserClient::GetExtensionSystemFactory() {
return ShellExtensionSystemFactory::GetInstance();
}
void ShellExtensionsBrowserClient::RegisterExtensionFunctions(
ExtensionFunctionRegistry* registry) const {
// Register core extension-system APIs.
core_api::GeneratedFunctionRegistry::RegisterAll(registry);
}
scoped_ptr<RuntimeAPIDelegate>
ShellExtensionsBrowserClient::CreateRuntimeAPIDelegate(
content::BrowserContext* context) const {
return scoped_ptr<RuntimeAPIDelegate>(new ShellRuntimeAPIDelegate());
}
ComponentExtensionResourceManager*
ShellExtensionsBrowserClient::GetComponentExtensionResourceManager() {
return NULL;
}
void ShellExtensionsBrowserClient::BroadcastEventToRenderers(
const std::string& event_name,
scoped_ptr<base::ListValue> args) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&ShellExtensionsBrowserClient::BroadcastEventToRenderers,
base::Unretained(this),
event_name,
base::Passed(&args)));
return;
}
scoped_ptr<Event> event(new Event(event_name, args.Pass()));
EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
}
net::NetLog* ShellExtensionsBrowserClient::GetNetLog() {
return NULL;
}
} // namespace extensions