Chromium Code Reviews| 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& app_id, const std::string& profile_dir); | |
|
palmer
2013/03/11 19:28:18
The app_id and profile[_dir] arguments are in reve
jeremya
2013/03/12 03:45:36
Haha, oops. The names were only wrong in the decla
| |
| 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 | |
|
palmer
2013/03/11 19:28:18
NIT: Remove this blank line? Or is channel_ differ
jeremya
2013/03/12 03:45:36
Done.
| |
| 68 std::string app_id_; | |
| 69 Profile* profile_; | |
| 70 content::NotificationRegistrar registrar_; | |
| 71 }; | |
| 72 | |
| 73 AppShimHost::AppShimHost(const IPC::ChannelHandle& handle) | |
| 74 : channel_(NULL), profile_(NULL) { | |
| 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 76 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_SERVER, this, | |
| 77 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | |
| 78 } | |
| 79 | |
| 80 AppShimHost::~AppShimHost() { | |
| 81 DCHECK(CalledOnValidThread()); | |
| 82 delete channel_; | |
| 83 } | |
| 84 | |
| 85 bool AppShimHost::OnMessageReceived(const IPC::Message& message) { | |
| 86 DCHECK(CalledOnValidThread()); | |
| 87 bool handled = true; | |
| 88 IPC_BEGIN_MESSAGE_MAP(AppShimHost, message) | |
| 89 IPC_MESSAGE_HANDLER(AppShimHostMsg_LaunchApp, OnLaunchApp) | |
| 90 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 91 IPC_END_MESSAGE_MAP() | |
| 92 | |
| 93 return handled; | |
| 94 } | |
| 95 | |
| 96 void AppShimHost::OnLaunchApp(std::string profile_dir, std::string app_id) { | |
| 97 DCHECK(CalledOnValidThread()); | |
| 98 if (!channel_) | |
| 99 return; | |
| 100 bool success = LaunchAppImpl(profile_dir, app_id); | |
| 101 channel_->Send(new AppShimMsg_LaunchApp_Done(success)); | |
| 102 } | |
| 103 | |
| 104 bool AppShimHost::LaunchAppImpl(const std::string& profile_dir, | |
| 105 const std::string& app_id) { | |
| 106 DCHECK(CalledOnValidThread()); | |
| 107 if (profile_) { | |
| 108 // Only one app launch message per channel. | |
| 109 return false; | |
| 110 } | |
| 111 if (profile_dir.length() == 0) { | |
|
jeremy
2013/03/11 09:32:04
// profile_dir and app_id are received over IPC so
palmer
2013/03/11 19:28:18
This isn't enough validation. This could be an arb
jeremya
2013/03/12 03:45:36
I see, I was hoping that ProfileManager::GetProfil
| |
| 112 LOG(ERROR) << "Bad profile directory from app shim launch message."; | |
| 113 return false; | |
| 114 } | |
| 115 if (!extensions::Extension::IdIsValid(app_id)) { | |
| 116 LOG(ERROR) << "Bad app ID from app shim launch message."; | |
| 117 return false; | |
| 118 } | |
| 119 ProfileManager* profileManager = g_browser_process->profile_manager(); | |
| 120 base::FilePath path(profile_dir); | |
| 121 path = profileManager->user_data_dir().Append(path); | |
|
palmer
2013/03/11 19:28:18
Elsewhere, you use the expression PathService::Get
jeremya
2013/03/12 03:45:36
Unless PathService::Get(chrome::DIR_USER_DATA, &us
| |
| 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::ListenOnIO, base::Unretained(this))); | |
| 212 } | |
| 213 | |
| 214 void AppShimHostController::ListenOnIO() { | |
| 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 216 factory_->Listen(); | |
| 217 } | |
| 218 | |
| 219 void AppShimHostController::OnClientConnected( | |
| 220 const IPC::ChannelHandle& handle) { | |
| 221 // called on IO thread | |
| 222 BrowserThread::PostTask( | |
| 223 BrowserThread::UI, FROM_HERE, | |
| 224 base::Bind(&CreateAppShimHost, handle)); | |
| 225 } | |
| 226 | |
| 227 void AppShimHostController::OnListenError() { | |
| 228 // TODO(jeremya): set a timeout and attempt to reconstruct the channel. | |
| 229 } | |
| OLD | NEW |