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_TAB_CONTENTS_BACKGROUND_CONTENTS_H_
6#define CHROME_BROWSER_TAB_CONTENTS_BACKGROUND_CONTENTS_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/scoped_ptr.h"
13#include "chrome/browser/tab_contents/render_view_host_delegate_helper.h"
14#include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h"
15#include "chrome/common/view_types.h"
16#include "content/browser/renderer_host/render_view_host_delegate.h"
17#include "content/common/notification_registrar.h"
18#include "content/common/window_container_type.h"
19#include "webkit/glue/window_open_disposition.h"
20
21class TabContents;
22struct ExtensionHostMsg_DomMessage_Params;
23struct WebPreferences;
24
25namespace gfx {
26class Rect;
27}
28
29// This class is a peer of TabContents. It can host a renderer, but does not
30// have any visible display. Its navigation is not managed by a
31// NavigationController because is has no facility for navigating (other than
32// programatically view window.location.href) or RenderViewHostManager because
33// it is never allowed to navigate across a SiteInstance boundary.
34class BackgroundContents : public RenderViewHostDelegate,
35                           public RenderViewHostDelegate::View,
36                           public NotificationObserver,
37                           public JavaScriptAppModalDialogDelegate {
38 public:
39  class Delegate {
40   public:
41    // Called by ShowCreatedWindow. Asks the delegate to attach the opened
42    // TabContents to a suitable container (e.g. browser) or to show it if it's
43    // a popup window.
44    virtual void AddTabContents(TabContents* new_contents,
45                                WindowOpenDisposition disposition,
46                                const gfx::Rect& initial_pos,
47                                bool user_gesture) = 0;
48
49   protected:
50    virtual ~Delegate() {}
51  };
52
53  BackgroundContents(SiteInstance* site_instance,
54                     int routing_id,
55                     Delegate* delegate);
56  virtual ~BackgroundContents();
57
58  // Provide access to the RenderViewHost for the
59  // RenderViewHostDelegateViewHelper
60  RenderViewHost* render_view_host() { return render_view_host_; }
61
62  // RenderViewHostDelegate implementation.
63  virtual BackgroundContents* GetAsBackgroundContents();
64  virtual RenderViewHostDelegate::View* GetViewDelegate();
65  virtual const GURL& GetURL() const;
66  virtual ViewType::Type GetRenderViewType() const;
67  virtual int GetBrowserWindowID() const;
68  virtual void DidNavigate(RenderViewHost* render_view_host,
69                           const ViewHostMsg_FrameNavigate_Params& params);
70  virtual WebPreferences GetWebkitPrefs();
71  virtual void ProcessWebUIMessage(
72      const ExtensionHostMsg_DomMessage_Params& params);
73  virtual void RunJavaScriptMessage(const std::wstring& message,
74                                    const std::wstring& default_prompt,
75                                    const GURL& frame_url,
76                                    const int flags,
77                                    IPC::Message* reply_msg,
78                                    bool* did_suppress_message);
79  virtual void Close(RenderViewHost* render_view_host);
80  virtual RendererPreferences GetRendererPrefs(Profile* profile) const;
81  virtual void RenderViewGone(RenderViewHost* rvh,
82                              base::TerminationStatus status,
83                              int error_code);
84
85  // RenderViewHostDelegate::View
86  virtual void CreateNewWindow(
87      int route_id,
88      const ViewHostMsg_CreateWindow_Params& params);
89  virtual void CreateNewWidget(int route_id, WebKit::WebPopupType popup_type);
90  virtual void CreateNewFullscreenWidget(int route_id);
91  virtual void ShowCreatedWindow(int route_id,
92                                 WindowOpenDisposition disposition,
93                                 const gfx::Rect& initial_pos,
94                                 bool user_gesture);
95  virtual void ShowCreatedWidget(int route_id,
96                                 const gfx::Rect& initial_pos);
97  virtual void ShowCreatedFullscreenWidget(int route_id);
98  virtual void ShowContextMenu(const ContextMenuParams& params) {}
99  virtual void ShowPopupMenu(const gfx::Rect& bounds,
100                             int item_height,
101                             double item_font_size,
102                             int selected_item,
103                             const std::vector<WebMenuItem>& items,
104                             bool right_aligned) {}
105  virtual void StartDragging(const WebDropData& drop_data,
106                             WebKit::WebDragOperationsMask allowed_operations,
107                             const SkBitmap& image,
108                             const gfx::Point& image_offset) {}
109  virtual void UpdateDragCursor(WebKit::WebDragOperation operation) {}
110  virtual void GotFocus() {}
111  virtual void TakeFocus(bool reverse) {}
112  virtual void LostCapture() {}
113  virtual void Activate() {}
114  virtual void Deactivate() {}
115  virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
116                                      bool* is_keyboard_shortcut);
117  virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {}
118  virtual void HandleMouseMove() {}
119  virtual void HandleMouseDown() {}
120  virtual void HandleMouseLeave() {}
121  virtual void HandleMouseUp() {}
122  virtual void HandleMouseActivate() {}
123  virtual void UpdatePreferredSize(const gfx::Size& new_size) {}
124
125  // NotificationObserver
126  virtual void Observe(NotificationType type,
127                       const NotificationSource& source,
128                       const NotificationDetails& details);
129
130  // Overridden from JavaScriptAppModalDialogDelegate:
131  virtual void OnMessageBoxClosed(IPC::Message* reply_msg,
132                                  bool success,
133                                  const std::wstring& prompt);
134  virtual void SetSuppressMessageBoxes(bool suppress_message_boxes) {}
135  virtual gfx::NativeWindow GetMessageBoxRootWindow();
136  virtual TabContents* AsTabContents();
137  virtual ExtensionHost* AsExtensionHost();
138
139  virtual void UpdateInspectorSetting(const std::string& key,
140                                      const std::string& value);
141  virtual void ClearInspectorSettings();
142
143  // Helper to find the BackgroundContents that originated the given request.
144  // Can be NULL if the page has been closed or some other error occurs.
145  // Should only be called from the UI thread, since it accesses
146  // BackgroundContents.
147  static BackgroundContents* GetBackgroundContentsByID(int render_process_id,
148                                                       int render_view_id);
149
150 protected:
151  // Exposed for testing.
152  BackgroundContents();
153
154 private:
155  // The delegate for this BackgroundContents.
156  Delegate* delegate_;
157
158  // The host for our HTML content.
159  RenderViewHost* render_view_host_;
160
161  // Common implementations of some RenderViewHostDelegate::View methods.
162  RenderViewHostDelegateViewHelper delegate_view_helper_;
163
164  // The URL being hosted.
165  GURL url_;
166
167  NotificationRegistrar registrar_;
168
169  DISALLOW_COPY_AND_ASSIGN(BackgroundContents);
170};
171
172// This is the data sent out as the details with BACKGROUND_CONTENTS_OPENED.
173struct BackgroundContentsOpenedDetails {
174  // The BackgroundContents object that has just been opened.
175  BackgroundContents* contents;
176
177  // The name of the parent frame for these contents.
178  const string16& frame_name;
179
180  // The ID of the parent application (if any).
181  const string16& application_id;
182};
183
184#endif  // CHROME_BROWSER_TAB_CONTENTS_BACKGROUND_CONTENTS_H_
185