| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/page_capture/page_capture_api.h" | 5 #include "chrome/browser/extensions/api/page_capture/page_capture_api.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 11 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/memory/ptr_util.h" |
| 12 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/extensions/extension_tab_util.h" | 15 #include "chrome/browser/extensions/extension_tab_util.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/profiles/profiles_state.h" |
| 15 #include "content/public/browser/child_process_security_policy.h" | 18 #include "content/public/browser/child_process_security_policy.h" |
| 16 #include "content/public/browser/notification_details.h" | 19 #include "content/public/browser/notification_details.h" |
| 17 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
| 18 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/mhtml_generation_params.h" | 25 #include "content/public/common/mhtml_generation_params.h" |
| 23 #include "extensions/common/extension_messages.h" | 26 #include "extensions/common/extension_messages.h" |
| 24 | 27 |
| 28 #if defined(OS_CHROMEOS) |
| 29 #include "chrome/browser/chromeos/extensions/public_session_permission_helper.h" |
| 30 #endif |
| 31 |
| 25 using content::BrowserThread; | 32 using content::BrowserThread; |
| 26 using content::ChildProcessSecurityPolicy; | 33 using content::ChildProcessSecurityPolicy; |
| 27 using content::WebContents; | 34 using content::WebContents; |
| 28 using extensions::PageCaptureSaveAsMHTMLFunction; | 35 using extensions::PageCaptureSaveAsMHTMLFunction; |
| 29 using storage::ShareableFileReference; | 36 using storage::ShareableFileReference; |
| 30 | 37 |
| 31 namespace SaveAsMHTML = extensions::api::page_capture::SaveAsMHTML; | 38 namespace SaveAsMHTML = extensions::api::page_capture::SaveAsMHTML; |
| 32 | 39 |
| 33 namespace { | 40 namespace { |
| 34 | 41 |
| 35 const char kFileTooBigError[] = "The MHTML file generated is too big."; | 42 const char kFileTooBigError[] = "The MHTML file generated is too big."; |
| 36 const char kMHTMLGenerationFailedError[] = "Failed to generate MHTML."; | 43 const char kMHTMLGenerationFailedError[] = "Failed to generate MHTML."; |
| 37 const char kTemporaryFileError[] = "Failed to create a temporary file."; | 44 const char kTemporaryFileError[] = "Failed to create a temporary file."; |
| 38 const char kTabClosedError[] = "Cannot find the tab for this request."; | 45 const char kTabClosedError[] = "Cannot find the tab for this request."; |
| 46 #if defined(OS_CHROMEOS) |
| 47 const char kUserDenied[] = "User denied request."; |
| 48 #endif |
| 39 | 49 |
| 40 void ClearFileReferenceOnIOThread( | 50 void ClearFileReferenceOnIOThread( |
| 41 scoped_refptr<storage::ShareableFileReference>) {} | 51 scoped_refptr<storage::ShareableFileReference>) {} |
| 42 | 52 |
| 43 } // namespace | 53 } // namespace |
| 44 | 54 |
| 45 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; | 55 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; |
| 46 | 56 |
| 47 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { | 57 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { |
| 48 } | 58 } |
| 49 | 59 |
| 50 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { | 60 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { |
| 51 if (mhtml_file_.get()) { | 61 if (mhtml_file_.get()) { |
| 52 BrowserThread::PostTask( | 62 BrowserThread::PostTask( |
| 53 BrowserThread::IO, FROM_HERE, | 63 BrowserThread::IO, FROM_HERE, |
| 54 base::Bind(&ClearFileReferenceOnIOThread, base::Passed(&mhtml_file_))); | 64 base::Bind(&ClearFileReferenceOnIOThread, base::Passed(&mhtml_file_))); |
| 55 } | 65 } |
| 56 } | 66 } |
| 57 | 67 |
| 58 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { | 68 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { |
| 59 test_delegate_ = delegate; | 69 test_delegate_ = delegate; |
| 60 } | 70 } |
| 61 | 71 |
| 62 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { | 72 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { |
| 63 params_ = SaveAsMHTML::Params::Create(*args_); | 73 params_ = SaveAsMHTML::Params::Create(*args_); |
| 64 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 74 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 65 | 75 |
| 66 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() | 76 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() |
| 67 | 77 |
| 78 // In Public Sessions, extensions (and apps) are force-installed by admin |
| 79 // policy so the user does not get a chance to review the permissions for |
| 80 // these extensions. This is not acceptable from a security/privacy |
| 81 // standpoint, so when an extension uses the PageCapture API for the first |
| 82 // time, we show the user a dialog where they can choose whether to allow the |
| 83 // extension access to the API. |
| 84 #if defined(OS_CHROMEOS) |
| 85 if (profiles::IsPublicSession()) { |
| 86 WebContents* web_contents = GetWebContents(); |
| 87 if (!web_contents) { |
| 88 ReturnFailure(kTabClosedError); |
| 89 return true; |
| 90 } |
| 91 // This Unretained is safe because this object is Released() in |
| 92 // OnMessageReceived which gets called at some point after callback is run. |
| 93 auto callback = |
| 94 base::Bind(&PageCaptureSaveAsMHTMLFunction::ResolvePermissionRequest, |
| 95 base::Unretained(this)); |
| 96 permission_helper::HandlePermissionRequest( |
| 97 *extension(), {APIPermission::kPageCapture}, web_contents, callback, |
| 98 permission_helper::PromptFactory()); |
| 99 return true; |
| 100 } |
| 101 #endif |
| 102 |
| 68 BrowserThread::PostTask( | 103 BrowserThread::PostTask( |
| 69 BrowserThread::FILE, FROM_HERE, | 104 BrowserThread::FILE, FROM_HERE, |
| 70 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); | 105 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); |
| 71 return true; | 106 return true; |
| 72 } | 107 } |
| 73 | 108 |
| 74 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( | 109 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( |
| 75 const IPC::Message& message) { | 110 const IPC::Message& message) { |
| 76 if (message.type() != ExtensionHostMsg_ResponseAck::ID) | 111 if (message.type() != ExtensionHostMsg_ResponseAck::ID) |
| 77 return false; | 112 return false; |
| 78 | 113 |
| 79 int message_request_id; | 114 int message_request_id; |
| 80 base::PickleIterator iter(message); | 115 base::PickleIterator iter(message); |
| 81 if (!iter.ReadInt(&message_request_id)) { | 116 if (!iter.ReadInt(&message_request_id)) { |
| 82 NOTREACHED() << "malformed extension message"; | 117 NOTREACHED() << "malformed extension message"; |
| 83 return true; | 118 return true; |
| 84 } | 119 } |
| 85 | 120 |
| 86 if (message_request_id != request_id()) | 121 if (message_request_id != request_id()) |
| 87 return false; | 122 return false; |
| 88 | 123 |
| 89 // The extension process has processed the response and has created a | 124 // The extension process has processed the response and has created a |
| 90 // reference to the blob, it is safe for us to go away. | 125 // reference to the blob, it is safe for us to go away. |
| 91 Release(); // Balanced in Run() | 126 Release(); // Balanced in Run() |
| 92 | 127 |
| 93 return true; | 128 return true; |
| 94 } | 129 } |
| 95 | 130 |
| 131 #if defined(OS_CHROMEOS) |
| 132 void PageCaptureSaveAsMHTMLFunction::ResolvePermissionRequest( |
| 133 const PermissionIDSet& allowed_permissions) { |
| 134 if (allowed_permissions.ContainsID(APIPermission::kPageCapture)) { |
| 135 BrowserThread::PostTask( |
| 136 BrowserThread::FILE, FROM_HERE, |
| 137 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); |
| 138 } else { |
| 139 ReturnFailure(kUserDenied); |
| 140 } |
| 141 } |
| 142 #endif |
| 143 |
| 96 void PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile() { | 144 void PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile() { |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 145 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 98 bool success = base::CreateTemporaryFile(&mhtml_path_); | 146 bool success = base::CreateTemporaryFile(&mhtml_path_); |
| 99 BrowserThread::PostTask( | 147 BrowserThread::PostTask( |
| 100 BrowserThread::IO, FROM_HERE, | 148 BrowserThread::IO, FROM_HERE, |
| 101 base::Bind(&PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated, this, | 149 base::Bind(&PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated, this, |
| 102 success)); | 150 success)); |
| 103 } | 151 } |
| 104 | 152 |
| 105 void PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated(bool success) { | 153 void PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated(bool success) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 GetProfile(), | 243 GetProfile(), |
| 196 include_incognito(), | 244 include_incognito(), |
| 197 &browser, | 245 &browser, |
| 198 NULL, | 246 NULL, |
| 199 &web_contents, | 247 &web_contents, |
| 200 NULL)) { | 248 NULL)) { |
| 201 return NULL; | 249 return NULL; |
| 202 } | 250 } |
| 203 return web_contents; | 251 return web_contents; |
| 204 } | 252 } |
| OLD | NEW |