OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_tabs_module.h" | 5 #include "chrome/browser/extensions/extension_tabs_module.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 16 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
17 #include "chrome/browser/extensions/extension_host.h" | 17 #include "chrome/browser/extensions/extension_host.h" |
18 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
19 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 19 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/sessions/restore_tab_helper.h" | 21 #include "chrome/browser/sessions/restore_tab_helper.h" |
22 #include "chrome/browser/tabs/tab_strip_model.h" | 22 #include "chrome/browser/tabs/tab_strip_model.h" |
23 #include "chrome/browser/translate/translate_tab_helper.h" | 23 #include "chrome/browser/translate/translate_tab_helper.h" |
24 #include "chrome/browser/ui/browser.h" | 24 #include "chrome/browser/ui/browser.h" |
25 #include "chrome/browser/ui/browser_list.h" | 25 #include "chrome/browser/ui/browser_list.h" |
26 #include "chrome/browser/ui/browser_navigator.h" | 26 #include "chrome/browser/ui/browser_navigator.h" |
27 #include "chrome/browser/ui/browser_window.h" | 27 #include "chrome/browser/ui/browser_window.h" |
28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
29 #include "chrome/browser/ui/window_sizer.h" | 29 #include "chrome/browser/ui/window_sizer.h" |
| 30 #include "chrome/common/chrome_notification_types.h" |
30 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
31 #include "chrome/common/extensions/extension.h" | 32 #include "chrome/common/extensions/extension.h" |
32 #include "chrome/common/extensions/extension_error_utils.h" | 33 #include "chrome/common/extensions/extension_error_utils.h" |
33 #include "chrome/common/extensions/extension_messages.h" | 34 #include "chrome/common/extensions/extension_messages.h" |
34 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
35 #include "chrome/common/url_constants.h" | 36 #include "chrome/common/url_constants.h" |
36 #include "content/browser/renderer_host/backing_store.h" | 37 #include "content/browser/renderer_host/backing_store.h" |
37 #include "content/browser/renderer_host/render_view_host.h" | 38 #include "content/browser/renderer_host/render_view_host.h" |
38 #include "content/browser/renderer_host/render_view_host_delegate.h" | 39 #include "content/browser/renderer_host/render_view_host_delegate.h" |
39 #include "content/browser/tab_contents/navigation_entry.h" | 40 #include "content/browser/tab_contents/navigation_entry.h" |
(...skipping 1158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1198 // If a backing store is cached for the tab we want to capture, | 1199 // If a backing store is cached for the tab we want to capture, |
1199 // and it can be copied into a bitmap, then use it to generate the image. | 1200 // and it can be copied into a bitmap, then use it to generate the image. |
1200 BackingStore* backing_store = render_view_host->GetBackingStore(false); | 1201 BackingStore* backing_store = render_view_host->GetBackingStore(false); |
1201 if (backing_store && CaptureSnapshotFromBackingStore(backing_store)) | 1202 if (backing_store && CaptureSnapshotFromBackingStore(backing_store)) |
1202 return true; | 1203 return true; |
1203 | 1204 |
1204 // Ask the renderer for a snapshot of the tab. | 1205 // Ask the renderer for a snapshot of the tab. |
1205 TabContentsWrapper* wrapper = browser->GetSelectedTabContentsWrapper(); | 1206 TabContentsWrapper* wrapper = browser->GetSelectedTabContentsWrapper(); |
1206 wrapper->CaptureSnapshot(); | 1207 wrapper->CaptureSnapshot(); |
1207 registrar_.Add(this, | 1208 registrar_.Add(this, |
1208 NotificationType::TAB_SNAPSHOT_TAKEN, | 1209 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, |
1209 Source<TabContentsWrapper>(wrapper)); | 1210 Source<TabContentsWrapper>(wrapper)); |
1210 AddRef(); // Balanced in CaptureVisibleTabFunction::Observe(). | 1211 AddRef(); // Balanced in CaptureVisibleTabFunction::Observe(). |
1211 | 1212 |
1212 return true; | 1213 return true; |
1213 } | 1214 } |
1214 | 1215 |
1215 // Build the image of a tab's contents out of a backing store. | 1216 // Build the image of a tab's contents out of a backing store. |
1216 // This may fail if we can not copy a backing store into a bitmap. | 1217 // This may fail if we can not copy a backing store into a bitmap. |
1217 // For example, some uncommon X11 visual modes are not supported by | 1218 // For example, some uncommon X11 visual modes are not supported by |
1218 // CopyFromBackingStore(). | 1219 // CopyFromBackingStore(). |
1219 bool CaptureVisibleTabFunction::CaptureSnapshotFromBackingStore( | 1220 bool CaptureVisibleTabFunction::CaptureSnapshotFromBackingStore( |
1220 BackingStore* backing_store) { | 1221 BackingStore* backing_store) { |
1221 | 1222 |
1222 skia::PlatformCanvas temp_canvas; | 1223 skia::PlatformCanvas temp_canvas; |
1223 if (!backing_store->CopyFromBackingStore(gfx::Rect(backing_store->size()), | 1224 if (!backing_store->CopyFromBackingStore(gfx::Rect(backing_store->size()), |
1224 &temp_canvas)) { | 1225 &temp_canvas)) { |
1225 return false; | 1226 return false; |
1226 } | 1227 } |
1227 VLOG(1) << "captureVisibleTab() got image from backing store."; | 1228 VLOG(1) << "captureVisibleTab() got image from backing store."; |
1228 | 1229 |
1229 SendResultFromBitmap( | 1230 SendResultFromBitmap( |
1230 skia::GetTopDevice(temp_canvas)->accessBitmap(false)); | 1231 skia::GetTopDevice(temp_canvas)->accessBitmap(false)); |
1231 return true; | 1232 return true; |
1232 } | 1233 } |
1233 | 1234 |
1234 // If a backing store was not available in CaptureVisibleTabFunction::RunImpl, | 1235 // If a backing store was not available in CaptureVisibleTabFunction::RunImpl, |
1235 // than the renderer was asked for a snapshot. Listen for a notification | 1236 // than the renderer was asked for a snapshot. Listen for a notification |
1236 // that the snapshot is available. | 1237 // that the snapshot is available. |
1237 void CaptureVisibleTabFunction::Observe(NotificationType type, | 1238 void CaptureVisibleTabFunction::Observe(int type, |
1238 const NotificationSource& source, | 1239 const NotificationSource& source, |
1239 const NotificationDetails& details) { | 1240 const NotificationDetails& details) { |
1240 DCHECK(type == NotificationType::TAB_SNAPSHOT_TAKEN); | 1241 DCHECK(type == chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN); |
1241 | 1242 |
1242 const SkBitmap *screen_capture = Details<const SkBitmap>(details).ptr(); | 1243 const SkBitmap *screen_capture = Details<const SkBitmap>(details).ptr(); |
1243 const bool error = screen_capture->empty(); | 1244 const bool error = screen_capture->empty(); |
1244 | 1245 |
1245 if (error) { | 1246 if (error) { |
1246 error_ = keys::kInternalVisibleTabCaptureError; | 1247 error_ = keys::kInternalVisibleTabCaptureError; |
1247 SendResponse(false); | 1248 SendResponse(false); |
1248 } else { | 1249 } else { |
1249 VLOG(1) << "captureVisibleTab() got image from renderer."; | 1250 VLOG(1) << "captureVisibleTab() got image from renderer."; |
1250 SendResultFromBitmap(*screen_capture); | 1251 SendResultFromBitmap(*screen_capture); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1340 if (!helper->language_state().original_language().empty()) { | 1341 if (!helper->language_state().original_language().empty()) { |
1341 // Delay the callback invocation until after the current JS call has | 1342 // Delay the callback invocation until after the current JS call has |
1342 // returned. | 1343 // returned. |
1343 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 1344 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
1344 this, &DetectTabLanguageFunction::GotLanguage, | 1345 this, &DetectTabLanguageFunction::GotLanguage, |
1345 helper->language_state().original_language())); | 1346 helper->language_state().original_language())); |
1346 return true; | 1347 return true; |
1347 } | 1348 } |
1348 // The tab contents does not know its language yet. Let's wait until it | 1349 // The tab contents does not know its language yet. Let's wait until it |
1349 // receives it, or until the tab is closed/navigates to some other page. | 1350 // receives it, or until the tab is closed/navigates to some other page. |
1350 registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, | 1351 registrar_.Add(this, chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
1351 Source<TabContents>(contents->tab_contents())); | 1352 Source<TabContents>(contents->tab_contents())); |
1352 registrar_.Add(this, NotificationType::TAB_CLOSING, | 1353 registrar_.Add(this, content::NOTIFICATION_TAB_CLOSING, |
1353 Source<NavigationController>(&(contents->controller()))); | 1354 Source<NavigationController>(&(contents->controller()))); |
1354 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 1355 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
1355 Source<NavigationController>(&(contents->controller()))); | 1356 Source<NavigationController>(&(contents->controller()))); |
1356 return true; | 1357 return true; |
1357 } | 1358 } |
1358 | 1359 |
1359 void DetectTabLanguageFunction::Observe(NotificationType type, | 1360 void DetectTabLanguageFunction::Observe(int type, |
1360 const NotificationSource& source, | 1361 const NotificationSource& source, |
1361 const NotificationDetails& details) { | 1362 const NotificationDetails& details) { |
1362 std::string language; | 1363 std::string language; |
1363 if (type == NotificationType::TAB_LANGUAGE_DETERMINED) | 1364 if (type == chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED) |
1364 language = *Details<std::string>(details).ptr(); | 1365 language = *Details<std::string>(details).ptr(); |
1365 | 1366 |
1366 registrar_.RemoveAll(); | 1367 registrar_.RemoveAll(); |
1367 | 1368 |
1368 // Call GotLanguage in all cases as we want to guarantee the callback is | 1369 // Call GotLanguage in all cases as we want to guarantee the callback is |
1369 // called for every API call the extension made. | 1370 // called for every API call the extension made. |
1370 GotLanguage(language); | 1371 GotLanguage(language); |
1371 } | 1372 } |
1372 | 1373 |
1373 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1374 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
1374 result_.reset(Value::CreateStringValue(language.c_str())); | 1375 result_.reset(Value::CreateStringValue(language.c_str())); |
1375 SendResponse(true); | 1376 SendResponse(true); |
1376 | 1377 |
1377 Release(); // Balanced in Run() | 1378 Release(); // Balanced in Run() |
1378 } | 1379 } |
OLD | NEW |