1// Copyright (c) 2011 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_BROWSER_NOTIFICATIONS_BALLOON_HOST_H_
6#define CHROME_BROWSER_NOTIFICATIONS_BALLOON_HOST_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/scoped_ptr.h"
13#include "chrome/browser/extensions/extension_function_dispatcher.h"
14#include "chrome/browser/tab_contents/render_view_host_delegate_helper.h"
15#include "content/browser/renderer_host/render_view_host_delegate.h"
16#include "content/common/notification_observer.h"
17#include "content/common/notification_registrar.h"
18
19class Balloon;
20class Browser;
21class Profile;
22class SiteInstance;
23struct RendererPreferences;
24struct WebPreferences;
25
26class BalloonHost : public RenderViewHostDelegate,
27                    public RenderViewHostDelegate::View,
28                    public ExtensionFunctionDispatcher::Delegate,
29                    public NotificationObserver {
30 public:
31  explicit BalloonHost(Balloon* balloon);
32
33  // Initialize the view.
34  void Init();
35
36  // Stops showing the balloon.
37  void Shutdown();
38
39  // ExtensionFunctionDispatcher::Delegate overrides.
40  virtual Browser* GetBrowser();
41  virtual gfx::NativeView GetNativeViewOfHost();
42  virtual TabContents* associated_tab_contents() const;
43
44  RenderViewHost* render_view_host() const { return render_view_host_; }
45
46  const string16& GetSource() const;
47
48  // RenderViewHostDelegate overrides.
49  virtual WebPreferences GetWebkitPrefs();
50  virtual SiteInstance* GetSiteInstance() const;
51  virtual Profile* GetProfile() const;
52  virtual const GURL& GetURL() const;
53  virtual void Close(RenderViewHost* render_view_host);
54  virtual void RenderViewCreated(RenderViewHost* render_view_host);
55  virtual void RenderViewReady(RenderViewHost* render_view_host);
56  virtual void RenderViewGone(RenderViewHost* render_view_host,
57                              base::TerminationStatus status,
58                              int error_code);
59  virtual void UpdateTitle(RenderViewHost* render_view_host,
60                           int32 page_id, const std::wstring& title) {}
61  virtual int GetBrowserWindowID() const;
62  virtual ViewType::Type GetRenderViewType() const;
63  virtual RenderViewHostDelegate::View* GetViewDelegate();
64  virtual void ProcessWebUIMessage(
65      const ExtensionHostMsg_DomMessage_Params& params);
66
67  // NotificationObserver override.
68  virtual void Observe(NotificationType type,
69                       const NotificationSource& source,
70                       const NotificationDetails& details);
71
72
73  // RenderViewHostDelegate::View methods. Only the ones for opening new
74  // windows are currently implemented.
75  virtual void CreateNewWindow(
76      int route_id,
77      const ViewHostMsg_CreateWindow_Params& params);
78  virtual void CreateNewWidget(int route_id, WebKit::WebPopupType popup_type) {}
79  virtual void CreateNewFullscreenWidget(int route_id) {}
80  virtual void ShowCreatedWindow(int route_id,
81                                 WindowOpenDisposition disposition,
82                                 const gfx::Rect& initial_pos,
83                                 bool user_gesture);
84  virtual void ShowCreatedWidget(int route_id,
85                                 const gfx::Rect& initial_pos) {}
86  virtual void ShowCreatedFullscreenWidget(int route_id) {}
87  virtual void ShowContextMenu(const ContextMenuParams& params) {}
88  virtual void ShowPopupMenu(const gfx::Rect& bounds,
89                             int item_height,
90                             double item_font_size,
91                             int selected_item,
92                             const std::vector<WebMenuItem>& items,
93                             bool right_aligned) {}
94  virtual void StartDragging(const WebDropData& drop_data,
95                             WebKit::WebDragOperationsMask allowed_ops) {}
96  virtual void StartDragging(const WebDropData&,
97                             WebKit::WebDragOperationsMask,
98                             const SkBitmap&,
99                             const gfx::Point&) {}
100  virtual void UpdateDragCursor(WebKit::WebDragOperation operation) {}
101  virtual void GotFocus() {}
102  virtual void TakeFocus(bool reverse) {}
103  virtual void LostCapture() {}
104  virtual void Activate() {}
105  virtual void Deactivate() {}
106  virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
107                                      bool* is_keyboard_shortcut);
108  virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {}
109  virtual void HandleMouseMove() {}
110  virtual void HandleMouseDown();
111  virtual void HandleMouseLeave() {}
112  virtual void HandleMouseUp() {}
113  virtual void HandleMouseActivate() {}
114  virtual void UpdatePreferredSize(const gfx::Size& pref_size);
115  virtual RendererPreferences GetRendererPrefs(Profile* profile) const;
116
117  // Enable Web UI. This has to be called before renderer is created.
118  void EnableWebUI();
119
120  virtual void UpdateInspectorSetting(const std::string& key,
121                                      const std::string& value);
122  virtual void ClearInspectorSettings();
123
124 protected:
125  virtual ~BalloonHost();
126  // Must override in platform specific implementations.
127  virtual void InitRenderWidgetHostView() = 0;
128  virtual RenderWidgetHostView* render_widget_host_view() const = 0;
129
130  // Owned pointer to the host for the renderer process.
131  RenderViewHost* render_view_host_;
132
133 private:
134  // Called to send an event that the balloon has been disconnected from
135  // a renderer (if should_notify_on_disconnect_ is true).
136  void NotifyDisconnect();
137
138  // Non-owned pointer to the associated balloon.
139  Balloon* balloon_;
140
141  // True after Init() has completed.
142  bool initialized_;
143
144  // Indicates whether we should notify about disconnection of this balloon.
145  // This is used to ensure disconnection notifications only happen if
146  // a connection notification has happened and that they happen only once.
147  bool should_notify_on_disconnect_;
148
149  // Site instance for the balloon/profile, to be used for opening new links.
150  scoped_refptr<SiteInstance> site_instance_;
151
152  // Common implementations of some RenderViewHostDelegate::View methods.
153  RenderViewHostDelegateViewHelper delegate_view_helper_;
154
155  // Handles requests to extension APIs. Will only be non-NULL if we are
156  // rendering a page from an extension.
157  scoped_ptr<ExtensionFunctionDispatcher> extension_function_dispatcher_;
158
159  // A flag to enable Web UI.
160  bool enable_web_ui_;
161
162  NotificationRegistrar registrar_;
163};
164
165#endif  // CHROME_BROWSER_NOTIFICATIONS_BALLOON_HOST_H_
166