1 // Copyright (c) 2012 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 #include "chrome/browser/tab_contents/background_contents.h" 6 7 #include "chrome/browser/background/background_contents_service.h" 8 #include "chrome/browser/chrome_notification_types.h" 9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/renderer_preferences_util.h" 11 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" 12 #include "chrome/common/extensions/extension_constants.h" 13 #include "chrome/common/url_constants.h" 14 #include "content/public/browser/notification_service.h" 15 #include "content/public/browser/render_view_host.h" 16 #include "content/public/browser/site_instance.h" 17 #include "content/public/browser/web_contents.h" 18 #include "extensions/browser/view_type_utils.h" 19 #include "ui/gfx/rect.h" 20 21 using content::SiteInstance; 22 using content::WebContents; 23 24 BackgroundContents::BackgroundContents(SiteInstance* site_instance, 25 int routing_id, 26 Delegate* delegate) 27 : delegate_(delegate) { 28 profile_ = Profile::FromBrowserContext( 29 site_instance->GetBrowserContext()); 30 31 // TODO(rafaelw): Implement correct session storage. 32 WebContents::CreateParams create_params(profile_, site_instance); 33 create_params.routing_id = routing_id; 34 web_contents_.reset(WebContents::Create(create_params)); 35 extensions::SetViewType( 36 web_contents_.get(), extensions::VIEW_TYPE_BACKGROUND_CONTENTS); 37 web_contents_->SetDelegate(this); 38 content::WebContentsObserver::Observe(web_contents_.get()); 39 40 // Close ourselves when the application is shutting down. 41 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, 42 content::NotificationService::AllSources()); 43 44 // Register for our parent profile to shutdown, so we can shut ourselves down 45 // as well (should only be called for OTR profiles, as we should receive 46 // APP_TERMINATING before non-OTR profiles are destroyed). 47 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, 48 content::Source<Profile>(profile_)); 49 } 50 51 // Exposed to allow creating mocks. 52 BackgroundContents::BackgroundContents() 53 : delegate_(NULL), 54 profile_(NULL) { 55 } 56 57 BackgroundContents::~BackgroundContents() { 58 if (!web_contents_.get()) // Will be null for unit tests. 59 return; 60 61 // Unregister for any notifications before notifying observers that we are 62 // going away - this prevents any re-entrancy due to chained notifications 63 // (http://crbug.com/237781). 64 registrar_.RemoveAll(); 65 66 content::NotificationService::current()->Notify( 67 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, 68 content::Source<Profile>(profile_), 69 content::Details<BackgroundContents>(this)); 70 } 71 72 const GURL& BackgroundContents::GetURL() const { 73 return web_contents_.get() ? web_contents_->GetURL() : GURL::EmptyGURL(); 74 } 75 76 void BackgroundContents::CloseContents(WebContents* source) { 77 content::NotificationService::current()->Notify( 78 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED, 79 content::Source<Profile>(profile_), 80 content::Details<BackgroundContents>(this)); 81 delete this; 82 } 83 84 bool BackgroundContents::ShouldSuppressDialogs() { 85 return true; 86 } 87 88 void BackgroundContents::DidNavigateMainFramePostCommit(WebContents* tab) { 89 // Note: because BackgroundContents are only available to extension apps, 90 // navigation is limited to urls within the app's extent. This is enforced in 91 // RenderView::decidePolicyForNavigation. If BackgroundContents become 92 // available as a part of the web platform, it probably makes sense to have 93 // some way to scope navigation of a background page to its opener's security 94 // origin. Note: if the first navigation is to a URL outside the app's 95 // extent a background page will be opened but will remain at about:blank. 96 content::NotificationService::current()->Notify( 97 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED, 98 content::Source<Profile>(profile_), 99 content::Details<BackgroundContents>(this)); 100 } 101 102 // Forward requests to add a new WebContents to our delegate. 103 void BackgroundContents::AddNewContents(WebContents* source, 104 WebContents* new_contents, 105 WindowOpenDisposition disposition, 106 const gfx::Rect& initial_pos, 107 bool user_gesture, 108 bool* was_blocked) { 109 delegate_->AddWebContents( 110 new_contents, disposition, initial_pos, user_gesture, was_blocked); 111 } 112 113 void BackgroundContents::RenderProcessGone(base::TerminationStatus status) { 114 content::NotificationService::current()->Notify( 115 chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED, 116 content::Source<Profile>(profile_), 117 content::Details<BackgroundContents>(this)); 118 119 // Our RenderView went away, so we should go away also, so killing the process 120 // via the TaskManager doesn't permanently leave a BackgroundContents hanging 121 // around the system, blocking future instances from being created 122 // <http://crbug.com/65189>. 123 delete this; 124 } 125 126 void BackgroundContents::Observe(int type, 127 const content::NotificationSource& source, 128 const content::NotificationDetails& details) { 129 // TODO(rafaelw): Implement pagegroup ref-counting so that non-persistent 130 // background pages are closed when the last referencing frame is closed. 131 switch (type) { 132 case chrome::NOTIFICATION_PROFILE_DESTROYED: 133 case chrome::NOTIFICATION_APP_TERMINATING: { 134 delete this; 135 break; 136 } 137 default: 138 NOTREACHED() << "Unexpected notification sent."; 139 break; 140 } 141 } 142