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 "extensions/shell/browser/shell_extension_system.h"
6
7#include <string>
8
9#include "base/files/file_path.h"
10#include "base/files/file_util.h"
11#include "content/public/browser/browser_context.h"
12#include "content/public/browser/browser_thread.h"
13#include "content/public/browser/notification_details.h"
14#include "content/public/browser/notification_service.h"
15#include "content/public/browser/notification_source.h"
16#include "extensions/browser/api/app_runtime/app_runtime_api.h"
17#include "extensions/browser/event_router.h"
18#include "extensions/browser/extension_prefs.h"
19#include "extensions/browser/extension_registry.h"
20#include "extensions/browser/info_map.h"
21#include "extensions/browser/lazy_background_task_queue.h"
22#include "extensions/browser/notification_types.h"
23#include "extensions/browser/process_manager.h"
24#include "extensions/browser/quota_service.h"
25#include "extensions/browser/runtime_data.h"
26#include "extensions/common/file_util.h"
27
28using content::BrowserContext;
29using content::BrowserThread;
30
31namespace extensions {
32
33ShellExtensionSystem::ShellExtensionSystem(BrowserContext* browser_context)
34    : browser_context_(browser_context) {
35}
36
37ShellExtensionSystem::~ShellExtensionSystem() {
38}
39
40bool ShellExtensionSystem::LoadApp(const base::FilePath& app_dir) {
41  // app_shell only supports unpacked extensions.
42  // NOTE: If you add packed extension support consider removing the flag
43  // FOLLOW_SYMLINKS_ANYWHERE below. Packed extensions should not have symlinks.
44  CHECK(base::DirectoryExists(app_dir)) << app_dir.AsUTF8Unsafe();
45  int load_flags = Extension::FOLLOW_SYMLINKS_ANYWHERE;
46  std::string load_error;
47  extension_ = file_util::LoadExtension(
48      app_dir, Manifest::COMMAND_LINE, load_flags, &load_error);
49  if (!extension_.get()) {
50    LOG(ERROR) << "Loading extension at " << app_dir.value()
51               << " failed with: " << load_error;
52    return false;
53  }
54  app_id_ = extension_->id();
55
56  // TODO(jamescook): We may want to do some of these things here:
57  // * Create a PermissionsUpdater.
58  // * Call PermissionsUpdater::GrantActivePermissions().
59  // * Call ExtensionService::SatisfyImports().
60  // * Call ExtensionPrefs::OnExtensionInstalled().
61  // * Send NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED.
62
63  ExtensionRegistry::Get(browser_context_)->AddEnabled(extension_);
64
65  RegisterExtensionWithRequestContexts(extension_.get());
66
67  content::NotificationService::current()->Notify(
68      extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
69      content::Source<BrowserContext>(browser_context_),
70      content::Details<const Extension>(extension_.get()));
71
72  // Inform the rest of the extensions system to start.
73  ready_.Signal();
74  content::NotificationService::current()->Notify(
75      extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
76      content::Source<BrowserContext>(browser_context_),
77      content::NotificationService::NoDetails());
78  return true;
79}
80
81void ShellExtensionSystem::LaunchApp() {
82  // Send the onLaunched event.
83  DCHECK(extension_.get());
84  AppRuntimeEventRouter::DispatchOnLaunchedEvent(browser_context_,
85                                                 extension_.get());
86}
87
88void ShellExtensionSystem::Shutdown() {
89}
90
91void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled) {
92  runtime_data_.reset(
93      new RuntimeData(ExtensionRegistry::Get(browser_context_)));
94  lazy_background_task_queue_.reset(
95      new LazyBackgroundTaskQueue(browser_context_));
96  event_router_.reset(
97      new EventRouter(browser_context_, ExtensionPrefs::Get(browser_context_)));
98  process_manager_.reset(ProcessManager::Create(browser_context_));
99  quota_service_.reset(new QuotaService);
100}
101
102ExtensionService* ShellExtensionSystem::extension_service() {
103  return NULL;
104}
105
106RuntimeData* ShellExtensionSystem::runtime_data() {
107  return runtime_data_.get();
108}
109
110ManagementPolicy* ShellExtensionSystem::management_policy() {
111  return NULL;
112}
113
114SharedUserScriptMaster* ShellExtensionSystem::shared_user_script_master() {
115  return NULL;
116}
117
118ProcessManager* ShellExtensionSystem::process_manager() {
119  return process_manager_.get();
120}
121
122StateStore* ShellExtensionSystem::state_store() {
123  return NULL;
124}
125
126StateStore* ShellExtensionSystem::rules_store() {
127  return NULL;
128}
129
130InfoMap* ShellExtensionSystem::info_map() {
131  if (!info_map_.get())
132    info_map_ = new InfoMap;
133  return info_map_.get();
134}
135
136LazyBackgroundTaskQueue* ShellExtensionSystem::lazy_background_task_queue() {
137  return lazy_background_task_queue_.get();
138}
139
140EventRouter* ShellExtensionSystem::event_router() {
141  return event_router_.get();
142}
143
144WarningService* ShellExtensionSystem::warning_service() {
145  return NULL;
146}
147
148Blacklist* ShellExtensionSystem::blacklist() {
149  return NULL;
150}
151
152ErrorConsole* ShellExtensionSystem::error_console() {
153  return NULL;
154}
155
156InstallVerifier* ShellExtensionSystem::install_verifier() {
157  return NULL;
158}
159
160QuotaService* ShellExtensionSystem::quota_service() {
161  return quota_service_.get();
162}
163
164void ShellExtensionSystem::RegisterExtensionWithRequestContexts(
165    const Extension* extension) {
166  BrowserThread::PostTask(BrowserThread::IO,
167                          FROM_HERE,
168                          base::Bind(&InfoMap::AddExtension,
169                                     info_map(),
170                                     make_scoped_refptr(extension),
171                                     base::Time::Now(),
172                                     false,
173                                     false));
174}
175
176void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
177    const std::string& extension_id,
178    const UnloadedExtensionInfo::Reason reason) {
179}
180
181const OneShotEvent& ShellExtensionSystem::ready() const {
182  return ready_;
183}
184
185ContentVerifier* ShellExtensionSystem::content_verifier() {
186  return NULL;
187}
188
189scoped_ptr<ExtensionSet> ShellExtensionSystem::GetDependentExtensions(
190    const Extension* extension) {
191  scoped_ptr<ExtensionSet> empty(new ExtensionSet());
192  return empty.PassAs<ExtensionSet>();
193}
194
195DeclarativeUserScriptMaster*
196ShellExtensionSystem::GetDeclarativeUserScriptMasterByExtension(
197    const ExtensionId& extension_id) {
198  return NULL;
199}
200
201}  // namespace extensions
202