blob: a43d451178bf7310bc3015310408a0dbf70fb5e8 [file] [log] [blame]
[email protected]27a112c2011-01-06 04:19:301// Copyright (c) 2011 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_
[email protected]27a112c2011-01-06 04:19:307#pragma once
[email protected]f7817822009-09-24 05:11:588
[email protected]5778de6e2009-10-24 01:29:209#include <atlbase.h>
10#include <atlwin.h>
[email protected]4c3fe4ae2010-03-01 20:01:2411#include <queue>
[email protected]efd4dfc22010-03-26 20:14:4012#include <string>
13#include <vector>
[email protected]5778de6e2009-10-24 01:29:2014
[email protected]a1e62d12010-03-16 02:18:4315#include "base/file_path.h"
[email protected]20305ec2011-01-21 04:55:5216#include "base/synchronization/lock.h"
[email protected]a22f7e02011-02-09 07:15:3517#include "base/task.h"
18#include "chrome/common/automation_constants.h"
[email protected]f7817822009-09-24 05:11:5819#include "ipc/ipc_message.h"
20
[email protected]a22f7e02011-02-09 07:15:3521class GURL;
22struct AttachExternalTabParams;
23struct AutomationURLRequest;
24struct MiniContextMenuParams;
25struct NavigationInfo;
26
27namespace net {
28class URLRequestStatus;
29}
30
[email protected]f7817822009-09-24 05:11:5831// A common interface supported by all the browser specific ChromeFrame
32// implementations.
33class ChromeFrameDelegate {
34 public:
[email protected]f7817822009-09-24 05:11:5835 typedef HWND WindowType;
36
37 virtual WindowType GetWindow() const = 0;
38 virtual void GetBounds(RECT* bounds) = 0;
39 virtual std::string GetDocumentUrl() = 0;
40 virtual void OnAutomationServerReady() = 0;
41 virtual void OnAutomationServerLaunchFailed(
42 AutomationLaunchResult reason, const std::string& server_version) = 0;
[email protected]00f6b772009-10-23 17:03:4143 virtual void OnExtensionInstalled(
44 const FilePath& path,
45 void* user_data,
46 AutomationMsg_ExtensionResponseValues response) = 0;
[email protected]a1e62d12010-03-16 02:18:4347 virtual void OnGetEnabledExtensionsComplete(
48 void* user_data,
49 const std::vector<FilePath>& extension_directories) = 0;
[email protected]a95986a82010-12-24 06:19:2850 virtual bool OnMessageReceived(const IPC::Message& msg) = 0;
[email protected]efd4dfc22010-03-26 20:14:4051 virtual void OnChannelError() = 0;
[email protected]f7817822009-09-24 05:11:5852
53 // This remains in interface since we call it if Navigate()
54 // returns immediate error.
55 virtual void OnLoadFailed(int error_code, const std::string& url) = 0;
56
57 // Returns true if this instance is alive and well for processing automation
58 // messages.
59 virtual bool IsValid() const = 0;
60
[email protected]2b19e2fe2010-02-16 02:24:1861 // To be called when the top-most window of an application hosting
62 // ChromeFrame is moved.
63 virtual void OnHostMoved() = 0;
64
[email protected]f7817822009-09-24 05:11:5865 protected:
[email protected]efd4dfc22010-03-26 20:14:4066 virtual ~ChromeFrameDelegate() {}
[email protected]f7817822009-09-24 05:11:5867};
68
[email protected]c56428f22010-06-16 02:17:2369// Disable refcounting of ChromeFrameDelegate.
70DISABLE_RUNNABLE_METHOD_REFCOUNT(ChromeFrameDelegate);
[email protected]f7817822009-09-24 05:11:5871
72extern UINT kAutomationServerReady;
73extern UINT kMessageFromChromeFrame;
74
75class ChromeFrameDelegateImpl : public ChromeFrameDelegate {
76 public:
77 virtual WindowType GetWindow() { return NULL; }
78 virtual void GetBounds(RECT* bounds) {}
79 virtual std::string GetDocumentUrl() { return std::string(); }
80 virtual void OnAutomationServerReady() {}
81 virtual void OnAutomationServerLaunchFailed(
82 AutomationLaunchResult reason, const std::string& server_version) {}
[email protected]00f6b772009-10-23 17:03:4183 virtual void OnExtensionInstalled(
84 const FilePath& path,
85 void* user_data,
86 AutomationMsg_ExtensionResponseValues response) {}
[email protected]a1e62d12010-03-16 02:18:4387 virtual void OnGetEnabledExtensionsComplete(
88 void* user_data,
89 const std::vector<FilePath>& extension_directories) {}
[email protected]f7817822009-09-24 05:11:5890 virtual void OnLoadFailed(int error_code, const std::string& url) {}
[email protected]a95986a82010-12-24 06:19:2891 virtual bool OnMessageReceived(const IPC::Message& msg);
[email protected]efd4dfc22010-03-26 20:14:4092 virtual void OnChannelError() {}
[email protected]00f6b772009-10-23 17:03:4193
[email protected]f5494d42010-12-23 22:15:3494 static bool IsTabMessage(const IPC::Message& message);
[email protected]f7817822009-09-24 05:11:5895
96 virtual bool IsValid() const {
97 return true;
98 }
99
[email protected]2b19e2fe2010-02-16 02:24:18100 virtual void OnHostMoved() {}
101
[email protected]f7817822009-09-24 05:11:58102 protected:
103 // Protected methods to be overriden.
[email protected]f5494d42010-12-23 22:15:34104 virtual void OnNavigationStateChanged(
105 int flags, const NavigationInfo& nav_info) {}
106 virtual void OnUpdateTargetUrl(const std::wstring& new_target_url) {}
107 virtual void OnAcceleratorPressed(const MSG& accel_message) {}
108 virtual void OnTabbedOut(bool reverse) {}
109 virtual void OnOpenURL(
110 const GURL& url, const GURL& referrer, int open_disposition) {}
111 virtual void OnDidNavigate(const NavigationInfo& navigation_info) {}
112 virtual void OnNavigationFailed(int error_code, const GURL& gurl) {}
113 virtual void OnLoad(const GURL& url) {}
114 virtual void OnMessageFromChromeFrame(const std::string& message,
[email protected]f7817822009-09-24 05:11:58115 const std::string& origin,
116 const std::string& target) {}
[email protected]f5494d42010-12-23 22:15:34117 virtual void OnHandleContextMenu(HANDLE menu_handle, int align_flags,
118 const MiniContextMenuParams& params) {}
119 virtual void OnRequestStart(
120 int request_id, const AutomationURLRequest& request) {}
121 virtual void OnRequestRead(int request_id, int bytes_to_read) {}
[email protected]27a112c2011-01-06 04:19:30122 virtual void OnRequestEnd(int request_id,
123 const net::URLRequestStatus& status) {}
[email protected]f5494d42010-12-23 22:15:34124 virtual void OnDownloadRequestInHost(int request_id) {}
125 virtual void OnSetCookieAsync(const GURL& url, const std::string& cookie) {}
126 virtual void OnAttachExternalTab(
127 const AttachExternalTabParams& attach_params) {}
128 virtual void OnGoToHistoryEntryOffset(int offset) {}
[email protected]70daf0b2010-03-02 19:13:00129
[email protected]f5494d42010-12-23 22:15:34130 virtual void OnGetCookiesFromHost(const GURL& url, int cookie_id) {}
131 virtual void OnCloseTab() {}
[email protected]f7817822009-09-24 05:11:58132};
133
[email protected]efd4dfc22010-03-26 20:14:40134// This interface enables tasks to be marshaled to desired threads.
135class TaskMarshaller { // NOLINT
[email protected]5778de6e2009-10-24 01:29:20136 public:
137 virtual void PostTask(const tracked_objects::Location& from_here,
138 Task* task) = 0;
139};
140
141// T is expected to be something CWindowImpl derived, or at least to have
142// PostMessage(UINT, WPARAM) method. Do not forget to CHAIN_MSG_MAP
143template <class T> class TaskMarshallerThroughWindowsMessages
144 : public TaskMarshaller {
145 public:
[email protected]4c3fe4ae2010-03-01 20:01:24146 TaskMarshallerThroughWindowsMessages() {}
[email protected]5778de6e2009-10-24 01:29:20147 virtual void PostTask(const tracked_objects::Location& from_here,
148 Task* task) {
149 task->SetBirthPlace(from_here);
150 T* this_ptr = static_cast<T*>(this);
151 if (this_ptr->IsWindow()) {
152 this_ptr->AddRef();
[email protected]4c3fe4ae2010-03-01 20:01:24153 PushTask(task);
[email protected]5778de6e2009-10-24 01:29:20154 this_ptr->PostMessage(MSG_EXECUTE_TASK, reinterpret_cast<WPARAM>(task));
155 } else {
[email protected]2b9a9f162010-10-19 20:30:45156 DVLOG(1) << "Dropping MSG_EXECUTE_TASK message for destroyed window.";
[email protected]4c3fe4ae2010-03-01 20:01:24157 delete task;
158 }
159 }
160
[email protected]4c3fe4ae2010-03-01 20:01:24161 protected:
162 ~TaskMarshallerThroughWindowsMessages() {
163 DeleteAllPendingTasks();
164 }
165
166 void DeleteAllPendingTasks() {
[email protected]20305ec2011-01-21 04:55:52167 base::AutoLock lock(lock_);
[email protected]5044da82010-10-27 01:09:16168 DVLOG_IF(1, !pending_tasks_.empty()) << "Destroying "
169 << pending_tasks_.size()
170 << " pending tasks";
[email protected]4c3fe4ae2010-03-01 20:01:24171 while (!pending_tasks_.empty()) {
172 Task* task = pending_tasks_.front();
173 pending_tasks_.pop();
174 delete task;
[email protected]5778de6e2009-10-24 01:29:20175 }
176 }
177
178 BEGIN_MSG_MAP(PostMessageMarshaller)
179 MESSAGE_HANDLER(MSG_EXECUTE_TASK, ExecuteTask)
180 END_MSG_MAP()
181
182 private:
183 enum { MSG_EXECUTE_TASK = WM_APP + 6 };
184 inline LRESULT ExecuteTask(UINT, WPARAM wparam, LPARAM,
185 BOOL& handled) { // NOLINT
186 Task* task = reinterpret_cast<Task*>(wparam);
[email protected]3148c0ae2010-03-11 18:06:00187 if (task && PopTask(task)) {
188 task->Run();
189 delete task;
190 }
191
[email protected]5778de6e2009-10-24 01:29:20192 T* this_ptr = static_cast<T*>(this);
193 this_ptr->Release();
194 return 0;
195 }
[email protected]4c3fe4ae2010-03-01 20:01:24196
197 inline void PushTask(Task* task) {
[email protected]20305ec2011-01-21 04:55:52198 base::AutoLock lock(lock_);
[email protected]4c3fe4ae2010-03-01 20:01:24199 pending_tasks_.push(task);
200 }
201
[email protected]3148c0ae2010-03-11 18:06:00202 // If the given task is front of the queue, removes the task and returns true,
203 // otherwise we assume this is an already destroyed task (but Window message
204 // had remained in the thread queue).
205 inline bool PopTask(Task* task) {
[email protected]20305ec2011-01-21 04:55:52206 base::AutoLock lock(lock_);
[email protected]ada3d0b23842010-03-19 01:04:24207 if (!pending_tasks_.empty() && task == pending_tasks_.front()) {
[email protected]3148c0ae2010-03-11 18:06:00208 pending_tasks_.pop();
209 return true;
210 }
211
212 return false;
[email protected]4c3fe4ae2010-03-01 20:01:24213 }
214
[email protected]20305ec2011-01-21 04:55:52215 base::Lock lock_;
[email protected]4c3fe4ae2010-03-01 20:01:24216 std::queue<Task*> pending_tasks_;
[email protected]5778de6e2009-10-24 01:29:20217};
218
[email protected]f7817822009-09-24 05:11:58219#endif // CHROME_FRAME_CHROME_FRAME_DELEGATE_H_