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 #include "apps/app_shim/app_shim_host_mac.h" |
| 6 |
| 7 #include "apps/app_shim/app_shim_messages.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" |
| 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/extensions/extension_host.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/extension_system.h" |
| 15 #include "chrome/browser/extensions/shell_window_registry.h" |
| 16 #include "chrome/browser/profiles/profile_manager.h" |
| 17 #include "chrome/browser/ui/extensions/application_launch.h" |
| 18 #include "chrome/common/extensions/extension_constants.h" |
| 19 #include "ipc/ipc_channel_proxy.h" |
| 20 |
| 21 AppShimHost::AppShimHost() |
| 22 : channel_(NULL), profile_(NULL) { |
| 23 } |
| 24 |
| 25 AppShimHost::~AppShimHost() { |
| 26 DCHECK(CalledOnValidThread()); |
| 27 } |
| 28 |
| 29 void AppShimHost::ServeChannel(const IPC::ChannelHandle& handle) { |
| 30 DCHECK(CalledOnValidThread()); |
| 31 DCHECK(!channel_.get()); |
| 32 channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_SERVER, this, |
| 33 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); |
| 34 } |
| 35 |
| 36 bool AppShimHost::OnMessageReceived(const IPC::Message& message) { |
| 37 DCHECK(CalledOnValidThread()); |
| 38 bool handled = true; |
| 39 IPC_BEGIN_MESSAGE_MAP(AppShimHost, message) |
| 40 IPC_MESSAGE_HANDLER(AppShimHostMsg_LaunchApp, OnLaunchApp) |
| 41 IPC_MESSAGE_UNHANDLED(handled = false) |
| 42 IPC_END_MESSAGE_MAP() |
| 43 |
| 44 return handled; |
| 45 } |
| 46 |
| 47 bool AppShimHost::Send(IPC::Message* message) { |
| 48 DCHECK(channel_.get()); |
| 49 return channel_->Send(message); |
| 50 } |
| 51 |
| 52 void AppShimHost::OnLaunchApp(std::string profile_dir, std::string app_id) { |
| 53 DCHECK(CalledOnValidThread()); |
| 54 bool success = LaunchAppImpl(profile_dir, app_id); |
| 55 Send(new AppShimMsg_LaunchApp_Done(success)); |
| 56 } |
| 57 |
| 58 bool AppShimHost::LaunchAppImpl(const std::string& profile_dir, |
| 59 const std::string& app_id) { |
| 60 DCHECK(CalledOnValidThread()); |
| 61 if (profile_) { |
| 62 // Only one app launch message per channel. |
| 63 return false; |
| 64 } |
| 65 if (!extensions::Extension::IdIsValid(app_id)) { |
| 66 LOG(ERROR) << "Bad app ID from app shim launch message."; |
| 67 return false; |
| 68 } |
| 69 Profile* profile = FetchProfileForDirectory(profile_dir); |
| 70 if (!profile) |
| 71 return false; |
| 72 |
| 73 if (!LaunchApp(profile, app_id)) |
| 74 return false; |
| 75 |
| 76 profile_ = profile; |
| 77 app_id_ = app_id; |
| 78 |
| 79 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 80 content::Source<Profile>(profile_)); |
| 81 return true; |
| 82 } |
| 83 |
| 84 Profile* AppShimHost::FetchProfileForDirectory(const std::string& profile_dir) { |
| 85 ProfileManager* profileManager = g_browser_process->profile_manager(); |
| 86 // Even though the name of this function is "unsafe", there's no security |
| 87 // issue here -- the check for the profile name in the profile info cache |
| 88 // ensures that we never access any directory that isn't a known profile. |
| 89 base::FilePath path = base::FilePath::FromUTF8Unsafe(profile_dir); |
| 90 path = profileManager->user_data_dir().Append(path); |
| 91 ProfileInfoCache& cache = profileManager->GetProfileInfoCache(); |
| 92 // This ensures that the given profile path is acutally a profile that we |
| 93 // already know about. |
| 94 if (cache.GetIndexOfProfileWithPath(path) == std::string::npos) { |
| 95 LOG(ERROR) << "Requested directory is not a known profile."; |
| 96 return NULL; |
| 97 } |
| 98 Profile* profile = profileManager->GetProfile(path); |
| 99 if (!profile) { |
| 100 LOG(ERROR) << "Couldn't get profile for directory '" << profile_dir << "'."; |
| 101 return NULL; |
| 102 } |
| 103 return profile; |
| 104 } |
| 105 |
| 106 bool AppShimHost::LaunchApp(Profile* profile, const std::string& app_id) { |
| 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) << "Attempted to launch nonexistent app with id '" |
| 116 << app_id << "'."; |
| 117 return false; |
| 118 } |
| 119 // TODO(jeremya): Handle the case that launching the app fails. Probably we |
| 120 // need to watch for 'app successfully launched' or at least 'background page |
| 121 // exists/was created' and time out with failure if we don't see that sign of |
| 122 // life within a certain window. |
| 123 chrome::AppLaunchParams params(profile, |
| 124 extension, |
| 125 extension_misc::LAUNCH_NONE, |
| 126 NEW_WINDOW); |
| 127 chrome::OpenApplication(params); |
| 128 return true; |
| 129 } |
| 130 |
| 131 void AppShimHost::Observe(int type, |
| 132 const content::NotificationSource& source, |
| 133 const content::NotificationDetails& details) { |
| 134 DCHECK(CalledOnValidThread()); |
| 135 DCHECK(content::Source<Profile>(source).ptr() == profile_); |
| 136 switch (type) { |
| 137 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
| 138 extensions::ExtensionHost* extension_host = |
| 139 content::Details<extensions::ExtensionHost>(details).ptr(); |
| 140 if (app_id_ == extension_host->extension_id()) |
| 141 Close(); |
| 142 break; |
| 143 } |
| 144 default: |
| 145 NOTREACHED() << "Unexpected notification sent."; |
| 146 break; |
| 147 } |
| 148 } |
| 149 |
| 150 void AppShimHost::Close() { |
| 151 DCHECK(CalledOnValidThread()); |
| 152 delete this; |
| 153 } |
OLD | NEW |