Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / background / background_contents.h
bloba02d9b1df1ac5143f5e5d345110dc5c55d8badba
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.
5 #ifndef CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_H_
6 #define CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_H_
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/observer_list.h"
12 #include "content/public/browser/notification_observer.h"
13 #include "content/public/browser/notification_registrar.h"
14 #include "content/public/browser/web_contents_delegate.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "extensions/browser/deferred_start_render_host.h"
17 #include "ui/base/window_open_disposition.h"
18 #include "url/gurl.h"
20 class Profile;
22 namespace content {
23 class SessionStorageNamespace;
24 class SiteInstance;
27 namespace extensions {
28 class ExtensionHostDelegate;
31 // This class maintains a WebContents used in the background. It can host a
32 // renderer, but does not have any visible display.
33 // TODO(atwilson): Unify this with background pages; http://crbug.com/77790
34 class BackgroundContents : public extensions::DeferredStartRenderHost,
35 public content::WebContentsDelegate,
36 public content::WebContentsObserver,
37 public content::NotificationObserver {
38 public:
39 class Delegate {
40 public:
41 // Called by AddNewContents(). Asks the delegate to attach the opened
42 // WebContents to a suitable container (e.g. browser) or to show it if it's
43 // a popup window. If |was_blocked| is non-NULL, then |*was_blocked| will be
44 // set to true if the popup gets blocked, and left unchanged otherwise.
45 virtual void AddWebContents(content::WebContents* new_contents,
46 WindowOpenDisposition disposition,
47 const gfx::Rect& initial_rect,
48 bool user_gesture,
49 bool* was_blocked) = 0;
51 protected:
52 virtual ~Delegate() {}
55 BackgroundContents(
56 content::SiteInstance* site_instance,
57 int routing_id,
58 int main_frame_routing_id,
59 Delegate* delegate,
60 const std::string& partition_id,
61 content::SessionStorageNamespace* session_storage_namespace);
62 ~BackgroundContents() override;
64 content::WebContents* web_contents() const { return web_contents_.get(); }
65 virtual const GURL& GetURL() const;
67 // Adds this BackgroundContents to the queue of RenderViews to create.
68 void CreateRenderViewSoon(const GURL& url);
70 // content::WebContentsDelegate implementation:
71 void CloseContents(content::WebContents* source) override;
72 bool ShouldSuppressDialogs(content::WebContents* source) override;
73 void DidNavigateMainFramePostCommit(content::WebContents* tab) override;
74 void AddNewContents(content::WebContents* source,
75 content::WebContents* new_contents,
76 WindowOpenDisposition disposition,
77 const gfx::Rect& initial_rect,
78 bool user_gesture,
79 bool* was_blocked) override;
80 bool IsNeverVisible(content::WebContents* web_contents) override;
82 // content::WebContentsObserver implementation:
83 void RenderProcessGone(base::TerminationStatus status) override;
84 void DidStartLoading() override;
85 void DidStopLoading() override;
87 // content::NotificationObserver
88 void Observe(int type,
89 const content::NotificationSource& source,
90 const content::NotificationDetails& details) override;
92 protected:
93 // Exposed for testing.
94 BackgroundContents();
96 private:
97 // extensions::DeferredStartRenderHost implementation:
98 void CreateRenderViewNow() override;
99 void AddDeferredStartRenderHostObserver(
100 extensions::DeferredStartRenderHostObserver* observer) override;
101 void RemoveDeferredStartRenderHostObserver(
102 extensions::DeferredStartRenderHostObserver* observer) override;
104 // The delegate for this BackgroundContents.
105 Delegate* delegate_;
107 // Delegate for choosing an ExtensionHostQueue.
108 scoped_ptr<extensions::ExtensionHostDelegate> extension_host_delegate_;
110 Profile* profile_;
111 scoped_ptr<content::WebContents> web_contents_;
112 content::NotificationRegistrar registrar_;
113 base::ObserverList<extensions::DeferredStartRenderHostObserver>
114 deferred_start_render_host_observer_list_;
116 // The initial URL to load.
117 GURL initial_url_;
119 DISALLOW_COPY_AND_ASSIGN(BackgroundContents);
122 // This is the data sent out as the details with BACKGROUND_CONTENTS_OPENED.
123 struct BackgroundContentsOpenedDetails {
124 // The BackgroundContents object that has just been opened.
125 BackgroundContents* contents;
127 // The name of the parent frame for these contents.
128 const std::string& frame_name;
130 // The ID of the parent application (if any).
131 const base::string16& application_id;
134 #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_CONTENTS_H_