1 // Copyright (c) 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 "win8/viewer/metro_viewer_process_host.h" 6 7 #include <shlobj.h> 8 9 #include "base/command_line.h" 10 #include "base/file_util.h" 11 #include "base/files/file_path.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/process/process.h" 14 #include "base/strings/string16.h" 15 #include "base/synchronization/waitable_event.h" 16 #include "base/time/time.h" 17 #include "base/win/scoped_comptr.h" 18 #include "ipc/ipc_message.h" 19 #include "ipc/ipc_message_macros.h" 20 #include "ui/aura/remote_root_window_host_win.h" 21 #include "ui/metro_viewer/metro_viewer_messages.h" 22 #include "win8/viewer/metro_viewer_constants.h" 23 24 namespace win8 { 25 26 MetroViewerProcessHost::InternalMessageFilter::InternalMessageFilter( 27 MetroViewerProcessHost* owner) : owner_(owner) { 28 } 29 30 void MetroViewerProcessHost::InternalMessageFilter::OnChannelConnected( 31 int32 /* peer_pid */) { 32 owner_->NotifyChannelConnected(); 33 } 34 35 MetroViewerProcessHost::MetroViewerProcessHost( 36 base::SingleThreadTaskRunner* ipc_task_runner) { 37 38 channel_.reset(new IPC::ChannelProxy( 39 kMetroViewerIPCChannelName, 40 IPC::Channel::MODE_NAMED_SERVER, 41 this, 42 ipc_task_runner)); 43 } 44 45 MetroViewerProcessHost::~MetroViewerProcessHost() { 46 } 47 48 base::ProcessId MetroViewerProcessHost::GetViewerProcessId() { 49 if (channel_) 50 return channel_->peer_pid(); 51 return base::kNullProcessId; 52 } 53 54 bool MetroViewerProcessHost::LaunchViewerAndWaitForConnection( 55 const base::string16& app_user_model_id) { 56 DCHECK_EQ(base::kNullProcessId, channel_->peer_pid()); 57 58 channel_connected_event_.reset(new base::WaitableEvent(false, false)); 59 60 scoped_refptr<InternalMessageFilter> message_filter( 61 new InternalMessageFilter(this)); 62 channel_->AddFilter(message_filter); 63 64 base::win::ScopedComPtr<IApplicationActivationManager> activator; 65 HRESULT hr = activator.CreateInstance(CLSID_ApplicationActivationManager); 66 if (SUCCEEDED(hr)) { 67 DWORD pid = 0; 68 hr = activator->ActivateApplication( 69 app_user_model_id.c_str(), L"open", AO_NONE, &pid); 70 } 71 72 LOG_IF(ERROR, FAILED(hr)) << "Tried and failed to launch Metro Chrome. " 73 << "hr=" << std::hex << hr; 74 75 // Having launched the viewer process, now we wait for it to connect. 76 bool success = 77 channel_connected_event_->TimedWait(base::TimeDelta::FromSeconds(60)); 78 channel_connected_event_.reset(); 79 80 // |message_filter| is only used to signal |channel_connected_event_| above 81 // and can thus be removed after |channel_connected_event_| is no longer 82 // waiting. 83 channel_->RemoveFilter(message_filter); 84 return success; 85 } 86 87 bool MetroViewerProcessHost::Send(IPC::Message* msg) { 88 return channel_->Send(msg); 89 } 90 91 bool MetroViewerProcessHost::OnMessageReceived( 92 const IPC::Message& message) { 93 DCHECK(CalledOnValidThread()); 94 bool handled = true; 95 IPC_BEGIN_MESSAGE_MAP(MetroViewerProcessHost, message) 96 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SetTargetSurface, OnSetTargetSurface) 97 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_OpenURL, OnOpenURL) 98 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SearchRequest, OnHandleSearchRequest) 99 IPC_MESSAGE_UNHANDLED(handled = false) 100 IPC_END_MESSAGE_MAP() 101 return handled ? true : 102 aura::RemoteRootWindowHostWin::Instance()->OnMessageReceived(message); 103 } 104 105 void MetroViewerProcessHost::NotifyChannelConnected() { 106 if (channel_connected_event_) 107 channel_connected_event_->Signal(); 108 } 109 110 } // namespace win8 111