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 8#include <string> 9 10#include "base/memory/scoped_ptr.h" 11#include "content/public/browser/notification_observer.h" 12#include "content/public/browser/notification_registrar.h" 13#include "content/public/browser/web_contents_delegate.h" 14#include "content/public/browser/web_contents_observer.h" 15#include "ui/base/window_open_disposition.h" 16 17class Profile; 18 19namespace content { 20class SessionStorageNamespace; 21class SiteInstance; 22}; 23 24// This class consumes WebContents. It can host a renderer, but does not 25// have any visible display. 26class BackgroundContents : public content::WebContentsDelegate, 27 public content::WebContentsObserver, 28 public content::NotificationObserver { 29 public: 30 class Delegate { 31 public: 32 // Called by AddNewContents(). Asks the delegate to attach the opened 33 // WebContents to a suitable container (e.g. browser) or to show it if it's 34 // a popup window. If |was_blocked| is non-NULL, then |*was_blocked| will be 35 // set to true if the popup gets blocked, and left unchanged otherwise. 36 virtual void AddWebContents(content::WebContents* new_contents, 37 WindowOpenDisposition disposition, 38 const gfx::Rect& initial_pos, 39 bool user_gesture, 40 bool* was_blocked) = 0; 41 42 protected: 43 virtual ~Delegate() {} 44 }; 45 46 BackgroundContents( 47 content::SiteInstance* site_instance, 48 int routing_id, 49 Delegate* delegate, 50 const std::string& partition_id, 51 content::SessionStorageNamespace* session_storage_namespace); 52 virtual ~BackgroundContents(); 53 54 content::WebContents* web_contents() const { return web_contents_.get(); } 55 virtual const GURL& GetURL() const; 56 57 // content::WebContentsDelegate implementation: 58 virtual void CloseContents(content::WebContents* source) OVERRIDE; 59 virtual bool ShouldSuppressDialogs() OVERRIDE; 60 virtual void DidNavigateMainFramePostCommit( 61 content::WebContents* tab) OVERRIDE; 62 virtual void AddNewContents(content::WebContents* source, 63 content::WebContents* new_contents, 64 WindowOpenDisposition disposition, 65 const gfx::Rect& initial_pos, 66 bool user_gesture, 67 bool* was_blocked) OVERRIDE; 68 virtual bool IsNeverVisible(content::WebContents* web_contents) OVERRIDE; 69 70 // content::WebContentsObserver implementation: 71 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; 72 73 // content::NotificationObserver 74 virtual void Observe(int type, 75 const content::NotificationSource& source, 76 const content::NotificationDetails& details) OVERRIDE; 77 78 protected: 79 // Exposed for testing. 80 BackgroundContents(); 81 82 private: 83 // The delegate for this BackgroundContents. 84 Delegate* delegate_; 85 86 Profile* profile_; 87 scoped_ptr<content::WebContents> web_contents_; 88 content::NotificationRegistrar registrar_; 89 90 DISALLOW_COPY_AND_ASSIGN(BackgroundContents); 91}; 92 93// This is the data sent out as the details with BACKGROUND_CONTENTS_OPENED. 94struct BackgroundContentsOpenedDetails { 95 // The BackgroundContents object that has just been opened. 96 BackgroundContents* contents; 97 98 // The name of the parent frame for these contents. 99 const base::string16& frame_name; 100 101 // The ID of the parent application (if any). 102 const base::string16& application_id; 103}; 104 105#endif // CHROME_BROWSER_TAB_CONTENTS_BACKGROUND_CONTENTS_H_ 106