Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "chrome/browser/extensions/extension_offscreen_tabs_module.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/base64.h" | |
| 11 #include "base/json/json_writer.h" | |
| 12 #include "base/memory/ref_counted_memory.h" | |
| 13 #include "base/stl_util.h" | |
| 14 #include "base/string_number_conversions.h" | |
| 15 #include "base/string_util.h" | |
| 16 #include "base/values.h" | |
| 17 #include "chrome/browser/extensions/extension_event_router.h" | |
| 18 #include "chrome/browser/extensions/extension_function_dispatcher.h" | |
| 19 #include "chrome/browser/extensions/extension_host.h" | |
| 20 #include "chrome/browser/extensions/extension_message_service.h" | |
| 21 #include "chrome/browser/extensions/extension_offscreen_tabs_module_constants.h" | |
| 22 #include "chrome/browser/extensions/extension_service.h" | |
| 23 #include "chrome/browser/extensions/extension_tabs_module.h" | |
| 24 #include "chrome/browser/net/url_fixer_upper.h" | |
| 25 #include "chrome/browser/profiles/profile.h" | |
| 26 #include "chrome/browser/ui/browser.h" | |
| 27 #include "chrome/browser/ui/browser_navigator.h" | |
| 28 #include "chrome/browser/ui/browser_window.h" | |
| 29 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
| 30 #include "chrome/browser/ui/window_sizer.h" | |
| 31 #include "chrome/common/chrome_notification_types.h" | |
| 32 #include "chrome/common/extensions/extension.h" | |
| 33 #include "chrome/common/extensions/extension_error_utils.h" | |
| 34 #include "chrome/common/extensions/extension_messages.h" | |
| 35 #include "chrome/common/pref_names.h" | |
| 36 #include "chrome/common/url_constants.h" | |
| 37 #include "content/browser/renderer_host/backing_store.h" | |
| 38 #include "content/browser/renderer_host/render_view_host.h" | |
| 39 #include "content/browser/renderer_host/render_view_host_delegate.h" | |
| 40 #include "content/browser/tab_contents/navigation_entry.h" | |
| 41 #include "content/browser/tab_contents/tab_contents.h" | |
| 42 #include "content/browser/tab_contents/tab_contents_view.h" | |
| 43 #include "content/common/content_client.h" | |
| 44 #include "content/common/notification_service.h" | |
| 45 #include "skia/ext/image_operations.h" | |
| 46 #include "skia/ext/platform_canvas.h" | |
| 47 #include "third_party/skia/include/core/SkBitmap.h" | |
| 48 #include "ui/gfx/codec/jpeg_codec.h" | |
| 49 #include "ui/gfx/codec/png_codec.h" | |
| 50 | |
| 51 using WebKit::WebInputEvent; | |
| 52 | |
| 53 namespace keys = extension_offscreen_tabs_module_constants; | |
| 54 | |
| 55 namespace { | |
| 56 | |
| 57 // Offscreen Tab --------------------------------------------------------------- | |
| 58 | |
| 59 // This class is responsible for the creation and destruction of offscreen tabs, | |
| 60 // as well as dispatching an onUpdated event. | |
| 61 class OffscreenTab : public NotificationObserver { | |
| 62 public: | |
| 63 OffscreenTab(); | |
| 64 ~OffscreenTab(); | |
|
jstritar
2011/09/15 18:52:01
The destructors should be virtual (not Init).
alexbost
2011/09/15 19:39:36
Done.
| |
| 65 virtual void Init(const GURL& url, | |
| 66 const int width, | |
| 67 const int height, | |
| 68 Profile* profile); | |
| 69 | |
| 70 TabContents* contents(); | |
| 71 DictionaryValue* CreateValue(); // The caller owns the returned value. | |
| 72 | |
| 73 void SetUrl(const GURL& url); | |
|
jstritar
2011/09/15 18:52:01
nit: We tend to use SetURL rather than SetUrl
alexbost
2011/09/15 19:39:36
Done.
| |
| 74 void SetSize(int width, int height); | |
| 75 | |
| 76 private: | |
| 77 virtual void Observe(int type, | |
| 78 const NotificationSource& source, | |
| 79 const NotificationDetails& details) OVERRIDE; | |
| 80 | |
| 81 NotificationRegistrar registrar_; | |
| 82 | |
| 83 scoped_ptr<TabContentsWrapper> tab_; // TabContentsWrapper associated with | |
| 84 // this offscreen tab. | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(OffscreenTab); | |
| 87 }; | |
| 88 | |
| 89 typedef std::vector<OffscreenTab*> TabVector; | |
| 90 typedef TabVector::iterator TabIterator; | |
| 91 | |
| 92 // Tab ------------------------------------------------------------------------- | |
| 93 | |
| 94 // Holds info about a tab that has spawned at least one offscreen tab. | |
| 95 // Each Tab keeps track of its child offscreen tabs. The Tab is also responsible | |
| 96 // for killing its children when it navigates away or gets closed. | |
| 97 class Tab : public NotificationObserver { | |
| 98 public: | |
| 99 Tab(); | |
| 100 ~Tab(); | |
| 101 virtual void Init(TabContents* tab_contents, const std::string& extension_id); | |
| 102 | |
| 103 TabContents* contents(); | |
| 104 const TabVector& offscreen_tabs() { return offscreen_tabs_; } | |
| 105 const std::string& GetExtensionId() const { return extension_id_; } | |
|
jstritar
2011/09/15 18:52:01
GetExtensionId --> extension_id since it's inline
alexbost
2011/09/15 19:39:36
Done.
| |
| 106 | |
| 107 // Tab takes ownership of OffscreenTab. | |
| 108 void AddOffscreenTab(OffscreenTab *tab); | |
| 109 // Caller assumes ownership of OffscreenTab. | |
| 110 void RemoveOffscreenTab(OffscreenTab *tab); | |
| 111 | |
| 112 private: | |
| 113 virtual void Observe(int type, | |
| 114 const NotificationSource& source, | |
| 115 const NotificationDetails& details) OVERRIDE; | |
| 116 | |
| 117 NotificationRegistrar registrar_; | |
| 118 | |
| 119 TabContentsWrapper* tab_; // TabContentsWrapper associated with this tab. | |
| 120 TabVector offscreen_tabs_; // Offscreen tabs spawned by this tab. | |
| 121 std::string extension_id_; // Id of the extension running in this tab. | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(Tab); | |
| 124 }; | |
| 125 | |
| 126 // Map ------------------------------------------------------------------------- | |
| 127 | |
| 128 // This map keeps track of all tabs that are happy parents of offscreen tabs. | |
| 129 class Map { | |
| 130 public: | |
| 131 Map(); | |
| 132 ~Map(); | |
| 133 | |
| 134 // Gets an offscreen tab by ID. | |
| 135 bool GetOffscreenTab(const int tab_id, | |
| 136 ExtensionFunctionDispatcher* dispatcher, | |
| 137 Profile* profile, | |
| 138 OffscreenTab** offscreen_tab, | |
| 139 std::string* error_message); | |
| 140 // Gets a parent tab by ID. | |
| 141 bool GetTab(const int tab_id, | |
| 142 Tab** tab, | |
| 143 std::string* error_message); | |
| 144 // Gets the parent tab of an offscreen tab. | |
| 145 bool GetTabOfOffscreenTab(TabContents* offscreen_tab_contents, | |
| 146 Tab** tab, | |
| 147 std::string* error_message); | |
| 148 // Creates a mapping between a parent tab and an offscreen tab. | |
| 149 bool AddOffscreenTab(OffscreenTab* offscreen_tab, | |
| 150 ExtensionFunctionDispatcher* dispatcher, | |
| 151 Profile* profile, | |
| 152 const std::string& ext_id, | |
| 153 std::string* error_message); | |
| 154 // Removes the mapping between a parent tab and an offscreen tab. | |
| 155 bool RemoveOffscreenTab(const int tab_id, | |
| 156 ExtensionFunctionDispatcher* dispatcher, | |
| 157 Profile* profile, | |
| 158 std::string* error_message); | |
| 159 // Removes a parent tab from the map along with its child offscreen tabs. | |
| 160 bool RemoveTab(const int tab_id, std::string* error_message); | |
| 161 | |
| 162 private: | |
| 163 typedef base::hash_map<int, Tab*> TabMap; | |
| 164 TabMap map; | |
| 165 | |
| 166 DISALLOW_COPY_AND_ASSIGN(Map); | |
| 167 }; | |
| 168 | |
| 169 // Variables ------------------------------------------------------------------- | |
| 170 | |
| 171 Map* map = NULL; | |
| 172 | |
| 173 // We are assuming that offscreen tabs will not be created by background pages | |
| 174 // with the exception of the API test background page. We keep track of the | |
| 175 // offscreen tabs associated with the test API background page via this variable | |
| 176 // These tab contents are created just for convenience and do not do anything. | |
| 177 // TODO(alexbost): Think about handling multiple background pages each spawning | |
| 178 // offscreen tabs. Would background pages want to spawn offscreen tabs? | |
| 179 TabContents* background_page_tab_contents = NULL; | |
| 180 | |
| 181 // Util ------------------------------------------------------------------------ | |
| 182 | |
| 183 // Gets the map of parent tabs to offscreen tabs. | |
| 184 Map* GetMap() { | |
| 185 if (map == NULL) | |
| 186 map = new Map(); | |
| 187 | |
| 188 return map; | |
| 189 } | |
| 190 | |
| 191 // Gets the TabContents associated with the test API background page. | |
| 192 TabContents* GetBackgroundPageTabContents(Profile* profile) { | |
| 193 if (profile && background_page_tab_contents == NULL) { | |
| 194 background_page_tab_contents = | |
| 195 new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL, NULL); | |
| 196 new TabContentsWrapper(background_page_tab_contents); | |
| 197 } | |
| 198 | |
| 199 return background_page_tab_contents; | |
| 200 } | |
| 201 | |
| 202 // Gets the contents of the tab that instantiated the extension API call. | |
| 203 // In the case of background pages we use tab contents created by us. | |
| 204 bool GetCurrentTab(ExtensionFunctionDispatcher* dispatcher, | |
| 205 Profile* profile, | |
| 206 TabContents** tab_contents, | |
| 207 std::string* error_message) { | |
| 208 *tab_contents = dispatcher->delegate()->GetAssociatedTabContents(); | |
| 209 | |
| 210 // Background page (no associated tab contents). | |
| 211 if (!*tab_contents) | |
| 212 *tab_contents = GetBackgroundPageTabContents(profile); | |
| 213 | |
| 214 if (*tab_contents) | |
| 215 return true; | |
| 216 | |
| 217 *error_message = keys::kCurrentTabNotFound; | |
| 218 return false; | |
| 219 } | |
| 220 | |
| 221 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
| 222 // Takes |url_string| and returns a GURL which is either valid and absolute | |
| 223 // or invalid. If |url_string| is not directly interpretable as a valid (it is | |
| 224 // likely a relative URL) an attempt is made to resolve it. |extension| is | |
| 225 // provided so it can be resolved relative to its extension base | |
| 226 // (chrome-extension://<id>/). Using the source frame url would be more correct, | |
| 227 // but because the api shipped with urls resolved relative to their extension | |
| 228 // base, we decided it wasn't worth breaking existing extensions to fix. | |
| 229 GURL ResolvePossiblyRelativeURL(const std::string& url_string, | |
| 230 const Extension* extension) { | |
| 231 GURL url = GURL(url_string); | |
| 232 if (!url.is_valid()) | |
| 233 url = extension->GetResourceURL(url_string); | |
| 234 | |
| 235 return url; | |
| 236 } | |
| 237 | |
| 238 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
| 239 bool IsCrashURL(const GURL& url) { | |
| 240 // Check a fixed-up URL, to normalize the scheme and parse hosts correctly. | |
| 241 GURL fixed_url = | |
| 242 URLFixerUpper::FixupURL(url.possibly_invalid_spec(), std::string()); | |
| 243 return (fixed_url.SchemeIs(chrome::kChromeUIScheme) && | |
| 244 (fixed_url.host() == chrome::kChromeUIBrowserCrashHost || | |
| 245 fixed_url.host() == chrome::kChromeUICrashHost)); | |
| 246 } | |
| 247 | |
| 248 // Offscreen Tab --------------------------------------------------------------- | |
| 249 | |
| 250 OffscreenTab::OffscreenTab() {} | |
| 251 OffscreenTab::~OffscreenTab() {} | |
| 252 | |
| 253 void OffscreenTab::Init(const GURL& url, | |
| 254 const int width, | |
| 255 const int height, | |
| 256 Profile* profile) { | |
| 257 // Create the offscreen tab. | |
| 258 TabContents* tab_contents = new TabContents( | |
| 259 profile, NULL, MSG_ROUTING_NONE, NULL, NULL); | |
| 260 tab_.reset(new TabContentsWrapper(tab_contents)); | |
| 261 | |
| 262 SetSize(width, height); // Setting the size starts the renderer. | |
| 263 SetUrl(url); | |
| 264 | |
| 265 // Register for tab notifications. | |
| 266 registrar_.Add(this, | |
| 267 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 268 Source<NavigationController>(&contents()->controller())); | |
| 269 } | |
| 270 | |
| 271 TabContents* OffscreenTab::contents() { | |
| 272 return tab_.get()->tab_contents(); | |
| 273 } | |
| 274 | |
| 275 DictionaryValue* OffscreenTab::CreateValue() { | |
| 276 DictionaryValue* result = new DictionaryValue(); | |
| 277 | |
| 278 result->SetInteger(keys::kIdKey, ExtensionTabUtil::GetTabId(contents())); | |
| 279 result->SetString(keys::kUrlKey, contents()->GetURL().spec()); | |
| 280 result->SetInteger(keys::kWidthKey, | |
| 281 TabContentsWrapper::GetCurrentWrapperForContents(contents())-> | |
| 282 view()->GetContainerSize().width()); | |
| 283 result->SetInteger(keys::kHeightKey, | |
| 284 TabContentsWrapper::GetCurrentWrapperForContents(contents())-> | |
| 285 view()->GetContainerSize().height()); | |
| 286 | |
| 287 return result; | |
| 288 } | |
| 289 | |
| 290 void OffscreenTab::SetUrl(const GURL& url) { | |
| 291 contents()->controller().LoadURL( | |
| 292 url, GURL(), PageTransition::LINK, std::string()); | |
| 293 } | |
| 294 | |
| 295 void OffscreenTab::SetSize(int width, int height) { | |
| 296 contents()->view()->SizeContents(gfx::Size(width, height)); | |
| 297 } | |
| 298 | |
| 299 void OffscreenTab::Observe(int type, | |
| 300 const NotificationSource& source, | |
| 301 const NotificationDetails& details) { | |
| 302 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | |
|
jstritar
2011/09/15 18:52:01
You can convert these to CHECKs and CHECK_EQs if t
alexbost
2011/09/15 19:39:36
Done.
| |
| 303 | |
| 304 DictionaryValue* changed_properties = new DictionaryValue(); | |
| 305 changed_properties->SetString(keys::kUrlKey, contents()->GetURL().spec()); | |
| 306 | |
| 307 ListValue args; | |
| 308 args.Append( | |
| 309 Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents()))); | |
| 310 args.Append(changed_properties); | |
| 311 args.Append(CreateValue()); | |
| 312 std::string json_args; | |
| 313 base::JSONWriter::Write(&args, false, &json_args); | |
| 314 | |
| 315 Tab* tab; | |
| 316 GetMap()->GetTabOfOffscreenTab(contents(), &tab, new std::string()); | |
|
jstritar
2011/09/15 18:52:01
Can you just have each OffscreenTab hold a pointer
alexbost
2011/09/15 19:39:36
Done.
| |
| 317 DCHECK(tab); | |
| 318 | |
| 319 ListValue event_args; | |
| 320 event_args.Set(0, Value::CreateStringValue(keys::kEventOnUpdated)); | |
| 321 event_args.Set(1, Value::CreateStringValue(json_args)); | |
| 322 | |
| 323 // Dispatch an onUpdated event. | |
| 324 | |
|
jstritar
2011/09/15 18:52:01
nit: remove new line?
alexbost
2011/09/15 19:39:36
Done.
| |
| 325 // The primary use case for broadcasting the event is | |
| 326 // when the offscreen tab is generated by a test API background page. | |
| 327 if (tab->contents() == GetBackgroundPageTabContents(NULL)) { | |
| 328 Profile* profile = TabContentsWrapper::GetCurrentWrapperForContents( | |
| 329 tab->contents())->profile(); | |
| 330 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | |
| 331 keys::kEventOnUpdated, json_args, profile, GURL()); | |
| 332 } else { | |
| 333 // Send a routed event directly to the parent tab. | |
| 334 tab->contents()->render_view_host()->process()->Send( | |
| 335 new ExtensionMsg_MessageInvoke( | |
| 336 tab->contents()->render_view_host()->routing_id(), | |
| 337 tab->GetExtensionId(), | |
| 338 keys::kDispatchEvent, | |
| 339 event_args, | |
| 340 GURL())); | |
| 341 } | |
| 342 } | |
| 343 | |
| 344 // Tab ------------------------------------------------------------------------- | |
| 345 | |
| 346 Tab::Tab() { | |
| 347 tab_ = NULL; | |
| 348 } | |
| 349 | |
| 350 Tab::~Tab() { | |
| 351 // Kill child offscreen tabs. | |
| 352 STLDeleteElements(&offscreen_tabs_); | |
| 353 | |
| 354 bool removed = GetMap()->RemoveTab( | |
| 355 ExtensionTabUtil::GetTabId(contents()), new std::string()); | |
| 356 DCHECK(removed); | |
| 357 } | |
| 358 | |
| 359 void Tab::Init(TabContents* tab_contents, const std::string& extension_id) { | |
| 360 tab_ = TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); | |
| 361 extension_id_ = extension_id; | |
| 362 | |
| 363 // Register for tab notifications. | |
| 364 registrar_.Add(this, | |
| 365 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 366 Source<NavigationController>(&contents()->controller())); | |
| 367 | |
| 368 registrar_.Add(this, | |
| 369 content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | |
| 370 Source<TabContents>(contents())); | |
| 371 } | |
| 372 | |
| 373 TabContents* Tab::contents() { | |
| 374 return tab_->tab_contents(); | |
| 375 } | |
| 376 | |
| 377 void Tab::AddOffscreenTab(OffscreenTab *tab) { | |
| 378 offscreen_tabs_.push_back(tab); | |
| 379 } | |
| 380 | |
| 381 void Tab::RemoveOffscreenTab(OffscreenTab *tab) { | |
| 382 TabIterator it_tab = std::find( | |
| 383 offscreen_tabs_.begin(), offscreen_tabs_.end(), tab); | |
| 384 offscreen_tabs_.erase(it_tab); | |
| 385 } | |
| 386 | |
| 387 void Tab::Observe(int type, | |
| 388 const NotificationSource& source, | |
| 389 const NotificationDetails& details) { | |
| 390 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED || | |
| 391 type == content::NOTIFICATION_TAB_CONTENTS_DESTROYED); | |
| 392 | |
| 393 delete this; | |
| 394 } | |
| 395 | |
| 396 // Map ------------------------------------------------------------------------- | |
| 397 | |
| 398 Map::Map() {} | |
| 399 Map::~Map() {} | |
| 400 | |
| 401 bool Map::GetOffscreenTab(const int tab_id, | |
| 402 ExtensionFunctionDispatcher* dispatcher, | |
| 403 Profile* profile, | |
| 404 OffscreenTab** offscreen_tab, | |
| 405 std::string* error_message) { | |
| 406 // Ensure that the current tab is the parent of the offscreen tab. | |
| 407 TabContents* tab_contents = NULL; | |
| 408 if (!GetCurrentTab(dispatcher, profile, &tab_contents, error_message)) | |
| 409 return false; | |
| 410 | |
| 411 Tab* tab = NULL; | |
| 412 if (!GetTab(ExtensionTabUtil::GetTabId(tab_contents), &tab, error_message)) | |
| 413 return false; | |
| 414 TabVector offscreen_tabs = tab->offscreen_tabs(); | |
| 415 | |
| 416 for (TabIterator it = offscreen_tabs.begin(); | |
| 417 it != offscreen_tabs.end(); ++it) { | |
| 418 if (ExtensionTabUtil::GetTabId((*it)->contents()) == tab_id) { | |
| 419 *offscreen_tab = *it; | |
| 420 return true; | |
| 421 } | |
| 422 } | |
| 423 | |
| 424 *error_message = ExtensionErrorUtils::FormatErrorMessage( | |
| 425 keys::kOffscreenTabNotFoundError, base::IntToString(tab_id)); | |
| 426 return false; | |
| 427 } | |
| 428 | |
| 429 bool Map::GetTab(const int tab_id, Tab** tab, std::string* error_message) { | |
| 430 TabMap::iterator it = map.find(tab_id); | |
| 431 | |
| 432 if (it == map.end()) { | |
| 433 *error_message = ExtensionErrorUtils::FormatErrorMessage( | |
| 434 keys::kTabNotFoundError, base::IntToString(tab_id)); | |
| 435 return false; | |
| 436 } | |
| 437 | |
| 438 *tab = it->second; | |
| 439 | |
| 440 return true; | |
| 441 } | |
| 442 | |
| 443 bool Map::GetTabOfOffscreenTab(TabContents* offscreen_tab_contents, | |
| 444 Tab** tab, | |
| 445 std::string* error_message) { | |
| 446 int offscreen_tab_id = ExtensionTabUtil::GetTabId(offscreen_tab_contents); | |
| 447 | |
| 448 for (TabMap::iterator it = map.begin(); it != map.end(); ++it) { | |
| 449 TabVector offscreen_tabs = it->second->offscreen_tabs(); | |
| 450 for (TabIterator it_tab = offscreen_tabs.begin(); | |
| 451 it_tab != offscreen_tabs.end(); ++it_tab) { | |
| 452 if ((*it_tab)->contents() == offscreen_tab_contents) { | |
| 453 if (!GetTab(it->first, tab, error_message)) | |
| 454 return false; | |
| 455 | |
| 456 return true; | |
| 457 } | |
| 458 } | |
| 459 } | |
| 460 | |
| 461 *error_message = ExtensionErrorUtils::FormatErrorMessage( | |
| 462 keys::kTabOfOffscreenTabNotFoundError, | |
| 463 base::IntToString(offscreen_tab_id)); | |
| 464 return false; | |
| 465 } | |
| 466 | |
| 467 bool Map::AddOffscreenTab(OffscreenTab* offscreen_tab, | |
| 468 ExtensionFunctionDispatcher* dispatcher, | |
| 469 Profile* profile, | |
| 470 const std::string& ext_id, | |
| 471 std::string* error_message) { | |
| 472 // Get parent tab. | |
| 473 TabContents* tab_contents = NULL; | |
| 474 if (!GetCurrentTab(dispatcher, profile, &tab_contents, error_message)) | |
| 475 return false; | |
| 476 int tab_id = ExtensionTabUtil::GetTabId(tab_contents); | |
| 477 | |
| 478 Tab* tab = NULL; | |
| 479 if (!GetTab(tab_id, &tab, error_message)) { | |
| 480 tab = map[tab_id] = new Tab(); | |
| 481 tab->Init(tab_contents, ext_id); | |
| 482 } | |
| 483 | |
| 484 tab->AddOffscreenTab(offscreen_tab); | |
| 485 | |
| 486 return true; | |
| 487 } | |
| 488 | |
| 489 bool Map::RemoveOffscreenTab(const int tab_id, | |
| 490 ExtensionFunctionDispatcher* dispatcher, | |
| 491 Profile* profile, | |
| 492 std::string* error_message) { | |
| 493 OffscreenTab* offscreen_tab = NULL; | |
| 494 if (!GetOffscreenTab(tab_id, dispatcher, profile, | |
| 495 &offscreen_tab, error_message)) | |
| 496 return false; | |
| 497 | |
| 498 Tab* tab = NULL; | |
| 499 if (!GetTabOfOffscreenTab(offscreen_tab->contents(), &tab, error_message)) | |
| 500 return false; | |
| 501 | |
| 502 tab->RemoveOffscreenTab(offscreen_tab); | |
| 503 | |
| 504 // If this was the last offscreen tab for the parent tab, remove the parent. | |
| 505 if (tab->offscreen_tabs().empty()) | |
| 506 map.erase(ExtensionTabUtil::GetTabId(tab->contents())); | |
| 507 | |
| 508 return true; | |
| 509 } | |
| 510 | |
| 511 bool Map::RemoveTab(const int tab_id, std::string* error_message) { | |
| 512 if (map.find(tab_id) == map.end()) { | |
| 513 *error_message = ExtensionErrorUtils::FormatErrorMessage( | |
| 514 keys::kTabNotFoundError, base::IntToString(tab_id)); | |
| 515 return false; | |
| 516 } | |
| 517 | |
| 518 map.erase(tab_id); | |
| 519 | |
| 520 return true; | |
| 521 } | |
| 522 | |
| 523 } // namespace | |
| 524 | |
| 525 // API functions --------------------------------------------------------------- | |
| 526 | |
| 527 // create ---------------------------------------------------------------------- | |
| 528 | |
| 529 CreateOffscreenTabFunction::CreateOffscreenTabFunction() {} | |
| 530 CreateOffscreenTabFunction::~CreateOffscreenTabFunction() {} | |
| 531 | |
| 532 bool CreateOffscreenTabFunction::RunImpl() { | |
| 533 DictionaryValue* create_props; | |
| 534 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &create_props)); | |
| 535 | |
| 536 std::string url_string; | |
| 537 GURL url; | |
| 538 EXTENSION_FUNCTION_VALIDATE( | |
| 539 create_props->GetString(keys::kUrlKey, &url_string)); | |
| 540 url = ResolvePossiblyRelativeURL(url_string, GetExtension()); | |
| 541 if (!url.is_valid()) { | |
| 542 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
| 543 keys::kInvalidUrlError, url_string); | |
| 544 return false; | |
| 545 } | |
| 546 if (IsCrashURL(url)) { | |
| 547 error_ = keys::kNoCrashBrowserError; | |
| 548 return false; | |
| 549 } | |
| 550 | |
| 551 gfx::Rect window_bounds; | |
| 552 bool maximized; | |
| 553 if (!create_props->HasKey(keys::kWidthKey) || | |
| 554 !create_props->HasKey(keys::kHeightKey)) { | |
| 555 Browser* browser = GetCurrentBrowser(); | |
| 556 if (!browser) { | |
| 557 error_ = keys::kNoCurrentWindowError; | |
| 558 return false; | |
| 559 } | |
| 560 | |
| 561 WindowSizer::GetBrowserWindowBounds(std::string(), gfx::Rect(), | |
| 562 browser, &window_bounds, | |
| 563 &maximized); | |
| 564 } | |
| 565 | |
| 566 int width = window_bounds.width(); | |
| 567 if (create_props->HasKey(keys::kWidthKey)) | |
| 568 EXTENSION_FUNCTION_VALIDATE( | |
| 569 create_props->GetInteger(keys::kWidthKey, &width)); | |
| 570 | |
| 571 int height = window_bounds.height(); | |
| 572 if (create_props->HasKey(keys::kHeightKey)) | |
| 573 EXTENSION_FUNCTION_VALIDATE( | |
| 574 create_props->GetInteger(keys::kHeightKey, &height)); | |
| 575 | |
| 576 OffscreenTab* tab = new OffscreenTab(); | |
| 577 tab->Init(url, width, height, profile_); | |
| 578 | |
| 579 // Add the offscreen tab to the map so we don't lose track of it. | |
| 580 if (!GetMap()->AddOffscreenTab(tab, dispatcher(), profile_, | |
| 581 extension_id(), &error_)) { | |
| 582 delete tab; // Prevent leaking of offscreen tab. | |
| 583 return false; | |
| 584 } | |
| 585 | |
| 586 if (has_callback()) { | |
| 587 result_.reset(tab->CreateValue()); | |
| 588 SendResponse(true); | |
| 589 } | |
| 590 | |
| 591 return true; | |
| 592 } | |
| 593 | |
| 594 // get ------------------------------------------------------------------------- | |
| 595 | |
| 596 GetOffscreenTabFunction::GetOffscreenTabFunction() {} | |
| 597 GetOffscreenTabFunction::~GetOffscreenTabFunction() {} | |
| 598 | |
| 599 bool GetOffscreenTabFunction::RunImpl() { | |
| 600 int tab_id; | |
| 601 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
| 602 | |
| 603 OffscreenTab* tab = NULL; | |
| 604 if (!GetMap()-> | |
| 605 GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) { | |
| 606 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
| 607 keys::kOffscreenTabNotFoundError, base::IntToString(tab_id)); | |
| 608 return false; | |
| 609 } | |
| 610 | |
| 611 result_.reset(tab->CreateValue()); | |
| 612 SendResponse(true); | |
| 613 | |
| 614 return true; | |
| 615 } | |
| 616 | |
| 617 // getAll ---------------------------------------------------------------------- | |
| 618 | |
| 619 GetAllOffscreenTabFunction::GetAllOffscreenTabFunction() {} | |
| 620 GetAllOffscreenTabFunction::~GetAllOffscreenTabFunction() {} | |
| 621 | |
| 622 bool GetAllOffscreenTabFunction::RunImpl() { | |
| 623 TabContents* tab_contents = NULL; | |
| 624 if (!GetCurrentTab(dispatcher(), profile_, &tab_contents, &error_)) | |
| 625 return false; | |
| 626 | |
| 627 Tab* tab = NULL; | |
| 628 if (!GetMap()-> | |
| 629 GetTab(ExtensionTabUtil::GetTabId(tab_contents), &tab, &error_)) | |
| 630 return false; | |
| 631 | |
| 632 TabVector offscreen_tabs = tab->offscreen_tabs(); | |
| 633 | |
| 634 ListValue* tab_list = new ListValue(); | |
| 635 for (TabIterator it_tab = offscreen_tabs.begin(); | |
| 636 it_tab != offscreen_tabs.end(); ++it_tab) | |
| 637 tab_list->Append((*it_tab)->CreateValue()); | |
| 638 | |
| 639 result_.reset(tab_list); | |
| 640 SendResponse(true); | |
| 641 | |
| 642 return true; | |
| 643 } | |
| 644 | |
| 645 // remove ---------------------------------------------------------------------- | |
| 646 | |
| 647 RemoveOffscreenTabFunction::RemoveOffscreenTabFunction() {} | |
| 648 RemoveOffscreenTabFunction::~RemoveOffscreenTabFunction() {} | |
| 649 | |
| 650 bool RemoveOffscreenTabFunction::RunImpl() { | |
| 651 int tab_id; | |
| 652 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
| 653 | |
| 654 OffscreenTab* tab = NULL; | |
| 655 if (!GetMap()->GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) | |
| 656 return false; | |
| 657 | |
| 658 if (!GetMap()->RemoveOffscreenTab(tab_id, dispatcher(), profile_, &error_)) | |
| 659 return false; | |
| 660 | |
| 661 delete tab; | |
| 662 | |
| 663 return true; | |
| 664 } | |
| 665 | |
| 666 // sendKeyboardEvent ----------------------------------------------------------- | |
| 667 | |
| 668 SendKeyboardEventOffscreenTabFunction:: | |
| 669 SendKeyboardEventOffscreenTabFunction() {} | |
| 670 SendKeyboardEventOffscreenTabFunction:: | |
| 671 ~SendKeyboardEventOffscreenTabFunction() {} | |
| 672 | |
| 673 bool SendKeyboardEventOffscreenTabFunction::RunImpl() { | |
| 674 int tab_id; | |
| 675 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
| 676 | |
| 677 OffscreenTab* tab = NULL; | |
| 678 if (!GetMap()->GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) | |
| 679 return false; | |
| 680 | |
| 681 // JavaScript KeyboardEvent. | |
| 682 DictionaryValue* dict = NULL; | |
|
jstritar
2011/09/15 18:52:01
nit: maybe rename dict to something more informati
alexbost
2011/09/15 19:39:36
Done.
| |
| 683 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &dict)); | |
| 684 | |
| 685 NativeWebKeyboardEvent keyboard_event; | |
| 686 | |
| 687 std::string type; | |
| 688 if (dict->HasKey(keys::kKeyboardEventTypeKey)) { | |
| 689 EXTENSION_FUNCTION_VALIDATE( | |
| 690 dict->GetString(keys::kKeyboardEventTypeKey, &type)); | |
| 691 } else { | |
| 692 error_ = keys::kInvalidKeyboardEventObjectError; | |
| 693 return false; | |
| 694 } | |
| 695 | |
| 696 if (type.compare(keys::kKeyboardEventTypeValueKeypress) == 0) { | |
| 697 keyboard_event.type = WebInputEvent::Char; | |
| 698 } else if (type.compare(keys::kKeyboardEventTypeValueKeydown) == 0) { | |
| 699 keyboard_event.type = WebInputEvent::KeyDown; | |
| 700 } else if (type.compare(keys::kKeyboardEventTypeValueKeyup) == 0) { | |
| 701 keyboard_event.type = WebInputEvent::KeyUp; | |
| 702 } else { | |
| 703 error_ = keys::kInvalidKeyboardEventObjectError; | |
| 704 return false; | |
| 705 } | |
| 706 | |
| 707 int key_code; | |
| 708 if (dict->HasKey(keys::kKeyboardEventKeyCodeKey)) { | |
| 709 EXTENSION_FUNCTION_VALIDATE( | |
| 710 dict->GetInteger(keys::kKeyboardEventKeyCodeKey, &key_code)); | |
| 711 } else { | |
| 712 error_ = keys::kInvalidKeyboardEventObjectError; | |
| 713 return false; | |
| 714 } | |
| 715 | |
| 716 keyboard_event.nativeKeyCode = key_code; | |
| 717 keyboard_event.windowsKeyCode = key_code; | |
| 718 keyboard_event.setKeyIdentifierFromWindowsKeyCode(); | |
| 719 | |
| 720 // Keypress = type character | |
| 721 if (type.compare(keys::kKeyboardEventTypeValueKeypress) == 0) { | |
| 722 int char_code; | |
| 723 if (dict->HasKey(keys::kKeyboardEventCharCodeKey)) { | |
| 724 EXTENSION_FUNCTION_VALIDATE( | |
| 725 dict->GetInteger(keys::kKeyboardEventCharCodeKey, &char_code)); | |
| 726 keyboard_event.text[0] = char_code; | |
| 727 keyboard_event.unmodifiedText[0] = char_code; | |
| 728 } else { | |
| 729 error_ = keys::kInvalidKeyboardEventObjectError; | |
| 730 return false; | |
| 731 } | |
| 732 } | |
| 733 | |
| 734 bool alt_key; | |
| 735 if (dict->HasKey(keys::kKeyboardEventAltKeyKey)) | |
| 736 EXTENSION_FUNCTION_VALIDATE( | |
| 737 dict->GetBoolean(keys::kKeyboardEventAltKeyKey, &alt_key)); | |
| 738 if (alt_key) | |
| 739 keyboard_event.modifiers |= WebInputEvent::AltKey; | |
| 740 | |
| 741 bool ctrl_key; | |
| 742 if (dict->HasKey(keys::kKeyboardEventCtrlKeyKey)) | |
| 743 EXTENSION_FUNCTION_VALIDATE( | |
| 744 dict->GetBoolean(keys::kKeyboardEventCtrlKeyKey, &ctrl_key)); | |
| 745 if (ctrl_key) | |
| 746 keyboard_event.modifiers |= WebInputEvent::ControlKey; | |
| 747 | |
| 748 bool meta_key = false; | |
| 749 if (dict->HasKey(keys::kMouseEventMetaKeyKey)) | |
| 750 EXTENSION_FUNCTION_VALIDATE( | |
| 751 dict->GetBoolean(keys::kMouseEventMetaKeyKey, &meta_key)); | |
| 752 if (meta_key) | |
| 753 keyboard_event.modifiers |= WebInputEvent::MetaKey; | |
| 754 | |
| 755 bool shift_key; | |
| 756 if (dict->HasKey(keys::kKeyboardEventShiftKeyKey)) | |
| 757 EXTENSION_FUNCTION_VALIDATE( | |
| 758 dict->GetBoolean(keys::kKeyboardEventShiftKeyKey, &shift_key)); | |
| 759 if (shift_key) | |
| 760 keyboard_event.modifiers |= WebInputEvent::ShiftKey; | |
| 761 | |
| 762 // Forward the event. | |
| 763 tab->contents()->render_view_host()-> | |
| 764 ForwardKeyboardEvent(keyboard_event); | |
| 765 | |
| 766 if (has_callback()) { | |
| 767 result_.reset(tab->CreateValue()); | |
| 768 SendResponse(true); | |
| 769 } | |
| 770 | |
| 771 return true; | |
| 772 } | |
| 773 | |
| 774 // sendMouseEvent -------------------------------------------------------------- | |
| 775 | |
| 776 SendMouseEventOffscreenTabFunction::SendMouseEventOffscreenTabFunction() {} | |
| 777 SendMouseEventOffscreenTabFunction::~SendMouseEventOffscreenTabFunction() {} | |
| 778 | |
| 779 bool SendMouseEventOffscreenTabFunction::RunImpl() { | |
| 780 int tab_id; | |
| 781 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
| 782 | |
| 783 OffscreenTab* tab = NULL; | |
| 784 if (!GetMap()->GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) | |
| 785 return false; | |
| 786 | |
| 787 // JavaScript MouseEvent. | |
| 788 DictionaryValue* dict = NULL; | |
| 789 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &dict)); | |
| 790 | |
| 791 std::string type; | |
| 792 if (dict->HasKey(keys::kMouseEventTypeKey)) { | |
| 793 EXTENSION_FUNCTION_VALIDATE( | |
| 794 dict->GetString(keys::kMouseEventTypeKey, &type)); | |
| 795 } else { | |
| 796 error_ = keys::kInvalidMouseEventObjectError; | |
| 797 return false; | |
| 798 } | |
| 799 | |
| 800 if (type.compare(keys::kMouseEventTypeValueMousewheel) == 0) { | |
| 801 WebKit::WebMouseWheelEvent wheel_event; | |
| 802 | |
| 803 wheel_event.type = WebInputEvent::MouseWheel; | |
| 804 | |
| 805 if (dict->HasKey(keys::kMouseEventWheelDeltaXKey) && | |
| 806 dict->HasKey(keys::kMouseEventWheelDeltaYKey)) { | |
| 807 int delta_x, delta_y; | |
| 808 EXTENSION_FUNCTION_VALIDATE( | |
| 809 dict->GetInteger(keys::kMouseEventWheelDeltaXKey, &delta_x)); | |
| 810 EXTENSION_FUNCTION_VALIDATE( | |
| 811 dict->GetInteger(keys::kMouseEventWheelDeltaYKey, &delta_y)); | |
| 812 wheel_event.deltaX = delta_x; | |
| 813 wheel_event.deltaY = delta_y; | |
| 814 } else { | |
| 815 error_ = keys::kInvalidMouseEventObjectError; | |
| 816 return false; | |
| 817 } | |
| 818 | |
| 819 // Forward the event. | |
| 820 tab->contents()->render_view_host()->ForwardWheelEvent(wheel_event); | |
| 821 } else { | |
| 822 WebKit::WebMouseEvent mouse_event; | |
| 823 | |
| 824 if (type.compare(keys::kMouseEventTypeValueMousedown) == 0 || | |
| 825 type.compare(keys::kMouseEventTypeValueClick) == 0) { | |
| 826 mouse_event.type = WebKit::WebInputEvent::MouseDown; | |
| 827 } else if (type.compare(keys::kMouseEventTypeValueMouseup) == 0) { | |
| 828 mouse_event.type = WebKit::WebInputEvent::MouseUp; | |
| 829 } else if (type.compare(keys::kMouseEventTypeValueMousemove) == 0) { | |
| 830 mouse_event.type = WebKit::WebInputEvent::MouseMove; | |
| 831 } else { | |
| 832 error_ = keys::kInvalidMouseEventObjectError; | |
| 833 return false; | |
| 834 } | |
| 835 | |
| 836 int button; | |
| 837 if (dict->HasKey(keys::kMouseEventButtonKey)) { | |
| 838 EXTENSION_FUNCTION_VALIDATE( | |
| 839 dict->GetInteger(keys::kMouseEventButtonKey, &button)); | |
| 840 } else { | |
| 841 error_ = keys::kInvalidMouseEventObjectError; | |
| 842 return false; | |
| 843 } | |
| 844 | |
| 845 if (button == keys::kMouseEventButtonValueLeft) { | |
| 846 mouse_event.button = WebKit::WebMouseEvent::ButtonLeft; | |
| 847 } else if (button == keys::kMouseEventButtonValueMiddle) { | |
| 848 mouse_event.button = WebKit::WebMouseEvent::ButtonMiddle; | |
| 849 } else if (button == keys::kMouseEventButtonValueRight) { | |
| 850 mouse_event.button = WebKit::WebMouseEvent::ButtonRight; | |
| 851 } else { | |
| 852 error_ = keys::kInvalidMouseEventObjectError; | |
| 853 return false; | |
| 854 } | |
| 855 | |
| 856 if (HasOptionalArgument(2) && HasOptionalArgument(3)) { | |
| 857 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &mouse_event.x)); | |
| 858 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &mouse_event.y)); | |
| 859 } else { | |
| 860 error_ = keys::kNoMouseCoordinatesError; | |
| 861 return false; | |
| 862 } | |
| 863 | |
| 864 bool alt_key = false; | |
| 865 if (dict->HasKey(keys::kMouseEventAltKeyKey)) | |
| 866 EXTENSION_FUNCTION_VALIDATE( | |
| 867 dict->GetBoolean(keys::kMouseEventAltKeyKey, &alt_key)); | |
| 868 if (alt_key) | |
| 869 mouse_event.modifiers |= WebInputEvent::AltKey; | |
| 870 | |
| 871 bool ctrl_key = false; | |
| 872 if (dict->HasKey(keys::kMouseEventCtrlKeyKey)) | |
| 873 EXTENSION_FUNCTION_VALIDATE( | |
| 874 dict->GetBoolean(keys::kMouseEventCtrlKeyKey, &ctrl_key)); | |
| 875 if (ctrl_key) | |
| 876 mouse_event.modifiers |= WebInputEvent::ControlKey; | |
| 877 | |
| 878 bool meta_key = false; | |
| 879 if (dict->HasKey(keys::kMouseEventMetaKeyKey)) | |
| 880 EXTENSION_FUNCTION_VALIDATE( | |
| 881 dict->GetBoolean(keys::kMouseEventMetaKeyKey, &meta_key)); | |
| 882 if (meta_key) | |
| 883 mouse_event.modifiers |= WebInputEvent::MetaKey; | |
| 884 | |
| 885 bool shift_key = false; | |
| 886 if (dict->HasKey(keys::kMouseEventShiftKeyKey)) | |
| 887 EXTENSION_FUNCTION_VALIDATE( | |
| 888 dict->GetBoolean(keys::kMouseEventShiftKeyKey, &shift_key)); | |
| 889 if (shift_key) | |
| 890 mouse_event.modifiers |= WebInputEvent::ShiftKey; | |
| 891 | |
| 892 mouse_event.clickCount = 1; | |
| 893 | |
| 894 // Forward the event. | |
| 895 tab->contents()->render_view_host()->ForwardMouseEvent(mouse_event); | |
| 896 | |
| 897 // If the event is a click, | |
| 898 // fire a mouseup event in addition to the mousedown. | |
| 899 if (type.compare(keys::kMouseEventTypeValueClick) == 0) { | |
| 900 mouse_event.type = WebKit::WebInputEvent::MouseUp; | |
| 901 tab->contents()->render_view_host()->ForwardMouseEvent(mouse_event); | |
| 902 } | |
| 903 } | |
| 904 | |
| 905 if (has_callback()) { | |
| 906 result_.reset(tab->CreateValue()); | |
| 907 SendResponse(true); | |
| 908 } | |
| 909 | |
| 910 return true; | |
| 911 } | |
| 912 | |
| 913 // toDataUrl ------------------------------------------------------------------- | |
| 914 | |
| 915 ToDataUrlOffscreenTabFunction::ToDataUrlOffscreenTabFunction() {} | |
| 916 ToDataUrlOffscreenTabFunction::~ToDataUrlOffscreenTabFunction() {} | |
| 917 | |
| 918 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
| 919 // TODO(alexbost): We want to optimize this function in order to get more image | |
| 920 // updates on the browser side. One improvement would be to implement another | |
| 921 // hash map in order to get offscreen tabs in O(1). | |
|
jstritar
2011/09/15 18:52:01
Do you know how fast the actual TabContentsWrapper
alexbost
2011/09/15 19:39:36
I haven't tested it but it depends on the image wi
| |
| 922 bool ToDataUrlOffscreenTabFunction::RunImpl() { | |
| 923 int tab_id; | |
| 924 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
| 925 | |
| 926 | |
|
jstritar
2011/09/15 18:52:01
nit: delete new line
alexbost
2011/09/15 19:39:36
Done.
| |
| 927 OffscreenTab* tab = NULL; | |
| 928 if (!GetMap()->GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) | |
| 929 return false; | |
| 930 | |
| 931 image_format_ = FORMAT_JPEG; // Default format is JPEG. | |
| 932 image_quality_ = kDefaultQuality; // Default quality setting. | |
| 933 | |
| 934 if (HasOptionalArgument(1)) { | |
| 935 DictionaryValue* options; | |
| 936 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); | |
| 937 | |
| 938 if (options->HasKey(keys::kFormatKey)) { | |
| 939 std::string format; | |
| 940 EXTENSION_FUNCTION_VALIDATE( | |
| 941 options->GetString(keys::kFormatKey, &format)); | |
| 942 | |
| 943 if (format == keys::kFormatValueJpeg) { | |
| 944 image_format_ = FORMAT_JPEG; | |
| 945 } else if (format == keys::kFormatValuePng) { | |
| 946 image_format_ = FORMAT_PNG; | |
| 947 } else { | |
| 948 // Schema validation should make this unreachable. | |
| 949 EXTENSION_FUNCTION_VALIDATE(0); | |
| 950 } | |
| 951 } | |
| 952 | |
| 953 if (options->HasKey(keys::kQualityKey)) { | |
| 954 EXTENSION_FUNCTION_VALIDATE( | |
| 955 options->GetInteger(keys::kQualityKey, &image_quality_)); | |
| 956 } | |
| 957 } | |
| 958 | |
| 959 // captureVisibleTab() can return an image containing sensitive information | |
| 960 // that the browser would otherwise protect. Ensure the extension has | |
| 961 // permission to do this. | |
| 962 if (!GetExtension()-> | |
| 963 CanCaptureVisiblePage(tab->contents()->GetURL(), &error_)) | |
| 964 return false; | |
| 965 | |
| 966 // The backing store approach works on Linux but not on Mac. | |
| 967 // TODO(alexbost): Test on Windows | |
| 968 #if !defined(OS_MACOSX) | |
| 969 RenderViewHost* render_view_host = tab->contents()->render_view_host(); | |
| 970 | |
| 971 // If a backing store is cached for the tab we want to capture, | |
| 972 // and it can be copied into a bitmap, then use it to generate the image. | |
| 973 BackingStore* backing_store = render_view_host->GetBackingStore(false); | |
| 974 if (backing_store && CaptureSnapshotFromBackingStore(backing_store)) | |
| 975 return true; | |
| 976 #endif | |
| 977 | |
| 978 // Ask the renderer for a snapshot of the tab. | |
| 979 TabContentsWrapper* tab_wrapper = | |
| 980 TabContentsWrapper::GetCurrentWrapperForContents(tab->contents()); | |
| 981 tab_wrapper->CaptureSnapshot(); | |
| 982 registrar_.Add(this, | |
| 983 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, | |
| 984 Source<TabContentsWrapper>(tab_wrapper)); | |
| 985 | |
| 986 AddRef(); // Balanced in ToDataUrlOffscreenTabFunction::Observe(). | |
| 987 | |
| 988 return true; | |
| 989 } | |
| 990 | |
| 991 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
| 992 // Build the image of a tab's contents out of a backing store. | |
| 993 // This may fail if we can not copy a backing store into a bitmap. | |
| 994 // For example, some uncommon X11 visual modes are not supported by | |
| 995 // CopyFromBackingStore(). | |
| 996 bool ToDataUrlOffscreenTabFunction::CaptureSnapshotFromBackingStore( | |
| 997 BackingStore* backing_store) { | |
| 998 | |
| 999 skia::PlatformCanvas temp_canvas; | |
| 1000 if (!backing_store->CopyFromBackingStore(gfx::Rect(backing_store->size()), | |
| 1001 &temp_canvas)) { | |
| 1002 return false; | |
| 1003 } | |
| 1004 VLOG(1) << "captureVisibleTab() got image from backing store."; | |
| 1005 | |
| 1006 SendResultFromBitmap( | |
| 1007 skia::GetTopDevice(temp_canvas)->accessBitmap(false)); | |
| 1008 return true; | |
| 1009 } | |
| 1010 | |
| 1011 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
| 1012 void ToDataUrlOffscreenTabFunction::Observe(int type, | |
| 1013 const NotificationSource& source, | |
| 1014 const NotificationDetails& details) { | |
| 1015 DCHECK(type == chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN); | |
| 1016 | |
| 1017 const SkBitmap *screen_capture = Details<const SkBitmap>(details).ptr(); | |
| 1018 const bool error = screen_capture->empty(); | |
| 1019 | |
| 1020 if (error) { | |
| 1021 error_ = keys::kInternalVisibleTabCaptureError; | |
| 1022 SendResponse(false); | |
| 1023 } else { | |
| 1024 VLOG(1) << "Got image from renderer."; | |
| 1025 SendResultFromBitmap(*screen_capture); | |
| 1026 } | |
| 1027 | |
| 1028 Release(); // Balanced in ToDataUrlOffscreenTabFunction::RunImpl(). | |
| 1029 } | |
| 1030 | |
| 1031 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
| 1032 // Turn a bitmap of the screen into an image, set that image as the result, | |
| 1033 // and call SendResponse(). | |
| 1034 void ToDataUrlOffscreenTabFunction::SendResultFromBitmap( | |
| 1035 const SkBitmap& screen_capture) { | |
| 1036 std::vector<unsigned char> data; | |
| 1037 SkAutoLockPixels screen_capture_lock(screen_capture); | |
| 1038 bool encoded = false; | |
| 1039 std::string mime_type; | |
| 1040 switch (image_format_) { | |
| 1041 case FORMAT_JPEG: | |
| 1042 encoded = gfx::JPEGCodec::Encode( | |
| 1043 reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), | |
| 1044 gfx::JPEGCodec::FORMAT_SkBitmap, | |
| 1045 screen_capture.width(), | |
| 1046 screen_capture.height(), | |
| 1047 static_cast<int>(screen_capture.rowBytes()), | |
| 1048 image_quality_, | |
| 1049 &data); | |
| 1050 mime_type = keys::kMimeTypeJpeg; | |
| 1051 break; | |
| 1052 case FORMAT_PNG: | |
| 1053 encoded = gfx::PNGCodec::EncodeBGRASkBitmap( | |
| 1054 screen_capture, | |
| 1055 true, // Discard transparency. | |
| 1056 &data); | |
| 1057 mime_type = keys::kMimeTypePng; | |
| 1058 break; | |
| 1059 default: | |
| 1060 NOTREACHED() << "Invalid image format."; | |
| 1061 } | |
| 1062 | |
| 1063 if (!encoded) { | |
| 1064 error_ = keys::kInternalVisibleTabCaptureError; | |
| 1065 SendResponse(false); | |
| 1066 return; | |
| 1067 } | |
| 1068 | |
| 1069 std::string base64_result; | |
| 1070 base::StringPiece stream_as_string( | |
| 1071 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); | |
| 1072 | |
| 1073 base::Base64Encode(stream_as_string, &base64_result); | |
| 1074 base64_result.insert(0, base::StringPrintf("data:%s;base64,", | |
| 1075 mime_type.c_str())); | |
| 1076 result_.reset(new StringValue(base64_result)); | |
| 1077 SendResponse(true); | |
| 1078 } | |
| 1079 | |
| 1080 // update ---------------------------------------------------------------------- | |
| 1081 | |
| 1082 UpdateOffscreenTabFunction::UpdateOffscreenTabFunction() {} | |
| 1083 UpdateOffscreenTabFunction::~UpdateOffscreenTabFunction() {} | |
| 1084 | |
| 1085 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
| 1086 bool UpdateOffscreenTabFunction::RunImpl() { | |
| 1087 int tab_id; | |
| 1088 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
| 1089 | |
| 1090 OffscreenTab* tab = NULL; | |
| 1091 if (!GetMap()->GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) | |
| 1092 return false; | |
| 1093 | |
| 1094 DictionaryValue* update_props; | |
| 1095 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); | |
| 1096 | |
| 1097 // Url | |
| 1098 if (update_props->HasKey(keys::kUrlKey)) { | |
| 1099 std::string url_string; | |
| 1100 GURL url; | |
| 1101 EXTENSION_FUNCTION_VALIDATE( | |
| 1102 update_props->GetString(keys::kUrlKey, &url_string)); | |
| 1103 url = ResolvePossiblyRelativeURL(url_string, GetExtension()); | |
| 1104 if (!url.is_valid()) { | |
| 1105 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
| 1106 keys::kInvalidUrlError, url_string); | |
| 1107 return false; | |
| 1108 } | |
| 1109 if (IsCrashURL(url)) { | |
| 1110 error_ = keys::kNoCrashBrowserError; | |
| 1111 return false; | |
| 1112 } | |
| 1113 | |
| 1114 // JavaScript URLs can do the same kinds of things as cross-origin XHR, so | |
| 1115 // we need to check host permissions before allowing them. | |
| 1116 if (url.SchemeIs(chrome::kJavaScriptScheme)) { | |
| 1117 if (!GetExtension()->CanExecuteScriptOnPage( | |
| 1118 tab->contents()->GetURL(), NULL, &error_)) { | |
| 1119 return false; | |
| 1120 } | |
| 1121 | |
| 1122 ExtensionMsg_ExecuteCode_Params params; | |
| 1123 params.request_id = request_id(); | |
| 1124 params.extension_id = extension_id(); | |
| 1125 params.is_javascript = true; | |
| 1126 params.code = url.path(); | |
| 1127 params.all_frames = false; | |
| 1128 params.in_main_world = true; | |
| 1129 | |
| 1130 RenderViewHost* render_view_host = | |
| 1131 tab->contents()->render_view_host(); | |
| 1132 render_view_host->Send( | |
| 1133 new ExtensionMsg_ExecuteCode(render_view_host->routing_id(), | |
| 1134 params)); | |
| 1135 | |
| 1136 Observe(tab->contents()); | |
| 1137 AddRef(); // balanced in Observe() | |
| 1138 | |
| 1139 return true; | |
| 1140 } | |
| 1141 | |
| 1142 tab->SetUrl(url); | |
| 1143 | |
| 1144 // The URL of a tab contents never actually changes to a JavaScript URL, so | |
| 1145 // this check only makes sense in other cases. | |
| 1146 if (!url.SchemeIs(chrome::kJavaScriptScheme)) | |
| 1147 DCHECK_EQ(url.spec(), tab->contents()->GetURL().spec()); | |
| 1148 } | |
| 1149 | |
| 1150 // Width and height | |
| 1151 if (update_props->HasKey(keys::kWidthKey) || | |
| 1152 update_props->HasKey(keys::kHeightKey)) { | |
| 1153 int width; | |
| 1154 if (update_props->HasKey(keys::kWidthKey)) | |
| 1155 EXTENSION_FUNCTION_VALIDATE( | |
| 1156 update_props->GetInteger(keys::kWidthKey, &width)); | |
| 1157 else | |
| 1158 TabContentsWrapper::GetCurrentWrapperForContents( | |
| 1159 tab->contents())->view()->GetContainerSize().width(); | |
| 1160 | |
| 1161 int height; | |
| 1162 if (update_props->HasKey(keys::kHeightKey)) | |
| 1163 EXTENSION_FUNCTION_VALIDATE( | |
| 1164 update_props->GetInteger(keys::kHeightKey, &height)); | |
| 1165 else | |
| 1166 TabContentsWrapper::GetCurrentWrapperForContents( | |
| 1167 tab->contents())->view()->GetContainerSize().height(); | |
| 1168 | |
| 1169 tab->SetSize(width, height); | |
| 1170 } | |
| 1171 | |
| 1172 // Callback | |
| 1173 if (has_callback()) { | |
| 1174 result_.reset(tab->CreateValue()); | |
| 1175 SendResponse(true); | |
| 1176 } | |
| 1177 | |
| 1178 return true; | |
| 1179 } | |
| 1180 | |
| 1181 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
| 1182 bool UpdateOffscreenTabFunction::OnMessageReceived( | |
| 1183 const IPC::Message& message) { | |
| 1184 if (message.type() != ExtensionHostMsg_ExecuteCodeFinished::ID) | |
| 1185 return false; | |
| 1186 | |
| 1187 int message_request_id; | |
| 1188 void* iter = NULL; | |
| 1189 if (!message.ReadInt(&iter, &message_request_id)) { | |
| 1190 NOTREACHED() << "malformed extension message"; | |
| 1191 return true; | |
| 1192 } | |
| 1193 | |
| 1194 if (message_request_id != request_id()) | |
| 1195 return false; | |
| 1196 | |
| 1197 IPC_BEGIN_MESSAGE_MAP(UpdateOffscreenTabFunction, message) | |
| 1198 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished, | |
| 1199 OnExecuteCodeFinished) | |
| 1200 IPC_END_MESSAGE_MAP() | |
| 1201 return true; | |
| 1202 } | |
| 1203 | |
| 1204 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
| 1205 void UpdateOffscreenTabFunction::OnExecuteCodeFinished(int request_id, | |
| 1206 bool success, | |
| 1207 const std::string& error) { | |
|
jstritar
2011/09/15 18:52:01
nit: indent
alexbost
2011/09/15 19:39:36
Done.
| |
| 1208 if (!error.empty()) { | |
| 1209 CHECK(!success); | |
| 1210 error_ = error; | |
| 1211 } | |
| 1212 | |
| 1213 SendResponse(success); | |
| 1214 | |
| 1215 Observe(NULL); | |
| 1216 Release(); // balanced in Execute() | |
| 1217 } | |
| 1218 | |
| OLD | NEW |