| 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 #include "chrome/browser/extensions/api/offscreen_tabs/offscreen_tabs_api.h" | 5 #include "chrome/browser/extensions/api/offscreen_tabs/offscreen_tabs_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "chrome/browser/extensions/api/offscreen_tabs/offscreen_tabs_constants.
h" | 18 #include "chrome/browser/extensions/api/offscreen_tabs/offscreen_tabs_constants.
h" |
| 19 #include "chrome/browser/extensions/api/tabs/tabs.h" | 19 #include "chrome/browser/extensions/api/tabs/tabs.h" |
| 20 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 20 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 21 #include "chrome/browser/extensions/extension_event_names.h" | 21 #include "chrome/browser/extensions/extension_event_names.h" |
| 22 #include "chrome/browser/extensions/extension_event_router.h" | 22 #include "chrome/browser/extensions/extension_event_router.h" |
| 23 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 23 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 24 #include "chrome/browser/extensions/extension_service.h" | 24 #include "chrome/browser/extensions/extension_service.h" |
| 25 #include "chrome/browser/extensions/extension_tab_util.h" | 25 #include "chrome/browser/extensions/extension_tab_util.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
| 28 #include "chrome/browser/ui/browser_window.h" | 28 #include "chrome/browser/ui/browser_window.h" |
| 29 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 29 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 30 #include "chrome/browser/ui/window_sizer.h" | 30 #include "chrome/browser/ui/window_sizer.h" |
| 31 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" |
| 32 #include "chrome/common/extensions/extension.h" | 32 #include "chrome/common/extensions/extension.h" |
| 33 #include "chrome/common/extensions/extension_error_utils.h" | 33 #include "chrome/common/extensions/extension_error_utils.h" |
| 34 #include "chrome/common/extensions/extension_messages.h" | 34 #include "chrome/common/extensions/extension_messages.h" |
| 35 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
| 36 #include "chrome/common/url_constants.h" | 36 #include "chrome/common/url_constants.h" |
| 37 #include "content/public/browser/navigation_controller.h" | 37 #include "content/public/browser/navigation_controller.h" |
| 38 #include "content/public/browser/notification_details.h" | 38 #include "content/public/browser/notification_details.h" |
| 39 #include "content/public/browser/notification_source.h" | 39 #include "content/public/browser/notification_source.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 60 class OffscreenTab : public content::NotificationObserver { | 60 class OffscreenTab : public content::NotificationObserver { |
| 61 public: | 61 public: |
| 62 OffscreenTab(); | 62 OffscreenTab(); |
| 63 virtual ~OffscreenTab(); | 63 virtual ~OffscreenTab(); |
| 64 void Init(const GURL& url, | 64 void Init(const GURL& url, |
| 65 const int width, | 65 const int width, |
| 66 const int height, | 66 const int height, |
| 67 Profile* profile, | 67 Profile* profile, |
| 68 ParentTab* parent_tab); | 68 ParentTab* parent_tab); |
| 69 | 69 |
| 70 TabContentsWrapper* tab_contents() const { | 70 TabContents* tab_contents() const { |
| 71 return tab_contents_wrapper_.get(); | 71 return tab_contents_.get(); |
| 72 } | 72 } |
| 73 WebContents* web_contents() const { | 73 WebContents* web_contents() const { |
| 74 return tab_contents()->web_contents(); | 74 return tab_contents()->web_contents(); |
| 75 } | 75 } |
| 76 int GetID() const { return ExtensionTabUtil::GetTabId(web_contents()); } | 76 int GetID() const { return ExtensionTabUtil::GetTabId(web_contents()); } |
| 77 ParentTab* parent_tab() { return parent_tab_; } | 77 ParentTab* parent_tab() { return parent_tab_; } |
| 78 | 78 |
| 79 // Creates a representation of this OffscreenTab for use by the API methods. | 79 // Creates a representation of this OffscreenTab for use by the API methods. |
| 80 // Passes ownership to the caller. | 80 // Passes ownership to the caller. |
| 81 DictionaryValue* CreateValue() const; | 81 DictionaryValue* CreateValue() const; |
| 82 | 82 |
| 83 // Navigates the tab to the |url|. | 83 // Navigates the tab to the |url|. |
| 84 void NavigateToURL(const GURL& url) { | 84 void NavigateToURL(const GURL& url) { |
| 85 web_contents()->GetController().LoadURL( | 85 web_contents()->GetController().LoadURL( |
| 86 url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); | 86 url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); |
| 87 } | 87 } |
| 88 | 88 |
| 89 // Adjusts the tab's dimensions to the specified |width| and |height|. | 89 // Adjusts the tab's dimensions to the specified |width| and |height|. |
| 90 void SetSize(int width, int height) { | 90 void SetSize(int width, int height) { |
| 91 // TODO(jstritar): this doesn't seem to work on ChromeOS. | 91 // TODO(jstritar): this doesn't seem to work on ChromeOS. |
| 92 web_contents()->GetView()->SizeContents(gfx::Size(width, height)); | 92 web_contents()->GetView()->SizeContents(gfx::Size(width, height)); |
| 93 } | 93 } |
| 94 | 94 |
| 95 private: | 95 private: |
| 96 virtual void Observe(int type, | 96 virtual void Observe(int type, |
| 97 const NotificationSource& source, | 97 const NotificationSource& source, |
| 98 const NotificationDetails& details) OVERRIDE; | 98 const NotificationDetails& details) OVERRIDE; |
| 99 | 99 |
| 100 content::NotificationRegistrar registrar_; | 100 content::NotificationRegistrar registrar_; |
| 101 scoped_ptr<TabContentsWrapper> tab_contents_wrapper_; | 101 scoped_ptr<TabContents> tab_contents_; |
| 102 ParentTab* parent_tab_; | 102 ParentTab* parent_tab_; |
| 103 | 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(OffscreenTab); | 104 DISALLOW_COPY_AND_ASSIGN(OffscreenTab); |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 typedef ScopedVector<OffscreenTab> OffscreenTabs; | 107 typedef ScopedVector<OffscreenTab> OffscreenTabs; |
| 108 | 108 |
| 109 // Holds info about a tab that has spawned at least one offscreen tab. | 109 // Holds info about a tab that has spawned at least one offscreen tab. |
| 110 // Each ParentTab keeps track of its child offscreen tabs. The ParentTab is also | 110 // Each ParentTab keeps track of its child offscreen tabs. The ParentTab is also |
| 111 // responsible for killing its children when it navigates away or gets closed. | 111 // responsible for killing its children when it navigates away or gets closed. |
| 112 class ParentTab : public content::NotificationObserver { | 112 class ParentTab : public content::NotificationObserver { |
| 113 public: | 113 public: |
| 114 ParentTab(); | 114 ParentTab(); |
| 115 virtual ~ParentTab(); | 115 virtual ~ParentTab(); |
| 116 void Init(WebContents* web_contents, | 116 void Init(WebContents* web_contents, |
| 117 const std::string& extension_id); | 117 const std::string& extension_id); |
| 118 | 118 |
| 119 TabContentsWrapper* tab_contents() { return tab_contents_wrapper_; } | 119 TabContents* tab_contents() { return tab_contents_; } |
| 120 int GetID() { return ExtensionTabUtil::GetTabId(web_contents()); } | 120 int GetID() { return ExtensionTabUtil::GetTabId(web_contents()); } |
| 121 WebContents* web_contents() { | 121 WebContents* web_contents() { |
| 122 return tab_contents()->web_contents(); | 122 return tab_contents()->web_contents(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 // Returns the offscreen tabs spawned by this tab. | 125 // Returns the offscreen tabs spawned by this tab. |
| 126 const OffscreenTabs& offscreen_tabs() { return offscreen_tabs_; } | 126 const OffscreenTabs& offscreen_tabs() { return offscreen_tabs_; } |
| 127 const std::string& extension_id() const { return extension_id_; } | 127 const std::string& extension_id() const { return extension_id_; } |
| 128 | 128 |
| 129 // Tab takes ownership of OffscreenTab. | 129 // Tab takes ownership of OffscreenTab. |
| 130 void AddOffscreenTab(OffscreenTab *tab); | 130 void AddOffscreenTab(OffscreenTab *tab); |
| 131 | 131 |
| 132 // Removes the offscreen |tab| and returns true if this parent has no more | 132 // Removes the offscreen |tab| and returns true if this parent has no more |
| 133 // offscreen tabs. | 133 // offscreen tabs. |
| 134 bool RemoveOffscreenTab(OffscreenTab *tab); | 134 bool RemoveOffscreenTab(OffscreenTab *tab); |
| 135 | 135 |
| 136 private: | 136 private: |
| 137 virtual void Observe(int type, | 137 virtual void Observe(int type, |
| 138 const NotificationSource& source, | 138 const NotificationSource& source, |
| 139 const NotificationDetails& details) OVERRIDE; | 139 const NotificationDetails& details) OVERRIDE; |
| 140 | 140 |
| 141 content::NotificationRegistrar registrar_; | 141 content::NotificationRegistrar registrar_; |
| 142 | 142 |
| 143 TabContentsWrapper* tab_contents_wrapper_; | 143 TabContents* tab_contents_; |
| 144 OffscreenTabs offscreen_tabs_; | 144 OffscreenTabs offscreen_tabs_; |
| 145 std::string extension_id_; | 145 std::string extension_id_; |
| 146 | 146 |
| 147 DISALLOW_COPY_AND_ASSIGN(ParentTab); | 147 DISALLOW_COPY_AND_ASSIGN(ParentTab); |
| 148 }; | 148 }; |
| 149 | 149 |
| 150 // This map keeps track of all tabs that are happy parents of offscreen tabs. | 150 // This map keeps track of all tabs that are happy parents of offscreen tabs. |
| 151 class OffscreenTabMap { | 151 class OffscreenTabMap { |
| 152 public: | 152 public: |
| 153 OffscreenTabMap(); | 153 OffscreenTabMap(); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 OffscreenTab::~OffscreenTab() {} | 221 OffscreenTab::~OffscreenTab() {} |
| 222 | 222 |
| 223 void OffscreenTab::Init(const GURL& url, | 223 void OffscreenTab::Init(const GURL& url, |
| 224 const int width, | 224 const int width, |
| 225 const int height, | 225 const int height, |
| 226 Profile* profile, | 226 Profile* profile, |
| 227 ParentTab* parent_tab) { | 227 ParentTab* parent_tab) { |
| 228 // Create the offscreen tab. | 228 // Create the offscreen tab. |
| 229 WebContents* web_contents = WebContents::Create( | 229 WebContents* web_contents = WebContents::Create( |
| 230 profile, NULL, MSG_ROUTING_NONE, NULL, NULL); | 230 profile, NULL, MSG_ROUTING_NONE, NULL, NULL); |
| 231 tab_contents_wrapper_.reset(new TabContentsWrapper(web_contents)); | 231 tab_contents_.reset(new TabContents(web_contents)); |
| 232 | 232 |
| 233 // Setting the size starts the renderer. | 233 // Setting the size starts the renderer. |
| 234 SetSize(width, height); | 234 SetSize(width, height); |
| 235 NavigateToURL(url); | 235 NavigateToURL(url); |
| 236 | 236 |
| 237 parent_tab_ = parent_tab; | 237 parent_tab_ = parent_tab; |
| 238 | 238 |
| 239 // Register for tab notifications. | 239 // Register for tab notifications. |
| 240 registrar_.Add( | 240 registrar_.Add( |
| 241 this, | 241 this, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 272 std::string json_args; | 272 std::string json_args; |
| 273 base::JSONWriter::Write(&args, &json_args); | 273 base::JSONWriter::Write(&args, &json_args); |
| 274 | 274 |
| 275 // The event router only dispatches the event to renderers listening for the | 275 // The event router only dispatches the event to renderers listening for the |
| 276 // event. | 276 // event. |
| 277 Profile* profile = parent_tab_->tab_contents()->profile(); | 277 Profile* profile = parent_tab_->tab_contents()->profile(); |
| 278 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 278 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 279 events::kOnOffscreenTabUpdated, json_args, profile, GURL()); | 279 events::kOnOffscreenTabUpdated, json_args, profile, GURL()); |
| 280 } | 280 } |
| 281 | 281 |
| 282 ParentTab::ParentTab() : tab_contents_wrapper_(NULL) {} | 282 ParentTab::ParentTab() : tab_contents_(NULL) {} |
| 283 ParentTab::~ParentTab() {} | 283 ParentTab::~ParentTab() {} |
| 284 | 284 |
| 285 void ParentTab::Init(WebContents* web_contents, | 285 void ParentTab::Init(WebContents* web_contents, |
| 286 const std::string& extension_id) { | 286 const std::string& extension_id) { |
| 287 CHECK(web_contents); | 287 CHECK(web_contents); |
| 288 | 288 |
| 289 extension_id_ = extension_id; | 289 extension_id_ = extension_id; |
| 290 tab_contents_wrapper_ = | 290 tab_contents_ = TabContents::FromWebContents(web_contents); |
| 291 TabContentsWrapper::GetCurrentWrapperForContents(web_contents); | |
| 292 | 291 |
| 293 CHECK(tab_contents_wrapper_); | 292 CHECK(tab_contents_); |
| 294 | 293 |
| 295 // Register for tab notifications. | 294 // Register for tab notifications. |
| 296 registrar_.Add( | 295 registrar_.Add( |
| 297 this, | 296 this, |
| 298 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 297 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 299 content::Source<NavigationController>(&(web_contents->GetController()))); | 298 content::Source<NavigationController>(&(web_contents->GetController()))); |
| 300 registrar_.Add( | 299 registrar_.Add( |
| 301 this, | 300 this, |
| 302 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 301 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 303 content::Source<WebContents>(web_contents)); | 302 content::Source<WebContents>(web_contents)); |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 761 } | 760 } |
| 762 } | 761 } |
| 763 | 762 |
| 764 return true; | 763 return true; |
| 765 } | 764 } |
| 766 | 765 |
| 767 ToDataUrlOffscreenTabFunction::ToDataUrlOffscreenTabFunction() {} | 766 ToDataUrlOffscreenTabFunction::ToDataUrlOffscreenTabFunction() {} |
| 768 ToDataUrlOffscreenTabFunction::~ToDataUrlOffscreenTabFunction() {} | 767 ToDataUrlOffscreenTabFunction::~ToDataUrlOffscreenTabFunction() {} |
| 769 | 768 |
| 770 bool ToDataUrlOffscreenTabFunction::GetTabToCapture( | 769 bool ToDataUrlOffscreenTabFunction::GetTabToCapture( |
| 771 WebContents** web_contents, TabContentsWrapper** wrapper) { | 770 WebContents** web_contents, TabContents** tab_contents) { |
| 772 int offscreen_tab_id; | 771 int offscreen_tab_id; |
| 773 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &offscreen_tab_id)); | 772 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &offscreen_tab_id)); |
| 774 | 773 |
| 775 // TODO(alexbost): We want to optimize this function in order to get more | 774 // TODO(alexbost): We want to optimize this function in order to get more |
| 776 // image updates on the browser side. One improvement would be to implement | 775 // image updates on the browser side. One improvement would be to implement |
| 777 // another hash map in order to get offscreen tabs in O(1). | 776 // another hash map in order to get offscreen tabs in O(1). |
| 778 OffscreenTab* offscreen_tab = NULL; | 777 OffscreenTab* offscreen_tab = NULL; |
| 779 if (!GetMap()->GetOffscreenTab( | 778 if (!GetMap()->GetOffscreenTab( |
| 780 offscreen_tab_id, this, &offscreen_tab, &error_)) | 779 offscreen_tab_id, this, &offscreen_tab, &error_)) |
| 781 return false; | 780 return false; |
| 782 | 781 |
| 783 *web_contents = offscreen_tab->web_contents(); | 782 *web_contents = offscreen_tab->web_contents(); |
| 784 *wrapper = offscreen_tab->tab_contents(); | 783 *tab_contents = offscreen_tab->tab_contents(); |
| 785 return true; | 784 return true; |
| 786 } | 785 } |
| 787 | 786 |
| 788 UpdateOffscreenTabFunction::UpdateOffscreenTabFunction() {} | 787 UpdateOffscreenTabFunction::UpdateOffscreenTabFunction() {} |
| 789 UpdateOffscreenTabFunction::~UpdateOffscreenTabFunction() {} | 788 UpdateOffscreenTabFunction::~UpdateOffscreenTabFunction() {} |
| 790 | 789 |
| 791 bool UpdateOffscreenTabFunction::RunImpl() { | 790 bool UpdateOffscreenTabFunction::RunImpl() { |
| 792 int offscreen_tab_id; | 791 int offscreen_tab_id; |
| 793 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &offscreen_tab_id)); | 792 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &offscreen_tab_id)); |
| 794 | 793 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 // async case (when a "javascript": URL is sent to a tab). | 831 // async case (when a "javascript": URL is sent to a tab). |
| 833 if (!is_async) | 832 if (!is_async) |
| 834 SendResponse(true); | 833 SendResponse(true); |
| 835 | 834 |
| 836 return true; | 835 return true; |
| 837 } | 836 } |
| 838 | 837 |
| 839 void UpdateOffscreenTabFunction::PopulateResult() { | 838 void UpdateOffscreenTabFunction::PopulateResult() { |
| 840 // There's no result associated with this callback. | 839 // There's no result associated with this callback. |
| 841 } | 840 } |
| OLD | NEW |