• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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/extensions/chrome_notification_observer.h"
6 
7 #include "base/logging.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/common/extensions/features/feature_channel.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/browser/notification_types.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "extensions/browser/extension_system.h"
16 #include "extensions/browser/process_manager.h"
17 #include "extensions/common/extension_messages.h"
18 
19 namespace extensions {
20 
ChromeNotificationObserver()21 ChromeNotificationObserver::ChromeNotificationObserver() {
22   registrar_.Add(this,
23                  chrome::NOTIFICATION_BROWSER_WINDOW_READY,
24                  content::NotificationService::AllSources());
25   registrar_.Add(this,
26                  content::NOTIFICATION_RENDERER_PROCESS_CREATED,
27                  content::NotificationService::AllBrowserContextsAndSources());
28 }
29 
~ChromeNotificationObserver()30 ChromeNotificationObserver::~ChromeNotificationObserver() {}
31 
OnBrowserWindowReady(Browser * browser)32 void ChromeNotificationObserver::OnBrowserWindowReady(Browser* browser) {
33   Profile* profile = browser->profile();
34   DCHECK(profile);
35 
36   // Inform the process manager for this profile that the window is ready.
37   // We continue to observe the notification in case browser windows open for
38   // a related incognito profile or other regular profiles.
39   extensions::ProcessManager* manager =
40       ExtensionSystem::Get(profile)->process_manager();
41   if (!manager)  // Tests may not have a process manager.
42     return;
43   DCHECK_EQ(profile, manager->GetBrowserContext());
44   manager->OnBrowserWindowReady();
45 
46   // For incognito profiles also inform the original profile's process manager
47   // that the window is ready. This will usually be a no-op because the
48   // original profile's process manager should have been informed when the
49   // non-incognito window opened.
50   if (profile->IsOffTheRecord()) {
51     Profile* original_profile = profile->GetOriginalProfile();
52     extensions::ProcessManager* original_manager =
53         ExtensionSystem::Get(original_profile)->process_manager();
54     DCHECK(original_manager);
55     DCHECK_EQ(original_profile, original_manager->GetBrowserContext());
56     original_manager->OnBrowserWindowReady();
57   }
58 }
59 
OnRendererProcessCreated(content::RenderProcessHost * process)60 void ChromeNotificationObserver::OnRendererProcessCreated(
61     content::RenderProcessHost* process) {
62   // Extensions need to know the channel for API restrictions. Send the channel
63   // to all renderers, as the non-extension renderers may have content scripts.
64   process->Send(new ExtensionMsg_SetChannel(GetCurrentChannel()));
65 }
66 
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)67 void ChromeNotificationObserver::Observe(int type,
68                        const content::NotificationSource& source,
69                        const content::NotificationDetails& details) {
70   switch (type) {
71     case chrome::NOTIFICATION_BROWSER_WINDOW_READY: {
72       Browser* browser = content::Source<Browser>(source).ptr();
73       OnBrowserWindowReady(browser);
74       break;
75     }
76 
77     case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
78       content::RenderProcessHost* process =
79           content::Source<content::RenderProcessHost>(source).ptr();
80       OnRendererProcessCreated(process);
81       break;
82     }
83 
84     default:
85       NOTREACHED();
86   }
87 }
88 
89 }  // namespace extensions
90