[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 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 CHROME_FRAME_CHROME_FRAME_DELEGATE_H_ |
| 6 | #define CHROME_FRAME_CHROME_FRAME_DELEGATE_H_ |
| 7 | |
[email protected] | 5778de6e | 2009-10-24 01:29:20 | [diff] [blame] | 8 | #include <atlbase.h> |
| 9 | #include <atlwin.h> |
[email protected] | 4c3fe4ae | 2010-03-01 20:01:24 | [diff] [blame] | 10 | #include <queue> |
[email protected] | efd4dfc2 | 2010-03-26 20:14:40 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
[email protected] | 5778de6e | 2009-10-24 01:29:20 | [diff] [blame] | 13 | |
[email protected] | a1e62d1 | 2010-03-16 02:18:43 | [diff] [blame] | 14 | #include "base/file_path.h" |
[email protected] | 4c3fe4ae | 2010-03-01 20:01:24 | [diff] [blame] | 15 | #include "base/lock.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 16 | #include "chrome/test/automation/automation_messages.h" |
| 17 | #include "ipc/ipc_message.h" |
| 18 | |
| 19 | // A common interface supported by all the browser specific ChromeFrame |
| 20 | // implementations. |
| 21 | class ChromeFrameDelegate { |
| 22 | public: |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 23 | typedef HWND WindowType; |
| 24 | |
| 25 | virtual WindowType GetWindow() const = 0; |
| 26 | virtual void GetBounds(RECT* bounds) = 0; |
| 27 | virtual std::string GetDocumentUrl() = 0; |
| 28 | virtual void OnAutomationServerReady() = 0; |
| 29 | virtual void OnAutomationServerLaunchFailed( |
| 30 | AutomationLaunchResult reason, const std::string& server_version) = 0; |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 31 | virtual void OnExtensionInstalled( |
| 32 | const FilePath& path, |
| 33 | void* user_data, |
| 34 | AutomationMsg_ExtensionResponseValues response) = 0; |
[email protected] | a1e62d1 | 2010-03-16 02:18:43 | [diff] [blame] | 35 | virtual void OnGetEnabledExtensionsComplete( |
| 36 | void* user_data, |
| 37 | const std::vector<FilePath>& extension_directories) = 0; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 38 | virtual void OnMessageReceived(const IPC::Message& msg) = 0; |
[email protected] | efd4dfc2 | 2010-03-26 20:14:40 | [diff] [blame] | 39 | virtual void OnChannelError() = 0; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 40 | |
| 41 | // This remains in interface since we call it if Navigate() |
| 42 | // returns immediate error. |
| 43 | virtual void OnLoadFailed(int error_code, const std::string& url) = 0; |
| 44 | |
| 45 | // Returns true if this instance is alive and well for processing automation |
| 46 | // messages. |
| 47 | virtual bool IsValid() const = 0; |
| 48 | |
[email protected] | 2b19e2fe | 2010-02-16 02:24:18 | [diff] [blame] | 49 | // To be called when the top-most window of an application hosting |
| 50 | // ChromeFrame is moved. |
| 51 | virtual void OnHostMoved() = 0; |
| 52 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 53 | protected: |
[email protected] | efd4dfc2 | 2010-03-26 20:14:40 | [diff] [blame] | 54 | virtual ~ChromeFrameDelegate() {} |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 55 | }; |
| 56 | |
[email protected] | c56428f2 | 2010-06-16 02:17:23 | [diff] [blame] | 57 | // Disable refcounting of ChromeFrameDelegate. |
| 58 | DISABLE_RUNNABLE_METHOD_REFCOUNT(ChromeFrameDelegate); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 59 | |
| 60 | extern UINT kAutomationServerReady; |
| 61 | extern UINT kMessageFromChromeFrame; |
| 62 | |
| 63 | class ChromeFrameDelegateImpl : public ChromeFrameDelegate { |
| 64 | public: |
| 65 | virtual WindowType GetWindow() { return NULL; } |
| 66 | virtual void GetBounds(RECT* bounds) {} |
| 67 | virtual std::string GetDocumentUrl() { return std::string(); } |
| 68 | virtual void OnAutomationServerReady() {} |
| 69 | virtual void OnAutomationServerLaunchFailed( |
| 70 | AutomationLaunchResult reason, const std::string& server_version) {} |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 71 | virtual void OnExtensionInstalled( |
| 72 | const FilePath& path, |
| 73 | void* user_data, |
| 74 | AutomationMsg_ExtensionResponseValues response) {} |
[email protected] | a1e62d1 | 2010-03-16 02:18:43 | [diff] [blame] | 75 | virtual void OnGetEnabledExtensionsComplete( |
| 76 | void* user_data, |
| 77 | const std::vector<FilePath>& extension_directories) {} |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 78 | virtual void OnLoadFailed(int error_code, const std::string& url) {} |
| 79 | virtual void OnMessageReceived(const IPC::Message& msg); |
[email protected] | efd4dfc2 | 2010-03-26 20:14:40 | [diff] [blame] | 80 | virtual void OnChannelError() {} |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 81 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 82 | static bool IsTabMessage(const IPC::Message& message, int* tab_handle); |
| 83 | |
| 84 | virtual bool IsValid() const { |
| 85 | return true; |
| 86 | } |
| 87 | |
[email protected] | 2b19e2fe | 2010-02-16 02:24:18 | [diff] [blame] | 88 | virtual void OnHostMoved() {} |
| 89 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 90 | protected: |
| 91 | // Protected methods to be overriden. |
| 92 | virtual void OnNavigationStateChanged(int tab_handle, int flags, |
| 93 | const IPC::NavigationInfo& nav_info) {} |
| 94 | virtual void OnUpdateTargetUrl(int tab_handle, |
| 95 | const std::wstring& new_target_url) {} |
| 96 | virtual void OnAcceleratorPressed(int tab_handle, const MSG& accel_message) {} |
| 97 | virtual void OnTabbedOut(int tab_handle, bool reverse) {} |
| 98 | virtual void OnOpenURL(int tab_handle, const GURL& url, |
[email protected] | b36a9f9 | 2009-10-19 17:34:57 | [diff] [blame] | 99 | const GURL& referrer, int open_disposition) {} |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 100 | virtual void OnDidNavigate(int tab_handle, |
| 101 | const IPC::NavigationInfo& navigation_info) {} |
| 102 | virtual void OnNavigationFailed(int tab_handle, int error_code, |
| 103 | const GURL& gurl) {} |
| 104 | virtual void OnLoad(int tab_handle, const GURL& url) {} |
| 105 | virtual void OnMessageFromChromeFrame(int tab_handle, |
| 106 | const std::string& message, |
| 107 | const std::string& origin, |
| 108 | const std::string& target) {} |
| 109 | virtual void OnHandleContextMenu(int tab_handle, HANDLE menu_handle, |
[email protected] | 35f13ab | 2009-12-16 23:59:17 | [diff] [blame] | 110 | int align_flags, |
| 111 | const IPC::ContextMenuParams& params) {} |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 112 | virtual void OnRequestStart(int tab_handle, int request_id, |
| 113 | const IPC::AutomationURLRequest& request) {} |
| 114 | virtual void OnRequestRead(int tab_handle, int request_id, |
| 115 | int bytes_to_read) {} |
| 116 | virtual void OnRequestEnd(int tab_handle, int request_id, |
| 117 | const URLRequestStatus& status) {} |
[email protected] | fc6fb7fb | 2009-11-07 02:35:04 | [diff] [blame] | 118 | virtual void OnDownloadRequestInHost(int tab_handle, int request_id) {} |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 119 | virtual void OnSetCookieAsync(int tab_handle, const GURL& url, |
| 120 | const std::string& cookie) {} |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 121 | virtual void OnAttachExternalTab(int tab_handle, |
| 122 | const IPC::AttachExternalTabParams& attach_params) {} |
[email protected] | f9cc4c45 | 2009-10-13 14:56:38 | [diff] [blame] | 123 | virtual void OnGoToHistoryEntryOffset(int tab_handle, int offset) {} |
[email protected] | 70daf0b | 2010-03-02 19:13:00 | [diff] [blame] | 124 | |
| 125 | virtual void OnGetCookiesFromHost(int tab_handle, const GURL& url, |
| 126 | int cookie_id) {} |
[email protected] | e16dd167 | 2010-06-07 21:40:29 | [diff] [blame] | 127 | virtual void OnCloseTab(int tab_handle) {} |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 128 | }; |
| 129 | |
[email protected] | efd4dfc2 | 2010-03-26 20:14:40 | [diff] [blame] | 130 | // This interface enables tasks to be marshaled to desired threads. |
| 131 | class TaskMarshaller { // NOLINT |
[email protected] | 5778de6e | 2009-10-24 01:29:20 | [diff] [blame] | 132 | public: |
| 133 | virtual void PostTask(const tracked_objects::Location& from_here, |
| 134 | Task* task) = 0; |
| 135 | }; |
| 136 | |
| 137 | // T is expected to be something CWindowImpl derived, or at least to have |
| 138 | // PostMessage(UINT, WPARAM) method. Do not forget to CHAIN_MSG_MAP |
| 139 | template <class T> class TaskMarshallerThroughWindowsMessages |
| 140 | : public TaskMarshaller { |
| 141 | public: |
[email protected] | 4c3fe4ae | 2010-03-01 20:01:24 | [diff] [blame] | 142 | TaskMarshallerThroughWindowsMessages() {} |
[email protected] | 5778de6e | 2009-10-24 01:29:20 | [diff] [blame] | 143 | virtual void PostTask(const tracked_objects::Location& from_here, |
| 144 | Task* task) { |
| 145 | task->SetBirthPlace(from_here); |
| 146 | T* this_ptr = static_cast<T*>(this); |
| 147 | if (this_ptr->IsWindow()) { |
| 148 | this_ptr->AddRef(); |
[email protected] | 4c3fe4ae | 2010-03-01 20:01:24 | [diff] [blame] | 149 | PushTask(task); |
[email protected] | 5778de6e | 2009-10-24 01:29:20 | [diff] [blame] | 150 | this_ptr->PostMessage(MSG_EXECUTE_TASK, reinterpret_cast<WPARAM>(task)); |
| 151 | } else { |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 152 | DVLOG(1) << "Dropping MSG_EXECUTE_TASK message for destroyed window."; |
[email protected] | 4c3fe4ae | 2010-03-01 20:01:24 | [diff] [blame] | 153 | delete task; |
| 154 | } |
| 155 | } |
| 156 | |
[email protected] | 4c3fe4ae | 2010-03-01 20:01:24 | [diff] [blame] | 157 | protected: |
| 158 | ~TaskMarshallerThroughWindowsMessages() { |
| 159 | DeleteAllPendingTasks(); |
| 160 | } |
| 161 | |
| 162 | void DeleteAllPendingTasks() { |
| 163 | AutoLock lock(lock_); |
[email protected] | 5044da8 | 2010-10-27 01:09:16 | [diff] [blame^] | 164 | DVLOG_IF(1, !pending_tasks_.empty()) << "Destroying " |
| 165 | << pending_tasks_.size() |
| 166 | << " pending tasks"; |
[email protected] | 4c3fe4ae | 2010-03-01 20:01:24 | [diff] [blame] | 167 | while (!pending_tasks_.empty()) { |
| 168 | Task* task = pending_tasks_.front(); |
| 169 | pending_tasks_.pop(); |
| 170 | delete task; |
[email protected] | 5778de6e | 2009-10-24 01:29:20 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | BEGIN_MSG_MAP(PostMessageMarshaller) |
| 175 | MESSAGE_HANDLER(MSG_EXECUTE_TASK, ExecuteTask) |
| 176 | END_MSG_MAP() |
| 177 | |
| 178 | private: |
| 179 | enum { MSG_EXECUTE_TASK = WM_APP + 6 }; |
| 180 | inline LRESULT ExecuteTask(UINT, WPARAM wparam, LPARAM, |
| 181 | BOOL& handled) { // NOLINT |
| 182 | Task* task = reinterpret_cast<Task*>(wparam); |
[email protected] | 3148c0ae | 2010-03-11 18:06:00 | [diff] [blame] | 183 | if (task && PopTask(task)) { |
| 184 | task->Run(); |
| 185 | delete task; |
| 186 | } |
| 187 | |
[email protected] | 5778de6e | 2009-10-24 01:29:20 | [diff] [blame] | 188 | T* this_ptr = static_cast<T*>(this); |
| 189 | this_ptr->Release(); |
| 190 | return 0; |
| 191 | } |
[email protected] | 4c3fe4ae | 2010-03-01 20:01:24 | [diff] [blame] | 192 | |
| 193 | inline void PushTask(Task* task) { |
| 194 | AutoLock lock(lock_); |
| 195 | pending_tasks_.push(task); |
| 196 | } |
| 197 | |
[email protected] | 3148c0ae | 2010-03-11 18:06:00 | [diff] [blame] | 198 | // If the given task is front of the queue, removes the task and returns true, |
| 199 | // otherwise we assume this is an already destroyed task (but Window message |
| 200 | // had remained in the thread queue). |
| 201 | inline bool PopTask(Task* task) { |
[email protected] | 4c3fe4ae | 2010-03-01 20:01:24 | [diff] [blame] | 202 | AutoLock lock(lock_); |
[email protected] | ada3d0b2384 | 2010-03-19 01:04:24 | [diff] [blame] | 203 | if (!pending_tasks_.empty() && task == pending_tasks_.front()) { |
[email protected] | 3148c0ae | 2010-03-11 18:06:00 | [diff] [blame] | 204 | pending_tasks_.pop(); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | return false; |
[email protected] | 4c3fe4ae | 2010-03-01 20:01:24 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | Lock lock_; |
| 212 | std::queue<Task*> pending_tasks_; |
[email protected] | 5778de6e | 2009-10-24 01:29:20 | [diff] [blame] | 213 | }; |
| 214 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 215 | #endif // CHROME_FRAME_CHROME_FRAME_DELEGATE_H_ |