Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2039)

Unified Diff: chrome/browser/web_applications/app_shim_host_controller.mm

Issue 12623005: [mac] App shims (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..75db51f294830325254efe3730b7bf8bba562221
--- /dev/null
+++ b/chrome/browser/web_applications/app_shim_host_controller.mm
@@ -0,0 +1,210 @@
+// 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:
+ bool OnMessageReceived(const IPC::Message& message);
jeremy 2013/03/11 06:49:59 * virtual * OVERRIDE * Inherited from comment.
jeremya 2013/03/11 08:42:26 Done.
+
+ // 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& app_id, const std::string& profile_dir);
+
+ // 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)
+
jeremy 2013/03/11 06:49:59 Remove NL?
jeremya 2013/03/11 08:42:26 Done.
+ 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;
+ }
+ ProfileManager* profileManager = g_browser_process->profile_manager();
+ base::FilePath path(profile_dir);
+ path = profileManager->user_data_dir().Append(path);
+ 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,
jeremy 2013/03/11 06:49:59 nit: one arg per line.
jeremya 2013/03/11 08:42:26 Done.
+ 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, AsWeakPtr()));
+}
+
+AppShimHostController::~AppShimHostController() {
+ delete factory_;
+}
+
+void AppShimHostController::InitOnFileThread() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ base::FilePath user_data_dir;
+ PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
jeremy 2013/03/11 06:49:59 What if this fails?
jeremya 2013/03/11 08:42:26 Then the controller can't run... not sure if we sh
+ 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::ListenOnIO, AsWeakPtr()));
+}
+
+void AppShimHostController::ListenOnIO() {
+ factory_->Listen();
+}
+
+void AppShimHostController::OnClientConnected(
+ const IPC::ChannelHandle& handle) {
+ // called on IO thread
+ BrowserThread::PostTask(
+ BrowserThread::UI, FROM_HERE,
+ base::Bind(&CreateAppShimHost, handle));
+}

Powered by Google App Engine
This is Rietveld 408576698