blob: b57cc5b8896dffd0b482d692b2ea9f11fe18b6bc [file] [log] [blame]
[email protected]2b9a9f162010-10-19 20:30:451// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]f7817822009-09-24 05:11:582// 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]efd4dfc22010-03-26 20:14:4011#include <string>
12#include <vector>
[email protected]5778de6e2009-10-24 01:29:2013
[email protected]a1e62d12010-03-16 02:18:4314#include "base/file_path.h"
[email protected]4c3fe4ae2010-03-01 20:01:2415#include "base/lock.h"
[email protected]f7817822009-09-24 05:11:5816#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.
21class ChromeFrameDelegate {
22 public:
[email protected]f7817822009-09-24 05:11:5823 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]00f6b772009-10-23 17:03:4131 virtual void OnExtensionInstalled(
32 const FilePath& path,
33 void* user_data,
34 AutomationMsg_ExtensionResponseValues response) = 0;
[email protected]a1e62d12010-03-16 02:18:4335 virtual void OnGetEnabledExtensionsComplete(
36 void* user_data,
37 const std::vector<FilePath>& extension_directories) = 0;
[email protected]f7817822009-09-24 05:11:5838 virtual void OnMessageReceived(const IPC::Message& msg) = 0;
[email protected]efd4dfc22010-03-26 20:14:4039 virtual void OnChannelError() = 0;
[email protected]f7817822009-09-24 05:11:5840
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]2b19e2fe2010-02-16 02:24:1849 // To be called when the top-most window of an application hosting
50 // ChromeFrame is moved.
51 virtual void OnHostMoved() = 0;
52
[email protected]f7817822009-09-24 05:11:5853 protected:
[email protected]efd4dfc22010-03-26 20:14:4054 virtual ~ChromeFrameDelegate() {}
[email protected]f7817822009-09-24 05:11:5855};
56
[email protected]c56428f22010-06-16 02:17:2357// Disable refcounting of ChromeFrameDelegate.
58DISABLE_RUNNABLE_METHOD_REFCOUNT(ChromeFrameDelegate);
[email protected]f7817822009-09-24 05:11:5859
60extern UINT kAutomationServerReady;
61extern UINT kMessageFromChromeFrame;
62
63class 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]00f6b772009-10-23 17:03:4171 virtual void OnExtensionInstalled(
72 const FilePath& path,
73 void* user_data,
74 AutomationMsg_ExtensionResponseValues response) {}
[email protected]a1e62d12010-03-16 02:18:4375 virtual void OnGetEnabledExtensionsComplete(
76 void* user_data,
77 const std::vector<FilePath>& extension_directories) {}
[email protected]f7817822009-09-24 05:11:5878 virtual void OnLoadFailed(int error_code, const std::string& url) {}
79 virtual void OnMessageReceived(const IPC::Message& msg);
[email protected]efd4dfc22010-03-26 20:14:4080 virtual void OnChannelError() {}
[email protected]00f6b772009-10-23 17:03:4181
[email protected]f7817822009-09-24 05:11:5882 static bool IsTabMessage(const IPC::Message& message, int* tab_handle);
83
84 virtual bool IsValid() const {
85 return true;
86 }
87
[email protected]2b19e2fe2010-02-16 02:24:1888 virtual void OnHostMoved() {}
89
[email protected]f7817822009-09-24 05:11:5890 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]b36a9f92009-10-19 17:34:5799 const GURL& referrer, int open_disposition) {}
[email protected]f7817822009-09-24 05:11:58100 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]35f13ab2009-12-16 23:59:17110 int align_flags,
111 const IPC::ContextMenuParams& params) {}
[email protected]f7817822009-09-24 05:11:58112 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]fc6fb7fb2009-11-07 02:35:04118 virtual void OnDownloadRequestInHost(int tab_handle, int request_id) {}
[email protected]f7817822009-09-24 05:11:58119 virtual void OnSetCookieAsync(int tab_handle, const GURL& url,
120 const std::string& cookie) {}
[email protected]b1c55638612010-03-08 16:26:11121 virtual void OnAttachExternalTab(int tab_handle,
122 const IPC::AttachExternalTabParams& attach_params) {}
[email protected]f9cc4c452009-10-13 14:56:38123 virtual void OnGoToHistoryEntryOffset(int tab_handle, int offset) {}
[email protected]70daf0b2010-03-02 19:13:00124
125 virtual void OnGetCookiesFromHost(int tab_handle, const GURL& url,
126 int cookie_id) {}
[email protected]e16dd1672010-06-07 21:40:29127 virtual void OnCloseTab(int tab_handle) {}
[email protected]f7817822009-09-24 05:11:58128};
129
[email protected]efd4dfc22010-03-26 20:14:40130// This interface enables tasks to be marshaled to desired threads.
131class TaskMarshaller { // NOLINT
[email protected]5778de6e2009-10-24 01:29:20132 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
139template <class T> class TaskMarshallerThroughWindowsMessages
140 : public TaskMarshaller {
141 public:
[email protected]4c3fe4ae2010-03-01 20:01:24142 TaskMarshallerThroughWindowsMessages() {}
[email protected]5778de6e2009-10-24 01:29:20143 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]4c3fe4ae2010-03-01 20:01:24149 PushTask(task);
[email protected]5778de6e2009-10-24 01:29:20150 this_ptr->PostMessage(MSG_EXECUTE_TASK, reinterpret_cast<WPARAM>(task));
151 } else {
[email protected]2b9a9f162010-10-19 20:30:45152 DVLOG(1) << "Dropping MSG_EXECUTE_TASK message for destroyed window.";
[email protected]4c3fe4ae2010-03-01 20:01:24153 delete task;
154 }
155 }
156
[email protected]4c3fe4ae2010-03-01 20:01:24157 protected:
158 ~TaskMarshallerThroughWindowsMessages() {
159 DeleteAllPendingTasks();
160 }
161
162 void DeleteAllPendingTasks() {
163 AutoLock lock(lock_);
[email protected]5044da82010-10-27 01:09:16164 DVLOG_IF(1, !pending_tasks_.empty()) << "Destroying "
165 << pending_tasks_.size()
166 << " pending tasks";
[email protected]4c3fe4ae2010-03-01 20:01:24167 while (!pending_tasks_.empty()) {
168 Task* task = pending_tasks_.front();
169 pending_tasks_.pop();
170 delete task;
[email protected]5778de6e2009-10-24 01:29:20171 }
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]3148c0ae2010-03-11 18:06:00183 if (task && PopTask(task)) {
184 task->Run();
185 delete task;
186 }
187
[email protected]5778de6e2009-10-24 01:29:20188 T* this_ptr = static_cast<T*>(this);
189 this_ptr->Release();
190 return 0;
191 }
[email protected]4c3fe4ae2010-03-01 20:01:24192
193 inline void PushTask(Task* task) {
194 AutoLock lock(lock_);
195 pending_tasks_.push(task);
196 }
197
[email protected]3148c0ae2010-03-11 18:06:00198 // 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]4c3fe4ae2010-03-01 20:01:24202 AutoLock lock(lock_);
[email protected]ada3d0b23842010-03-19 01:04:24203 if (!pending_tasks_.empty() && task == pending_tasks_.front()) {
[email protected]3148c0ae2010-03-11 18:06:00204 pending_tasks_.pop();
205 return true;
206 }
207
208 return false;
[email protected]4c3fe4ae2010-03-01 20:01:24209 }
210
211 Lock lock_;
212 std::queue<Task*> pending_tasks_;
[email protected]5778de6e2009-10-24 01:29:20213};
214
[email protected]f7817822009-09-24 05:11:58215#endif // CHROME_FRAME_CHROME_FRAME_DELEGATE_H_