blob: 63409b90aa32cf1421dea8dcad985b2c9dc073aa [file] [log] [blame]
[email protected]f7817822009-09-24 05:11:581// 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]5778de6e2009-10-24 01:29:208#include <atlbase.h>
9#include <atlwin.h>
[email protected]4c3fe4ae2010-03-01 20:01:2410#include <queue>
[email protected]5778de6e2009-10-24 01:29:2011
[email protected]4c3fe4ae2010-03-01 20:01:2412#include "base/lock.h"
[email protected]f7817822009-09-24 05:11:5813#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.
18class ChromeFrameDelegate {
19 public:
[email protected]f7817822009-09-24 05:11:5820 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]00f6b772009-10-23 17:03:4128 virtual void OnExtensionInstalled(
29 const FilePath& path,
30 void* user_data,
31 AutomationMsg_ExtensionResponseValues response) = 0;
[email protected]f7817822009-09-24 05:11:5832 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]2b19e2fe2010-02-16 02:24:1842 // To be called when the top-most window of an application hosting
43 // ChromeFrame is moved.
44 virtual void OnHostMoved() = 0;
45
[email protected]f7817822009-09-24 05:11:5846 protected:
47 ~ChromeFrameDelegate() {}
48};
49
50// Template specialization
51template <> struct RunnableMethodTraits<ChromeFrameDelegate> {
[email protected]39fe32a2009-09-30 04:29:2052 void RetainCallee(ChromeFrameDelegate* obj) {}
53 void ReleaseCallee(ChromeFrameDelegate* obj) {}
[email protected]f7817822009-09-24 05:11:5854};
55
56extern UINT kAutomationServerReady;
57extern UINT kMessageFromChromeFrame;
58
59class 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]00f6b772009-10-23 17:03:4167 virtual void OnExtensionInstalled(
68 const FilePath& path,
69 void* user_data,
70 AutomationMsg_ExtensionResponseValues response) {}
[email protected]f7817822009-09-24 05:11:5871 virtual void OnLoadFailed(int error_code, const std::string& url) {}
72 virtual void OnMessageReceived(const IPC::Message& msg);
[email protected]00f6b772009-10-23 17:03:4173
[email protected]f7817822009-09-24 05:11:5874 static bool IsTabMessage(const IPC::Message& message, int* tab_handle);
75
76 virtual bool IsValid() const {
77 return true;
78 }
79
[email protected]2b19e2fe2010-02-16 02:24:1880 virtual void OnHostMoved() {}
81
[email protected]f7817822009-09-24 05:11:5882 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]b36a9f92009-10-19 17:34:5791 const GURL& referrer, int open_disposition) {}
[email protected]f7817822009-09-24 05:11:5892 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]35f13ab2009-12-16 23:59:17102 int align_flags,
103 const IPC::ContextMenuParams& params) {}
[email protected]f7817822009-09-24 05:11:58104 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]fc6fb7fb2009-11-07 02:35:04110 virtual void OnDownloadRequestInHost(int tab_handle, int request_id) {}
[email protected]f7817822009-09-24 05:11:58111 virtual void OnSetCookieAsync(int tab_handle, const GURL& url,
112 const std::string& cookie) {}
[email protected]b1c55638612010-03-08 16:26:11113 virtual void OnAttachExternalTab(int tab_handle,
114 const IPC::AttachExternalTabParams& attach_params) {}
[email protected]f9cc4c452009-10-13 14:56:38115 virtual void OnGoToHistoryEntryOffset(int tab_handle, int offset) {}
[email protected]70daf0b2010-03-02 19:13:00116
117 virtual void OnGetCookiesFromHost(int tab_handle, const GURL& url,
118 int cookie_id) {}
[email protected]f7817822009-09-24 05:11:58119};
120
[email protected]5778de6e2009-10-24 01:29:20121// This interface enables tasks to be marshalled to desired threads.
122class 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
130template <class T> class TaskMarshallerThroughWindowsMessages
131 : public TaskMarshaller {
132 public:
[email protected]4c3fe4ae2010-03-01 20:01:24133 TaskMarshallerThroughWindowsMessages() {}
[email protected]5778de6e2009-10-24 01:29:20134 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]4c3fe4ae2010-03-01 20:01:24140 PushTask(task);
[email protected]5778de6e2009-10-24 01:29:20141 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]4c3fe4ae2010-03-01 20:01:24144 delete task;
145 }
146 }
147
[email protected]4c3fe4ae2010-03-01 20:01:24148 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]5778de6e2009-10-24 01:29:20161 }
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]3148c0ae2010-03-11 18:06:00173 if (task && PopTask(task)) {
174 task->Run();
175 delete task;
176 }
177
[email protected]5778de6e2009-10-24 01:29:20178 T* this_ptr = static_cast<T*>(this);
179 this_ptr->Release();
180 return 0;
181 }
[email protected]4c3fe4ae2010-03-01 20:01:24182
183 inline void PushTask(Task* task) {
184 AutoLock lock(lock_);
185 pending_tasks_.push(task);
186 }
187
[email protected]3148c0ae2010-03-11 18:06:00188 // 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]4c3fe4ae2010-03-01 20:01:24192 AutoLock lock(lock_);
[email protected]3148c0ae2010-03-11 18:06:00193 if (task == pending_tasks_.front()) {
194 pending_tasks_.pop();
195 return true;
196 }
197
198 return false;
[email protected]4c3fe4ae2010-03-01 20:01:24199 }
200
201 Lock lock_;
202 std::queue<Task*> pending_tasks_;
[email protected]5778de6e2009-10-24 01:29:20203};
204
[email protected]f7817822009-09-24 05:11:58205#endif // CHROME_FRAME_CHROME_FRAME_DELEGATE_H_