| Index: chrome/browser/web_applications/app_shim_host_controller.mm
|
| diff --git a/chrome/browser/web_applications/app_shim_host_controller.mm b/chrome/browser/web_applications/app_shim_host_controller.mm
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c580602091d07ba58e1be2b01e1c9d08b4d37b02
|
| --- /dev/null
|
| +++ b/chrome/browser/web_applications/app_shim_host_controller.mm
|
| @@ -0,0 +1,230 @@
|
| +// Copyright 2013 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.
|
| +
|
| +#import <Cocoa/Cocoa.h>
|
| +
|
| +#include "chrome/browser/web_applications/app_shim_host_controller.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/files/file_path.h"
|
| +#include "base/logging.h"
|
| +#include "base/path_service.h"
|
| +#include "base/threading/non_thread_safe.h"
|
| +#include "chrome/browser/browser_process.h"
|
| +#include "chrome/browser/extensions/extension_host.h"
|
| +#include "chrome/browser/extensions/extension_service.h"
|
| +#include "chrome/browser/extensions/extension_system.h"
|
| +#include "chrome/browser/extensions/shell_window_registry.h"
|
| +#include "chrome/browser/profiles/profile_manager.h"
|
| +#include "chrome/browser/ui/extensions/application_launch.h"
|
| +#include "chrome/browser/ui/extensions/shell_window.h"
|
| +#include "chrome/common/app_shim_messages.h"
|
| +#include "chrome/common/chrome_paths.h"
|
| +#include "chrome/common/extensions/extension_constants.h"
|
| +#include "chrome/common/mac/app_mode_common.h"
|
| +#include "content/public/browser/notification_observer.h"
|
| +#include "content/public/browser/notification_registrar.h"
|
| +#include "ipc/ipc_channel_proxy.h"
|
| +#include "ipc/ipc_listener.h"
|
| +#include "ipc/ipc_message.h"
|
| +
|
| +//-----------------------------------------------------------------------------
|
| +
|
| +// This is the counterpart to AppShimController in
|
| +// chrome/app/chrome_main_app_mode_mac.mm. The AppShimHost owns itself, and is
|
| +// destroyed when the app it corresponds to is closed or when the channel
|
| +// connected to the app shim is closed.
|
| +class AppShimHost : public IPC::Listener,
|
| + public content::NotificationObserver,
|
| + public base::NonThreadSafe {
|
| + public:
|
| + explicit AppShimHost(const IPC::ChannelHandle& channel);
|
| + virtual ~AppShimHost();
|
| +
|
| + private:
|
| + // IPC::Listener implementation.
|
| + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
|
| +
|
| + // The app shim process is requesting that an app be launched. Once it has
|
| + // done so the profile and app id are stored, and all future messages from
|
| + // the app shim relate to the app it launched. It is an error for the app
|
| + // shim to send multiple launch messages.
|
| + void OnLaunchApp(std::string profile, std::string app_id);
|
| +
|
| + bool LaunchAppImpl(const std::string& profile_dir, const std::string& app_id);
|
| +
|
| + // When the app closes, a signal is sent (by way of closing the channel) to
|
| + // the app shim to tell it to quit.
|
| + virtual void Observe(int type,
|
| + const content::NotificationSource& source,
|
| + const content::NotificationDetails& details) OVERRIDE;
|
| +
|
| + // Closes the channel and destroys the AppShimHost.
|
| + void Close();
|
| +
|
| + IPC::ChannelProxy* channel_;
|
| + std::string app_id_;
|
| + Profile* profile_;
|
| + content::NotificationRegistrar registrar_;
|
| +};
|
| +
|
| +AppShimHost::AppShimHost(const IPC::ChannelHandle& handle)
|
| + : channel_(NULL), profile_(NULL) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| + channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_SERVER, this,
|
| + BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
|
| +}
|
| +
|
| +AppShimHost::~AppShimHost() {
|
| + DCHECK(CalledOnValidThread());
|
| + delete channel_;
|
| +}
|
| +
|
| +bool AppShimHost::OnMessageReceived(const IPC::Message& message) {
|
| + DCHECK(CalledOnValidThread());
|
| + bool handled = true;
|
| + IPC_BEGIN_MESSAGE_MAP(AppShimHost, message)
|
| + IPC_MESSAGE_HANDLER(AppShimHostMsg_LaunchApp, OnLaunchApp)
|
| + IPC_MESSAGE_UNHANDLED(handled = false)
|
| + IPC_END_MESSAGE_MAP()
|
| +
|
| + return handled;
|
| +}
|
| +
|
| +void AppShimHost::OnLaunchApp(std::string profile_dir, std::string app_id) {
|
| + DCHECK(CalledOnValidThread());
|
| + if (!channel_)
|
| + return;
|
| + bool success = LaunchAppImpl(profile_dir, app_id);
|
| + channel_->Send(new AppShimMsg_LaunchApp_Done(success));
|
| +}
|
| +
|
| +bool AppShimHost::LaunchAppImpl(const std::string& profile_dir,
|
| + const std::string& app_id) {
|
| + DCHECK(CalledOnValidThread());
|
| + if (profile_) {
|
| + // Only one app launch message per channel.
|
| + return false;
|
| + }
|
| + if (!extensions::Extension::IdIsValid(app_id)) {
|
| + LOG(ERROR) << "Bad app ID from app shim launch message.";
|
| + return false;
|
| + }
|
| + ProfileManager* profileManager = g_browser_process->profile_manager();
|
| + base::FilePath path(profile_dir);
|
| + path = profileManager->user_data_dir().Append(path);
|
| + ProfileInfoCache& cache = profileManager->GetProfileInfoCache();
|
| + if (cache.GetIndexOfProfileWithPath(path) == std::string::npos) {
|
| + LOG(ERROR) << "Bad profile directory from app shim launch message.";
|
| + return false;
|
| + }
|
| + profile_ = profileManager->GetProfile(path);
|
| + if (!profile_) {
|
| + LOG(ERROR) << "Unable to locate a suitable profile for profile directory '"
|
| + << profile_dir << "' while trying to load app with id '"
|
| + << app_id << "'.";
|
| + return false;
|
| + }
|
| + extensions::ExtensionSystem* extension_system =
|
| + extensions::ExtensionSystem::Get(profile_);
|
| + ExtensionServiceInterface* extension_service =
|
| + extension_system->extension_service();
|
| + const extensions::Extension* extension =
|
| + extension_service->GetExtensionById(
|
| + app_id, false);
|
| + if (!extension) {
|
| + LOG(ERROR) << "Shortcut attempted to launch nonexistent app with id '"
|
| + << app_id << "'.";
|
| + return false;
|
| + }
|
| + app_id_ = app_id;
|
| + chrome::AppLaunchParams params(profile_,
|
| + extension,
|
| + extension_misc::LAUNCH_NONE,
|
| + NEW_WINDOW);
|
| + chrome::OpenApplication(params);
|
| +
|
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
|
| + content::Source<Profile>(profile_));
|
| + return true;
|
| +}
|
| +
|
| +void AppShimHost::Observe(int type,
|
| + const content::NotificationSource& source,
|
| + const content::NotificationDetails& details) {
|
| + DCHECK(CalledOnValidThread());
|
| + DCHECK(content::Source<Profile>(source).ptr() == profile_);
|
| + switch (type) {
|
| + case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
|
| + extensions::ExtensionHost* extension_host =
|
| + content::Details<extensions::ExtensionHost>(details).ptr();
|
| + if (app_id_ == extension_host->extension_id())
|
| + Close();
|
| + break;
|
| + }
|
| + default:
|
| + NOTREACHED() << "Unexpected notification sent.";
|
| + break;
|
| + }
|
| +}
|
| +
|
| +void AppShimHost::Close() {
|
| + DCHECK(CalledOnValidThread());
|
| + delete this;
|
| +}
|
| +
|
| +//-----------------------------------------------------------------------------
|
| +
|
| +namespace {
|
| +
|
| +void CreateAppShimHost(const IPC::ChannelHandle& handle) {
|
| + new AppShimHost(handle);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +AppShimHostController::AppShimHostController() : factory_(NULL) {
|
| + BrowserThread::PostTask(
|
| + BrowserThread::FILE, FROM_HERE,
|
| + base::Bind(&AppShimHostController::InitOnFileThread,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +AppShimHostController::~AppShimHostController() {
|
| + delete factory_;
|
| +}
|
| +
|
| +void AppShimHostController::InitOnFileThread() {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
|
| + base::FilePath user_data_dir;
|
| + if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) {
|
| + LOG(ERROR) << "Couldn't get user data directory while creating App Shim "
|
| + << "Host controller.";
|
| + return;
|
| + }
|
| + base::FilePath socket_path =
|
| + user_data_dir.Append(app_mode::kAppShimSocketName);
|
| + factory_ = new IPC::ChannelFactory(socket_path, this);
|
| + BrowserThread::PostTask(
|
| + BrowserThread::IO, FROM_HERE,
|
| + base::Bind(&AppShimHostController::ListenOnIOThread,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void AppShimHostController::ListenOnIOThread() {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| + factory_->Listen();
|
| +}
|
| +
|
| +void AppShimHostController::OnClientConnected(
|
| + const IPC::ChannelHandle& handle) {
|
| + // called on IO thread
|
| + BrowserThread::PostTask(
|
| + BrowserThread::UI, FROM_HERE,
|
| + base::Bind(&CreateAppShimHost, handle));
|
| +}
|
| +
|
| +void AppShimHostController::OnListenError() {
|
| + // TODO(jeremya): set a timeout and attempt to reconstruct the channel.
|
| +}
|
|
|