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 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.
| |
| 47 | |
| 48 // The app shim process is requesting that an app be launched. Once it has | |
| 49 // done so the profile and app id are stored, and all future messages from | |
| 50 // the app shim relate to the app it launched. It is an error for the app | |
| 51 // shim to send multiple launch messages. | |
| 52 void OnLaunchApp(std::string profile, std::string app_id); | |
| 53 | |
| 54 bool LaunchAppImpl(const std::string& app_id, const std::string& profile_dir); | |
| 55 | |
| 56 // When the app closes, a signal is sent (by way of closing the channel) to | |
| 57 // the app shim to tell it to quit. | |
| 58 virtual void Observe(int type, | |
| 59 const content::NotificationSource& source, | |
| 60 const content::NotificationDetails& details) OVERRIDE; | |
| 61 | |
| 62 // Closes the channel and destroys the AppShimHost. | |
| 63 void Close(); | |
| 64 | |
| 65 IPC::ChannelProxy* channel_; | |
| 66 | |
| 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 | |
|
jeremy
2013/03/11 06:49:59
Remove NL?
jeremya
2013/03/11 08:42:26
Done.
| |
| 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 ProfileManager* profileManager = g_browser_process->profile_manager(); | |
| 112 base::FilePath path(profile_dir); | |
| 113 path = profileManager->user_data_dir().Append(path); | |
| 114 profile_ = profileManager->GetProfile(path); | |
| 115 if (!profile_) { | |
| 116 LOG(ERROR) << "Unable to locate a suitable profile for profile directory '" | |
| 117 << profile_dir << "' while trying to load app with id '" | |
| 118 << app_id << "'."; | |
| 119 return false; | |
| 120 } | |
| 121 extensions::ExtensionSystem* extension_system = | |
| 122 extensions::ExtensionSystem::Get(profile_); | |
| 123 ExtensionServiceInterface* extension_service = | |
| 124 extension_system->extension_service(); | |
| 125 const extensions::Extension* extension = | |
| 126 extension_service->GetExtensionById( | |
| 127 app_id, false); | |
| 128 if (!extension) { | |
| 129 LOG(ERROR) << "Shortcut attempted to launch nonexistent app with id '" | |
| 130 << app_id << "'."; | |
| 131 return false; | |
| 132 } | |
| 133 app_id_ = app_id; | |
| 134 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.
| |
| 135 extension_misc::LAUNCH_NONE, | |
| 136 NEW_WINDOW); | |
| 137 chrome::OpenApplication(params); | |
| 138 | |
| 139 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
| 140 content::Source<Profile>(profile_)); | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 void AppShimHost::Observe(int type, | |
| 145 const content::NotificationSource& source, | |
| 146 const content::NotificationDetails& details) { | |
| 147 DCHECK(CalledOnValidThread()); | |
| 148 DCHECK(content::Source<Profile>(source).ptr() == profile_); | |
| 149 switch (type) { | |
| 150 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | |
| 151 extensions::ExtensionHost* extension_host = | |
| 152 content::Details<extensions::ExtensionHost>(details).ptr(); | |
| 153 if (app_id_ == extension_host->extension_id()) | |
| 154 Close(); | |
| 155 break; | |
| 156 } | |
| 157 default: | |
| 158 NOTREACHED() << "Unexpected notification sent."; | |
| 159 break; | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 void AppShimHost::Close() { | |
| 164 DCHECK(CalledOnValidThread()); | |
| 165 delete this; | |
| 166 } | |
| 167 | |
| 168 //----------------------------------------------------------------------------- | |
| 169 | |
| 170 namespace { | |
| 171 | |
| 172 void CreateAppShimHost(const IPC::ChannelHandle& handle) { | |
| 173 new AppShimHost(handle); | |
| 174 } | |
| 175 | |
| 176 } // namespace | |
| 177 | |
| 178 AppShimHostController::AppShimHostController() : factory_(NULL) { | |
| 179 BrowserThread::PostTask( | |
| 180 BrowserThread::FILE, FROM_HERE, | |
| 181 base::Bind(&AppShimHostController::InitOnFileThread, AsWeakPtr())); | |
| 182 } | |
| 183 | |
| 184 AppShimHostController::~AppShimHostController() { | |
| 185 delete factory_; | |
| 186 } | |
| 187 | |
| 188 void AppShimHostController::InitOnFileThread() { | |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 190 base::FilePath user_data_dir; | |
| 191 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
| |
| 192 base::FilePath socket_path = | |
| 193 user_data_dir.Append(app_mode::kAppShimSocketName); | |
| 194 factory_ = new IPC::ChannelFactory(socket_path, this); | |
| 195 BrowserThread::PostTask( | |
| 196 BrowserThread::IO, FROM_HERE, | |
| 197 base::Bind(&AppShimHostController::ListenOnIO, AsWeakPtr())); | |
| 198 } | |
| 199 | |
| 200 void AppShimHostController::ListenOnIO() { | |
| 201 factory_->Listen(); | |
| 202 } | |
| 203 | |
| 204 void AppShimHostController::OnClientConnected( | |
| 205 const IPC::ChannelHandle& handle) { | |
| 206 // called on IO thread | |
| 207 BrowserThread::PostTask( | |
| 208 BrowserThread::UI, FROM_HERE, | |
| 209 base::Bind(&CreateAppShimHost, handle)); | |
| 210 } | |
| OLD | NEW |