summaryrefslogtreecommitdiffstats
path: root/apps/app_shim/app_shim_host_mac.cc
blob: 8cff755a21ef674490d6cee2a904e1a9f439fb22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "apps/app_shim/app_shim_host_mac.h"

#include "apps/app_shim/app_shim_messages.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/shell_window_registry.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/common/extensions/extension_constants.h"
#include "ipc/ipc_channel_proxy.h"

AppShimHost::AppShimHost()
    : channel_(NULL), profile_(NULL) {
}

AppShimHost::~AppShimHost() {
  DCHECK(CalledOnValidThread());
}

void AppShimHost::ServeChannel(const IPC::ChannelHandle& handle) {
  DCHECK(CalledOnValidThread());
  DCHECK(!channel_.get());
  channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_SERVER, this,
      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
}

bool AppShimHost::OnMessageReceived(const IPC::Message& message) {
  DCHECK(CalledOnValidThread());
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(AppShimHost, message)
    IPC_MESSAGE_HANDLER(AppShimHostMsg_LaunchApp, OnLaunchApp)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()

  return handled;
}

bool AppShimHost::Send(IPC::Message* message) {
  DCHECK(channel_.get());
  return channel_->Send(message);
}

void AppShimHost::OnLaunchApp(std::string profile_dir, std::string app_id) {
  DCHECK(CalledOnValidThread());
  bool success = LaunchAppImpl(profile_dir, app_id);
  Send(new AppShimMsg_LaunchApp_Done(success));
}

bool AppShimHost::LaunchAppImpl(const std::string& profile_dir,
                                const std::string& app_id) {
  DCHECK(CalledOnValidThread());
  if (profile_) {
    // Only one app launch message per channel.
    return false;
  }
  if (!extensions::Extension::IdIsValid(app_id)) {
    LOG(ERROR) << "Bad app ID from app shim launch message.";
    return false;
  }
  Profile* profile = FetchProfileForDirectory(profile_dir);
  if (!profile)
    return false;

  if (!LaunchApp(profile, app_id))
    return false;

  profile_ = profile;
  app_id_ = app_id;

  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
                 content::Source<Profile>(profile_));
  return true;
}

Profile* AppShimHost::FetchProfileForDirectory(const std::string& profile_dir) {
  ProfileManager* profileManager = g_browser_process->profile_manager();
  // Even though the name of this function is "unsafe", there's no security
  // issue here -- the check for the profile name in the profile info cache
  // ensures that we never access any directory that isn't a known profile.
  base::FilePath path = base::FilePath::FromUTF8Unsafe(profile_dir);
  path = profileManager->user_data_dir().Append(path);
  ProfileInfoCache& cache = profileManager->GetProfileInfoCache();
  // This ensures that the given profile path is acutally a profile that we
  // already know about.
  if (cache.GetIndexOfProfileWithPath(path) == std::string::npos) {
    LOG(ERROR) << "Requested directory is not a known profile.";
    return NULL;
  }
  Profile* profile = profileManager->GetProfile(path);
  if (!profile) {
    LOG(ERROR) << "Couldn't get profile for directory '" << profile_dir << "'.";
    return NULL;
  }
  return profile;
}

bool AppShimHost::LaunchApp(Profile* profile, const std::string& app_id) {
  extensions::ExtensionSystem* extension_system =
      extensions::ExtensionSystem::Get(profile);
  ExtensionServiceInterface* extension_service =
      extension_system->extension_service();
  const extensions::Extension* extension =
      extension_service->GetExtensionById(
          app_id, false);
  if (!extension) {
    LOG(ERROR) << "Attempted to launch nonexistent app with id '"
               << app_id << "'.";
    return false;
  }
  // TODO(jeremya): Handle the case that launching the app fails. Probably we
  // need to watch for 'app successfully launched' or at least 'background page
  // exists/was created' and time out with failure if we don't see that sign of
  // life within a certain window.
  chrome::AppLaunchParams params(profile,
                                 extension,
                                 extension_misc::LAUNCH_NONE,
                                 NEW_WINDOW);
  chrome::OpenApplication(params);
  return true;
}

void AppShimHost::Observe(int type,
                          const content::NotificationSource& source,
                          const content::NotificationDetails& details) {
  DCHECK(CalledOnValidThread());
  DCHECK(content::Source<Profile>(source).ptr() == profile_);
  switch (type) {
    case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
      extensions::ExtensionHost* extension_host =
          content::Details<extensions::ExtensionHost>(details).ptr();
      if (app_id_ == extension_host->extension_id())
        Close();
      break;
    }
    default:
      NOTREACHED() << "Unexpected notification sent.";
      break;
  }
}

void AppShimHost::Close() {
  DCHECK(CalledOnValidThread());
  delete this;
}