OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 #import <Cocoa/Cocoa.h> |
| 6 |
| 7 #include "chrome/browser/web_applications/app_shim_host_controller.h" |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/path_service.h" |
| 13 #include "base/threading/non_thread_safe.h" |
| 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/extensions/extension_host.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/extensions/extension_system.h" |
| 18 #include "chrome/browser/extensions/shell_window_registry.h" |
| 19 #include "chrome/browser/profiles/profile_manager.h" |
| 20 #include "chrome/browser/ui/extensions/application_launch.h" |
| 21 #include "chrome/browser/ui/extensions/shell_window.h" |
| 22 #include "chrome/common/app_shim_messages.h" |
| 23 #include "chrome/common/chrome_paths.h" |
| 24 #include "chrome/common/extensions/extension_constants.h" |
| 25 #include "chrome/common/mac/app_mode_common.h" |
| 26 #include "content/public/browser/notification_observer.h" |
| 27 #include "content/public/browser/notification_registrar.h" |
| 28 #include "ipc/ipc_channel_proxy.h" |
| 29 #include "ipc/ipc_listener.h" |
| 30 #include "ipc/ipc_message.h" |
| 31 |
| 32 //----------------------------------------------------------------------------- |
| 33 |
| 34 // This is the counterpart to AppShimController in |
| 35 // chrome/app/chrome_main_app_mode_mac.mm. The AppShimHost owns itself, and is |
| 36 // destroyed when the app it corresponds to is closed or when the channel |
| 37 // connected to the app shim is closed. |
| 38 class AppShimHost : public IPC::Listener, |
| 39 public content::NotificationObserver, |
| 40 public base::NonThreadSafe { |
| 41 public: |
| 42 explicit AppShimHost(const IPC::ChannelHandle& channel); |
| 43 virtual ~AppShimHost(); |
| 44 |
| 45 private: |
| 46 // IPC::Listener implementation. |
| 47 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 48 |
| 49 // The app shim process is requesting that an app be launched. Once it has |
| 50 // done so the profile and app id are stored, and all future messages from |
| 51 // the app shim relate to the app it launched. It is an error for the app |
| 52 // shim to send multiple launch messages. |
| 53 void OnLaunchApp(std::string profile, std::string app_id); |
| 54 |
| 55 bool LaunchAppImpl(const std::string& profile_dir, const std::string& app_id); |
| 56 |
| 57 // When the app closes, a signal is sent (by way of closing the channel) to |
| 58 // the app shim to tell it to quit. |
| 59 virtual void Observe(int type, |
| 60 const content::NotificationSource& source, |
| 61 const content::NotificationDetails& details) OVERRIDE; |
| 62 |
| 63 // Closes the channel and destroys the AppShimHost. |
| 64 void Close(); |
| 65 |
| 66 IPC::ChannelProxy* channel_; |
| 67 std::string app_id_; |
| 68 Profile* profile_; |
| 69 content::NotificationRegistrar registrar_; |
| 70 }; |
| 71 |
| 72 AppShimHost::AppShimHost(const IPC::ChannelHandle& handle) |
| 73 : channel_(NULL), profile_(NULL) { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 75 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_SERVER, this, |
| 76 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
| 77 } |
| 78 |
| 79 AppShimHost::~AppShimHost() { |
| 80 DCHECK(CalledOnValidThread()); |
| 81 delete channel_; |
| 82 } |
| 83 |
| 84 bool AppShimHost::OnMessageReceived(const IPC::Message& message) { |
| 85 DCHECK(CalledOnValidThread()); |
| 86 bool handled = true; |
| 87 IPC_BEGIN_MESSAGE_MAP(AppShimHost, message) |
| 88 IPC_MESSAGE_HANDLER(AppShimHostMsg_LaunchApp, OnLaunchApp) |
| 89 IPC_MESSAGE_UNHANDLED(handled = false) |
| 90 IPC_END_MESSAGE_MAP() |
| 91 |
| 92 return handled; |
| 93 } |
| 94 |
| 95 void AppShimHost::OnLaunchApp(std::string profile_dir, std::string app_id) { |
| 96 DCHECK(CalledOnValidThread()); |
| 97 if (!channel_) |
| 98 return; |
| 99 bool success = LaunchAppImpl(profile_dir, app_id); |
| 100 channel_->Send(new AppShimMsg_LaunchApp_Done(success)); |
| 101 } |
| 102 |
| 103 bool AppShimHost::LaunchAppImpl(const std::string& profile_dir, |
| 104 const std::string& app_id) { |
| 105 DCHECK(CalledOnValidThread()); |
| 106 if (profile_) { |
| 107 // Only one app launch message per channel. |
| 108 return false; |
| 109 } |
| 110 if (!extensions::Extension::IdIsValid(app_id)) { |
| 111 LOG(ERROR) << "Bad app ID from app shim launch message."; |
| 112 return false; |
| 113 } |
| 114 ProfileManager* profileManager = g_browser_process->profile_manager(); |
| 115 base::FilePath path(profile_dir); |
| 116 path = profileManager->user_data_dir().Append(path); |
| 117 ProfileInfoCache& cache = profileManager->GetProfileInfoCache(); |
| 118 if (cache.GetIndexOfProfileWithPath(path) == std::string::npos) { |
| 119 LOG(ERROR) << "Bad profile directory from app shim launch message."; |
| 120 return false; |
| 121 } |
| 122 profile_ = profileManager->GetProfile(path); |
| 123 if (!profile_) { |
| 124 LOG(ERROR) << "Unable to locate a suitable profile for profile directory '" |
| 125 << profile_dir << "' while trying to load app with id '" |
| 126 << app_id << "'."; |
| 127 return false; |
| 128 } |
| 129 extensions::ExtensionSystem* extension_system = |
| 130 extensions::ExtensionSystem::Get(profile_); |
| 131 ExtensionServiceInterface* extension_service = |
| 132 extension_system->extension_service(); |
| 133 const extensions::Extension* extension = |
| 134 extension_service->GetExtensionById( |
| 135 app_id, false); |
| 136 if (!extension) { |
| 137 LOG(ERROR) << "Shortcut attempted to launch nonexistent app with id '" |
| 138 << app_id << "'."; |
| 139 return false; |
| 140 } |
| 141 app_id_ = app_id; |
| 142 chrome::AppLaunchParams params(profile_, |
| 143 extension, |
| 144 extension_misc::LAUNCH_NONE, |
| 145 NEW_WINDOW); |
| 146 chrome::OpenApplication(params); |
| 147 |
| 148 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 149 content::Source<Profile>(profile_)); |
| 150 return true; |
| 151 } |
| 152 |
| 153 void AppShimHost::Observe(int type, |
| 154 const content::NotificationSource& source, |
| 155 const content::NotificationDetails& details) { |
| 156 DCHECK(CalledOnValidThread()); |
| 157 DCHECK(content::Source<Profile>(source).ptr() == profile_); |
| 158 switch (type) { |
| 159 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
| 160 extensions::ExtensionHost* extension_host = |
| 161 content::Details<extensions::ExtensionHost>(details).ptr(); |
| 162 if (app_id_ == extension_host->extension_id()) |
| 163 Close(); |
| 164 break; |
| 165 } |
| 166 default: |
| 167 NOTREACHED() << "Unexpected notification sent."; |
| 168 break; |
| 169 } |
| 170 } |
| 171 |
| 172 void AppShimHost::Close() { |
| 173 DCHECK(CalledOnValidThread()); |
| 174 delete this; |
| 175 } |
| 176 |
| 177 //----------------------------------------------------------------------------- |
| 178 |
| 179 namespace { |
| 180 |
| 181 void CreateAppShimHost(const IPC::ChannelHandle& handle) { |
| 182 new AppShimHost(handle); |
| 183 } |
| 184 |
| 185 } // namespace |
| 186 |
| 187 AppShimHostController::AppShimHostController() : factory_(NULL) { |
| 188 BrowserThread::PostTask( |
| 189 BrowserThread::FILE, FROM_HERE, |
| 190 base::Bind(&AppShimHostController::InitOnFileThread, |
| 191 base::Unretained(this))); |
| 192 } |
| 193 |
| 194 AppShimHostController::~AppShimHostController() { |
| 195 delete factory_; |
| 196 } |
| 197 |
| 198 void AppShimHostController::InitOnFileThread() { |
| 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 200 base::FilePath user_data_dir; |
| 201 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) { |
| 202 LOG(ERROR) << "Couldn't get user data directory while creating App Shim " |
| 203 << "Host controller."; |
| 204 return; |
| 205 } |
| 206 base::FilePath socket_path = |
| 207 user_data_dir.Append(app_mode::kAppShimSocketName); |
| 208 factory_ = new IPC::ChannelFactory(socket_path, this); |
| 209 BrowserThread::PostTask( |
| 210 BrowserThread::IO, FROM_HERE, |
| 211 base::Bind(&AppShimHostController::ListenOnIOThread, |
| 212 base::Unretained(this))); |
| 213 } |
| 214 |
| 215 void AppShimHostController::ListenOnIOThread() { |
| 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 217 factory_->Listen(); |
| 218 } |
| 219 |
| 220 void AppShimHostController::OnClientConnected( |
| 221 const IPC::ChannelHandle& handle) { |
| 222 // called on IO thread |
| 223 BrowserThread::PostTask( |
| 224 BrowserThread::UI, FROM_HERE, |
| 225 base::Bind(&CreateAppShimHost, handle)); |
| 226 } |
| 227 |
| 228 void AppShimHostController::OnListenError() { |
| 229 // TODO(jeremya): set a timeout and attempt to reconstruct the channel. |
| 230 } |
OLD | NEW |