OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "apps/app_shim/app_shim_host_mac.h" | 5 #include "apps/app_shim/app_shim_host_mac.h" |
6 | 6 |
7 #include "apps/app_shim/app_shim_handler_mac.h" | 7 #include "apps/app_shim/app_shim_handler_mac.h" |
8 #include "apps/app_shim/app_shim_messages.h" | 8 #include "apps/app_shim/app_shim_messages.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/extensions/extension_host.h" | |
14 #include "chrome/browser/extensions/extension_service.h" | |
15 #include "chrome/browser/extensions/extension_system.h" | |
16 #include "chrome/browser/extensions/shell_window_registry.h" | |
17 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
18 #include "chrome/browser/ui/extensions/application_launch.h" | 14 #include "content/public/browser/browser_thread.h" |
19 #include "chrome/browser/ui/extensions/shell_window.h" | |
20 #include "chrome/common/extensions/extension_constants.h" | |
21 #include "ipc/ipc_channel_proxy.h" | 15 #include "ipc/ipc_channel_proxy.h" |
22 #include "ui/base/cocoa/focus_window_set.h" | |
23 | 16 |
24 AppShimHost::AppShimHost() | 17 AppShimHost::AppShimHost() |
25 : channel_(NULL), profile_(NULL) { | 18 : channel_(NULL), profile_(NULL) { |
26 } | 19 } |
27 | 20 |
28 AppShimHost::~AppShimHost() { | 21 AppShimHost::~AppShimHost() { |
29 DCHECK(CalledOnValidThread()); | 22 DCHECK(CalledOnValidThread()); |
30 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); | 23 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); |
31 if (handler) | 24 if (handler) |
32 handler->OnShimClose(this); | 25 handler->OnShimClose(this); |
33 } | 26 } |
34 | 27 |
35 void AppShimHost::ServeChannel(const IPC::ChannelHandle& handle) { | 28 void AppShimHost::ServeChannel(const IPC::ChannelHandle& handle) { |
36 DCHECK(CalledOnValidThread()); | 29 DCHECK(CalledOnValidThread()); |
37 DCHECK(!channel_.get()); | 30 DCHECK(!channel_.get()); |
38 channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_SERVER, this, | 31 channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_SERVER, this, |
39 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | 32 content::BrowserThread::GetMessageLoopProxyForThread( |
| 33 content::BrowserThread::IO))); |
| 34 } |
| 35 |
| 36 Profile* AppShimHost::GetProfile() const { |
| 37 return profile_; |
| 38 } |
| 39 |
| 40 std::string AppShimHost::GetAppId() const { |
| 41 return app_id_; |
40 } | 42 } |
41 | 43 |
42 bool AppShimHost::OnMessageReceived(const IPC::Message& message) { | 44 bool AppShimHost::OnMessageReceived(const IPC::Message& message) { |
43 DCHECK(CalledOnValidThread()); | 45 DCHECK(CalledOnValidThread()); |
44 bool handled = true; | 46 bool handled = true; |
45 IPC_BEGIN_MESSAGE_MAP(AppShimHost, message) | 47 IPC_BEGIN_MESSAGE_MAP(AppShimHost, message) |
46 IPC_MESSAGE_HANDLER(AppShimHostMsg_LaunchApp, OnLaunchApp) | 48 IPC_MESSAGE_HANDLER(AppShimHostMsg_LaunchApp, OnLaunchApp) |
47 IPC_MESSAGE_HANDLER(AppShimHostMsg_FocusApp, OnFocus) | 49 IPC_MESSAGE_HANDLER(AppShimHostMsg_FocusApp, OnFocus) |
48 IPC_MESSAGE_UNHANDLED(handled = false) | 50 IPC_MESSAGE_UNHANDLED(handled = false) |
49 IPC_END_MESSAGE_MAP() | 51 IPC_END_MESSAGE_MAP() |
50 | 52 |
51 return handled; | 53 return handled; |
52 } | 54 } |
53 | 55 |
54 void AppShimHost::OnChannelError() { | 56 void AppShimHost::OnChannelError() { |
55 Close(); | 57 Close(); |
56 } | 58 } |
57 | 59 |
58 bool AppShimHost::Send(IPC::Message* message) { | 60 bool AppShimHost::Send(IPC::Message* message) { |
59 DCHECK(channel_.get()); | 61 DCHECK(channel_.get()); |
60 return channel_->Send(message); | 62 return channel_->Send(message); |
61 } | 63 } |
62 | 64 |
63 void AppShimHost::OnLaunchApp(std::string profile_dir, std::string app_id) { | 65 void AppShimHost::OnLaunchApp(std::string profile_dir, std::string app_id) { |
64 DCHECK(CalledOnValidThread()); | 66 DCHECK(CalledOnValidThread()); |
| 67 DCHECK(!profile_); |
| 68 if (profile_) { |
| 69 // Only one app launch message per channel. |
| 70 Send(new AppShimMsg_LaunchApp_Done(false)); |
| 71 return; |
| 72 } |
| 73 |
| 74 profile_ = FetchProfileForDirectory(profile_dir); |
65 app_id_ = app_id; | 75 app_id_ = app_id; |
66 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); | 76 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); |
67 bool success = | 77 bool success = handler && handler->OnShimLaunch(this); |
68 handler ? handler->OnShimLaunch(this) : LaunchAppImpl(profile_dir); | |
69 Send(new AppShimMsg_LaunchApp_Done(success)); | 78 Send(new AppShimMsg_LaunchApp_Done(success)); |
70 } | 79 } |
71 | 80 |
72 void AppShimHost::OnFocus() { | 81 void AppShimHost::OnFocus() { |
73 DCHECK(CalledOnValidThread()); | 82 DCHECK(CalledOnValidThread()); |
74 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); | 83 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); |
75 if (handler) { | 84 if (handler) |
76 handler->OnShimFocus(this); | 85 handler->OnShimFocus(this); |
77 return; | |
78 } | |
79 | |
80 if (!profile_) | |
81 return; | |
82 extensions::ShellWindowRegistry* registry = | |
83 extensions::ShellWindowRegistry::Get(profile_); | |
84 const extensions::ShellWindowRegistry::ShellWindowList windows = | |
85 registry->GetShellWindowsForApp(app_id_); | |
86 std::set<gfx::NativeWindow> native_windows; | |
87 for (extensions::ShellWindowRegistry::ShellWindowList::const_iterator i = | |
88 windows.begin(); | |
89 i != windows.end(); | |
90 ++i) { | |
91 native_windows.insert((*i)->GetNativeWindow()); | |
92 } | |
93 ui::FocusWindowSet(native_windows); | |
94 } | |
95 | |
96 bool AppShimHost::LaunchAppImpl(const std::string& profile_dir) { | |
97 DCHECK(CalledOnValidThread()); | |
98 if (profile_) { | |
99 // Only one app launch message per channel. | |
100 return false; | |
101 } | |
102 if (!extensions::Extension::IdIsValid(app_id_)) { | |
103 LOG(ERROR) << "Bad app ID from app shim launch message."; | |
104 return false; | |
105 } | |
106 Profile* profile = FetchProfileForDirectory(profile_dir); | |
107 if (!profile) | |
108 return false; | |
109 | |
110 if (!LaunchApp(profile)) | |
111 return false; | |
112 | |
113 profile_ = profile; | |
114 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
115 content::Source<Profile>(profile_)); | |
116 return true; | |
117 } | 86 } |
118 | 87 |
119 Profile* AppShimHost::FetchProfileForDirectory(const std::string& profile_dir) { | 88 Profile* AppShimHost::FetchProfileForDirectory(const std::string& profile_dir) { |
120 ProfileManager* profileManager = g_browser_process->profile_manager(); | 89 ProfileManager* profileManager = g_browser_process->profile_manager(); |
121 // Even though the name of this function is "unsafe", there's no security | 90 // Even though the name of this function is "unsafe", there's no security |
122 // issue here -- the check for the profile name in the profile info cache | 91 // issue here -- the check for the profile name in the profile info cache |
123 // ensures that we never access any directory that isn't a known profile. | 92 // ensures that we never access any directory that isn't a known profile. |
124 base::FilePath path = base::FilePath::FromUTF8Unsafe(profile_dir); | 93 base::FilePath path = base::FilePath::FromUTF8Unsafe(profile_dir); |
125 path = profileManager->user_data_dir().Append(path); | 94 path = profileManager->user_data_dir().Append(path); |
126 ProfileInfoCache& cache = profileManager->GetProfileInfoCache(); | 95 ProfileInfoCache& cache = profileManager->GetProfileInfoCache(); |
127 // This ensures that the given profile path is acutally a profile that we | 96 // This ensures that the given profile path is acutally a profile that we |
128 // already know about. | 97 // already know about. |
129 if (cache.GetIndexOfProfileWithPath(path) == std::string::npos) { | 98 if (cache.GetIndexOfProfileWithPath(path) == std::string::npos) { |
130 LOG(ERROR) << "Requested directory is not a known profile '" | 99 LOG(ERROR) << "Requested directory is not a known profile '" |
131 << profile_dir << "'."; | 100 << profile_dir << "'."; |
132 return NULL; | 101 return NULL; |
133 } | 102 } |
134 Profile* profile = profileManager->GetProfile(path); | 103 Profile* profile = profileManager->GetProfile(path); |
135 if (!profile) { | 104 if (!profile) { |
136 LOG(ERROR) << "Couldn't get profile for directory '" << profile_dir << "'."; | 105 LOG(ERROR) << "Couldn't get profile for directory '" << profile_dir << "'."; |
137 return NULL; | 106 return NULL; |
138 } | 107 } |
139 return profile; | 108 return profile; |
140 } | 109 } |
141 | 110 |
142 bool AppShimHost::LaunchApp(Profile* profile) { | |
143 extensions::ExtensionSystem* extension_system = | |
144 extensions::ExtensionSystem::Get(profile); | |
145 ExtensionServiceInterface* extension_service = | |
146 extension_system->extension_service(); | |
147 const extensions::Extension* extension = | |
148 extension_service->GetExtensionById( | |
149 app_id_, false); | |
150 if (!extension) { | |
151 LOG(ERROR) << "Attempted to launch nonexistent app with id '" | |
152 << app_id_ << "'."; | |
153 return false; | |
154 } | |
155 // TODO(jeremya): Handle the case that launching the app fails. Probably we | |
156 // need to watch for 'app successfully launched' or at least 'background page | |
157 // exists/was created' and time out with failure if we don't see that sign of | |
158 // life within a certain window. | |
159 chrome::AppLaunchParams params(profile, | |
160 extension, | |
161 extension_misc::LAUNCH_NONE, | |
162 NEW_WINDOW); | |
163 chrome::OpenApplication(params); | |
164 return true; | |
165 } | |
166 | |
167 void AppShimHost::Observe(int type, | |
168 const content::NotificationSource& source, | |
169 const content::NotificationDetails& details) { | |
170 DCHECK(CalledOnValidThread()); | |
171 DCHECK(content::Source<Profile>(source).ptr() == profile_); | |
172 switch (type) { | |
173 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | |
174 extensions::ExtensionHost* extension_host = | |
175 content::Details<extensions::ExtensionHost>(details).ptr(); | |
176 if (app_id_ == extension_host->extension_id()) | |
177 Close(); | |
178 break; | |
179 } | |
180 default: | |
181 NOTREACHED() << "Unexpected notification sent."; | |
182 break; | |
183 } | |
184 } | |
185 | |
186 void AppShimHost::OnAppClosed() { | 111 void AppShimHost::OnAppClosed() { |
187 Close(); | 112 Close(); |
188 } | 113 } |
189 | 114 |
190 void AppShimHost::Close() { | 115 void AppShimHost::Close() { |
191 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
192 delete this; | 117 delete this; |
193 } | 118 } |
OLD | NEW |