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 // Use the "connect" verb to 69 hr = activator->ActivateApplication( 70 app_user_model_id.c_str(), kMetroViewerConnectVerb, AO_NONE, &pid); 71 } 72 73 LOG_IF(ERROR, FAILED(hr)) << "Tried and failed to launch Metro Chrome. " 74 << "hr=" << std::hex << hr; 75 76 // Having launched the viewer process, now we wait for it to connect. 77 bool success = 78 channel_connected_event_->TimedWait(base::TimeDelta::FromSeconds(60)); 79 channel_connected_event_.reset(); 80 81 // |message_filter| is only used to signal |channel_connected_event_| above 82 // and can thus be removed after |channel_connected_event_| is no longer 83 // waiting. 84 channel_->RemoveFilter(message_filter); 85 return success; 86 } 87 88 bool MetroViewerProcessHost::Send(IPC::Message* msg) { 89 return channel_->Send(msg); 90 } 91 92 bool MetroViewerProcessHost::OnMessageReceived( 93 const IPC::Message& message) { 94 DCHECK(CalledOnValidThread()); 95 bool handled = true; 96 IPC_BEGIN_MESSAGE_MAP(MetroViewerProcessHost, message) 97 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SetTargetSurface, OnSetTargetSurface) 98 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_OpenURL, OnOpenURL) 99 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SearchRequest, OnHandleSearchRequest) 100 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_WindowSizeChanged, 101 OnWindowSizeChanged) 102 IPC_MESSAGE_UNHANDLED(handled = false) 103 IPC_END_MESSAGE_MAP() 104 return handled ? true : 105 aura::RemoteRootWindowHostWin::Instance()->OnMessageReceived(message); 106 } 107 108 void MetroViewerProcessHost::NotifyChannelConnected() { 109 if (channel_connected_event_) 110 channel_connected_event_->Signal(); 111 } 112 113 } // namespace win8 114