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 class AppShimHost : public IPC::Listener, | |
|
Mark Mentovai
2013/03/08 16:50:17
Some short comments about everything in this class
jeremya
2013/03/11 02:47:33
Done.
| |
| 35 public content::NotificationObserver, | |
| 36 public base::NonThreadSafe { | |
| 37 public: | |
| 38 explicit AppShimHost(const IPC::ChannelHandle& channel); | |
| 39 virtual ~AppShimHost(); | |
| 40 | |
| 41 private: | |
| 42 bool OnMessageReceived(const IPC::Message& message); | |
| 43 bool LaunchAppImpl(const std::string& app_id, const std::string& profile_dir); | |
| 44 | |
| 45 virtual void Observe(int type, | |
| 46 const content::NotificationSource& source, | |
| 47 const content::NotificationDetails& details) OVERRIDE; | |
| 48 | |
| 49 void Close(); | |
| 50 | |
| 51 void OnLaunchApp(std::string profile, std::string app_id); | |
| 52 | |
| 53 IPC::ChannelProxy* channel_; | |
| 54 | |
| 55 std::string app_id_; | |
| 56 Profile* profile_; | |
| 57 content::NotificationRegistrar registrar_; | |
| 58 }; | |
| 59 | |
| 60 AppShimHost::AppShimHost(const IPC::ChannelHandle& handle) | |
| 61 : channel_(NULL), profile_(NULL) { | |
| 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 63 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_SERVER, this, | |
| 64 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | |
| 65 } | |
| 66 | |
| 67 AppShimHost::~AppShimHost() { | |
| 68 DCHECK(CalledOnValidThread()); | |
| 69 delete channel_; | |
| 70 channel_ = NULL; | |
|
Mark Mentovai
2013/03/08 16:50:17
It’s not really necessary to zero things out in a
jeremya
2013/03/11 02:47:33
Done.
| |
| 71 profile_ = NULL; | |
| 72 } | |
| 73 | |
| 74 bool AppShimHost::OnMessageReceived(const IPC::Message& message) { | |
| 75 DCHECK(CalledOnValidThread()); | |
| 76 bool handled = true; | |
| 77 IPC_BEGIN_MESSAGE_MAP(AppShimHost, message) | |
| 78 IPC_MESSAGE_HANDLER(AppShimHostMsg_LaunchApp, OnLaunchApp) | |
| 79 | |
| 80 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 81 IPC_END_MESSAGE_MAP() | |
| 82 | |
| 83 return handled; | |
| 84 } | |
| 85 | |
| 86 void AppShimHost::OnLaunchApp(std::string profile_dir, std::string app_id) { | |
| 87 bool success = LaunchAppImpl(profile_dir, app_id); | |
| 88 channel_->Send(new AppShimMsg_LaunchApp_Done(success)); | |
| 89 } | |
| 90 | |
| 91 bool AppShimHost::LaunchAppImpl(const std::string& profile_dir, | |
| 92 const std::string& app_id) { | |
| 93 DCHECK(CalledOnValidThread()); | |
| 94 if (profile_) | |
| 95 // Only one app launch message per channel. | |
|
Mark Mentovai
2013/03/08 16:50:17
{braces} around multi-line scopes, even if the sco
jeremya
2013/03/11 02:47:33
Done.
| |
| 96 return false; | |
| 97 ProfileManager* profileManager = g_browser_process->profile_manager(); | |
| 98 base::FilePath path(profile_dir); | |
| 99 path = profileManager->user_data_dir().Append(path); | |
| 100 profile_ = profileManager->GetProfile(path); | |
| 101 if (!profile_) { | |
| 102 LOG(ERROR) << "Unable to locate a suitable profile for profile directory '" | |
| 103 << profile_dir << "' while trying to load app with id '" | |
| 104 << app_id << "'."; | |
| 105 return false; | |
| 106 } | |
| 107 extensions::ExtensionSystem* extension_system = | |
| 108 extensions::ExtensionSystem::Get(profile_); | |
| 109 ExtensionServiceInterface* extension_service = | |
| 110 extension_system->extension_service(); | |
| 111 const extensions::Extension* extension = | |
| 112 extension_service->GetExtensionById( | |
| 113 app_id, false); | |
| 114 if (!extension) { | |
| 115 LOG(ERROR) << "Shortcut attempted to launch nonexistent app with id '" | |
| 116 << app_id << "'."; | |
| 117 return false; | |
| 118 } | |
| 119 app_id_ = app_id; | |
| 120 chrome::AppLaunchParams params(profile_, extension, | |
| 121 extension_misc::LAUNCH_NONE, | |
|
Mark Mentovai
2013/03/08 16:50:17
Weird indentation.
jeremya
2013/03/11 02:47:33
Done.
| |
| 122 NEW_WINDOW); | |
| 123 chrome::OpenApplication(params); | |
| 124 | |
| 125 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
| 126 content::Source<Profile>(profile_)); | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 void AppShimHost::Close() { | |
| 131 DCHECK(CalledOnValidThread()); | |
| 132 delete this; | |
| 133 } | |
| 134 | |
| 135 void AppShimHost::Observe(int type, | |
| 136 const content::NotificationSource& source, | |
| 137 const content::NotificationDetails& details) { | |
| 138 DCHECK(content::Source<Profile>(source).ptr() == profile_); | |
| 139 switch (type) { | |
| 140 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | |
| 141 extensions::ExtensionHost* extension_host = | |
| 142 content::Details<extensions::ExtensionHost>(details).ptr(); | |
| 143 if (app_id_ == extension_host->extension_id()) | |
| 144 Close(); | |
| 145 break; | |
| 146 } | |
| 147 default: | |
| 148 NOTREACHED() << "Unexpected notification sent."; | |
| 149 break; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 //----------------------------------------------------------------------------- | |
| 154 | |
| 155 AppShimHostController::AppShimHostController() : factory_(NULL) { | |
| 156 BrowserThread::PostTask( | |
| 157 BrowserThread::FILE, FROM_HERE, | |
| 158 base::Bind(&AppShimHostController::InitOnFile, AsWeakPtr())); | |
| 159 } | |
| 160 | |
| 161 AppShimHostController::~AppShimHostController() { | |
| 162 delete factory_; | |
| 163 } | |
| 164 | |
| 165 void AppShimHostController::InitOnFile() { | |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 167 base::FilePath user_data_dir; | |
| 168 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
| 169 base::FilePath socket_path = | |
| 170 user_data_dir.Append(app_mode::kAppShimSocketName); | |
| 171 factory_ = new IPC::ChannelFactory(socket_path, this); | |
| 172 BrowserThread::PostTask( | |
| 173 BrowserThread::IO, FROM_HERE, | |
| 174 base::Bind(&AppShimHostController::ListenOnIO, AsWeakPtr())); | |
| 175 } | |
| 176 | |
| 177 void AppShimHostController::ListenOnIO() { | |
| 178 factory_->Listen(); | |
| 179 } | |
| 180 | |
| 181 void CreateAppShimHost(const IPC::ChannelHandle& handle) { | |
|
Mark Mentovai
2013/03/08 16:50:17
Anonymous namespace or static.
jeremya
2013/03/11 02:47:33
Done.
| |
| 182 new AppShimHost(handle); | |
| 183 } | |
| 184 | |
| 185 void AppShimHostController::OnClientConnected( | |
| 186 const IPC::ChannelHandle& handle) { | |
| 187 // called on IO thread | |
| 188 BrowserThread::PostTask( | |
| 189 BrowserThread::UI, FROM_HERE, | |
| 190 base::Bind(&CreateAppShimHost, handle)); | |
| 191 } | |
| OLD | NEW |