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