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