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.
5 #ifndef EXTENSIONS_BROWSER_LAZY_BACKGROUND_TASK_QUEUE_H_
6 #define EXTENSIONS_BROWSER_LAZY_BACKGROUND_TASK_QUEUE_H_
11 #include "base/callback_forward.h"
12 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/scoped_observer.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "extensions/browser/extension_registry_observer.h"
24 namespace extensions
{
27 class ExtensionRegistry
;
29 // This class maintains a queue of tasks that should execute when an
30 // extension's lazy background page is loaded. It is also in charge of loading
31 // the page when the first task is queued.
33 // It is the consumer's responsibility to use this class when appropriate, i.e.
34 // only with extensions that have not-yet-loaded lazy background pages.
35 class LazyBackgroundTaskQueue
: public content::NotificationObserver
,
36 public ExtensionRegistryObserver
{
38 typedef base::Callback
<void(ExtensionHost
*)> PendingTask
;
40 explicit LazyBackgroundTaskQueue(content::BrowserContext
* browser_context
);
41 ~LazyBackgroundTaskQueue() override
;
43 // Returns the number of extensions having pending tasks.
44 size_t extensions_with_pending_tasks() { return pending_tasks_
.size(); }
46 // Returns true if the task should be added to the queue (that is, if the
47 // extension has a lazy background page that isn't ready yet). If the
48 // extension has a lazy background page that is being suspended this method
49 // cancels that suspension.
50 bool ShouldEnqueueTask(content::BrowserContext
* context
,
51 const Extension
* extension
);
53 // Adds a task to the queue for a given extension. If this is the first
54 // task added for the extension, its lazy background page will be loaded.
55 // The task will be called either when the page is loaded, or when the
56 // page fails to load for some reason (e.g. a crash or browser
57 // shutdown). In the latter case, the ExtensionHost parameter is NULL.
59 content::BrowserContext
* context
,
60 const std::string
& extension_id
,
61 const PendingTask
& task
);
64 FRIEND_TEST_ALL_PREFIXES(LazyBackgroundTaskQueueTest
, ProcessPendingTasks
);
66 // A map between a BrowserContext/extension_id pair and the queue of tasks
67 // pending the load of its background page.
68 typedef std::string ExtensionID
;
69 typedef std::pair
<content::BrowserContext
*, ExtensionID
> PendingTasksKey
;
70 typedef std::vector
<PendingTask
> PendingTasksList
;
71 typedef std::map
<PendingTasksKey
,
72 linked_ptr
<PendingTasksList
> > PendingTasksMap
;
74 // content::NotificationObserver interface.
75 void Observe(int type
,
76 const content::NotificationSource
& source
,
77 const content::NotificationDetails
& details
) override
;
79 // ExtensionRegistryObserver interface.
80 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
81 const Extension
* extension
,
82 UnloadedExtensionInfo::Reason reason
) override
;
84 // Called when a lazy background page has finished loading, or has failed to
85 // load (host is NULL in that case). All enqueued tasks are run in order.
86 void ProcessPendingTasks(
88 content::BrowserContext
* context
,
89 const Extension
* extension
);
91 content::BrowserContext
* browser_context_
;
92 content::NotificationRegistrar registrar_
;
93 PendingTasksMap pending_tasks_
;
95 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
96 extension_registry_observer_
;
99 } // namespace extensions
101 #endif // EXTENSIONS_BROWSER_LAZY_BACKGROUND_TASK_QUEUE_H_