Durable Storage: Refactor browser test and test the basic "deny" flow.
[chromium-blink-merge.git] / extensions / browser / lazy_background_task_queue.h
blob52cbd91f85994ee8b09e0fbe9311e65e55d6e3ee
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_
8 #include <map>
9 #include <string>
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 "components/keyed_service/core/keyed_service.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "extensions/browser/extension_registry_observer.h"
21 namespace content {
22 class BrowserContext;
25 namespace extensions {
26 class Extension;
27 class ExtensionHost;
28 class ExtensionRegistry;
30 // This class maintains a queue of tasks that should execute when an
31 // extension's lazy background page is loaded. It is also in charge of loading
32 // the page when the first task is queued.
34 // It is the consumer's responsibility to use this class when appropriate, i.e.
35 // only with extensions that have not-yet-loaded lazy background pages.
36 class LazyBackgroundTaskQueue : public KeyedService,
37 public content::NotificationObserver,
38 public ExtensionRegistryObserver {
39 public:
40 typedef base::Callback<void(ExtensionHost*)> PendingTask;
42 explicit LazyBackgroundTaskQueue(content::BrowserContext* browser_context);
43 ~LazyBackgroundTaskQueue() override;
45 // Convenience method to return the LazyBackgroundTaskQueue for a given
46 // |context|.
47 static LazyBackgroundTaskQueue* Get(content::BrowserContext* context);
49 // Returns the number of extensions having pending tasks.
50 size_t extensions_with_pending_tasks() { return pending_tasks_.size(); }
52 // Returns true if the task should be added to the queue (that is, if the
53 // extension has a lazy background page that isn't ready yet). If the
54 // extension has a lazy background page that is being suspended this method
55 // cancels that suspension.
56 bool ShouldEnqueueTask(content::BrowserContext* context,
57 const Extension* extension);
59 // Adds a task to the queue for a given extension. If this is the first
60 // task added for the extension, its lazy background page will be loaded.
61 // The task will be called either when the page is loaded, or when the
62 // page fails to load for some reason (e.g. a crash or browser
63 // shutdown). In the latter case, the ExtensionHost parameter is NULL.
64 void AddPendingTask(
65 content::BrowserContext* context,
66 const std::string& extension_id,
67 const PendingTask& task);
69 private:
70 FRIEND_TEST_ALL_PREFIXES(LazyBackgroundTaskQueueTest, ProcessPendingTasks);
72 // A map between a BrowserContext/extension_id pair and the queue of tasks
73 // pending the load of its background page.
74 typedef std::string ExtensionID;
75 typedef std::pair<content::BrowserContext*, ExtensionID> PendingTasksKey;
76 typedef std::vector<PendingTask> PendingTasksList;
77 typedef std::map<PendingTasksKey,
78 linked_ptr<PendingTasksList> > PendingTasksMap;
80 // content::NotificationObserver interface.
81 void Observe(int type,
82 const content::NotificationSource& source,
83 const content::NotificationDetails& details) override;
85 // ExtensionRegistryObserver interface.
86 void OnExtensionUnloaded(content::BrowserContext* browser_context,
87 const Extension* extension,
88 UnloadedExtensionInfo::Reason reason) override;
90 // Called when a lazy background page has finished loading, or has failed to
91 // load (host is NULL in that case). All enqueued tasks are run in order.
92 void ProcessPendingTasks(
93 ExtensionHost* host,
94 content::BrowserContext* context,
95 const Extension* extension);
97 content::BrowserContext* browser_context_;
98 content::NotificationRegistrar registrar_;
99 PendingTasksMap pending_tasks_;
101 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
102 extension_registry_observer_;
105 } // namespace extensions
107 #endif // EXTENSIONS_BROWSER_LAZY_BACKGROUND_TASK_QUEUE_H_