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