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 17 class Profile; 18 19 namespace content { 20 class SessionStorageNamespace; 21 class SiteInstance; 22 }; 23 24 // This class consumes WebContents. It can host a renderer, but does not 25 // have any visible display. 26 class 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 69 // content::WebContentsObserver implementation: 70 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; 71 72 // content::NotificationObserver 73 virtual void Observe(int type, 74 const content::NotificationSource& source, 75 const content::NotificationDetails& details) OVERRIDE; 76 77 protected: 78 // Exposed for testing. 79 BackgroundContents(); 80 81 private: 82 // The delegate for this BackgroundContents. 83 Delegate* delegate_; 84 85 Profile* profile_; 86 scoped_ptr<content::WebContents> web_contents_; 87 content::NotificationRegistrar registrar_; 88 89 DISALLOW_COPY_AND_ASSIGN(BackgroundContents); 90 }; 91 92 // This is the data sent out as the details with BACKGROUND_CONTENTS_OPENED. 93 struct BackgroundContentsOpenedDetails { 94 // The BackgroundContents object that has just been opened. 95 BackgroundContents* contents; 96 97 // The name of the parent frame for these contents. 98 const base::string16& frame_name; 99 100 // The ID of the parent application (if any). 101 const base::string16& application_id; 102 }; 103 104 #endif // CHROME_BROWSER_TAB_CONTENTS_BACKGROUND_CONTENTS_H_ 105