/// <summary> /// この機能を使用して起動したChromeを閉じる /// </summary> /// <exception cref="PlatformInvokeException"> /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理の呼び出しに失敗した場合に発生 /// </exception> /// <exception cref="Win32OperateException"> /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理に失敗した場合に発生 /// </exception> /// <returns> /// 閉じたChromeのサイズ位置の情報 /// サイズ位置の情報が取得できない場合はNULLを返却 /// </returns> public static SizePoint Close() { // 閉じたChromeのサイズ位置の情報の変数を宣言 SizePoint sizePoint = null; // Chromeの音声認識が起動しているかチェック if (ChromeProcessInfoKey.HasValue) { // 閉じるChromeのウィンドウサイズ位置情報を取得 foreach (ProcessInfo info in StartProcess.StartProcessList) { // 起動したChromeのウィンドウハンドルを取得しサイズ位置情報を取得する if (info.StartNumId == ChromeProcessInfoKey.Value && info.HasWindowInfo) { sizePoint = WindowOperate.GetWindowRect(info.WindowInfo.WindowHandle); } // サイズ位置情報が取得できた場合、ループを抜ける if (sizePoint != null) { break; } } // 起動している場合、対象のChromeを閉じる StartProcess.CloseWindow(ChromeProcessInfoKey.Value); ChromeProcessInfoKey = null; } // ローカルHTTPサーバが起動しているかチェック if (Server != null) { // 起動している場合は停止する Server.Stop(); Server = null; } // 取得したサイズ位置情報を返却 return(sizePoint); }
/// <summary> /// Chromeによる音声認識を開始する /// <see cref="ChromePath"/> プロパティが未設定の場合はChromeのEXEファイルパスを自動的に検索し使用する /// </summary> /// <param name="htmlFilePath"> /// 音声認識に使用する既定の「音声認識用.html」ファイルのパス /// </param> /// <param name="postRegex"> /// 「音声認識用.html」において、音声認識認識した文字列をPOSTする際に使用するURLの正規表現 /// (「音声認識用.html」と整合性をとる必要があるパラメータ) /// </param> /// <param name="postParamName"> /// 「音声認識用.html」において、音声認識認識した文字列をPOSTする際に使用するPOSTデータのパラメータ名 /// (「音声認識用.html」と整合性をとる必要があるパラメータ) /// </param> /// <param name="sizePoint"> /// 音声認識用に起動するChromeの初期のサイズと位置 /// (NULLを指定した場合はデフォルトのサイズと位置で起動する) /// </param> /// <param name="localHttpServerPort"> /// 音声認識に使用するローカルHTTPサーバのポート番号を指定する場合に設定するポート番号 /// 指定しない場合(NULLの場合)、または指定したポート番号が使用できない場合は乱数からポート番号を取得し使用する /// </param> /// <param name="responceFunctions"> /// 音声認識した文字に対する処理の指定(複数指定可、優先順位は上から) /// ・引数1 string:音声認識した文字列を渡す /// ・戻り値 byte[]:処理を実行した結果返却するデータ /// </param> /// <exception cref="ArgumentNullException"> /// 引数の <paramref name="htmlFilePath"/> 又は <paramref name="postRegex"/> がNULLの場合に発生 /// </exception> /// <exception cref="ArgumentException"> /// 下記の場合に発生する /// ・引数の <paramref name="htmlFilePath"/> が下記の場合 /// 長さ 0 の文字列 /// 空白のみで構成される /// <see cref="Path.InvalidPathChars"/> で定義される 1 つ以上の正しくない文字を含む /// ・引数の <paramref name="postRegex"/> が正規表現として不正な値の場合 /// </exception> /// <exception cref="SpeechRecognitionException"> /// Chromeが使用不可能な場合に発生 /// (<see cref="ChromePath"/> の指定が間違ってる 又は、Chromeがインストールされていない場合) /// </exception> /// <exception cref="IOException"> /// 下記の場合に発生 /// ・引数の <paramref name="htmlFilePath"/> がシステム定義の最大長を超えている場合 /// [<see cref="PathTooLongException"/>] /// (たとえば、Windowsでは、パスは 248文字未満、ファイル名は 260 文字未満である必要がある) /// ・引数の <paramref name="htmlFilePath"/> が存在しないディレクトリを示している場合 /// [<see cref="DirectoryNotFoundException"/>] /// ・引数の <paramref name="htmlFilePath"/> で指定されたファイルが存在しない場合 /// [<see cref="FileNotFoundException"/>] /// ・I/O エラーが発生した場合 /// [<see cref="IOException"/>] /// </exception> /// <exception cref="UnauthorizedAccessException"> /// 引数の <paramref name="htmlFilePath"/> がファイルを指定しないない(ディレクトリを指定)場合、 /// 又は、呼び出し元に必要なアクセス許可がない場合に発生 /// </exception> /// <exception cref="Win32Exception"> /// 下記の要因で。指定ファイルを開いているときにエラーが発生した場合に発生 /// ・指定ファイルへの完全パスの長さと起動パラメータの長さの合計が、2080 文字を超えている場合 /// ・指定ファイルへのアクセスが拒否された場合 /// </exception> /// <exception cref="PlatformInvokeException"> /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理の呼び出しに失敗した場合に発生 /// </exception> /// <exception cref="Win32OperateException"> /// <see cref="WindowOperate"/> クラスのWin32Apiの処理において、処理に失敗した場合に発生 /// </exception> public static void Start( string htmlFilePath, string postRegex, string postParamName, SizePoint sizePoint, int?localHttpServerPort, params Func <string, byte[]>[] responceFunctions) { // NULLチェック if (htmlFilePath == null) { throw new ArgumentNullException(nameof(htmlFilePath)); } else if (postRegex == null) { throw new ArgumentNullException(nameof(postRegex)); } // Chromeが使用可能かチェックする switch (CanUseChrome) { case ChromeState.CanUse: // OK:処理を継続する break; case ChromeState.NoInstall: // NG:メッセージを表示して処理を中止する throw new SpeechRecognitionException(ErrorMessage.ChromeSpeechRecognitionMessageNoInstall); case ChromeState.ChromePathIsWrong: default: // NG:メッセージを表示して処理を中止する throw new SpeechRecognitionException( ErrorMessage.ChromeSpeechRecognitionMessageChromePathIsWrong); } // 開始前の既にローカルHTTPサーバが起動している場合は落とす if (Server != null) { Server.Stop(); Server = null; } // 開始前の既に起動している音声認識用のChromeが存在する場合は落とす if (ChromeProcessInfoKey.HasValue) { StartProcess.CloseWindow(ChromeProcessInfoKey.Value); ChromeProcessInfoKey = null; } // HTTPサーバのクラスを生成 HttpResponseData faviconResponse = FaviconData == null ? LocalHttpServerCommon.EmptyDataResponse : LocalHttpServerCommon.GetIconDataResponse(FaviconData); Server = new LocalHttpServer( LocalHttpServerCommon.GetHttpFileDataResponse(htmlFilePath), faviconResponse, GetResponceProcesses(responceFunctions, postRegex, postParamName)); // HTTPサーバのクラスをリソース解放対象として登録する // (非同期での動作を止めるために登録) EntryPoint.DisposeClass.Add(Server); // HTTPサーバを起動する Server.Start(localHttpServerPort); // 起動パラメータを設定 string startUpParam = string.Format( CultureInfo.InvariantCulture, ChromeStartUpParamFormat, Server.Url.ToString()); // Chromeを起動する long?key = StartProcess.Start(ChromePath, startUpParam, sizePoint); // 起動キーをプロパティに保持する ChromeProcessInfoKey = key; }