Index: chrome/browser/extensions/extension_offscreen_tabs_module.cc |
=================================================================== |
--- chrome/browser/extensions/extension_offscreen_tabs_module.cc (revision 0) |
+++ chrome/browser/extensions/extension_offscreen_tabs_module.cc (revision 0) |
@@ -0,0 +1,1209 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/extension_offscreen_tabs_module.h" |
+ |
+#include <algorithm> |
+#include <hash_map> |
+#include <vector> |
+ |
+#include "base/base64.h" |
+#include "base/json/json_writer.h" |
+#include "base/memory/ref_counted_memory.h" |
+#include "base/stl_util.h" |
+#include "base/string_number_conversions.h" |
+#include "base/string_util.h" |
+#include "base/values.h" |
+#include "chrome/browser/extensions/extension_event_router.h" |
+#include "chrome/browser/extensions/extension_function_dispatcher.h" |
+#include "chrome/browser/extensions/extension_host.h" |
+#include "chrome/browser/extensions/extension_message_service.h" |
+#include "chrome/browser/extensions/extension_offscreen_tabs_module_constants.h" |
+#include "chrome/browser/extensions/extension_service.h" |
+#include "chrome/browser/extensions/extension_tabs_module.h" |
+#include "chrome/browser/net/url_fixer_upper.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/browser_navigator.h" |
+#include "chrome/browser/ui/browser_window.h" |
+#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
+#include "chrome/browser/ui/window_sizer.h" |
+#include "chrome/common/chrome_notification_types.h" |
+#include "chrome/common/extensions/extension.h" |
+#include "chrome/common/extensions/extension_error_utils.h" |
+#include "chrome/common/extensions/extension_messages.h" |
+#include "chrome/common/pref_names.h" |
+#include "chrome/common/url_constants.h" |
+#include "content/browser/renderer_host/backing_store.h" |
+#include "content/browser/renderer_host/render_view_host.h" |
+#include "content/browser/renderer_host/render_view_host_delegate.h" |
+#include "content/browser/tab_contents/navigation_entry.h" |
+#include "content/browser/tab_contents/tab_contents.h" |
+#include "content/browser/tab_contents/tab_contents_view.h" |
+#include "content/common/content_client.h" |
+#include "content/common/notification_service.h" |
+#include "skia/ext/image_operations.h" |
+#include "skia/ext/platform_canvas.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+#include "ui/gfx/codec/jpeg_codec.h" |
+#include "ui/gfx/codec/png_codec.h" |
+ |
+using WebKit::WebInputEvent; |
+ |
+namespace keys = extension_offscreen_tabs_module_constants; |
+ |
+const int ToDataUrlOffscreenTabFunction::kDefaultQuality = 90; |
sky
2011/09/08 18:12:26
Can you assign the value in the definition? If not
alexbost
2011/09/10 04:47:53
Done.
|
+ |
+namespace { |
+ |
+// Maps |
+// tab id -> tab contents |
+typedef base::hash_map<int, TabContents*> TabIdToTabMap; |
+TabIdToTabMap* tabs_map = NULL; |
jstritar
2011/09/08 16:58:20
nit: add 1 line of whitespace to make it easier to
jstritar
2011/09/08 16:58:20
actually, I don't think you need TabIdToTabMap at
alexbost
2011/09/10 04:47:53
Done.
alexbost
2011/09/10 04:47:53
I have combined all maps into a Map class.
|
+// tab id -> vector of offscreen tab contents |
+typedef base::hash_map<int, std::vector<TabContents*> > TabIdToOffscreenTabsMap; |
+TabIdToOffscreenTabsMap* offscreen_tabs_map = NULL; |
jstritar
2011/09/08 16:58:20
how about calling this map OffscreenTabsMap and up
alexbost
2011/09/10 04:47:53
Done.
|
+// tab id -> extension id |
+typedef base::hash_map<int, const std::string*> TabIdToExtensionIdMap; |
+TabIdToExtensionIdMap* extension_id_map = NULL; |
sky
2011/09/08 18:12:26
Do you really need so many mappings? Could you ins
alexbost
2011/09/10 04:47:53
Done.
|
+ |
+// Event managers |
+TabsEventManager* tabs_event_manager = NULL; |
+OffscreenTabsEventManager* offscreen_tabs_event_manager = NULL; |
jstritar
2011/09/08 16:58:20
move these to ExtensionService?
alexbost
2011/09/10 04:47:53
I would leave this for a future refactoring patch.
|
+ |
+// Background page tab contents. Used by the test API. |
+TabContents* background_page_tab_contents = NULL; |
+ |
+// Util ------------------------------------------------------------------------ |
+ |
+TabIdToTabMap* GetTabIdToTabMap() { |
+ if (tabs_map == NULL) |
+ tabs_map = new TabIdToTabMap(); |
+ |
+ return tabs_map; |
+} |
+ |
+TabIdToOffscreenTabsMap* GetTabIdToOffscreenTabsMap() { |
+ if (offscreen_tabs_map == NULL) |
+ offscreen_tabs_map = new TabIdToOffscreenTabsMap(); |
+ |
+ return offscreen_tabs_map; |
+} |
+ |
+TabIdToExtensionIdMap* GetTabIdToExtensionIdMap() { |
+ if (extension_id_map == NULL) |
+ extension_id_map = new TabIdToExtensionIdMap(); |
+ |
+ return extension_id_map; |
+} |
+ |
+TabsEventManager* GetTabsEventManager() { |
+ if (tabs_event_manager == NULL) |
+ tabs_event_manager = new TabsEventManager(); |
+ |
+ return tabs_event_manager; |
+} |
+ |
+OffscreenTabsEventManager* GetOffscreenTabsEventManager() { |
+ if (offscreen_tabs_event_manager == NULL) |
+ offscreen_tabs_event_manager = new OffscreenTabsEventManager(); |
+ |
+ return offscreen_tabs_event_manager; |
+} |
+ |
+TabContents* GetBackgroundPageTabContents(Profile* profile = NULL) { |
jstritar
2011/09/08 16:58:20
We usually try to stay way from default arguments.
alexbost
2011/09/10 04:47:53
Done.
|
+ if (profile && background_page_tab_contents == NULL) |
sky
2011/09/08 18:12:26
What if the profile differs from the profile you c
alexbost
2011/09/10 04:47:53
We are assuming that offscreen tabs will not be cr
|
+ background_page_tab_contents = |
+ new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL, NULL); |
+ |
+ return background_page_tab_contents; |
+} |
+ |
+int GetTabId(const TabContents* tab_contents) { |
jstritar
2011/09/08 16:58:20
can you get rid of this method and call ExtensionT
alexbost
2011/09/10 04:47:53
Done.
|
+ return ExtensionTabUtil::GetTabId(tab_contents); |
+} |
+ |
+bool GetTabById(const int tab_id, |
+ TabContents** tab_contents, |
+ std::string* error_message) { |
+ *tab_contents = (*GetTabIdToTabMap())[tab_id]; |
sky
2011/09/08 18:12:26
Don't you want to use find so that you don't creat
alexbost
2011/09/10 04:47:53
Done.
|
+ |
+ if (tab_contents) |
sky
2011/09/08 18:12:26
Shouldn't this be *tab_contents?
alexbost
2011/09/10 04:47:53
Done.
|
+ return true; |
+ |
+ *error_message = ExtensionErrorUtils::FormatErrorMessage( |
+ keys::kTabNotFoundError, base::IntToString(tab_id)); |
+ return false; |
+} |
+ |
+bool GetOffscreenTabById(const int tab_id, |
+ TabContentsWrapper** offscreen_tab, |
+ const TabContents* tab_contents, |
+ std::string* error_message) { |
jstritar
2011/09/08 16:58:20
Could you group the input and output arguments tog
alexbost
2011/09/10 04:47:53
Done.
|
+ std::vector<TabContents*> offscreen_tab_contents_vector = |
jstritar
2011/09/08 16:58:20
nit: renaming this offscreen_tabs might make same
alexbost
2011/09/10 04:47:53
Done.
|
+ (*GetTabIdToOffscreenTabsMap())[GetTabId(tab_contents)]; |
jstritar
2011/09/08 16:58:20
I think it'd be a little more readable to do:
GetT
alexbost
2011/09/10 04:47:53
Done.
|
+ |
+ for (std::vector<TabContents*>::iterator |
+ it_offscreen_tab_contents = offscreen_tab_contents_vector.begin(); |
jstritar
2011/09/08 16:58:20
you could probably rename this to just "it" or "of
alexbost
2011/09/10 04:47:53
Done.
|
+ it_offscreen_tab_contents != offscreen_tab_contents_vector.end(); |
+ ++it_offscreen_tab_contents) |
+ if (GetTabId(*it_offscreen_tab_contents) == tab_id) { |
+ *offscreen_tab = TabContentsWrapper::GetCurrentWrapperForContents( |
+ *it_offscreen_tab_contents); |
+ |
+ return true; |
+ } |
+ |
+ *error_message = ExtensionErrorUtils::FormatErrorMessage( |
+ keys::kOffscreenTabNotFoundError, base::IntToString(tab_id)); |
+ return false; |
+} |
+ |
+bool GetTabOfOffscreenTab(TabContents* offscreen_tab_contents, |
+ TabContents** tab_contents, |
+ std::string* error_message) { |
+ int offscreen_tab_id = GetTabId(offscreen_tab_contents); |
+ |
+ for (TabIdToOffscreenTabsMap::iterator |
+ it = GetTabIdToOffscreenTabsMap()->begin(); |
+ it != GetTabIdToOffscreenTabsMap()->end(); ++it) |
+ for (std::vector<TabContents*>::iterator it_offscreen_tab_contents = |
+ it->second.begin(); |
+ it_offscreen_tab_contents != it->second.end(); |
+ ++it_offscreen_tab_contents) |
+ if (GetTabId(*it_offscreen_tab_contents) == offscreen_tab_id) { |
+ if (!GetTabById(it->first, tab_contents, error_message)) |
+ return false; |
+ |
+ return true; |
+ } |
+ |
+ *error_message = ExtensionErrorUtils::FormatErrorMessage( |
+ keys::kTabOfOffscreenTabNotFoundError, |
+ base::IntToString(offscreen_tab_id)); |
+ return false; |
+} |
+ |
+// TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. |
+// Takes |url_string| and returns a GURL which is either valid and absolute |
+// or invalid. If |url_string| is not directly interpretable as a valid (it is |
+// likely a relative URL) an attempt is made to resolve it. |extension| is |
+// provided so it can be resolved relative to its extension base |
+// (chrome-extension://<id>/). Using the source frame url would be more correct, |
+// but because the api shipped with urls resolved relative to their extension |
+// base, we decided it wasn't worth breaking existing extensions to fix. |
+GURL ResolvePossiblyRelativeURL(const std::string& url_string, |
+ const Extension* extension) { |
+ GURL url = GURL(url_string); |
+ if (!url.is_valid()) |
+ url = extension->GetResourceURL(url_string); |
+ |
+ return url; |
+} |
+ |
+// TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. |
+bool IsCrashURL(const GURL& url) { |
+ // Check a fixed-up URL, to normalize the scheme and parse hosts correctly. |
+ GURL fixed_url = |
+ URLFixerUpper::FixupURL(url.possibly_invalid_spec(), std::string()); |
+ return (fixed_url.SchemeIs(chrome::kChromeUIScheme) && |
+ (fixed_url.host() == chrome::kChromeUIBrowserCrashHost || |
+ fixed_url.host() == chrome::kChromeUICrashHost)); |
+} |
+ |
+std::string GetTabUrl(const TabContents* tab_contents) { |
jstritar
2011/09/08 16:58:20
Not sure you really need this method...
alexbost
2011/09/10 04:47:53
Done.
|
+ return tab_contents->GetURL().spec(); |
+} |
+ |
+int GetTabWidth(const TabContents* tab_contents) { |
+ const TabContentsWrapper* tab = |
+ TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); |
+ |
+ if (tab && tab->view()) |
+ return tab->view()->GetContainerSize().width(); |
+ |
+ return -1; |
jstritar
2011/09/08 16:58:20
under what circumstances would tab this return -1?
alexbost
2011/09/10 04:47:53
I did this as a precaution so we don't segfault on
|
+} |
+ |
+int GetTabHeight(const TabContents* tab_contents) { |
+ const TabContentsWrapper* tab = |
+ TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); |
+ |
+ if (tab && tab->view()) |
+ return tab->view()->GetContainerSize().height(); |
+ |
+ return -1; |
+} |
+ |
+bool GetCurrentTab(TabContents** tab_contents, |
+ ExtensionFunctionDispatcher* dispatcher, |
+ Profile* profile, |
+ std::string error_message) { |
+ *tab_contents = dispatcher->delegate()->GetAssociatedTabContents(); |
+ |
+ // Background page (no associated tab contents) |
+ if (!*tab_contents) |
+ *tab_contents = GetBackgroundPageTabContents(profile); |
+ |
+ if (tab_contents) |
+ return true; |
+ |
+ error_message = keys::kCurrentTabNotFound; |
+ return false; |
+} |
+ |
+void CloseOffscreenTab(TabContents* tab_contents) { |
+ GetOffscreenTabsEventManager()->UnregisterForTabNotifications(tab_contents); |
+ |
+ // TODO(alexbost): Is this the best way to close an offscreen tab? |
+ delete TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); |
+} |
+ |
+DictionaryValue* CreateOffscreenTabValue(const TabContentsWrapper* tab) { |
+ DictionaryValue* result = new DictionaryValue(); |
+ result->SetInteger(keys::kIdKey, GetTabId(tab->tab_contents())); |
jstritar
2011/09/08 16:58:20
How about you put the TabContents in a local varia
alexbost
2011/09/10 04:47:53
Done.
|
+ result->SetString(keys::kUrlKey, GetTabUrl(tab->tab_contents())); |
+ result->SetInteger(keys::kWidthKey, GetTabWidth(tab->tab_contents())); |
+ result->SetInteger(keys::kHeightKey, GetTabHeight(tab->tab_contents())); |
+ |
+ return result; |
+} |
+ |
+DictionaryValue* CreateOffscreenTabValue(const TabContents* tab_contents) { |
+ return CreateOffscreenTabValue( |
+ TabContentsWrapper::GetCurrentWrapperForContents(tab_contents)); |
+} |
+ |
+} // namespace |
+ |
+// Tabs Event Manager ---------------------------------------------------------- |
+ |
+TabsEventManager::TabsEventManager() {} |
+TabsEventManager::~TabsEventManager() {} |
+ |
+void TabsEventManager::RegisterForTabNotifications( |
+ const TabContents* tab_contents) { |
+ registrar_.Add(this, |
+ content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
+ Source<NavigationController>(&tab_contents->controller())); |
+ |
+ registrar_.Add(this, |
+ content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
+ Source<TabContents>(tab_contents)); |
+} |
+ |
+void TabsEventManager::UnregisterForTabNotifications( |
+ const TabContents* tab_contents) { |
+ registrar_.Remove(this, |
+ content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
+ Source<NavigationController>(&tab_contents->controller())); |
+ |
+ registrar_.Remove(this, |
+ content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
+ Source<TabContents>(tab_contents)); |
+} |
+ |
+void TabsEventManager::Observe(const int type, |
+ const NotificationSource& source, |
+ const NotificationDetails& details) { |
+ TabContents* tab_contents; |
+ |
+ if (type == content::NOTIFICATION_TAB_CONTENTS_DESTROYED) |
+ tab_contents = Source<TabContents>(source).ptr(); |
+ else if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) |
+ tab_contents = Source<NavigationController>(source).ptr()->tab_contents(); |
+ else |
+ return; |
jstritar
2011/09/08 16:58:20
You can add a NOTREACHED() here since you should o
alexbost
2011/09/10 04:47:53
Done.
|
+ |
+ DCHECK(tab_contents); |
+ |
+ int tab_contents_id = GetTabId(tab_contents); |
+ |
+ // Close all offscreen tabs spawned by this tab |
+ std::vector<TabContents*> offscreen_tab_contents_vector = |
jstritar
2011/09/08 16:58:20
can you shorten these variable names a bit too? of
alexbost
2011/09/10 04:47:53
Done.
|
+ (*GetTabIdToOffscreenTabsMap())[tab_contents_id]; |
+ for (std::vector<TabContents*>::iterator |
+ it_offscreen_tab_contents = offscreen_tab_contents_vector.begin(); |
+ it_offscreen_tab_contents != offscreen_tab_contents_vector.end(); |
+ ++it_offscreen_tab_contents) |
+ CloseOffscreenTab(*it_offscreen_tab_contents); |
+ |
+ GetTabsEventManager()->UnregisterForTabNotifications(tab_contents); |
jstritar
2011/09/08 16:58:20
this can just be UnregisterForTabNotifications(tab
alexbost
2011/09/10 04:47:53
Done.
|
+ |
+ GetTabIdToOffscreenTabsMap()->erase(tab_contents_id); |
+ GetTabIdToTabMap()->erase(tab_contents_id); |
+ GetTabIdToExtensionIdMap()->erase(tab_contents_id); |
+} |
+ |
+// Offscreen Tabs Event Manager ------------------------------------------------ |
+ |
+OffscreenTabsEventManager::OffscreenTabsEventManager() {} |
+OffscreenTabsEventManager::~OffscreenTabsEventManager() {} |
+ |
+void OffscreenTabsEventManager::RegisterForTabNotifications( |
+ const TabContents* tab_contents) { |
+ registrar_.Add(this, |
+ content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
+ Source<NavigationController>(&tab_contents->controller())); |
+} |
+ |
+void OffscreenTabsEventManager::UnregisterForTabNotifications( |
+ const TabContents* tab_contents) { |
+ registrar_.Remove(this, |
+ content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
+ Source<NavigationController>(&tab_contents->controller())); |
+} |
+ |
+void OffscreenTabsEventManager::Observe(const int type, |
+ const NotificationSource& source, |
+ const NotificationDetails& details) { |
+ DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED); |
+ |
+ TabContents* offscreen_tab_contents = |
+ Source<NavigationController>(source).ptr()->tab_contents(); |
+ DCHECK(offscreen_tab_contents); |
+ |
+ DictionaryValue* changed_properties = new DictionaryValue(); |
+ changed_properties->SetString(keys::kUrlKey, |
+ GetTabUrl(offscreen_tab_contents)); |
+ |
+ int offscreen_tab_id = GetTabId(offscreen_tab_contents); |
+ |
+ ListValue args; |
+ args.Append(Value::CreateIntegerValue(offscreen_tab_id)); |
+ args.Append(changed_properties); |
+ args.Append(CreateOffscreenTabValue(offscreen_tab_contents)); |
+ std::string json_args; |
+ base::JSONWriter::Write(&args, false, &json_args); |
+ |
+ TabContents* tab_contents; |
+ std::string error_; |
+ if (!GetTabOfOffscreenTab(offscreen_tab_contents, &tab_contents, &error_)) { |
+ LOG(ERROR) << error_; |
+ return; |
+ } |
+ |
+ ListValue event_args; |
+ event_args.Set(0, Value::CreateStringValue(keys::kEventOnUpdated)); |
+ event_args.Set(1, Value::CreateStringValue(json_args)); |
+ |
+ // Dispatch "experimental.offscreenTabs.onUpdated" event. |
+ |
+ // The primary use case for broadcasting the event is |
+ // when the offscreen tab is generated by a test API background page. |
+ if (tab_contents == GetBackgroundPageTabContents()) { |
+ Profile* profile = Profile::FromBrowserContext( |
+ offscreen_tab_contents->browser_context()); |
+ profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
+ keys::kEventOnUpdated, json_args, profile, GURL()); |
+ // Send a routed event directly the tab that spawned the offscreen tab. |
jstritar
2011/09/08 16:58:20
nit: "directly to the tab"
alexbost
2011/09/10 04:47:53
Done.
|
+ } else { |
+ tab_contents->render_view_host()->process()->Send( |
+ new ExtensionMsg_MessageInvoke( |
+ tab_contents->render_view_host()->routing_id(), |
+ *(*GetTabIdToExtensionIdMap())[GetTabId(tab_contents)], |
+ keys::kDispatchEvent, |
+ event_args, |
+ GURL())); |
+ } |
+} |
+ |
+// create ---------------------------------------------------------------------- |
+ |
+CreateOffscreenTabFunction::CreateOffscreenTabFunction() {} |
+CreateOffscreenTabFunction::~CreateOffscreenTabFunction() {} |
+ |
+bool CreateOffscreenTabFunction::RunImpl() { |
+ Browser* browser = GetCurrentBrowser(); |
+ if (!browser) { |
sky
2011/09/08 18:12:26
Why do many of your run methods require a browser?
alexbost
2011/09/10 04:47:53
Done.
|
+ error_ = keys::kNoCurrentWindowError; |
+ return false; |
+ } |
+ |
+ DictionaryValue* create_props; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &create_props)); |
+ |
+ // Url |
jstritar
2011/09/08 16:58:20
Implementation comments should usually be complete
alexbost
2011/09/10 04:47:53
Done.
|
+ std::string url_string; |
+ GURL url; |
+ EXTENSION_FUNCTION_VALIDATE( |
+ create_props->GetString(keys::kUrlKey, &url_string)); |
+ url = ResolvePossiblyRelativeURL(url_string, GetExtension()); |
+ if (!url.is_valid()) { |
+ error_ = ExtensionErrorUtils::FormatErrorMessage( |
+ keys::kInvalidUrlError, url_string); |
+ return false; |
+ } |
+ if (IsCrashURL(url)) { |
+ error_ = keys::kNoCrashBrowserError; |
+ return false; |
+ } |
+ |
+ // Width and height |
+ gfx::Rect window_bounds; |
+ bool maximized; |
+ if (!create_props->HasKey(keys::kWidthKey) || |
+ !create_props->HasKey(keys::kHeightKey)) |
+ WindowSizer::GetBrowserWindowBounds(std::string(), gfx::Rect(), |
+ browser, &window_bounds, |
+ &maximized); |
+ |
+ int width; |
+ if (create_props->HasKey(keys::kWidthKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ create_props->GetInteger(keys::kWidthKey, &width)); |
+ else |
+ width = window_bounds.width(); |
jstritar
2011/09/08 16:58:20
Can you initialize width with window_bounds.width(
alexbost
2011/09/10 04:47:53
Done.
|
+ |
+ int height; |
+ if (create_props->HasKey(keys::kHeightKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ create_props->GetInteger(keys::kHeightKey, &height)); |
+ else |
+ height = window_bounds.height(); |
jstritar
2011/09/08 16:58:20
ditto
alexbost
2011/09/10 04:47:53
Done.
|
+ |
+ // New offscreen tab |
jstritar
2011/09/08 16:58:20
fix these comments too
alexbost
2011/09/10 04:47:53
Done.
|
+ TabContents* offscreen_tab_contents = |
+ new TabContents(profile_, NULL, MSG_ROUTING_NONE, NULL, NULL); |
+ TabContentsWrapper* offscreen_tab = |
+ new TabContentsWrapper(offscreen_tab_contents); |
+ |
+ // Setting the size starts the renderer |
+ offscreen_tab_contents->view()->SizeContents(gfx::Size(width, height)); |
+ |
+ // Navigate |
+ offscreen_tab_contents->controller().LoadURL( |
+ url, GURL(), PageTransition::LINK); |
+ |
+ // Map the offscreen tab to the tab that spawned it |
+ TabContents* tab_contents; |
+ if (!GetCurrentTab(&tab_contents, dispatcher(), profile_, error_)) |
+ return false; |
sky
2011/09/08 18:12:26
If you return here you leak the tabs you just crea
alexbost
2011/09/10 04:47:53
Good catch.
|
+ |
+ int tab_contents_id = GetTabId(tab_contents); |
+ |
+ // Register for offscreen tab notifications |
+ (*GetTabIdToOffscreenTabsMap())[tab_contents_id]. |
+ push_back(offscreen_tab_contents); |
+ GetOffscreenTabsEventManager()-> |
+ RegisterForTabNotifications(offscreen_tab_contents); |
+ |
+ // If this is the first offscreen tab spawned by the tab, |
+ // register for tab notifications and update maps |
+ if ((*GetTabIdToOffscreenTabsMap())[tab_contents_id]. |
sky
2011/09/08 18:12:26
Rather than using a bunch of static maps and what
alexbost
2011/09/10 04:47:53
Done.
|
+ size() == 1) { |
+ (*GetTabIdToTabMap())[tab_contents_id] = tab_contents; |
+ (*GetTabIdToExtensionIdMap())[tab_contents_id] = &extension_id(); |
+ |
+ GetTabsEventManager()->RegisterForTabNotifications(tab_contents); |
+ } |
+ |
+ // Callback |
+ if (has_callback()) { |
+ result_.reset(CreateOffscreenTabValue(offscreen_tab)); |
+ SendResponse(true); |
+ } |
+ |
+ return true; |
+} |
+ |
+// get ------------------------------------------------------------------------- |
+ |
+GetOffscreenTabFunction::GetOffscreenTabFunction() {} |
+GetOffscreenTabFunction::~GetOffscreenTabFunction() {} |
+ |
+bool GetOffscreenTabFunction::RunImpl() { |
+ int tab_id; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
+ |
+ Browser* browser = GetCurrentBrowser(); |
+ if (!browser) { |
+ error_ = keys::kNoCurrentWindowError; |
+ return false; |
+ } |
+ TabContents* tab_contents; |
+ if (!GetCurrentTab(&tab_contents, dispatcher(), profile_, error_)) |
+ return false; |
+ |
+ TabContentsWrapper* offscreen_tab = NULL; |
+ if (!GetOffscreenTabById(tab_id, &offscreen_tab, tab_contents, &error_)) |
+ return false; |
+ |
+ result_.reset(CreateOffscreenTabValue(offscreen_tab)); |
+ SendResponse(true); |
+ |
+ return true; |
+} |
+ |
+// getAll ---------------------------------------------------------------------- |
+ |
+GetAllOffscreenTabFunction::GetAllOffscreenTabFunction() {} |
+GetAllOffscreenTabFunction::~GetAllOffscreenTabFunction() {} |
+ |
+bool GetAllOffscreenTabFunction::RunImpl() { |
+ Browser* browser = GetCurrentBrowser(); |
+ if (!browser) { |
+ error_ = keys::kNoCurrentWindowError; |
+ return false; |
+ } |
+ TabContents* current_tab_contents; |
+ if (!GetCurrentTab(¤t_tab_contents, dispatcher(), profile_, error_)) |
+ return false; |
+ |
+ std::vector<TabContents*> offscreen_tab_contents_vector = |
+ (*GetTabIdToOffscreenTabsMap())[GetTabId(current_tab_contents)]; |
+ |
+ ListValue* tab_list = new ListValue(); |
+ for (std::vector<TabContents*>::iterator |
+ it_offscreen_tab_contents = offscreen_tab_contents_vector.begin(); |
+ it_offscreen_tab_contents != offscreen_tab_contents_vector.end(); |
+ ++it_offscreen_tab_contents) |
+ tab_list->Append(CreateOffscreenTabValue(TabContentsWrapper:: |
+ GetCurrentWrapperForContents(*it_offscreen_tab_contents))); |
+ |
+ result_.reset(tab_list); |
+ SendResponse(true); |
+ |
+ return true; |
+} |
+ |
+// remove ---------------------------------------------------------------------- |
+ |
+RemoveOffscreenTabFunction::RemoveOffscreenTabFunction() {} |
+RemoveOffscreenTabFunction::~RemoveOffscreenTabFunction() {} |
+ |
+bool RemoveOffscreenTabFunction::RunImpl() { |
+ int tab_id; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
+ |
+ for (TabIdToOffscreenTabsMap::iterator |
+ it = GetTabIdToOffscreenTabsMap()->begin(); |
+ it != GetTabIdToOffscreenTabsMap()->end(); ++it) |
+ for (std::vector<TabContents*>::iterator it_offscreen_tab_contents = |
+ it->second.begin(); |
+ it_offscreen_tab_contents != it->second.end(); |
+ ++it_offscreen_tab_contents) |
+ if (GetTabId(*it_offscreen_tab_contents) == tab_id) { |
+ TabContents* tab_contents; |
+ if (!GetTabById( |
+ it->first, &tab_contents, &error_)) |
+ return false; |
+ |
+ CloseOffscreenTab(*it_offscreen_tab_contents); |
+ |
+ it->second.erase(it_offscreen_tab_contents); |
+ |
+ // If this was the last offscreen tab for the tab, |
+ // unregister the tab for notifications and remove it from the maps |
+ if (it->second.empty()) { |
+ GetTabsEventManager()->UnregisterForTabNotifications(tab_contents); |
+ |
+ GetTabIdToOffscreenTabsMap()->erase(it); |
+ GetTabIdToTabMap()->erase(it->first); |
+ GetTabIdToExtensionIdMap()->erase(it->first); |
+ } |
+ |
+ return true; |
+ } |
+ |
+ error_ = ExtensionErrorUtils::FormatErrorMessage( |
+ keys::kOffscreenTabNotFoundError, base::IntToString(tab_id)); |
+ return false; |
+} |
+ |
+// sendKeyboardEvent ----------------------------------------------------------- |
+ |
+SendKeyboardEventOffscreenTabFunction:: |
+ SendKeyboardEventOffscreenTabFunction() {} |
+SendKeyboardEventOffscreenTabFunction:: |
+ ~SendKeyboardEventOffscreenTabFunction() {} |
+ |
+bool SendKeyboardEventOffscreenTabFunction::RunImpl() { |
+ int tab_id; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
+ |
+ Browser* browser = GetCurrentBrowser(); |
+ if (!browser) { |
+ error_ = keys::kNoCurrentWindowError; |
+ return false; |
+ } |
+ TabContents* current_tab_contents; |
jstritar
2011/09/08 16:58:20
initialize to NULL
alexbost
2011/09/10 04:47:53
Done.
|
+ if (!GetCurrentTab(¤t_tab_contents, dispatcher(), profile_, error_)) |
+ return false; |
+ TabContentsWrapper* tab = NULL; |
+ if (!GetOffscreenTabById(tab_id, &tab, current_tab_contents, &error_)) |
+ return false; |
+ |
+ // JavaScript KeyboardEvent |
+ DictionaryValue* dict; |
jstritar
2011/09/08 16:58:20
ditto
alexbost
2011/09/10 04:47:53
Done.
|
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &dict)); |
+ |
+ NativeWebKeyboardEvent keyboard_event; |
+ |
+ // Type |
+ std::string type; |
+ if (dict->HasKey(keys::kKeyboardEventTypeKey)) { |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetString(keys::kKeyboardEventTypeKey, &type)); |
+ } else { |
+ error_ = keys::kInvalidKeyboardEventObjectError; |
+ return false; |
+ } |
+ |
+ if (type.compare(keys::kKeyboardEventTypeValueKeypress) == 0) { |
+ keyboard_event.type = WebInputEvent::Char; |
+ } else if (type.compare(keys::kKeyboardEventTypeValueKeydown) == 0) { |
+ keyboard_event.type = WebInputEvent::KeyDown; |
+ } else if (type.compare(keys::kKeyboardEventTypeValueKeyup) == 0) { |
+ keyboard_event.type = WebInputEvent::KeyUp; |
+ } else { |
+ error_ = keys::kInvalidKeyboardEventObjectError; |
+ return false; |
+ } |
+ |
+ // Key code |
+ int key_code; |
+ if (dict->HasKey(keys::kKeyboardEventKeyCodeKey)) { |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetInteger(keys::kKeyboardEventKeyCodeKey, &key_code)); |
+ } else { |
+ error_ = keys::kInvalidKeyboardEventObjectError; |
+ return false; |
+ } |
+ |
+ keyboard_event.nativeKeyCode = key_code; |
+ keyboard_event.windowsKeyCode = key_code; |
+ |
+ // Key identifier |
+ keyboard_event.setKeyIdentifierFromWindowsKeyCode(); |
+ |
+ // Keypress = type character |
+ if (type.compare(keys::kKeyboardEventTypeValueKeypress) == 0) { |
+ int char_code; |
+ if (dict->HasKey(keys::kKeyboardEventCharCodeKey)) { |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetInteger(keys::kKeyboardEventCharCodeKey, &char_code)); |
+ keyboard_event.text[0] = char_code; |
+ keyboard_event.unmodifiedText[0] = char_code; |
+ } else { |
+ error_ = keys::kInvalidKeyboardEventObjectError; |
+ return false; |
+ } |
+ } |
+ |
+ // Modifiers |
+ bool alt_key; |
+ if (dict->HasKey(keys::kKeyboardEventAltKeyKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetBoolean(keys::kKeyboardEventAltKeyKey, &alt_key)); |
+ if (alt_key) |
+ keyboard_event.modifiers |= WebInputEvent::AltKey; |
+ |
+ bool ctrl_key; |
+ if (dict->HasKey(keys::kKeyboardEventCtrlKeyKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetBoolean(keys::kKeyboardEventCtrlKeyKey, &ctrl_key)); |
+ if (ctrl_key) |
+ keyboard_event.modifiers |= WebInputEvent::ControlKey; |
+ |
+ bool meta_key = false; |
+ if (dict->HasKey(keys::kMouseEventMetaKeyKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetBoolean(keys::kMouseEventMetaKeyKey, &meta_key)); |
+ if (meta_key) |
+ keyboard_event.modifiers |= WebInputEvent::MetaKey; |
+ |
+ bool shift_key; |
+ if (dict->HasKey(keys::kKeyboardEventShiftKeyKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetBoolean(keys::kKeyboardEventShiftKeyKey, &shift_key)); |
+ if (shift_key) |
+ keyboard_event.modifiers |= WebInputEvent::ShiftKey; |
+ |
+ // Forward event |
+ tab->tab_contents()->render_view_host()-> |
+ ForwardKeyboardEvent(keyboard_event); |
+ |
+ // Callback |
+ if (has_callback()) { |
+ result_.reset(CreateOffscreenTabValue(tab)); |
+ SendResponse(true); |
+ } |
+ |
+ return true; |
+} |
+ |
+// sendMouseEvent -------------------------------------------------------------- |
+ |
+SendMouseEventOffscreenTabFunction::SendMouseEventOffscreenTabFunction() {} |
+SendMouseEventOffscreenTabFunction::~SendMouseEventOffscreenTabFunction() {} |
+ |
+bool SendMouseEventOffscreenTabFunction::RunImpl() { |
+ int tab_id; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
+ |
+ Browser* browser = GetCurrentBrowser(); |
+ if (!browser) { |
+ error_ = keys::kNoCurrentWindowError; |
+ return false; |
+ } |
+ TabContents* current_tab_contents; |
+ if (!GetCurrentTab(¤t_tab_contents, dispatcher(), profile_, error_)) |
+ return false; |
+ TabContentsWrapper* tab = NULL; |
+ if (!GetOffscreenTabById(tab_id, &tab, current_tab_contents, &error_)) |
+ return false; |
+ |
+ // JavaScript MouseEvent |
+ DictionaryValue* dict; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &dict)); |
+ |
+ std::string type; |
+ if (dict->HasKey(keys::kMouseEventTypeKey)) { |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetString(keys::kMouseEventTypeKey, &type)); |
+ } else { |
+ error_ = keys::kInvalidMouseEventObjectError; |
+ return false; |
+ } |
+ |
+ if (type.compare(keys::kMouseEventTypeValueMousewheel) == 0) { |
+ WebKit::WebMouseWheelEvent wheel_event; |
+ |
+ // Type |
+ wheel_event.type = WebInputEvent::MouseWheel; |
+ |
+ // deltaX, deltaY |
+ if (dict->HasKey(keys::kMouseEventWheelDeltaXKey) && |
+ dict->HasKey(keys::kMouseEventWheelDeltaYKey)) { |
+ int delta_x, delta_y; |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetInteger(keys::kMouseEventWheelDeltaXKey, &delta_x)); |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetInteger(keys::kMouseEventWheelDeltaYKey, &delta_y)); |
+ wheel_event.deltaX = delta_x; |
+ wheel_event.deltaY = delta_y; |
+ } else { |
+ error_ = keys::kInvalidMouseEventObjectError; |
+ return false; |
+ } |
+ |
+ // Forward the event |
+ tab->tab_contents()->render_view_host()->ForwardWheelEvent(wheel_event); |
+ } else { |
+ WebKit::WebMouseEvent mouse_event; |
+ |
+ // Type |
+ if (type.compare(keys::kMouseEventTypeValueMousedown) == 0 || |
+ type.compare(keys::kMouseEventTypeValueClick) == 0) { |
+ mouse_event.type = WebKit::WebInputEvent::MouseDown; |
+ } else if (type.compare(keys::kMouseEventTypeValueMouseup) == 0) { |
+ mouse_event.type = WebKit::WebInputEvent::MouseUp; |
+ } else if (type.compare(keys::kMouseEventTypeValueMousemove) == 0) { |
+ mouse_event.type = WebKit::WebInputEvent::MouseMove; |
+ } else { |
+ error_ = keys::kInvalidMouseEventObjectError; |
+ return false; |
+ } |
+ |
+ // Button |
+ int button; |
+ if (dict->HasKey(keys::kMouseEventButtonKey)) { |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetInteger(keys::kMouseEventButtonKey, &button)); |
+ } else { |
+ error_ = keys::kInvalidMouseEventObjectError; |
+ return false; |
+ } |
+ |
+ if (button == keys::kMouseEventButtonValueLeft) { |
+ mouse_event.button = WebKit::WebMouseEvent::ButtonLeft; |
+ } else if (button == keys::kMouseEventButtonValueMiddle) { |
+ mouse_event.button = WebKit::WebMouseEvent::ButtonMiddle; |
+ } else if (button == keys::kMouseEventButtonValueRight) { |
+ mouse_event.button = WebKit::WebMouseEvent::ButtonRight; |
+ } else { |
+ error_ = keys::kInvalidMouseEventObjectError; |
+ return false; |
+ } |
+ |
+ // x, y |
+ if (HasOptionalArgument(2) && HasOptionalArgument(3)) { |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &mouse_event.x)); |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &mouse_event.y)); |
+ } else { |
+ error_ = keys::kNoMouseCoordinatesError; |
+ return false; |
+ } |
+ |
+ // Modifiers |
+ bool alt_key = false; |
+ if (dict->HasKey(keys::kMouseEventAltKeyKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetBoolean(keys::kMouseEventAltKeyKey, &alt_key)); |
+ if (alt_key) |
+ mouse_event.modifiers |= WebInputEvent::AltKey; |
+ |
+ bool ctrl_key = false; |
+ if (dict->HasKey(keys::kMouseEventCtrlKeyKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetBoolean(keys::kMouseEventCtrlKeyKey, &ctrl_key)); |
+ if (ctrl_key) |
+ mouse_event.modifiers |= WebInputEvent::ControlKey; |
+ |
+ bool meta_key = false; |
+ if (dict->HasKey(keys::kMouseEventMetaKeyKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetBoolean(keys::kMouseEventMetaKeyKey, &meta_key)); |
+ if (meta_key) |
+ mouse_event.modifiers |= WebInputEvent::MetaKey; |
+ |
+ bool shift_key = false; |
+ if (dict->HasKey(keys::kMouseEventShiftKeyKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ dict->GetBoolean(keys::kMouseEventShiftKeyKey, &shift_key)); |
+ if (shift_key) |
+ mouse_event.modifiers |= WebInputEvent::ShiftKey; |
+ |
+ // Click count |
+ mouse_event.clickCount = 1; |
+ |
+ // Forward the event |
+ tab->tab_contents()->render_view_host()->ForwardMouseEvent(mouse_event); |
+ |
+ // If the event is a click, |
+ // fire a mouseup event in addition to the mousedown |
+ if (type.compare(keys::kMouseEventTypeValueClick) == 0) { |
+ mouse_event.type = WebKit::WebInputEvent::MouseUp; |
+ tab->tab_contents()->render_view_host()->ForwardMouseEvent(mouse_event); |
+ } |
+ } |
+ |
+ // Callback |
+ if (has_callback()) { |
+ result_.reset(CreateOffscreenTabValue(tab)); |
+ SendResponse(true); |
+ } |
+ |
+ return true; |
+} |
+ |
+// toDataUrl ------------------------------------------------------------------- |
+ |
+ToDataUrlOffscreenTabFunction::ToDataUrlOffscreenTabFunction() {} |
+ToDataUrlOffscreenTabFunction::~ToDataUrlOffscreenTabFunction() {} |
+ |
+// TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. |
+// TODO(alexbost): We want to optimize this function in order to get more image |
+// updates on the browser side. One improvement would be to implement another |
+// hash map in order to get offscreen tabs in O(1). |
+bool ToDataUrlOffscreenTabFunction::RunImpl() { |
jstritar
2011/09/08 16:58:20
How do you know how often to call this method? It
alexbost
2011/09/10 04:47:53
We let the application explicitly call this method
|
+ int tab_id; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
+ |
+ Browser* browser = GetCurrentBrowser(); |
+ if (!browser) { |
+ error_ = keys::kNoCurrentWindowError; |
+ return false; |
+ } |
+ TabContents* current_tab_contents; |
+ if (!GetCurrentTab(¤t_tab_contents, dispatcher(), profile_, error_)) |
+ return false; |
+ TabContentsWrapper* tab = NULL; |
+ if (!GetOffscreenTabById(tab_id, &tab, current_tab_contents, &error_)) |
+ return false; |
+ |
+ image_format_ = FORMAT_JPEG; // Default format is JPEG. |
+ image_quality_ = kDefaultQuality; // Default quality setting. |
+ |
+ if (HasOptionalArgument(1)) { |
+ DictionaryValue* options; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); |
+ |
+ if (options->HasKey(keys::kFormatKey)) { |
+ std::string format; |
+ EXTENSION_FUNCTION_VALIDATE( |
+ options->GetString(keys::kFormatKey, &format)); |
+ |
+ if (format == keys::kFormatValueJpeg) { |
+ image_format_ = FORMAT_JPEG; |
+ } else if (format == keys::kFormatValuePng) { |
+ image_format_ = FORMAT_PNG; |
+ } else { |
+ // Schema validation should make this unreachable. |
+ EXTENSION_FUNCTION_VALIDATE(0); |
+ } |
+ } |
+ |
+ if (options->HasKey(keys::kQualityKey)) { |
+ EXTENSION_FUNCTION_VALIDATE( |
+ options->GetInteger(keys::kQualityKey, &image_quality_)); |
+ } |
+ } |
+ |
+ // captureVisibleTab() can return an image containing sensitive information |
+ // that the browser would otherwise protect. Ensure the extension has |
+ // permission to do this. |
+ if (!GetExtension()-> |
+ CanCaptureVisiblePage(tab->tab_contents()->GetURL(), &error_)) |
+ return false; |
+ |
+// The backing store approach works on Linux but not on Mac. |
+// TODO(alexbost): Test on Windows |
+#if !defined(OS_MACOSX) |
+ RenderViewHost* render_view_host = tab->tab_contents()->render_view_host(); |
+ |
+ // If a backing store is cached for the tab we want to capture, |
+ // and it can be copied into a bitmap, then use it to generate the image. |
+ BackingStore* backing_store = render_view_host->GetBackingStore(false); |
+ if (backing_store && CaptureSnapshotFromBackingStore(backing_store)) |
+ return true; |
+#endif |
+ |
+ // Ask the renderer for a snapshot of the tab. |
+ tab->CaptureSnapshot(); |
+ registrar_.Add(this, |
+ chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, |
+ Source<TabContentsWrapper>(tab)); |
+ |
+ AddRef(); // Balanced in ToDataUrlOffscreenTabFunction::Observe(). |
+ |
+ return true; |
+} |
+ |
+// TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. |
+// Build the image of a tab's contents out of a backing store. |
+// This may fail if we can not copy a backing store into a bitmap. |
+// For example, some uncommon X11 visual modes are not supported by |
+// CopyFromBackingStore(). |
+bool ToDataUrlOffscreenTabFunction::CaptureSnapshotFromBackingStore( |
+ BackingStore* backing_store) { |
+ |
+ skia::PlatformCanvas temp_canvas; |
+ if (!backing_store->CopyFromBackingStore(gfx::Rect(backing_store->size()), |
+ &temp_canvas)) { |
+ return false; |
+ } |
+ VLOG(1) << "captureVisibleTab() got image from backing store."; |
+ |
+ SendResultFromBitmap( |
+ skia::GetTopDevice(temp_canvas)->accessBitmap(false)); |
+ return true; |
+} |
+ |
+// TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. |
+void ToDataUrlOffscreenTabFunction::Observe(const int type, |
+ const NotificationSource& source, |
+ const NotificationDetails& details) { |
+ DCHECK(type == chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN); |
+ |
+ const SkBitmap *screen_capture = Details<const SkBitmap>(details).ptr(); |
+ const bool error = screen_capture->empty(); |
+ |
+ if (error) { |
+ error_ = keys::kInternalVisibleTabCaptureError; |
+ SendResponse(false); |
+ } else { |
+ VLOG(1) << "Got image from renderer."; |
+ SendResultFromBitmap(*screen_capture); |
+ } |
+ |
+ Release(); // Balanced in ToDataUrlOffscreenTabFunction::RunImpl(). |
+} |
+ |
+// TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. |
+// Turn a bitmap of the screen into an image, set that image as the result, |
+// and call SendResponse(). |
+void ToDataUrlOffscreenTabFunction::SendResultFromBitmap( |
+ const SkBitmap& screen_capture) { |
+ std::vector<unsigned char> data; |
+ SkAutoLockPixels screen_capture_lock(screen_capture); |
+ bool encoded = false; |
+ std::string mime_type; |
+ switch (image_format_) { |
+ case FORMAT_JPEG: |
+ encoded = gfx::JPEGCodec::Encode( |
+ reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), |
+ gfx::JPEGCodec::FORMAT_SkBitmap, |
+ screen_capture.width(), |
+ screen_capture.height(), |
+ static_cast<int>(screen_capture.rowBytes()), |
+ image_quality_, |
+ &data); |
+ mime_type = keys::kMimeTypeJpeg; |
+ break; |
+ case FORMAT_PNG: |
+ encoded = gfx::PNGCodec::EncodeBGRASkBitmap( |
+ screen_capture, |
+ true, // Discard transparency. |
+ &data); |
+ mime_type = keys::kMimeTypePng; |
+ break; |
+ default: |
+ NOTREACHED() << "Invalid image format."; |
+ } |
+ |
+ if (!encoded) { |
+ error_ = keys::kInternalVisibleTabCaptureError; |
+ SendResponse(false); |
+ return; |
+ } |
+ |
+ std::string base64_result; |
+ base::StringPiece stream_as_string( |
+ reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); |
+ |
+ base::Base64Encode(stream_as_string, &base64_result); |
+ base64_result.insert(0, base::StringPrintf("data:%s;base64,", |
+ mime_type.c_str())); |
+ result_.reset(new StringValue(base64_result)); |
+ SendResponse(true); |
+} |
+ |
+// update ---------------------------------------------------------------------- |
+ |
+UpdateOffscreenTabFunction::UpdateOffscreenTabFunction() {} |
+UpdateOffscreenTabFunction::~UpdateOffscreenTabFunction() {} |
+ |
+// TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. |
+bool UpdateOffscreenTabFunction::RunImpl() { |
+ int tab_id; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
+ |
+ Browser* browser = GetCurrentBrowser(); |
+ if (!browser) { |
+ error_ = keys::kNoCurrentWindowError; |
+ return false; |
+ } |
+ TabContents* current_tab_contents; |
+ if (!GetCurrentTab(¤t_tab_contents, dispatcher(), profile_, error_)) |
+ return false; |
+ TabContentsWrapper* tab = NULL; |
+ if (!GetOffscreenTabById(tab_id, &tab, current_tab_contents, &error_)) |
+ return false; |
+ |
+ DictionaryValue* update_props; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); |
+ |
+ // Url |
+ if (update_props->HasKey(keys::kUrlKey)) { |
+ std::string url_string; |
+ GURL url; |
+ EXTENSION_FUNCTION_VALIDATE( |
+ update_props->GetString(keys::kUrlKey, &url_string)); |
+ url = ResolvePossiblyRelativeURL(url_string, GetExtension()); |
+ if (!url.is_valid()) { |
+ error_ = ExtensionErrorUtils::FormatErrorMessage( |
+ keys::kInvalidUrlError, url_string); |
+ return false; |
+ } |
+ if (IsCrashURL(url)) { |
+ error_ = keys::kNoCrashBrowserError; |
+ return false; |
+ } |
+ |
+ // JavaScript URLs can do the same kinds of things as cross-origin XHR, so |
+ // we need to check host permissions before allowing them. |
+ if (url.SchemeIs(chrome::kJavaScriptScheme)) { |
+ if (!GetExtension()->CanExecuteScriptOnPage( |
+ tab->tab_contents()->GetURL(), NULL, &error_)) { |
+ return false; |
+ } |
+ |
+ ExtensionMsg_ExecuteCode_Params params; |
+ params.request_id = request_id(); |
+ params.extension_id = extension_id(); |
+ params.is_javascript = true; |
+ params.code = url.path(); |
+ params.all_frames = false; |
+ params.in_main_world = true; |
+ |
+ RenderViewHost* render_view_host = |
+ tab->tab_contents()->render_view_host(); |
+ render_view_host->Send( |
+ new ExtensionMsg_ExecuteCode(render_view_host->routing_id(), |
+ params)); |
+ |
+ Observe(tab->tab_contents()); |
+ AddRef(); // balanced in Observe() |
+ |
+ return true; |
+ } |
+ |
+ tab->tab_contents()->controller(). |
+ LoadURL(url, GURL(), PageTransition::LINK); |
+ |
+ // The URL of a tab contents never actually changes to a JavaScript URL, so |
+ // this check only makes sense in other cases. |
+ if (!url.SchemeIs(chrome::kJavaScriptScheme)) |
+ DCHECK_EQ(url.spec(), tab->tab_contents()->GetURL().spec()); |
+ } |
+ |
+ // Width and height |
+ if (update_props->HasKey(keys::kWidthKey) || |
+ update_props->HasKey(keys::kHeightKey)) { |
+ int width; |
+ if (update_props->HasKey(keys::kWidthKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ update_props->GetInteger(keys::kWidthKey, &width)); |
+ else |
+ GetTabWidth(tab->tab_contents()); |
+ |
+ int height; |
+ if (update_props->HasKey(keys::kHeightKey)) |
+ EXTENSION_FUNCTION_VALIDATE( |
+ update_props->GetInteger(keys::kHeightKey, &height)); |
+ else |
+ GetTabHeight(tab->tab_contents()); |
+ |
+ tab->tab_contents()->view()->SizeContents(gfx::Size(width, height)); |
+ } |
+ |
+ // Callback |
+ if (has_callback()) { |
+ result_.reset(CreateOffscreenTabValue(tab)); |
+ SendResponse(true); |
+ } |
+ |
+ return true; |
+} |
+ |
+// TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. |
+bool UpdateOffscreenTabFunction::OnMessageReceived( |
+ const IPC::Message& message) { |
+ if (message.type() != ExtensionHostMsg_ExecuteCodeFinished::ID) |
+ return false; |
+ |
+ int message_request_id; |
+ void* iter = NULL; |
+ if (!message.ReadInt(&iter, &message_request_id)) { |
+ NOTREACHED() << "malformed extension message"; |
+ return true; |
+ } |
+ |
+ if (message_request_id != request_id()) |
+ return false; |
+ |
+ IPC_BEGIN_MESSAGE_MAP(UpdateOffscreenTabFunction, message) |
+ IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished, |
+ OnExecuteCodeFinished) |
+ IPC_END_MESSAGE_MAP() |
+ return true; |
+} |
+ |
+// TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. |
+void UpdateOffscreenTabFunction::OnExecuteCodeFinished(int request_id, |
+ bool success, |
+ const std::string& error) { |
+ if (!error.empty()) { |
+ CHECK(!success); |
+ error_ = error; |
+ } |
+ |
+ SendResponse(success); |
+ |
+ Observe(NULL); |
+ Release(); // balanced in Execute() |
+} |
+ |
Property changes on: chrome/browser/extensions/extension_offscreen_tabs_module.cc |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |