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/automation/automation_provider_observers.h" | 5 #include "chrome/browser/automation/automation_provider_observers.h" |
6 | 6 |
7 #include <deque> | 7 #include <deque> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 1863 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1874 reply_message_.release()).SendSuccess(NULL); | 1874 reply_message_.release()).SendSuccess(NULL); |
1875 } | 1875 } |
1876 delete this; | 1876 delete this; |
1877 } else { | 1877 } else { |
1878 NOTREACHED(); | 1878 NOTREACHED(); |
1879 } | 1879 } |
1880 } | 1880 } |
1881 | 1881 |
1882 PageSnapshotTaker::PageSnapshotTaker(AutomationProvider* automation, | 1882 PageSnapshotTaker::PageSnapshotTaker(AutomationProvider* automation, |
1883 IPC::Message* reply_message, | 1883 IPC::Message* reply_message, |
1884 RenderViewHost* render_view, | 1884 TabContentsWrapper* tab_contents, |
1885 const FilePath& path) | 1885 const FilePath& path) |
1886 : automation_(automation->AsWeakPtr()), | 1886 : automation_(automation->AsWeakPtr()), |
1887 reply_message_(reply_message), | 1887 reply_message_(reply_message), |
1888 render_view_(render_view), | 1888 tab_contents_(tab_contents), |
1889 image_path_(path), | 1889 image_path_(path) { |
1890 received_width_(false) {} | 1890 registrar_.Add(this, chrome::NOTIFICATION_APP_MODAL_DIALOG_SHOWN, |
| 1891 NotificationService::AllSources()); |
| 1892 } |
1891 | 1893 |
1892 PageSnapshotTaker::~PageSnapshotTaker() {} | 1894 PageSnapshotTaker::~PageSnapshotTaker() {} |
1893 | 1895 |
1894 void PageSnapshotTaker::Start() { | 1896 void PageSnapshotTaker::Start() { |
1895 ExecuteScript(L"window.domAutomationController.send(document.width);"); | 1897 StartObserving(tab_contents_->automation_tab_helper()); |
| 1898 tab_contents_->automation_tab_helper()->SnapshotEntirePage(); |
1896 } | 1899 } |
1897 | 1900 |
1898 void PageSnapshotTaker::OnDomOperationCompleted(const std::string& json) { | 1901 void PageSnapshotTaker::OnSnapshotEntirePageACK( |
1899 int dimension; | 1902 bool success, |
1900 if (!base::StringToInt(json, &dimension)) { | 1903 const std::vector<unsigned char>& png_data, |
1901 SendMessage(false, "could not parse received dimensions: " + json); | 1904 const std::string& error_msg) { |
1902 } else if (!received_width_) { | 1905 bool overall_success = success; |
1903 received_width_ = true; | 1906 std::string overall_error_msg = error_msg; |
1904 entire_page_size_.set_width(dimension); | 1907 if (success) { |
1905 | 1908 base::ThreadRestrictions::ScopedAllowIO allow_io; |
1906 ExecuteScript(L"window.domAutomationController.send(document.height);"); | 1909 int bytes_written = file_util::WriteFile(image_path_, |
1907 } else { | 1910 reinterpret_cast<const char*>(&png_data[0]), png_data.size()); |
1908 entire_page_size_.set_height(dimension); | 1911 overall_success = (bytes_written == static_cast<int>(png_data.size())); |
1909 | 1912 if (!overall_success) |
1910 ThumbnailGenerator* generator = | 1913 overall_error_msg = "could not write snapshot to disk"; |
1911 g_browser_process->GetThumbnailGenerator(); | |
1912 ThumbnailGenerator::ThumbnailReadyCallback callback = | |
1913 base::Bind(&PageSnapshotTaker::OnSnapshotTaken, base::Unretained(this)); | |
1914 // Don't actually start the thumbnail generator, this leads to crashes on | |
1915 // Mac, crbug.com/62986. Instead, just hook the generator to the | |
1916 // RenderViewHost manually. | |
1917 | |
1918 generator->MonitorRenderer(render_view_, true); | |
1919 generator->AskForSnapshot(render_view_, false, callback, | |
1920 entire_page_size_, entire_page_size_); | |
1921 } | 1914 } |
| 1915 SendMessage(overall_success, overall_error_msg); |
1922 } | 1916 } |
1923 | 1917 |
1924 void PageSnapshotTaker::OnModalDialogShown() { | 1918 void PageSnapshotTaker::Observe(int type, |
| 1919 const NotificationSource& source, |
| 1920 const NotificationDetails& details) { |
1925 SendMessage(false, "a modal dialog is active"); | 1921 SendMessage(false, "a modal dialog is active"); |
1926 } | 1922 } |
1927 | 1923 |
1928 void PageSnapshotTaker::OnSnapshotTaken(const SkBitmap& bitmap) { | |
1929 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
1930 std::vector<unsigned char> png_data; | |
1931 SkAutoLockPixels lock_input(bitmap); | |
1932 bool success = gfx::PNGCodec::Encode( | |
1933 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
1934 gfx::PNGCodec::FORMAT_BGRA, | |
1935 gfx::Size(bitmap.width(), bitmap.height()), | |
1936 bitmap.rowBytes(), | |
1937 true, // discard_transparency | |
1938 std::vector<gfx::PNGCodec::Comment>(), | |
1939 &png_data); | |
1940 std::string error_msg; | |
1941 if (!success) { | |
1942 error_msg = "could not encode bitmap as PNG"; | |
1943 } else { | |
1944 int bytes_written = file_util::WriteFile(image_path_, | |
1945 reinterpret_cast<char*>(&png_data[0]), png_data.size()); | |
1946 success = bytes_written == static_cast<int>(png_data.size()); | |
1947 if (!success) | |
1948 error_msg = "could not write snapshot to disk"; | |
1949 } | |
1950 SendMessage(success, error_msg); | |
1951 } | |
1952 | |
1953 void PageSnapshotTaker::ExecuteScript(const std::wstring& javascript) { | |
1954 std::wstring set_automation_id; | |
1955 base::SStringPrintf( | |
1956 &set_automation_id, | |
1957 L"window.domAutomationController.setAutomationId(%d);", | |
1958 reply_message_->routing_id()); | |
1959 | |
1960 render_view_->ExecuteJavascriptInWebFrame(string16(), | |
1961 WideToUTF16Hack(set_automation_id)); | |
1962 render_view_->ExecuteJavascriptInWebFrame(string16(), | |
1963 WideToUTF16Hack(javascript)); | |
1964 } | |
1965 | 1924 |
1966 void PageSnapshotTaker::SendMessage(bool success, | 1925 void PageSnapshotTaker::SendMessage(bool success, |
1967 const std::string& error_msg) { | 1926 const std::string& error_msg) { |
1968 if (automation_) { | 1927 if (automation_) { |
1969 if (success) { | 1928 if (success) { |
1970 AutomationJSONReply(automation_, reply_message_.release()) | 1929 AutomationJSONReply(automation_, reply_message_.release()) |
1971 .SendSuccess(NULL); | 1930 .SendSuccess(NULL); |
1972 } else { | 1931 } else { |
1973 AutomationJSONReply(automation_, reply_message_.release()) | 1932 AutomationJSONReply(automation_, reply_message_.release()) |
1974 .SendError("Failed to take snapshot of page: " + error_msg); | 1933 .SendError("Failed to take snapshot of page: " + error_msg); |
(...skipping 915 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2890 if (automation_) { | 2849 if (automation_) { |
2891 AutomationJSONReply(automation_, reply_message_.release()) | 2850 AutomationJSONReply(automation_, reply_message_.release()) |
2892 .SendSuccess(NULL); | 2851 .SendSuccess(NULL); |
2893 } | 2852 } |
2894 delete this; | 2853 delete this; |
2895 } | 2854 } |
2896 } else { | 2855 } else { |
2897 NOTREACHED(); | 2856 NOTREACHED(); |
2898 } | 2857 } |
2899 } | 2858 } |
OLD | NEW |