OLD | NEW |
(Empty) | |
| 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 #ifndef WIN8_VIEWER_METRO_VIEWER_PROCESS_HOST_H_ |
| 6 #define WIN8_VIEWER_METRO_VIEWER_PROCESS_HOST_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/strings/string16.h" |
| 15 #include "base/threading/non_thread_safe.h" |
| 16 #include "ipc/ipc_channel_proxy.h" |
| 17 #include "ipc/ipc_listener.h" |
| 18 #include "ipc/ipc_sender.h" |
| 19 #include "ipc/message_filter.h" |
| 20 #include "ui/gfx/native_widget_types.h" |
| 21 #include "win8/viewer/metro_viewer_exports.h" |
| 22 |
| 23 namespace base { |
| 24 class SingleThreadTaskRunner; |
| 25 class WaitableEvent; |
| 26 } |
| 27 |
| 28 namespace IPC { |
| 29 class ChannelProxy; |
| 30 class Message; |
| 31 } |
| 32 |
| 33 namespace win8 { |
| 34 |
| 35 // Abstract base class for various Metro viewer process host implementations. |
| 36 class METRO_VIEWER_EXPORT MetroViewerProcessHost : public IPC::Listener, |
| 37 public IPC::Sender, |
| 38 public base::NonThreadSafe { |
| 39 public: |
| 40 typedef base::Callback<void(const base::FilePath&, int, void*)> |
| 41 OpenFileCompletion; |
| 42 |
| 43 typedef base::Callback<void(const std::vector<base::FilePath>&, void*)> |
| 44 OpenMultipleFilesCompletion; |
| 45 |
| 46 typedef base::Callback<void(const base::FilePath&, int, void*)> |
| 47 SaveFileCompletion; |
| 48 |
| 49 typedef base::Callback<void(const base::FilePath&, int, void*)> |
| 50 SelectFolderCompletion; |
| 51 |
| 52 typedef base::Callback<void(void*)> FileSelectionCanceled; |
| 53 |
| 54 // Initializes a viewer process host to connect to the Metro viewer process |
| 55 // over IPC. The given task runner correspond to a thread on which |
| 56 // IPC::Channel is created and used (e.g. IO thread). Instantly connects to |
| 57 // the viewer process if one is already connected to |ipc_channel_name|; a |
| 58 // viewer can otherwise be launched synchronously via |
| 59 // LaunchViewerAndWaitForConnection(). |
| 60 explicit MetroViewerProcessHost( |
| 61 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner); |
| 62 ~MetroViewerProcessHost() override; |
| 63 |
| 64 // Returns the process id of the viewer process if one is connected to this |
| 65 // host, returns base::kNullProcessId otherwise. |
| 66 base::ProcessId GetViewerProcessId(); |
| 67 |
| 68 // Launches the viewer process associated with the given |app_user_model_id| |
| 69 // and blocks until that viewer process connects or until a timeout is |
| 70 // reached. Returns true if the viewer process connects before the timeout is |
| 71 // reached. NOTE: this assumes that the app referred to by |app_user_model_id| |
| 72 // is registered as the default browser. |
| 73 bool LaunchViewerAndWaitForConnection( |
| 74 const base::string16& app_user_model_id); |
| 75 |
| 76 // Handles the activate desktop command for Metro Chrome Ash. The |ash_exit| |
| 77 // parameter indicates whether the Ash process would be shutdown after |
| 78 // activating the desktop. |
| 79 static void HandleActivateDesktop(const base::FilePath& shortcut, |
| 80 bool ash_exit); |
| 81 |
| 82 // Handles the metro exit command. Notifies the metro viewer to shutdown |
| 83 // gracefully. |
| 84 static void HandleMetroExit(); |
| 85 |
| 86 // Handles the open file operation for Metro Chrome Ash. The on_success |
| 87 // callback passed in is invoked when we receive the opened file name from |
| 88 // the metro viewer. The on failure callback is invoked on failure. |
| 89 static void HandleOpenFile(const base::string16& title, |
| 90 const base::FilePath& default_path, |
| 91 const base::string16& filter, |
| 92 const OpenFileCompletion& on_success, |
| 93 const FileSelectionCanceled& on_failure); |
| 94 |
| 95 // Handles the open multiple file operation for Metro Chrome Ash. The |
| 96 // on_success callback passed in is invoked when we receive the opened file |
| 97 // names from the metro viewer. The on failure callback is invoked on failure. |
| 98 static void HandleOpenMultipleFiles( |
| 99 const base::string16& title, |
| 100 const base::FilePath& default_path, |
| 101 const base::string16& filter, |
| 102 const OpenMultipleFilesCompletion& on_success, |
| 103 const FileSelectionCanceled& on_failure); |
| 104 |
| 105 // Handles the save file operation for Metro Chrome Ash. The on_success |
| 106 // callback passed in is invoked when we receive the saved file name from |
| 107 // the metro viewer. The on failure callback is invoked on failure. |
| 108 static void HandleSaveFile(const base::string16& title, |
| 109 const base::FilePath& default_path, |
| 110 const base::string16& filter, |
| 111 int filter_index, |
| 112 const base::string16& default_extension, |
| 113 const SaveFileCompletion& on_success, |
| 114 const FileSelectionCanceled& on_failure); |
| 115 |
| 116 // Handles the select folder for Metro Chrome Ash. The on_success |
| 117 // callback passed in is invoked when we receive the folder name from the |
| 118 // metro viewer. The on failure callback is invoked on failure. |
| 119 static void HandleSelectFolder(const base::string16& title, |
| 120 const SelectFolderCompletion& on_success, |
| 121 const FileSelectionCanceled& on_failure); |
| 122 |
| 123 protected: |
| 124 // IPC::Sender implementation: |
| 125 bool Send(IPC::Message* msg) override; |
| 126 |
| 127 // IPC::Listener implementation: |
| 128 bool OnMessageReceived(const IPC::Message& message) override; |
| 129 void OnChannelError() override = 0; |
| 130 |
| 131 private: |
| 132 // The following are the implementation for the corresponding static methods |
| 133 // above, see them for descriptions. |
| 134 void HandleOpenFileImpl(const base::string16& title, |
| 135 const base::FilePath& default_path, |
| 136 const base::string16& filter, |
| 137 const OpenFileCompletion& on_success, |
| 138 const FileSelectionCanceled& on_failure); |
| 139 void HandleOpenMultipleFilesImpl( |
| 140 const base::string16& title, |
| 141 const base::FilePath& default_path, |
| 142 const base::string16& filter, |
| 143 const OpenMultipleFilesCompletion& on_success, |
| 144 const FileSelectionCanceled& on_failure); |
| 145 void HandleSaveFileImpl(const base::string16& title, |
| 146 const base::FilePath& default_path, |
| 147 const base::string16& filter, |
| 148 int filter_index, |
| 149 const base::string16& default_extension, |
| 150 const SaveFileCompletion& on_success, |
| 151 const FileSelectionCanceled& on_failure); |
| 152 void HandleSelectFolderImpl(const base::string16& title, |
| 153 const SelectFolderCompletion& on_success, |
| 154 const FileSelectionCanceled& on_failure); |
| 155 |
| 156 // Called over IPC by the viewer process to tell this host that it should be |
| 157 // drawing to |target_surface|. |
| 158 virtual void OnSetTargetSurface(gfx::NativeViewId target_surface, |
| 159 float device_scale) = 0; |
| 160 |
| 161 // Called over IPC by the viewer process to request that the url passed in be |
| 162 // opened. |
| 163 virtual void OnOpenURL(const base::string16& url) = 0; |
| 164 |
| 165 // Called over IPC by the viewer process to request that the search string |
| 166 // passed in is passed to the default search provider and a URL navigation be |
| 167 // performed. |
| 168 virtual void OnHandleSearchRequest(const base::string16& search_string) = 0; |
| 169 |
| 170 // Called over IPC by the viewer process when the window size has changed. |
| 171 virtual void OnWindowSizeChanged(uint32_t width, uint32_t height) = 0; |
| 172 |
| 173 void NotifyChannelConnected(); |
| 174 |
| 175 // IPC message handing methods: |
| 176 void OnFileSaveAsDone(bool success, |
| 177 const base::FilePath& filename, |
| 178 int filter_index); |
| 179 void OnFileOpenDone(bool success, const base::FilePath& filename); |
| 180 void OnMultiFileOpenDone(bool success, |
| 181 const std::vector<base::FilePath>& files); |
| 182 void OnSelectFolderDone(bool success, const base::FilePath& folder); |
| 183 |
| 184 // Inner message filter used to handle connection event on the IPC channel |
| 185 // proxy's background thread. This prevents consumers of |
| 186 // MetroViewerProcessHost from having to pump messages on their own message |
| 187 // loop. |
| 188 class InternalMessageFilter : public IPC::MessageFilter { |
| 189 public: |
| 190 InternalMessageFilter(MetroViewerProcessHost* owner); |
| 191 |
| 192 // IPC::MessageFilter implementation. |
| 193 void OnChannelConnected(int32_t peer_pid) override; |
| 194 |
| 195 private: |
| 196 ~InternalMessageFilter() override; |
| 197 |
| 198 MetroViewerProcessHost* owner_; |
| 199 DISALLOW_COPY_AND_ASSIGN(InternalMessageFilter); |
| 200 }; |
| 201 |
| 202 scoped_ptr<IPC::ChannelProxy> channel_; |
| 203 scoped_ptr<base::WaitableEvent> channel_connected_event_; |
| 204 scoped_refptr<InternalMessageFilter> message_filter_; |
| 205 |
| 206 static MetroViewerProcessHost* instance_; |
| 207 |
| 208 // Saved callbacks which inform the caller about the result of the open file/ |
| 209 // save file/select operations. |
| 210 OpenFileCompletion file_open_completion_callback_; |
| 211 OpenMultipleFilesCompletion multi_file_open_completion_callback_; |
| 212 SaveFileCompletion file_saveas_completion_callback_; |
| 213 SelectFolderCompletion select_folder_completion_callback_; |
| 214 FileSelectionCanceled failure_callback_; |
| 215 |
| 216 DISALLOW_COPY_AND_ASSIGN(MetroViewerProcessHost); |
| 217 }; |
| 218 |
| 219 } // namespace win8 |
| 220 |
| 221 #endif // WIN8_VIEWER_METRO_VIEWER_PROCESS_HOST_H_ |
OLD | NEW |