| 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 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // only with extensions that have not-yet-loaded lazy background pages. | 31 // only with extensions that have not-yet-loaded lazy background pages. |
| 32 class LazyBackgroundTaskQueue | 32 class LazyBackgroundTaskQueue |
| 33 : public content::NotificationObserver, | 33 : public content::NotificationObserver, |
| 34 public base::SupportsWeakPtr<LazyBackgroundTaskQueue> { | 34 public base::SupportsWeakPtr<LazyBackgroundTaskQueue> { |
| 35 public: | 35 public: |
| 36 typedef base::Callback<void(ExtensionHost*)> PendingTask; | 36 typedef base::Callback<void(ExtensionHost*)> PendingTask; |
| 37 | 37 |
| 38 explicit LazyBackgroundTaskQueue(Profile* profile); | 38 explicit LazyBackgroundTaskQueue(Profile* profile); |
| 39 virtual ~LazyBackgroundTaskQueue(); | 39 virtual ~LazyBackgroundTaskQueue(); |
| 40 | 40 |
| 41 // Returns true if the task should be added to the queue (that is, if the | |
| 42 // extension has a lazy background page that isn't ready yet). | |
| 43 bool ShouldEnqueueTask(Profile* profile, const Extension* extension); | |
| 44 | |
| 45 // Adds a task to the queue for a given extension. If this is the first | 41 // Adds a task to the queue for a given extension. If this is the first |
| 46 // task added for the extension, its lazy background page will be loaded. | 42 // task added for the extension, its lazy background page will be loaded. |
| 47 // The task will be called either when the page is loaded, or when the | 43 // The task will be called either when the page is loaded, or when the |
| 48 // page fails to load for some reason (e.g. a crash). In the latter case, | 44 // page fails to load for some reason (e.g. a crash). In the latter case, |
| 49 // the ExtensionHost parameter is NULL. | 45 // the ExtensionHost parameter is NULL. If there is currently a background |
| 46 // page available to handle the task, any suspend the page was in the middle |
| 47 // of will be canceled and the task will be executed with the background page |
| 48 // as an argument. If the extension id isn't lazy, |task| will be executed |
| 49 // straight away. |
| 50 void AddPendingTask( | 50 void AddPendingTask( |
| 51 Profile* profile, | 51 Profile* profile, |
| 52 const std::string& extension_id, | 52 const std::string& extension_id, |
| 53 const PendingTask& task); | 53 const PendingTask& task); |
| 54 | 54 |
| 55 private: | 55 private: |
| 56 // 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 |
| 57 // the load of its background page. | 57 // the load of its background page. |
| 58 typedef std::string ExtensionID; | 58 typedef std::string ExtensionID; |
| 59 typedef std::pair<Profile*, ExtensionID> PendingTasksKey; | 59 typedef std::pair<Profile*, ExtensionID> PendingTasksKey; |
| 60 typedef std::vector<PendingTask> PendingTasksList; | 60 typedef std::vector<PendingTask> PendingTasksList; |
| 61 typedef std::map<PendingTasksKey, | 61 typedef std::map<PendingTasksKey, |
| 62 linked_ptr<PendingTasksList> > PendingTasksMap; | 62 linked_ptr<PendingTasksList> > PendingTasksMap; |
| 63 typedef std::set<PendingTasksKey> PendingPageLoadList; | 63 typedef std::set<PendingTasksKey> PendingPageLoadList; |
| 64 | 64 |
| 65 // Returns true if the task should be added to the queue (that is, if the |
| 66 // extension has a lazy background page that isn't ready yet). |
| 67 bool ShouldEnqueueTask(Profile* profile, const Extension* extension); |
| 68 |
| 65 void StartLazyBackgroundPage(Profile* profile, | 69 void StartLazyBackgroundPage(Profile* profile, |
| 66 const std::string& extension_id); | 70 const std::string& extension_id); |
| 67 | 71 |
| 72 // Executes |task| on an existing ExtensionHost if it exists. If the host is |
| 73 // in the middle of a suspend, that suspend is canceled. Returns true if |
| 74 // |task| was executed. |
| 75 bool ExecuteTaskNowIfPossible(Profile* profile, |
| 76 const std::string& extension_id, |
| 77 const PendingTask& task); |
| 78 |
| 68 // content::NotificationObserver interface. | 79 // content::NotificationObserver interface. |
| 69 virtual void Observe(int type, | 80 virtual void Observe(int type, |
| 70 const content::NotificationSource& source, | 81 const content::NotificationSource& source, |
| 71 const content::NotificationDetails& details) OVERRIDE; | 82 const content::NotificationDetails& details) OVERRIDE; |
| 72 | 83 |
| 73 // Called when a lazy background page has finished loading, or has failed to | 84 // Called when a lazy background page has finished loading, or has failed to |
| 74 // load (host is NULL in that case). All enqueued tasks are run in order. | 85 // load (host is NULL in that case). All enqueued tasks are run in order. |
| 75 void ProcessPendingTasks( | 86 void ProcessPendingTasks( |
| 76 ExtensionHost* host, | 87 ExtensionHost* host, |
| 77 Profile* profile, | 88 Profile* profile, |
| 78 const Extension* extension); | 89 const Extension* extension); |
| 79 | 90 |
| 80 Profile* profile_; | 91 Profile* profile_; |
| 81 content::NotificationRegistrar registrar_; | 92 content::NotificationRegistrar registrar_; |
| 82 PendingTasksMap pending_tasks_; | 93 PendingTasksMap pending_tasks_; |
| 83 PendingPageLoadList pending_page_loads_; | 94 PendingPageLoadList pending_page_loads_; |
| 84 }; | 95 }; |
| 85 | 96 |
| 86 } // namespace extensions | 97 } // namespace extensions |
| 87 | 98 |
| 88 #endif // CHROME_BROWSER_EXTENSIONS_LAZY_BACKGROUND_TASK_QUEUE_H_ | 99 #endif // CHROME_BROWSER_EXTENSIONS_LAZY_BACKGROUND_TASK_QUEUE_H_ |
| OLD | NEW |