| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_LAZY_BACKGROUND_TASK_QUEUE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_LAZY_BACKGROUND_TASK_QUEUE_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_LAZY_BACKGROUND_TASK_QUEUE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_LAZY_BACKGROUND_TASK_QUEUE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> |
| 10 #include <string> | 11 #include <string> |
| 11 | 12 |
| 12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 13 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 14 #include "base/memory/linked_ptr.h" | 15 #include "base/memory/linked_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" |
| 15 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 16 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 17 | 19 |
| 18 class Extension; | 20 class Extension; |
| 19 class ExtensionHost; | 21 class ExtensionHost; |
| 20 class Profile; | 22 class Profile; |
| 21 | 23 |
| 22 namespace extensions { | 24 namespace extensions { |
| 23 | 25 |
| 24 // This class maintains a queue of tasks that should execute when an | 26 // This class maintains a queue of tasks that should execute when an |
| 25 // extension's lazy background page is loaded. It is also in charge of loading | 27 // extension's lazy background page is loaded. It is also in charge of loading |
| 26 // the page when the first task is queued. | 28 // the page when the first task is queued. |
| 27 // | 29 // |
| 28 // It is the consumer's responsibility to use this class when appropriate, i.e. | 30 // It is the consumer's responsibility to use this class when appropriate, i.e. |
| 29 // only with extensions that have not-yet-loaded lazy background pages. | 31 // only with extensions that have not-yet-loaded lazy background pages. |
| 30 class LazyBackgroundTaskQueue : public content::NotificationObserver { | 32 class LazyBackgroundTaskQueue |
| 33 : public content::NotificationObserver, |
| 34 public base::SupportsWeakPtr<LazyBackgroundTaskQueue> { |
| 31 public: | 35 public: |
| 32 typedef base::Callback<void(ExtensionHost*)> PendingTask; | 36 typedef base::Callback<void(ExtensionHost*)> PendingTask; |
| 33 | 37 |
| 34 explicit LazyBackgroundTaskQueue(Profile* profile); | 38 explicit LazyBackgroundTaskQueue(Profile* profile); |
| 35 virtual ~LazyBackgroundTaskQueue(); | 39 virtual ~LazyBackgroundTaskQueue(); |
| 36 | 40 |
| 37 // Returns true if the task should be added to the queue (that is, if the | 41 // Returns true if the task should be added to the queue (that is, if the |
| 38 // extension has a lazy background page that isn't ready yet). | 42 // extension has a lazy background page that isn't ready yet). |
| 39 bool ShouldEnqueueTask(Profile* profile, const Extension* extension); | 43 bool ShouldEnqueueTask(Profile* profile, const Extension* extension); |
| 40 | 44 |
| 41 // Adds a task to the queue for a given extension. If this is the first | 45 // Adds a task to the queue for a given extension. If this is the first |
| 42 // task added for the extension, its lazy background page will be loaded. | 46 // task added for the extension, its lazy background page will be loaded. |
| 43 // The task will be called either when the page is loaded, or when the | 47 // The task will be called either when the page is loaded, or when the |
| 44 // page fails to load for some reason (e.g. a crash). In the latter case, | 48 // page fails to load for some reason (e.g. a crash). In the latter case, |
| 45 // the ExtensionHost parameter is NULL. | 49 // the ExtensionHost parameter is NULL. |
| 46 void AddPendingTask( | 50 void AddPendingTask( |
| 47 Profile* profile, | 51 Profile* profile, |
| 48 const std::string& extension_id, | 52 const std::string& extension_id, |
| 49 const PendingTask& task); | 53 const PendingTask& task); |
| 50 | 54 |
| 51 private: | 55 private: |
| 52 // A map between an extension_id,Profile pair and the queue of tasks pending | 56 // A map between an extension_id,Profile pair and the queue of tasks pending |
| 53 // the load of its background page. | 57 // the load of its background page. |
| 54 typedef std::string ExtensionID; | 58 typedef std::string ExtensionID; |
| 55 typedef std::pair<Profile*, ExtensionID> PendingTasksKey; | 59 typedef std::pair<Profile*, ExtensionID> PendingTasksKey; |
| 56 typedef std::vector<PendingTask> PendingTasksList; | 60 typedef std::vector<PendingTask> PendingTasksList; |
| 57 typedef std::map<PendingTasksKey, | 61 typedef std::map<PendingTasksKey, |
| 58 linked_ptr<PendingTasksList> > PendingTasksMap; | 62 linked_ptr<PendingTasksList> > PendingTasksMap; |
| 63 typedef std::set<PendingTasksKey> PendingPageLoadList; |
| 64 |
| 65 void StartLazyBackgroundPage(Profile* profile, |
| 66 const std::string& extension_id); |
| 59 | 67 |
| 60 // content::NotificationObserver interface. | 68 // content::NotificationObserver interface. |
| 61 virtual void Observe(int type, | 69 virtual void Observe(int type, |
| 62 const content::NotificationSource& source, | 70 const content::NotificationSource& source, |
| 63 const content::NotificationDetails& details) OVERRIDE; | 71 const content::NotificationDetails& details) OVERRIDE; |
| 64 | 72 |
| 65 // Called when a lazy background page has finished loading, or has failed to | 73 // Called when a lazy background page has finished loading, or has failed to |
| 66 // load (host is NULL in that case). All enqueued tasks are run in order. | 74 // load (host is NULL in that case). All enqueued tasks are run in order. |
| 67 void ProcessPendingTasks( | 75 void ProcessPendingTasks( |
| 68 ExtensionHost* host, | 76 ExtensionHost* host, |
| 69 Profile* profile, | 77 Profile* profile, |
| 70 const Extension* extension); | 78 const Extension* extension); |
| 71 | 79 |
| 72 Profile* profile_; | 80 Profile* profile_; |
| 73 content::NotificationRegistrar registrar_; | 81 content::NotificationRegistrar registrar_; |
| 74 PendingTasksMap pending_tasks_; | 82 PendingTasksMap pending_tasks_; |
| 83 PendingPageLoadList pending_page_loads_; |
| 75 }; | 84 }; |
| 76 | 85 |
| 77 } // namespace extensions | 86 } // namespace extensions |
| 78 | 87 |
| 79 #endif // CHROME_BROWSER_EXTENSIONS_LAZY_BACKGROUND_TASK_QUEUE_H_ | 88 #endif // CHROME_BROWSER_EXTENSIONS_LAZY_BACKGROUND_TASK_QUEUE_H_ |
| OLD | NEW |